public void TestEnumeration() { var options = Options; options.BTreeOrder = 4; using (BPlusTree <int, string> data = new BPlusTree <int, string>(options)) { data.EnableCount(); data.DebugSetOutput(new StringWriter()); data.DebugSetValidateOnCheckpoint(true); for (int id = 0; id < 10; id++) { data.Add(id, id.ToString()); } using (IEnumerator <KeyValuePair <int, string> > enu = data.GetEnumerator()) { Assert.IsTrue(enu.MoveNext()); Assert.AreEqual(0, enu.Current.Key); for (int id = 2; id < 10; id++) { Assert.IsTrue(data.Remove(id)); } for (int id = 6; id < 11; id++) { data.Add(id, id.ToString()); } Assert.IsTrue(enu.MoveNext()); Assert.AreEqual(1, enu.Current.Key); Assert.IsTrue(enu.MoveNext()); Assert.AreEqual(6, enu.Current.Key); Assert.IsTrue(enu.MoveNext()); Assert.AreEqual(7, enu.Current.Key); Assert.IsTrue(data.Remove(8)); Assert.IsTrue(data.Remove(9)); Assert.IsTrue(data.Remove(10)); data.Add(11, 11.ToString()); Assert.IsTrue(enu.MoveNext()); Assert.AreEqual(11, enu.Current.Key); Assert.IsTrue(false == enu.MoveNext()); } data.Clear(); } }
public override void Initialize() { if (!Directory.Exists(path.Path)) { Directory.CreateDirectory(path.Path); } //string indexFileName = path.CreatePath ("index").Path; string blockFileName = path.CreatePath(name).Path; Console.WriteLine("Name: {0}", name); //Console.WriteLine ("Index: {0}", indexFileName); Console.WriteLine("Block: {0}", blockFileName); //bool indexExists = File.Exists (indexFileName); bool blockExists = File.Exists(blockFileName); options = new BPlusTree <byte[], long> .OptionsV2(BytesSerializer.RawBytes, PrimitiveSerializer.Int64); options.KeyComparer = ByteSequenceComparer.Shared; options.StorageType = StorageType.Memory; index = new BPlusTree <byte[], long> (options); index.DebugSetOutput(Console.Out); Console.WriteLine("Count: {0}", index.Count); Console.WriteLine(); fs = File.Open(blockFileName, FileMode.OpenOrCreate); if ((capacity = fs.Length) == 0) { fs.SetLength(capacity = 0x110000); } fs.Close(); mmfBlock = MemoryMappedFile.CreateFromFile(blockFileName, FileMode.Open); maMeta = mmfBlock.CreateViewAccessor(0, reservedBytes, MemoryMappedFileAccess.ReadWrite); if (!blockExists) { mh.MAGIC = 0x88DFB78311EFF07A; mh.WriteOffset = 0; mh.GrowRate = 0x100000; maMeta.Write <MetaHeader> (0, ref mh); } else { maMeta.Read <MetaHeader> (0, out mh); if (mh.MAGIC != 0x88DFB78311EFF07A) { throw new InvalidDataException("file magic doesn't match."); } } CreateIndex(); }