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

            return(BufferHelper.ReadBufferInt64(buffer, offset));
        }
        byte[] FixedLengthSerialize(TreeNode <K, V> node)
        {
            var entrySize = this.keySerializer.Length + this.valueSerializer.Length;
            var size      = 16
                            + node.Entries.Length * entrySize
                            + node.ChildrenIds.Length * 4;

            if (size >= (1024 * 64))
            {
                throw new Exception("Serialized node size too large: " + size);
            }
            var buffer = new byte[size];

            // First 4 bytes of the buffer is parent id of this node
            BufferHelper.WriteBuffer(node.ParentId, buffer, 0);

            // Followed by 4 bytes of how many entries
            BufferHelper.WriteBuffer((uint)node.EntriesCount, buffer, 4);

            // Followed by 4 bytes of how manu child references
            BufferHelper.WriteBuffer((uint)node.ChildrenNodeCount, buffer, 8);

            // Start writing entries && child refs
            for (var i = 0; i < node.EntriesCount; i++)
            {
                // Write entry
                var entry = node.GetEntry(i);
                Buffer.BlockCopy(this.keySerializer.Serialize(entry.Item1), 0, buffer, 12 + i * entrySize, this.keySerializer.Length);
                Buffer.BlockCopy(this.valueSerializer.Serialize(entry.Item2), 0, buffer, 12 + i * entrySize + this.keySerializer.Length, this.valueSerializer.Length);
            }

            // Start writing child references
            var childrenIds = node.ChildrenIds;

            for (var i = 0; i < node.ChildrenNodeCount; i++)
            {
                // Write child refs
                BufferHelper.WriteBuffer(childrenIds[i], buffer, 12 + entrySize * node.EntriesCount + (i * 4));
            }

            // Return generated buffer
            return(buffer);
        }
        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));
        }
Exemplo n.º 4
0
        public void SetHeader(int field, long value)
        {
            if (isDisposed)
            {
                throw new ObjectDisposedException("Block");
            }

            if (field < 0)
            {
                throw new IndexOutOfRangeException();
            }

            // Update cache if this field is cached
            if (field < cachedHeaderValue.Length)
            {
                cachedHeaderValue[field] = value;
            }

            // Write in cached buffer
            BufferHelper.WriteBuffer((long)value, firstSector, field * 8);
            isFirstSectorDirty = true;
        }