/// <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); } }
/// <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); }