public void TestAtomicInterfaces()
        {
            SynchronizedDictionary <int, string> data =
                new SynchronizedDictionary <int, string>(new Dictionary <int, string>());

            data[1] = "a";

            AddUpdateValue update = new AddUpdateValue();

            Assert.IsFalse(data.AddOrUpdate(1, ref update));
            Assert.AreEqual("a", update.OldValue);
            Assert.IsFalse(data.AddOrUpdate(2, ref update));
            Assert.IsNull(update.OldValue);
            Assert.IsFalse(data.TryRemove(1, ref update));
            Assert.AreEqual("a", update.OldValue);

            Assert.AreEqual(1, data.Count);
            Assert.AreEqual("a", data[1]);

            update.Value = "b";
            Assert.IsTrue(data.AddOrUpdate(1, ref update));
            Assert.AreEqual("a", update.OldValue);
            Assert.IsTrue(data.AddOrUpdate(2, ref update));
            Assert.IsNull(update.OldValue);

            Assert.AreEqual(2, data.Count);
            Assert.AreEqual("b", data[1]);
            Assert.AreEqual("b", data[2]);

            Assert.IsTrue(data.TryRemove(1, ref update));
            Assert.AreEqual("b", update.OldValue);
            Assert.IsTrue(data.TryRemove(2, ref update));
            Assert.AreEqual("b", update.OldValue);
            Assert.AreEqual(0, data.Count);
        }
        public void TestAtomicInterfaces()
        {
            using (BPlusTree <int, string> data = Create(Options))
            {
                data.EnableCount();
                data[1] = "a";

                AddUpdateValue update = new AddUpdateValue();
                Assert.IsFalse(data.AddOrUpdate(1, ref update));
                Assert.AreEqual("a", update.OldValue);
                Assert.IsFalse(data.AddOrUpdate(2, ref update));
                Assert.IsNull(update.OldValue);
                Assert.IsFalse(data.TryRemove(1, ref update));
                Assert.AreEqual("a", update.OldValue);

                Assert.AreEqual(1, data.Count);
                Assert.AreEqual("a", data[1]);

                update.Value = "b";
                Assert.IsTrue(data.AddOrUpdate(1, ref update));
                Assert.AreEqual("a", update.OldValue);
                Assert.IsTrue(data.AddOrUpdate(2, ref update));
                Assert.IsNull(update.OldValue);

                Assert.AreEqual(2, data.Count);
                Assert.AreEqual("b", data[1]);
                Assert.AreEqual("b", data[2]);

                Assert.IsTrue(data.TryRemove(1, ref update));
                Assert.AreEqual("b", update.OldValue);
                Assert.IsTrue(data.TryRemove(2, ref update));
                Assert.AreEqual("b", update.OldValue);
                Assert.AreEqual(0, data.Count);
            }
        }
Beispiel #3
0
        public void TestAtomicInterfaces()
        {
            var data = new LurchTableTest <int, string>();

            data[1] = "a";

            AddUpdateValue update = new AddUpdateValue();

            Assert.IsFalse(data.AddOrUpdate(1, ref update));
            Assert.AreEqual("a", update.OldValue);
            Assert.IsFalse(data.AddOrUpdate(2, ref update));
            Assert.IsNull(update.OldValue);
            Assert.IsFalse(data.TryRemove(1, ref update));
            Assert.AreEqual("a", update.OldValue);

            Assert.AreEqual(1, data.Count);
            Assert.AreEqual("a", data[1]);

            update.Value = "b";
            Assert.IsTrue(data.AddOrUpdate(1, ref update));
            Assert.AreEqual("a", update.OldValue);
            Assert.IsTrue(data.AddOrUpdate(2, ref update));
            Assert.IsNull(update.OldValue);

            Assert.AreEqual(2, data.Count);
            Assert.AreEqual("b", data[1]);
            Assert.AreEqual("b", data[2]);

            Assert.IsTrue(data.TryRemove(1, ref update));
            Assert.AreEqual("b", update.OldValue);
            Assert.IsTrue(data.TryRemove(2, ref update));
            Assert.AreEqual("b", update.OldValue);
            Assert.AreEqual(0, data.Count);
        }
Beispiel #4
0
        public static void Run()
        {
            Console.WriteLine("Now generating sorted items ...");

            int nNewItems   = 100000;
            int nOldItems   = 100000;
            var rnd         = new Random();
            var sortedItems = new SortedDictionary <double, string>();
            int newItem     = 0;

            for (int i = 1; i <= nNewItems; i++)
            {
                do
                {
                    newItem = rnd.Next(0, Int32.MaxValue);
                }while (sortedItems.ContainsKey(newItem));

                sortedItems.Add(newItem, Convert.ToString(rnd.Next(0, Int32.MaxValue)));
                Console.Write("\rAdding {0,5}% : {1,10:N0}/{2:N0}", Math.Round((((double)i / (double)nNewItems) * 100)), i, nNewItems);
            }

            Stopwatch stp = new Stopwatch();

            var options = new BPlusTree <double, string> .OptionsV2(PrimitiveSerializer.Double, PrimitiveSerializer.String);

            options.CalcBTreeOrder(16, 24);
            options.FileBlockSize = 8192;
            options.CreateFile    = CreatePolicy.Always;
            options.FileName      = "I:\\test.tmp";

            BulkInsertOptions opts = new BulkInsertOptions();

            opts.CommitOnCompletion = true; // check how to properly set this value using Roger examples.
            opts.DuplicateHandling  = DuplicateHandling.LastValueWins;
            opts.InputIsSorted      = true;
            opts.ReplaceContents    = false;

            AddUpdateValue update = new AddUpdateValue();

            Console.WriteLine();
            Console.WriteLine("Now creating tree ...");
            using (var tree = new BPlusTree <double, string>(options))
            {
                stp.Start();
                for (int i = 0; i < nOldItems; i++)
                {
                    tree.AddOrUpdate(rnd.Next(0, Int32.MaxValue), ref update);
                }
                stp.Stop();
                Console.WriteLine("Initial <{0:N0}> items =>  ET : {1}     Speed : {2:N0} item/sec", nOldItems, stp.Elapsed, Math.Round((double)(nOldItems / stp.Elapsed.TotalSeconds), 5));

                stp.Restart();
                tree.AddRangeSorted(sortedItems, true);
                stp.Stop();
                Console.WriteLine("Bulk    <{0:N0}> items =>  ET : {1}     Speed : {2:N0} item/sec", nNewItems, stp.Elapsed, Math.Round((double)(nNewItems / stp.Elapsed.TotalSeconds), 5));
            }

            Console.ReadLine();
        }
Beispiel #5
0
        private static void TEST_AddOrUpdate()
        {
            BPlusTree <int, string> .OptionsV2 options = new BPlusTree <int, string> .OptionsV2(PrimitiveSerializer.Int32, PrimitiveSerializer.String);

            options.CreateFile = CreatePolicy.Never;

            AddUpdateValue update = new AddUpdateValue();

            var tree = new BPlusTree <int, string>(options);

            update.Value = "Hamed";

            //tree[1] = "a";
            tree.AddOrUpdate(1, ref update);
            tree.AddOrUpdate(2, ref update);

            update.Value = "Vahid";
            tree.AddOrUpdate(2, ref update);

            update.Value = "New";
            tree.AddOrUpdate(3, ref update);
        }