void TestRecoveryOnNew(BPlusTree<Guid, TestInfo>.OptionsV2 options, int count, int added)
        {
            BPlusTree<Guid, TestInfo> tree = null;
            var temp = TempFile.Attach(options.FileName);
            Dictionary<Guid, TestInfo> data = new Dictionary<Guid, TestInfo>();
            try
            {
                Assert.IsNotNull(options.TransactionLog);
                temp.Delete();
                tree = new BPlusTree<Guid, TestInfo>(options);
                using (var log = options.TransactionLog)
                {
                    using ((IDisposable)new PropertyValue(tree, "_storage").Value)
                        Insert(tree, data, Environment.ProcessorCount, count, TimeSpan.MaxValue);
                    //Add extra data...
                    AppendToLog(log, TestInfo.Create(added, data));
                }
                tree = null;
                //No file... yet...
                Assert.IsFalse(File.Exists(options.FileName));
                //Now recover...
                using (var recovered = new BPlusTree<Guid, TestInfo>(options))
                {
                    TestInfo.AssertEquals(data, recovered);
                }

                Assert.IsTrue(File.Exists(options.FileName));
            }
            finally
            {
                temp.Dispose();
                if (tree != null)
                    tree.Dispose();
            }
        }
Exemplo n.º 2
0
        void TestRecoveryOnExisting(BPlusTree <Guid, TestInfo> .OptionsV2 options, int count, int added)
        {
            BPlusTree <Guid, TestInfo> tree = null;
            var temp = TempFile.Attach(options.FileName);
            Dictionary <Guid, TestInfo> dataFirst, data = new Dictionary <Guid, TestInfo>();

            try
            {
                temp.Delete();
                Assert.IsNotNull(options.TransactionLog);

                using (tree = new BPlusTree <Guid, TestInfo>(options))
                {
                    Insert(tree, data, 1, 100, TimeSpan.MaxValue);
                    TestInfo.AssertEquals(data, tree);
                    Assert.IsFalse(temp.Exists);
                }
                tree = null;
                Assert.IsTrue(File.Exists(options.TransactionLogFileName));

                // All data commits to output file
                Assert.IsTrue(temp.Exists);
                TestInfo.AssertEquals(data, BPlusTree <Guid, TestInfo> .EnumerateFile(options));

                dataFirst = new Dictionary <Guid, TestInfo>(data);
                DateTime modified = temp.Info.LastWriteTimeUtc;

                tree = new BPlusTree <Guid, TestInfo>(options);
                using (var log = options.TransactionLog)
                {
                    using ((IDisposable) new PropertyValue(tree, "_storage").Value)
                        Insert(tree, data, Environment.ProcessorCount, count, TimeSpan.MaxValue);
                    //Add extra data...
                    AppendToLog(log, TestInfo.Create(added, data));
                }
                tree = null;

                //Still only contains original data
                Assert.AreEqual(modified, temp.Info.LastWriteTimeUtc);
                TestInfo.AssertEquals(dataFirst, BPlusTree <Guid, TestInfo> .EnumerateFile(options));

                //Now recover...
                using (var recovered = new BPlusTree <Guid, TestInfo>(options))
                {
                    TestInfo.AssertEquals(data, recovered);
                }
            }
            finally
            {
                temp.Dispose();
                if (tree != null)
                {
                    tree.Dispose();
                }
            }
        }
Exemplo n.º 3
0
        void TestRecoveryOnNew(BPlusTree <Guid, TestInfo> .OptionsV2 options, int count, int added)
        {
            BPlusTree <Guid, TestInfo> tree = null;
            var temp = TempFile.Attach(options.FileName);
            Dictionary <Guid, TestInfo> data = new Dictionary <Guid, TestInfo>();

            try
            {
                Assert.IsNotNull(options.TransactionLog);
                temp.Delete();
                tree = new BPlusTree <Guid, TestInfo>(options);
                using (var log = options.TransactionLog)
                {
                    using ((IDisposable) new PropertyValue(tree, "_storage").Value)
                        Insert(tree, data, Environment.ProcessorCount, count, TimeSpan.MaxValue);
                    //Add extra data...
                    AppendToLog(log, TestInfo.Create(added, data));
                }
                tree = null;
                //No data... yet...
                using (TempFile testempty = TempFile.FromCopy(options.FileName))
                {
                    var testoptions = options.Clone();
                    testoptions.TransactionLogFileName = null;
                    testoptions.TransactionLog         = null;
                    testoptions.FileName = testempty.TempPath;

                    using (var empty = new BPlusTree <Guid, TestInfo>(testoptions))
                    {
                        empty.EnableCount();
                        Assert.AreEqual(0, empty.Count);
                    }
                }

                //Now recover...
                using (var recovered = new BPlusTree <Guid, TestInfo>(options))
                {
                    TestInfo.AssertEquals(data, recovered);
                }
            }
            finally
            {
                temp.Dispose();
                if (tree != null)
                {
                    tree.Dispose();
                }
            }
        }
Exemplo n.º 4
0
        public void TestWriteToTemporaryCopy()
        {
            Dictionary <Guid, TestInfo> first, data = new Dictionary <Guid, TestInfo>();

            using (TempFile temp = new TempFile())
            {
                temp.Delete();
                var options = GetOptions(temp);
                options.TransactionLogFileName = Path.ChangeExtension(options.FileName, ".tlog");

                using (var tree = new BPlusTree <Guid, TestInfo>(options))
                {
                    Insert(tree, data, 1, 100, TimeSpan.MaxValue);
                    TestInfo.AssertEquals(data, tree);
                    Assert.IsFalse(temp.Exists);
                }

                // All data commits to output file
                Assert.IsTrue(temp.Exists);
                TestInfo.AssertEquals(data, BPlusTree <Guid, TestInfo> .EnumerateFile(options));

                first = new Dictionary <Guid, TestInfo>(data);

                using (var tree = new BPlusTree <Guid, TestInfo>(options))
                {
                    Insert(tree, data, 1, 100, TimeSpan.MaxValue);

                    //We are writing to a backup, the original file still contains 100 items:
                    TestInfo.AssertEquals(first, BPlusTree <Guid, TestInfo> .EnumerateFile(options));

                    //Commit the changes and the original file will now contain our changes:
                    tree.CommitChanges();
                    TestInfo.AssertEquals(data, BPlusTree <Guid, TestInfo> .EnumerateFile(options));

                    //Add a few more records...
                    Insert(tree, data, 1, 100, TimeSpan.MaxValue);
                }
                //Dispose of the tree will commit changes...
                TestInfo.AssertEquals(data, BPlusTree <Guid, TestInfo> .EnumerateFile(options));
            }
        }
Exemplo n.º 5
0
        public void TestRestoreLargeLog()
        {
            using (TempFile savelog = new TempFile())
                using (TempFile temp = new TempFile())
                {
                    var options = GetOptions(temp);
                    options.FileBlockSize      = 512;
                    options.StoragePerformance = StoragePerformance.Fastest;
                    options.CalcBTreeOrder(Marshal.SizeOf(typeof(Guid)), Marshal.SizeOf(typeof(TestInfo)));
                    options.TransactionLog = new TransactionLog <Guid, TestInfo>(
                        new TransactionLogOptions <Guid, TestInfo>(
                            options.TransactionLogFileName,
                            options.KeySerializer,
                            options.ValueSerializer
                            )
                        );

                    //Now recover...
                    Dictionary <Guid, TestInfo> first = new Dictionary <Guid, TestInfo>();
                    Dictionary <Guid, TestInfo> sample;

                    using (var tree = new BPlusTree <Guid, TestInfo>(options))
                    {
                        tree.EnableCount();
                        Insert(tree, first, 1, 100, TimeSpan.FromMinutes(1));
                        tree.Commit();

                        Assert.AreEqual(100, tree.Count);

                        sample = new Dictionary <Guid, TestInfo>(first);
                        Insert(tree, sample, 7, 5000, TimeSpan.FromMinutes(1));

                        Assert.AreEqual(35100, tree.Count);

                        for (int i = 0; i < 1; i++)
                        {
                            foreach (var rec in tree)
                            {
                                var value = rec.Value;
                                value.UpdateCount++;
                                value.ReadCount++;
                                tree[rec.Key] = value;
                            }
                        }

                        File.Copy(options.TransactionLog.FileName, savelog.TempPath, true);
                        tree.Rollback();

                        TestInfo.AssertEquals(first, tree);
                    }

                    //file still has initial committed data
                    TestInfo.AssertEquals(first, BPlusTree <Guid, TestInfo> .EnumerateFile(options));

                    //restore the log and verify all data.
                    File.Copy(savelog.TempPath, options.TransactionLog.FileName, true);
                    using (var tree = new BPlusTree <Guid, TestInfo>(options))
                    {
                        TestInfo.AssertEquals(sample, tree);
                    }

                    //file still has initial committed data
                    TestInfo.AssertEquals(sample, BPlusTree <Guid, TestInfo> .EnumerateFile(options));
                }
        }