/// <summary> /// Extracts object data from leaf. /// </summary> /// <param name="block"></param> /// <returns></returns> unsafe List <ObjectInfo> ExtractObjectsFromLeaf(Block block) { fixed(byte *p = block.Data) { // We extract header and finish if not leaf. BTreeNode *node = (BTreeNode *)p; if (node->Flags != BTreeNodeOptions.Leaf) { return(null); } // We iterate through objects. List <ObjectInfo> all = new List <ObjectInfo>(node->Count); BTreeNodePart * data = (BTreeNodePart *)&node->Data; for (uint i = 0; i < node->Count; i++) { all.Add(new ObjectInfo(data[i].ObjectIndex, data[i].ObjectSize, data[i].BlockAddress)); } // We are finished. return(all); } }
public unsafe void Insert(ulong key, ulong value) { if (Find(root, key) != null) { //TODO: insert value into existing leaf throw new NotImplementedException(); } BTreeRecord* pointer = MakeRecord(value); if (root == null) { root = StartNewTree(key, pointer); return; } var leaf = FindLeaf(root, key); if (leaf->NumKeys < Order - 1) { InsertIntoLeaf(leaf, key, pointer); } else { root = InsertIntoLeafAfterSplitting(root, leaf, key, pointer); } }
/// <summary> /// Writes the objects to leaf. /// </summary> /// <param name="block">The block.</param> /// <param name="maxObjects">The max objects.</param> /// <param name="info">The info.</param> /// <returns></returns> unsafe bool WriteObjectsToLeaf(Block block, uint maxObjects, List <ObjectInfo> info) { if ((uint)info.Count > maxObjects) { return(false); fixed(byte *p = block.Data) { // Initialize header. BTreeNode *node = (BTreeNode *)p; node->Count = (ushort)info.Count; node->Flags = BTreeNodeOptions.Leaf; // And write data. BTreeNodePart *data = (BTreeNodePart *)&node->Data; for (int i = 0; i < info.Count; i++) { data[i].ObjectSize = info[i].Size; data[i].ObjectIndex = info[i].Index; data[i].BlockAddress = info[i].Address; } } // Sucessfully written it. return(true); } /// <summary> /// Inserts to ordered collection. /// </summary> /// <param name="info">The info.</param> /// <param name="newEntry">The new entry.</param> void InsertToOrderedCollection(List <ObjectInfo> info, ObjectInfo newEntry) { // First find a position to insert. int i; for (i = 0; i < info.Count; i++) { if (info[i].Index > newEntry.Index) { break; } } // We insert it now. info.Insert(i, newEntry); }
/// <summary> /// Creates an empty B+ tree. /// </summary> /// <returns>The address of B+ tree.</returns> public unsafe ulong CreateEmptyBTree() { // We allocate address. ulong address = Allocate(0); // We clear out the data and write it. byte[] data = new byte[provider.BlockSize]; fixed(byte *p = data) { BTreeNode *node = (BTreeNode *)p; node->Flags = BTreeNodeOptions.Leaf; node->Count = 0; } provider.Write(BlockType.BPlusTreeBlock, address, data); // And return address. return(address); }
public unsafe void Read(BinaryReader reader) { root = ReadKeys(reader, null); }
public unsafe void Dispose() { DisposeTree(root); root = null; }