byte[] VariableKeyLengthSerialize(TreeNode <K, V> node) { using (var m = new MemoryStream()) { // 4 bytes uint parent id m.Write(LittleEndianByteOrderHelper.GetBytes((uint)node.ParentId), 0, 4); // Followed by 4 bytes of how many entries m.Write(LittleEndianByteOrderHelper.GetBytes((uint)node.EntriesCount), 0, 4); // Followed by 4 bytes of how manu child references m.Write(LittleEndianByteOrderHelper.GetBytes((uint)node.ChildrenNodeCount), 0, 4); // Write entries.. for (var i = 0; i < node.EntriesCount; i++) { // Write entry var entry = node.GetEntry(i); var key = this.keySerializer.Serialize(entry.Item1); var value = this.valueSerializer.Serialize(entry.Item2); m.Write(LittleEndianByteOrderHelper.GetBytes((int)key.Length), 0, 4); m.Write(key, 0, key.Length); m.Write(value, 0, value.Length); } // Write child refs.. var childrenIds = node.ChildrenIds; for (var i = 0; i < node.ChildrenNodeCount; i++) { m.Write(LittleEndianByteOrderHelper.GetBytes((uint)childrenIds[i]), 0, 4); } return(m.ToArray()); } }
// // Private Methods // TreeNode <K, V> CreateFirstRoot() { // Write down the id of first node into the first block recordStorage.Create(LittleEndianByteOrderHelper.GetBytes((uint)2)); // Return a new node, this node should has id of 2 return(Create(null, null)); }
void AppendUInt32ToContent(IBlock block, uint value) { var contentLength = block.GetHeader(kBlockContentLength); if ((contentLength % 4) != 0) { throw new DataMisalignedException("Block content length not %4: " + contentLength); } block.Write(src: LittleEndianByteOrderHelper.GetBytes(value), srcOffset: 0, dstOffset: (int)contentLength, count: 4); }
/// <summary> /// Expect the upcoming 4 bytes are int32, read it and return it; /// </summary> public static int ExpectInt32(this Stream target) { var buff = new byte[4]; if (target.Read(buff) == 4) { return(LittleEndianByteOrderHelper.GetInt32(buff)); } else { throw new EndOfStreamException(); } }
public static double ExpectDouble(this Stream target) { var buff = new byte[8]; if (target.Read(buff) == 8) { return(LittleEndianByteOrderHelper.GetDouble(buff)); } else { throw new EndOfStreamException(); } }
public TreeNode <K, V> CreateNewRoot(K key, V value, uint leftNodeId, uint rightNodeId) { // Create new node as normal var node = Create(new Tuple <K, V>[] { new Tuple <K, V> (key, value) }, new uint[] { leftNodeId, rightNodeId }); // Make it the root node this.rootNode = node; recordStorage.Update(1u, LittleEndianByteOrderHelper.GetBytes(node.Id)); // Then return it return(this.rootNode); }
uint ReadUInt32FromTrailingContent(IBlock block) { var buffer = new byte[4]; var contentLength = block.GetHeader(kBlockContentLength); if ((contentLength % 4) != 0) { throw new DataMisalignedException("Block content length not %4: " + contentLength); } if (contentLength == 0) { throw new InvalidDataException("Trying to dequeue UInt32 from an empty block"); } block.Read(dst: buffer, dstOffset: 0, srcOffset: (int)contentLength - 4, count: 4); return(LittleEndianByteOrderHelper.GetUInt32(buffer)); }
public byte[] Serialize(long value) { return(LittleEndianByteOrderHelper.GetBytes(value)); }
public void MakeRoot(TreeNode <K, V> node) { this.rootNode = node; recordStorage.Update(1u, LittleEndianByteOrderHelper.GetBytes(node.Id)); }