Example #1
0
        public void TestReadOnlyCopy()
        {
            using (var tempFile = new TempFile())
            {
                var options = new BPlusTree <int, string> .OptionsV2(new PrimitiveSerializer(), new PrimitiveSerializer())
                {
                    CreateFile = CreatePolicy.Always,
                    FileName   = tempFile.TempPath,
                }

                .CalcBTreeOrder(4, 10);

                var readcopy = options.Clone();
                readcopy.CreateFile = CreatePolicy.Never;
                readcopy.ReadOnly   = true;

                using (var tree = new BPlusTree <int, string>(options))
                {
                    using (var copy = new BPlusTree <int, string>(readcopy))
                    {
                        copy.EnableCount();
                        Assert.AreEqual(0, copy.Count);
                    }

                    //insert some data...
                    tree.AddRange(MakeValues(0, 100));

                    using (var copy = new BPlusTree <int, string>(readcopy))
                    {
                        copy.EnableCount();
                        Assert.AreEqual(0, copy.Count);
                    }
                    tree.Commit();

                    //insert some data...
                    for (int i = 0; i < 100; i++)
                    {
                        tree.Remove(i);
                    }
                    tree.AddRange(MakeValues(1000, 1000));

                    using (var copy = new BPlusTree <int, string>(readcopy))
                    {
                        copy.EnableCount();
                        Assert.AreEqual(100, copy.Count);
                        Assert.AreEqual(0, copy.First().Key);
                        Assert.AreEqual(99, copy.Last().Key);
                    }

                    tree.Commit();
                }
            }
        }
        public void TestReadOnlyCopy()
        {
            using (var tempFile = new TempFile())
            {
                var options = new BPlusTree<int, string>.OptionsV2(new PrimitiveSerializer(), new PrimitiveSerializer())
                                  {
                                      CreateFile = CreatePolicy.Always,
                                      FileName = tempFile.TempPath,
                                  }.CalcBTreeOrder(4, 10);

                var readcopy = options.Clone();
                readcopy.CreateFile = CreatePolicy.Never;
                readcopy.ReadOnly = true;

                using (var tree = new BPlusTree<int, string>(options))
                {
                    using (var copy = new BPlusTree<int, string>(readcopy))
                    {
                        copy.EnableCount();
                        Assert.AreEqual(0, copy.Count);
                    }

                    //insert some data...
                    tree.AddRange(MakeValues(0, 100));

                    using (var copy = new BPlusTree<int, string>(readcopy))
                    {
                        copy.EnableCount();
                        Assert.AreEqual(0, copy.Count);
                    }
                    tree.Commit();

                    //insert some data...
                    for (int i = 0; i < 100; i++)
                        tree.Remove(i);
                    tree.AddRange(MakeValues(1000, 1000));

                    using (var copy = new BPlusTree<int, string>(readcopy))
                    {
                        copy.EnableCount();
                        Assert.AreEqual(100, copy.Count);
                        Assert.AreEqual(0, copy.First().Key);
                        Assert.AreEqual(99, copy.Last().Key);
                    }

                    tree.Commit();

                }
            }
        }
        public void TestBulkInsert()
        {
            Stopwatch sw      = Stopwatch.StartNew();
            var       options = Options.Clone();

            using (TempFile temp = new TempFile())
            {
                //using (BPlusTree<int, string> data = Create(Options))
                using (BPlusTree <int, string> data = Create(options))
                {
                    const bool bulk = true;
                    Insert(data, CreateRandom(1000, 3000), bulk, false);

                    data.EnableCount();
                    Assert.AreEqual(1000, data.Count);

                    Insert(data, CreateCount(data.Last().Key + 1, 1, 1000), bulk, true);
                    Assert.AreEqual(2000, data.Count);

                    Insert(data, CreateCount(data.Last().Key + 10001, -1, 1000), bulk, false);
                    Assert.AreEqual(3000, data.Count);

                    int lastKey = data.Last().Key;
                    data.AddRange(CreateCount(1, 2, lastKey / 2), true);
                }
                temp.Dispose();
            }
            Trace.WriteLine("Inserted in " + sw.Elapsed);
        }
Example #4
0
 void AddRanges(BPlusTree <Guid, TestInfo> tree)
 {
     while (!mreStop.WaitOne(0, false))
     {
         tree.AddRange(CreateData(100));
         AddIdle(tree);
     }
 }
Example #5
0
        /// <summary>
        /// Добавляет инфомрацию о пачке продаж в CMS
        /// </summary>
        /// <param name="saleEvents">События о продажах</param>
        public void AddRange(IEnumerable<SaleEvent> saleEvents)
        {
            if (saleEvents == null)
            {
                throw new ArgumentNullException(nameof(saleEvents));
            }

            var data = saleEvents.Select(x => new KeyValuePair<DateTime, IndexValue>(x.DateTime, new IndexValue() { Article = x.Article, Store = x.StoreName, Count = x.Count }));
            bTree.AddRange(data);
            bTree.Commit();
        }
        public void TestKeyValueCollections()
        {
            List <KeyValuePair <int, string> > sample = new List <KeyValuePair <int, string> >();

            for (int i = 0; i < 20; i++)
            {
                sample.Add(new KeyValuePair <int, string>(i, i.ToString()));
            }

            using (BPlusTree <int, string> data = Create(Options))
            {
                data.AddRange(sample);
                //Key collection
                Assert.AreEqual(data.Count, data.Keys.Count);
                Assert.IsTrue(data.Keys.IsReadOnly);
                for (int i = 0; i < sample.Count && i < 5; i++)
                {
                    Assert.IsTrue(data.Keys.Contains(sample[i].Key));
                }

                IEnumerator <int> ek = data.Keys.GetEnumerator();
                Assert.IsTrue(ek.MoveNext());
                int firstkey = ek.Current;
                Assert.IsTrue(ek.MoveNext());
                Assert.AreNotEqual(firstkey, ek.Current);
                ek.Reset();
                Assert.IsTrue(ek.MoveNext());
                Assert.AreEqual(firstkey, ek.Current);
                Assert.AreEqual(firstkey, ((System.Collections.IEnumerator)ek).Current);

                //Value collection
                Assert.AreEqual(data.Count, data.Values.Count);
                Assert.IsTrue(data.Values.IsReadOnly);
                for (int i = 0; i < sample.Count && i < 5; i++)
                {
                    Assert.IsTrue(data.Values.Contains(sample[i].Value));
                }

                IEnumerator <string> ev = data.Values.GetEnumerator();
                Assert.IsTrue(ev.MoveNext());
                string firstvalue = ev.Current;
                Assert.IsTrue(ev.MoveNext());
                Assert.AreNotEqual(firstvalue, ev.Current);
                ev.Reset();
                Assert.IsTrue(ev.MoveNext());
                Assert.AreEqual(firstvalue, ((System.Collections.IEnumerator)ev).Current);
            }
        }
 static void Insert(BPlusTree <int, string> data, IEnumerable <KeyValuePair <int, string> > items, bool bulk, bool sorted)
 {
     if (bulk)
     {
         if (sorted)
         {
             data.AddRangeSorted(items);
         }
         else
         {
             data.AddRange(items);
         }
     }
     else
     {
         foreach (KeyValuePair <int, string> kv in items)
         {
             data.Add(kv.Key, kv.Value);
         }
     }
 }
 void AddRanges(BPlusTree<Guid, TestInfo> tree)
 {
     while (!mreStop.WaitOne(0, false))
     {
         tree.AddRange(CreateData(100));
         AddIdle(tree);
     }
 }
Example #9
0
        public void TestSyncFromLogging()
        {
            using (var tempFile = new TempFile())
                using (var logfile = new TempFile())
                    using (var tempCopy = new TempFile())
                    {
                        var options = new BPlusTree <int, string> .OptionsV2(new PrimitiveSerializer(), new PrimitiveSerializer())
                        {
                            CreateFile             = CreatePolicy.Always,
                            FileName               = tempFile.TempPath,
                            TransactionLogFileName = logfile.TempPath,
                        }

                        .CalcBTreeOrder(4, 10);

                        var readcopy = options.Clone();
                        readcopy.FileName           = tempCopy.TempPath;
                        readcopy.StoragePerformance = StoragePerformance.Fastest;

                        using (var tree = new BPlusTree <int, string>(options))
                            using (var copy = new BPlusTree <int, string>(readcopy))
                                using (var tlog = new TransactionLog <int, string>(
                                           new TransactionLogOptions <int, string>(logfile.TempPath, PrimitiveSerializer.Int32, PrimitiveSerializer.String)
                                {
                                    ReadOnly = true
                                }))
                                {
                                    tree.Add(0, "0");
                                    tree.Commit();

                                    long logpos = 0;
                                    copy.EnableCount();
                                    //start by copying the data from tree's file into the copy instance:
                                    copy.BulkInsert(
                                        BPlusTree <int, string> .EnumerateFile(options),
                                        new BulkInsertOptions {
                                        InputIsSorted = true, CommitOnCompletion = false, ReplaceContents = true
                                    }
                                        );

                                    Assert.AreEqual(1, copy.Count);
                                    Assert.AreEqual("0", copy[0]);

                                    tlog.ReplayLog(copy, ref logpos);
                                    Assert.AreEqual(1, copy.Count);

                                    //insert some data...
                                    tree.AddRange(MakeValues(1, 99));

                                    tlog.ReplayLog(copy, ref logpos);
                                    Assert.AreEqual(100, copy.Count);

                                    //insert some data...
                                    for (int i = 0; i < 100; i++)
                                    {
                                        tree.Remove(i);
                                    }
                                    tlog.ReplayLog(copy, ref logpos);
                                    Assert.AreEqual(0, copy.Count);

                                    tree.AddRange(MakeValues(1000, 1000));

                                    tlog.ReplayLog(copy, ref logpos);
                                    Assert.AreEqual(1000, copy.Count);
                                }
                    }
        }
        public void TestSyncFromLogging()
        {
            using (var tempFile = new TempFile())
            using (var logfile = new TempFile())
            using (var tempCopy = new TempFile())
            {
                var options = new BPlusTree<int, string>.OptionsV2(new PrimitiveSerializer(), new PrimitiveSerializer())
                {
                    CreateFile = CreatePolicy.Always,
                    FileName = tempFile.TempPath,
                    TransactionLogFileName = logfile.TempPath,
                }.CalcBTreeOrder(4, 10);

                var readcopy = options.Clone();
                readcopy.FileName = tempCopy.TempPath;
                readcopy.StoragePerformance = StoragePerformance.Fastest;

                using (var tree = new BPlusTree<int, string>(options))
                using (var copy = new BPlusTree<int, string>(readcopy))
                using (var tlog = new TransactionLog<int, string>(
                    new TransactionLogOptions<int, string>(logfile.TempPath, PrimitiveSerializer.Int32, PrimitiveSerializer.String) { ReadOnly = true }))
                {
                    tree.Add(0, "0");
                    tree.Commit();

                    long logpos = 0;
                    copy.EnableCount();
                    //start by copying the data from tree's file into the copy instance:
                    copy.BulkInsert(
                        BPlusTree<int, string>.EnumerateFile(options),
                        new BulkInsertOptions { InputIsSorted = true, CommitOnCompletion = false, ReplaceContents = true }
                        );

                    Assert.AreEqual(1, copy.Count);
                    Assert.AreEqual("0", copy[0]);

                    tlog.ReplayLog(copy, ref logpos);
                    Assert.AreEqual(1, copy.Count);

                    //insert some data...
                    tree.AddRange(MakeValues(1, 99));

                    tlog.ReplayLog(copy, ref logpos);
                    Assert.AreEqual(100, copy.Count);

                    //insert some data...
                    for (int i = 0; i < 100; i++)
                        tree.Remove(i);
                    tlog.ReplayLog(copy, ref logpos);
                    Assert.AreEqual(0, copy.Count);

                    tree.AddRange(MakeValues(1000, 1000));

                    tlog.ReplayLog(copy, ref logpos);
                    Assert.AreEqual(1000, copy.Count);
                }
            }
        }