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);
        }
Beispiel #2
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;
        }