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); }
public void Insert(SourceEntity source) { if (source.Title == null || source.Type == null) { throw new ArgumentNullException(); } options.CalcBTreeOrder(4, 4); options.CreateFile = CreatePolicy.Never; options.FileName = Path.GetFileName("C:/Users/Дмитрий/Desktop/DMDProject/DMDProject/TreeSource"); using (FileStream fs = new FileStream("C:/Users/Дмитрий/Desktop/DMDProject/DMDProject/Source", FileMode.OpenOrCreate)) { using (var tree = new BPlusTree <int, int>(options)) { KeyValuePair <int, int> kv = tree.Last(); int key = kv.Key; key++; source.Source_id = key.ToString(); tree.Add(Convert.ToInt32(source.Source_id), (int)fs.Length); } fs.Seek((int)fs.Length, 0); using (BinaryWriter bw = new BinaryWriter(fs)) { bw.Write(Convert.ToInt32(source.Source_id)); bw.Write(source.Title); bw.Write(source.Type); } } }
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 TestFirstAndLast() { using (BPlusTree <int, string> data = Create(Options)) { data.Add(1, "a"); data.Add(2, "b"); data.Add(3, "c"); data.Add(4, "d"); data.Add(5, "e"); Assert.AreEqual(1, data.First().Key); Assert.AreEqual("a", data.First().Value); data.Remove(1); Assert.AreEqual(2, data.First().Key); Assert.AreEqual("b", data.First().Value); Assert.AreEqual(5, data.Last().Key); Assert.AreEqual("e", data.Last().Value); data.Remove(5); Assert.AreEqual(4, data.Last().Key); Assert.AreEqual("d", data.Last().Value); data.Remove(4); data.Remove(3); KeyValuePair <int, string> kv; Assert.IsTrue(data.TryGetLast(out kv)); Assert.IsTrue(data.TryGetFirst(out kv)); data.Remove(2); Assert.IsFalse(data.TryGetLast(out kv)); Assert.IsFalse(data.TryGetFirst(out kv)); try { data.First(); Assert.Fail("Should raise InvalidOperationException"); } catch (InvalidOperationException) { } try { data.Last(); Assert.Fail("Should raise InvalidOperationException"); } catch (InvalidOperationException) { } } }
public void Insert(Author_DocumentEntity a_d) { if (a_d.Author_id == null) { throw new ArgumentNullException(); } foreach (var item in a_d.Author_id) { if (!Char.IsNumber(item)) { throw new FormatException(); } } if (a_d.Document_id == null) { throw new ArgumentNullException(); } foreach (var item in a_d.Document_id) { if (!Char.IsNumber(item)) { throw new FormatException(); } } options.CalcBTreeOrder(4, 4); options.CreateFile = CreatePolicy.Never; options.FileName = Path.GetFileName("C:/Users/Дмитрий/Desktop/DMDProject/DMDProject/TreeAuthor_Document"); using (FileStream fs = new FileStream("C:/Users/Дмитрий/Desktop/DMDProject/DMDProject/Author_Document", FileMode.OpenOrCreate)) { using (var tree = new BPlusTree <int, int>(options)) { KeyValuePair <int, int> kv = tree.Last(); int key = kv.Key; key++; a_d.A_D_id = key.ToString(); tree.Add(Convert.ToInt32(a_d.A_D_id), (int)fs.Length); } fs.Seek((int)fs.Length, 0); using (BinaryWriter bw = new BinaryWriter(fs)) { bw.Write(Convert.ToInt32(a_d.A_D_id)); bw.Write(Convert.ToInt32(a_d.Author_id)); bw.Write(Convert.ToInt32(a_d.Document_id)); } } }
public void Insert(AuthorEntity author) { if (author.Name == null) { throw new ArgumentNullException(); } if (author.Research_area == null) { author.Research_area = " "; } if (author.Institution == null) { author.Institution = " "; } options.CalcBTreeOrder(4, 4); options.CreateFile = CreatePolicy.Never; options.FileName = Path.GetFileName("C:/Users/Дмитрий/Desktop/DMDProject/DMDProject/TreeAuthor"); using (FileStream fs = new FileStream("C:/Users/Дмитрий/Desktop/DMDProject/DMDProject/Author", FileMode.OpenOrCreate)) { using (var tree = new BPlusTree <int, int>(options)) { KeyValuePair <int, int> kv = tree.Last(); int key = kv.Key; key++; author.Author_id = key.ToString(); tree.Add(Convert.ToInt32(author.Author_id), (int)fs.Length); } fs.Seek((int)fs.Length, 0); using (BinaryWriter bw = new BinaryWriter(fs)) { bw.Write(Convert.ToInt32(author.Author_id)); bw.Write(author.Name); bw.Write(author.Research_area); bw.Write(author.Institution); } } }
public void Insert(DocumentEntity document) { if (document.Title == null) { throw new ArgumentNullException(); } if (document.Document_type == null) { document.Document_type = " "; } if (document.Year == null) { document.Year = "0"; } foreach (var item in document.Year) { if (!Char.IsNumber(item)) { throw new FormatException(); } } if (document.Volume == null) { document.Volume = " "; } if (document.Url == null) { document.Url = " "; } if (document.Ee == null) { document.Ee = " "; } if (document.Keywords == null) { document.Keywords = " "; } if (document.Source_id == null) { document.Source_id = "0"; } options.CalcBTreeOrder(4, 4); options.CreateFile = CreatePolicy.Never; options.FileName = Path.GetFileName("C:/Users/Дмитрий/Desktop/DMDProject/DMDProject/TreeDocument"); using (FileStream fs = new FileStream("C:/Users/Дмитрий/Desktop/DMDProject/DMDProject/Document", FileMode.OpenOrCreate)) { using (var tree = new BPlusTree <int, int>(options)) { KeyValuePair <int, int> kv = tree.Last(); int key = kv.Key; key++; document.Document_id = key.ToString(); tree.Add(Convert.ToInt32(document.Document_id), (int)fs.Length); } fs.Seek((int)fs.Length, 0); using (BinaryWriter bw = new BinaryWriter(fs)) { bw.Write(Convert.ToInt32(document.Document_id)); bw.Write(document.Title); bw.Write(document.Document_type); bw.Write(Convert.ToInt32(document.Year)); bw.Write(document.Volume); bw.Write(document.Url); bw.Write(document.Ee); bw.Write(document.Keywords); bw.Write(Convert.ToInt32(document.Source_id)); } } }
private static void BasicTest() { BPlusTree <double, string> .OptionsV2 options = new BPlusTree <double, string> .OptionsV2(PrimitiveSerializer.Double, PrimitiveSerializer.String); options.CalcBTreeOrder(16, 24); options.CreateFile = CreatePolicy.Always; options.FileName = System.IO.Path.GetTempFileName(); using (var tree = new BPlusTree <double, string>(options)) { // Insertion to tree. // Note: numbers are NOT inserted sorted. tree.Add(30.1, "30.2"); tree.Add(10.1, "10.2"); tree.Add(20.1, "20.2"); tree.Add(80.1, "80.2"); tree.Add(40.1, "40.2"); tree.Add(60.1, "60.2"); tree.Add(70.1, "70.2"); tree.Add(50.1, "50.2"); // To get first element. // Since sorted, first element is: 10.1 KeyValuePair <double, string> first_with_Try; tree.TryGetFirst(out first_with_Try); // Similar to previous function. var first = tree.First(); // To get last element. // Since sorted, last element is: 80.1 KeyValuePair <double, string> last_with_Try; tree.TryGetLast(out last_with_Try); // Similar to previous function. var last = tree.Last(); // Given key get the value. // Key is valid, region.e., it is available in tree. // Hence it returns: "50.2" string value_of_valid_key; tree.TryGetValue(50.1, out value_of_valid_key); // Given key get the value. // Key is invalid, region.e., it is NOT available in tree. // Hence it returns: null (default value of int) string value_of_invalid_key; tree.TryGetValue(55, out value_of_invalid_key); // The "100" key is not available and no key is available greater than // that, hence .Current should return default value of key type (0 in this case). var key_not_available = tree.EnumerateFrom(100).GetEnumerator().Current; // Runtime error //var list = tree.ToList(); // Gets an enumerator. IEnumerator <KeyValuePair <double, string> > enumerator = tree.GetEnumerator(); // Iterating through items with enumerator. // starting from first item to last. while (enumerator.MoveNext()) { var item = enumerator.Current; } // Another syntac of iterations, which is automatically // calling "GetEnumerator" function. // starting from first item to last. foreach (var item in tree) { } // Iterates through items starting from given key: 40.1 (inclusively) // and goes till the last item. foreach (var item in tree.EnumerateFrom(39.1)) { } // Iterates through items starting from given index: 2 (inclusively) // and goes till the last item. foreach (var item in tree.EnumerateFrom(tree.ElementAtOrDefault(2).Key)) { } // Iterate from an item that is NOT available in collection // to the item which is neither available. foreach (var item in tree.EnumerateRange(20.5, 40.9)) { } // Gets the item at specific index. // All return valid values, but the last one which is // refereing to an index out-of-bound; the return of this // call is the default value for key and value. var element_at_0 = tree.ElementAtOrDefault(0); var element_at_1 = tree.ElementAtOrDefault(1); var element_at_2 = tree.ElementAtOrDefault(2); var element_at_3 = tree.ElementAtOrDefault(3); var element_at_100 = tree.ElementAtOrDefault(100); using (BPlusTree <double, string> data = new BPlusTree <double, string>(options)) { bool sT1 = data.TryAdd(1, "a"); bool sF1 = data.TryAdd(1, "a"); data[1] = "did it"; bool sT2 = data.TryUpdate(1, "a"); bool sT3 = data.TryUpdate(1, "c"); bool sT4 = data.TryUpdate(1, "d", "c"); bool sF2 = data.TryUpdate(1, "f", "c"); bool equality1 = "d".Equals(data[1]); bool sT5 = data.TryUpdate(1, "a", data[1]); bool equality2 = "a".Equals(data[1]); bool sF3 = data.TryUpdate(2, "b"); string val; bool st6 = data.TryRemove(1, out val) && val == "a"; bool sF4 = data.TryRemove(2, out val); bool notEqual = val.Equals("a"); } } }
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(); } } }