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);
            }
        }
        public void TestAtomicAddOrUpdate()
        {
            using (BPlusTree <int, string> data = Create(Options))
            {
                data.EnableCount();
                int[] counter = new int[] { -1 };

                for (int i = 0; i < 100; i++)
                {
                    data.AddOrUpdate(i, k => (++counter[0]).ToString(), (k, v) => { throw new InvalidOperationException(); });
                }

                for (int i = 0; i < 100; i++)
                {
                    Assert.AreEqual((i & 1) == 1, data.TryRemove(i, (k, v) => (int.Parse(v) & 1) == 1));
                }

                for (int i = 0; i < 100; i++)
                {
                    data.AddOrUpdate(i, k => (++counter[0]).ToString(), (k, v) => (++counter[0]).ToString());
                }

                Assert.AreEqual(100, data.Count);
                Assert.AreEqual(200, counter[0] + 1);

                for (int i = 0; i < 100; i++)
                {
                    Assert.IsTrue(data.TryRemove(i, (k, v) => int.Parse(v) - 100 == i));
                }

                Assert.AreEqual(0, data.Count);
            }
        }
        public void TestNewAddOrUpdate()
        {
            using (BPlusTree <int, string> data = Create(Options))
            {
                Assert.AreEqual("a", data.AddOrUpdate(1, "a", (k, v) => k.ToString()));
                Assert.AreEqual("1", data.AddOrUpdate(1, "a", (k, v) => k.ToString()));

                Assert.AreEqual("b", data.AddOrUpdate(2, k => "b", (k, v) => k.ToString()));
                Assert.AreEqual("2", data.AddOrUpdate(2, k => "b", (k, v) => k.ToString()));
            }
        }
Exemple #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();
        }
Exemple #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);
        }
Exemple #6
0
        public static void RandomOverlaps(string outputPath, string TestName)
        {
            int       regionCount = 200000;
            int       sampleCount = 300;
            Random    rnd         = new Random();
            Stopwatch stopWatch   = new Stopwatch();

            if (!Directory.Exists(outputPath + Path.DirectorySeparatorChar))
            {
                Directory.CreateDirectory(outputPath + Path.DirectorySeparatorChar);
            }
            StreamWriter writer = new StreamWriter(outputPath + Path.DirectorySeparatorChar + "speed" + TestName + ".txt");

            writer.WriteLine("Di3 indexing speed test: " + TestName);
            writer.WriteLine("Speed(interval/sec)\tET\tET(ms)");


            BPlusTree <int, int> .OptionsV2 options = new BPlusTree <int, int> .OptionsV2(PrimitiveSerializer.Int32, PrimitiveSerializer.Int32);

            options.CreateFile = CreatePolicy.Always;
            options.FileName   = outputPath + Path.DirectorySeparatorChar + "speed" + TestName;


            LargeInsertionAddUpdateValue update = new LargeInsertionAddUpdateValue();

            using (var tree = new BPlusTree <int, int>(options))
            {
                for (int sample = 0; sample < sampleCount; sample++)
                {
                    Console.WriteLine("processing sample   : {0:N0}", sample);
                    stopWatch.Restart();
                    for (int region = 0; region < regionCount; region++)
                    {
                        update.Value = rnd.Next(0, Int32.MaxValue);
                        tree.AddOrUpdate(rnd.Next(10, 12), ref update);
                    }
                    stopWatch.Stop();
                    Console.WriteLine(".::. Writting Speed : {0} intervals\\sec", Math.Round(regionCount / stopWatch.Elapsed.TotalSeconds, 2));
                    Console.WriteLine("");
                    writer.WriteLine(Math.Round(regionCount / stopWatch.Elapsed.TotalSeconds, 2).ToString() + "\t" + stopWatch.Elapsed.ToString() + "\t" + stopWatch.ElapsedMilliseconds.ToString());
                    writer.Flush();
                }
            }
        }
Exemple #7
0
 public bool AddOrUpdate(string key, ContentRecord value)
 {
     AssertModify();
     return(_index.AddOrUpdate(key, value));
 }
Exemple #8
0
 /// <summary>
 /// Saves the specified value.
 /// </summary>
 /// <param name="value">The value.</param>
 public void Save(T value)
 {
     tree.AddOrUpdate(value.Id, value, (key, original) => value);
 }