Example #1
0
        public Dictionary <int, string> TestMergeRandom(BPlusTreeOptions <int, string> options, int nsets, int nsize)
        {
            Dictionary <int, string> test = new Dictionary <int, string>();

            IEnumerable <KeyValuePair <int, string> >[] sets =
                new List <IEnumerable <KeyValuePair <int, string> > >(CreateSets(nsets, nsize, test)).ToArray();

            using (BPlusTree <int, string> tree = new BPlusTree <int, string>(options))
            {
                foreach (IEnumerable <KeyValuePair <int, string> > set in sets)
                {
                    tree.BulkInsert(set, new BulkInsertOptions {
                        DuplicateHandling = DuplicateHandling.LastValueWins
                    });
                }

                VerifyDictionary(test, tree);

                tree.UnloadCache();
                tree.Add(int.MaxValue, "max");
                tree.Remove(int.MaxValue);

                VerifyDictionary(test, tree);
            }

            return(test);
        }
Example #2
0
        public void TestMergeRandomInFile()
        {
            BPlusTreeOptions <int, string> options = Options;

            using (TempFile temp = new TempFile())
            {
                temp.Delete();
                options.CreateFile = CreatePolicy.Always;
                options.FileName   = temp.TempPath;
                options.CalcBTreeOrder(4, 4);
                Stopwatch sw = Stopwatch.StartNew();

                Dictionary <int, string> expected =
                    TestMergeRandom(options, 2, 300);

                Trace.TraceInformation("Creating {0} nodes in {1}.", expected.Count, sw.Elapsed);
                sw = Stopwatch.StartNew();

                options            = Options;
                options.CreateFile = CreatePolicy.Never;
                options.FileName   = temp.TempPath;
                options.CalcBTreeOrder(4, 4);
                using (BPlusTree <int, string> tree = new BPlusTree <int, string>(options))
                {
                    VerifyDictionary(expected, tree);
                }

                Trace.TraceInformation("Verified {0} nodes in {1}.", expected.Count, sw.Elapsed);
            }
        }
        void TestRandomAddRemove(int repeat, int nodesz, int size)
        {
            List <int> keysAdded = new List <int>(250000);
            BPlusTreeOptions <int, string> options = Options;

            options.LockingFactory = new IgnoreLockFactory();

            Dictionary <int, string> keys = new Dictionary <int, string>();

            for (; repeat > 0; repeat--)
            {
                keys.Clear();
                options.BTreeOrder = nodesz;
                using (BPlusTree <int, string> data = Create(options))
                {
                    data.EnableCount();

                    AddRandomKeys(size, keys, data);
                    IsSameList(keys, data);
                    keysAdded.Clear();

                    for (int tc = 0; tc < 1; tc++)
                    {
                        int del = keys.Count / 3 + Random.Next(keys.Count / 3);
                        RemoveRandomKeys(del, keys, data);
                        IsSameList(keys, data);

                        data.Validate();

                        AddRandomKeys(del, keys, data);
                        IsSameList(keys, data);

                        data.Validate();
                    }

                    keysAdded.Clear();

                    foreach (KeyValuePair <int, string> kv in data)
                    {
                        keysAdded.Add(kv.Key);
                    }

                    foreach (int k in keysAdded)
                    {
                        Assert.IsTrue(data.Remove(k));
                        data.Add(k, k.ToString());
                        Assert.IsTrue(data.Remove(k));
                        string test;
                        Assert.IsFalse(data.TryGetValue(k, out test));
                        Assert.IsNull(test);
                    }
                }
            }
        }
Example #4
0
        public void TestFailedWrite()
        {
            BPlusTreeOptions <int, string> options = Options;

            using (BPlusTree <int, string> tree = new BPlusTree <int, string>(options))
            {
                for (int i = 0; i < 10; i++)
                {
                    tree[i] = i.ToString();
                }

                ((MyNodeStore)options.StorageSystem).ReadOnly = true;
                tree.Add(50, string.Empty);
            }
        }
Example #5
0
        void TestMergeSequenceInFile(BPlusTreeOptions <int, string> options, int count)
        {
            Dictionary <int, string> expected = new Dictionary <int, string>();

            for (int i = 0; i < count; i++)
            {
                expected.Add(i + 1, i.ToString());
            }

            using (BPlusTree <int, string> tree = new BPlusTree <int, string>(options))
            {
                Assert.AreEqual(expected.Count, tree.BulkInsert(expected));
                VerifyDictionary(expected, tree);
            }
        }
        int StartAndAbortWriters(BPlusTreeOptions <KeyInfo, DataValue> options, TempFile copy)
        {
            RecordsCreated = 0;
            int minRecordCreated;
            BPlusTree <KeyInfo, DataValue> dictionary = new BPlusTree <KeyInfo, DataValue>(options);

            try
            {
                using (WorkQueue work = new WorkQueue(Environment.ProcessorCount))
                {
                    Exception lastError = null;
                    work.OnError += delegate(object o, ErrorEventArgs e) { lastError = e.GetException(); };

                    Thread.Sleep(1);
                    for (int i = 0; i < Environment.ProcessorCount; i++)
                    {
                        work.Enqueue(new ThreadedTest(dictionary, 10000000).Run);
                    }

                    while (RecordsCreated < 1000)
                    {
                        Thread.Sleep(1);
                    }

                    minRecordCreated = Interlocked.CompareExchange(ref RecordsCreated, 0, 0);
                    if (copy != null)
                    {
                        File.Copy(options.FileName, copy.TempPath); //just grab a copy any old time.
                    }
                    work.Complete(false, 0);                        //hard-abort all threads

                    //if(lastError != null)
                    //    Assert.AreEqual(typeof(InvalidDataException), lastError.GetType());
                }

                // force the file to close without disposing the btree
                IDisposable tmp = (IDisposable) new PropertyValue(dictionary, "_storage").Value;
                tmp.Dispose();
            }
            catch
            {
                dictionary.Dispose();
                throw;
            }
            return(minRecordCreated);
        }
Example #7
0
        public void TestMergeSequenceInFile()
        {
            BPlusTreeOptions <int, string> options = Options;

            using (TempFile temp = new TempFile())
            {
                options = Options;
                temp.Delete();
                //options.CreateFile = CreatePolicy.Always;
                //options.FileName = temp.TempPath;
                options.MaximumValueNodes = 14;
                options.MinimumValueNodes = 7;
                options.MaximumChildNodes = 6;
                options.MinimumChildNodes = 2;

                // Just to make sure we don't break some fencepost condition in the future
                for (int i = 0; i <= (options.MaximumValueNodes * options.MaximumChildNodes) + 1; i++)
                {
                    TestMergeSequenceInFile(options.Clone(), i);
                }
                TestMergeSequenceInFile(options.Clone(), options.MaximumValueNodes * options.MaximumChildNodes * options.MaximumChildNodes);
                TestMergeSequenceInFile(options.Clone(), options.MaximumValueNodes * options.MaximumChildNodes * options.MaximumChildNodes + 1);
            }
        }
 protected virtual BPlusTree <int, string> Create(BPlusTreeOptions <int, string> options)
 {
     return(new BPlusTree <int, string>(options));
 }
Example #9
0
        public void TestStorageSystemOption()
        {
            BPlusTreeOptions <int, string> options = Options;

            Assert.AreEqual(StorageType.Custom, options.StorageType);
        }
 protected override BPlusTree <int, string> Create(BPlusTreeOptions <int, string> options)
 {
     // Test with all-default in-memory ctor
     return(new BPlusTree <int, string>());
 }
        public Dictionary<int, string> TestMergeRandom(BPlusTreeOptions<int, string> options, int nsets, int nsize)
        {
            Dictionary<int, string> test = new Dictionary<int, string>();
            IEnumerable<KeyValuePair<int, string>>[] sets = 
                new List<IEnumerable<KeyValuePair<int, string>>>(CreateSets(nsets, nsize, test)).ToArray();

            using (BPlusTree<int, string> tree = new BPlusTree<int, string>(options))
            {
                foreach(IEnumerable<KeyValuePair<int, string>> set in sets)
                    tree.BulkInsert(set, new BulkInsertOptions { DuplicateHandling = DuplicateHandling.LastValueWins });

                VerifyDictionary(test, tree);

                tree.UnloadCache();
                tree.Add(int.MaxValue, "max");
                tree.Remove(int.MaxValue);

                VerifyDictionary(test, tree);
            }

            return test;
        }
        void TestMergeSequenceInFile(BPlusTreeOptions<int, string> options, int count)
        {
            Dictionary<int, string> expected = new Dictionary<int, string>();

            for (int i = 0; i < count; i++)
                expected.Add(i + 1, i.ToString());

            using (BPlusTree<int, string> tree = new BPlusTree<int, string>(options))
            {
                Assert.AreEqual(expected.Count, tree.BulkInsert(expected));
                VerifyDictionary(expected, tree);
            }
        }
 protected override BPlusTree<int, string> Create(BPlusTreeOptions<int, string> options)
 {
     // Test with all-default in-memory ctor
     return new BPlusTree<int, string>();
 }