Ejemplo n.º 1
0
        public static double ReadBufferDouble(byte[] buffer, int bufferOffset)
        {
            var doubleBuffer = new byte[8];

            Buffer.BlockCopy(buffer, bufferOffset, doubleBuffer, 0, 8);
            return(LittleEndianByteOrder.GetDouble(doubleBuffer));
        }
Ejemplo n.º 2
0
        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.º 3
0
        public static long ReadBufferInt64(byte[] buffer, int bufferOffset)
        {
            var longBuffer = new byte[8];

            Buffer.BlockCopy(buffer, bufferOffset, longBuffer, 0, 8);
            return(LittleEndianByteOrder.GetInt64(longBuffer));
        }
Ejemplo n.º 4
0
        public static int ReadBufferInt32(byte[] buffer, int bufferOffset)
        {
            var intBuffer = new byte[4];

            Buffer.BlockCopy(buffer, bufferOffset, intBuffer, 0, 4);
            return(LittleEndianByteOrder.GetInt32(intBuffer));
        }
Ejemplo n.º 5
0
        //
        // 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.º 6
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);
        }
Ejemplo n.º 7
0
        /// <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(LittleEndianByteOrder.GetInt32(buff));
            }
            else
            {
                throw new EndOfStreamException();
            }
        }
Ejemplo n.º 8
0
        public static double ExpectDouble(this Stream target)
        {
            var buff = new byte[8];

            if (target.Read(buff) == 8)
            {
                return(LittleEndianByteOrder.GetDouble(buff));
            }
            else
            {
                throw new EndOfStreamException();
            }
        }
Ejemplo n.º 9
0
        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.º 10
0
        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(LittleEndianByteOrder.GetUInt32(buffer));
        }
Ejemplo n.º 11
0
 public void MakeRoot(TreeNode <K, V> node)
 {
     this.rootNode = node;
     recordStorage.Update(1u, LittleEndianByteOrder.GetBytes(node.Id));
 }
Ejemplo n.º 12
0
 public byte[] Serialize(int value)
 {
     return(LittleEndianByteOrder.GetBytes(value));
 }
Ejemplo n.º 13
0
 public static void WriteBuffer(int value, byte[] buffer, int bufferOffset)
 {
     Buffer.BlockCopy(LittleEndianByteOrder.GetBytes((int)value), 0, buffer, bufferOffset, 4);
 }