public void TestCloneWithCallLockV1() { var options = new BPlusTree <int, int> .Options(PrimitiveSerializer.Int32, PrimitiveSerializer.Int32) { CreateFile = CreatePolicy.IfNeeded, BTreeOrder = 4 }; var copy = options.Clone(); Assert.IsFalse(Object.ReferenceEquals(options, copy)); Assert.IsTrue(Object.ReferenceEquals(options.CallLevelLock, copy.CallLevelLock)); //If we get/set the lock prior to clone we will have the same lock instance. options.CallLevelLock = new SimpleReadWriteLocking(); copy = options.Clone(); Assert.IsFalse(Object.ReferenceEquals(options, copy)); Assert.IsTrue(Object.ReferenceEquals(options.CallLevelLock, copy.CallLevelLock)); }
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 TestICloneable() { ICloneable opt = new BPlusTree<int, int>.Options(PrimitiveSerializer.Int32, PrimitiveSerializer.Int32) { CreateFile = CreatePolicy.IfNeeded, BTreeOrder = 4 }; BPlusTree<int, int>.Options options = (BPlusTree<int, int>.Options)opt.Clone(); Assert.AreEqual(CreatePolicy.IfNeeded, options.CreateFile); Assert.AreEqual(4, options.MaximumChildNodes); Assert.AreEqual(4, options.MaximumValueNodes); }
public void TestICloneable() { ICloneable opt = new BPlusTree <int, int> .Options(PrimitiveSerializer.Int32, PrimitiveSerializer.Int32) { CreateFile = CreatePolicy.IfNeeded, BTreeOrder = 4 }; BPlusTree <int, int> .Options options = (BPlusTree <int, int> .Options)opt.Clone(); Assert.AreEqual(CreatePolicy.IfNeeded, options.CreateFile); Assert.AreEqual(4, options.MaximumChildNodes); Assert.AreEqual(4, options.MaximumValueNodes); }
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(); } } }
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(); } } }
public void TestCloneWithCallLockV1() { var options = new BPlusTree<int, int>.Options(PrimitiveSerializer.Int32, PrimitiveSerializer.Int32) { CreateFile = CreatePolicy.IfNeeded, BTreeOrder = 4 }; var copy = options.Clone(); Assert.IsFalse(Object.ReferenceEquals(options, copy)); Assert.IsTrue(Object.ReferenceEquals(options.CallLevelLock, copy.CallLevelLock)); //If we get/set the lock prior to clone we will have the same lock instance. options.CallLevelLock = new SimpleReadWriteLocking(); copy = options.Clone(); Assert.IsFalse(Object.ReferenceEquals(options, copy)); Assert.IsTrue(Object.ReferenceEquals(options.CallLevelLock, copy.CallLevelLock)); }
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); } } }
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(); } }
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); } } }