Ejemplo n.º 1
0
        public int Deserialize(byte[] buffer, int offset, int length)
        {
            if (length != 4)
            {
                throw new ArgumentException("Invalid length: " + length);
            }

            return(BufferHelper.ReadBufferInt32(buffer, offset));
        }
        TreeNode <K, V> VariableKeyLengthDeserialize(uint assignId, byte[] buffer)
        {
            // First 4 bytes uint32 is parent id
            var parentId = BufferHelper.ReadBufferUInt32(buffer, 0);

            // Followed by 4 bytes uint32 of by how many entries this node has
            var entriesCount = BufferHelper.ReadBufferUInt32(buffer, 4);

            // Followed by 4 bytes uint32 of how many child reference this node has
            var childrenCount = BufferHelper.ReadBufferUInt32(buffer, 8);

            // Deserialize entries
            var entries = new Tuple <K, V> [entriesCount];
            var p       = 12;

            for (var i = 0; i < entriesCount; i++)
            {
                var keyLength = BufferHelper.ReadBufferInt32(buffer, p);
                var key       = this.keySerializer.Deserialize(buffer
                                                               , p + 4
                                                               , keyLength);
                var value = this.valueSerializer.Deserialize(buffer
                                                             , p + 4 + keyLength
                                                             , this.valueSerializer.Length);

                entries[i] = new Tuple <K, V>(key, value);

                p += 4 + keyLength + valueSerializer.Length;
            }

            // Decode child refs..
            var children = new uint[childrenCount];

            for (var i = 0; i < childrenCount; i++)
            {
                // Decode child refs..
                children[i] = BufferHelper.ReadBufferUInt32(buffer, (int)(p + (i * 4)));
            }

            // Reconstuct the node
            return(new TreeNode <K, V>(nodeManager, assignId, parentId, entries, children));
        }