byte[] VariableKeyLengthSerialize(TreeNode <K, V> node)
        {
            using (var m = new MemoryStream())
            {
                // 4 bytes uint parent id
                m.Write(LittleEndianByteOrder.GetBytes((uint)node.ParentId), 0, 4);
                // Followed by 4 bytes of how many entries
                m.Write(LittleEndianByteOrder.GetBytes((uint)node.EntriesCount), 0, 4);
                // Followed by 4 bytes of how manu child references
                m.Write(LittleEndianByteOrder.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(LittleEndianByteOrder.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(LittleEndianByteOrder.GetBytes((uint)childrenIds[i]), 0, 4);
                }

                return(m.ToArray());
            }
        }
Ejemplo n.º 2
0
        public byte[] Serialize(byte[] value)
        {
            var data = new byte[
                4 +
                value.Length
                       ];

            Buffer.BlockCopy(
                src: LittleEndianByteOrder.GetBytes(value.Length),
                srcOffset: 0,
                dst: data,
                dstOffset: 0,
                count: 4
                );

            Buffer.BlockCopy(
                src: value,
                srcOffset: 0,
                dst: data,
                dstOffset: 4,
                count: value.Length
                );

            return(value);
        }
        //
        // Private Methods
        //

        TreeNode <K, V> CreateFirstRoot()
        {
            // Write down the id of first node into the first block
            recordStorage.Create(LittleEndianByteOrder.GetBytes((uint)2));

            // Return a new node, this node should has id of 2
            return(Create(null, null));
        }
Ejemplo n.º 4
0
        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: LittleEndianByteOrder.GetBytes(value), srcOffset: 0, dstOffset: (int)contentLength, count: 4);
        }
        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, LittleEndianByteOrder.GetBytes(node.Id));

            // Then return it
            return(this.rootNode);
        }
Ejemplo n.º 6
0
 public byte[] Serialize(long value)
 {
     return(LittleEndianByteOrder.GetBytes(value));
 }
Ejemplo n.º 7
0
 public static void WriteBuffer(int value, byte[] buffer, int bufferOffset)
 {
     Buffer.BlockCopy(LittleEndianByteOrder.GetBytes((int)value), 0, buffer, bufferOffset, 4);
 }
 public void MakeRoot(TreeNode <K, V> node)
 {
     this.rootNode = node;
     recordStorage.Update(1u, LittleEndianByteOrder.GetBytes(node.Id));
 }