コード例 #1
0
        /// <summary>
        /// Construct a tree from given storage, using the specified comparer of key
        /// </summary>
        /// <param name="keySerializer">Tool to serialize node keys.</param>
        /// <param name="valueSerializer">Tool to serialize node values<param>
        /// <param name="recordStorage">Underlying tool for storage.</param>
        /// <param name="keyComparer">Key comparer.</param>
        public TreeDiskNodeManager(ISerializer <K> keySerializer
                                   , ISerializer <V> valueSerializer
                                   , IRecordStorage recordStorage
                                   , IComparer <K> keyComparer)
        {
            if (recordStorage == null)
            {
                throw new ArgumentNullException("nodeStorage");
            }

            this.recordStorage = recordStorage;
            this.serializer    = new TreeDiskNodeSerializer <K, V>(this, keySerializer, valueSerializer);
            this.KeyComparer   = keyComparer;
            this.EntryComparer = Comparer <Tuple <K, V> > .Create((a, b) =>
            {
                return(KeyComparer.Compare(a.Item1, b.Item1));
            });

            // The first record of nodeStorage stores id of root node,
            // if this record do not exist at the time this index instanitate,
            // then attempt to create it
            var firstBlockData = recordStorage.Find(1u);

            if (firstBlockData != null)
            {
                this.rootNode = Find(BufferHelper.ReadBufferUInt32(firstBlockData, 0));
            }
            else
            {
                this.rootNode = CreateFirstRoot();
            }
        }
コード例 #2
0
        public uint Deserialize(byte[] buffer, int offset, int length)
        {
            if (length != 4)
            {
                throw new ArgumentException("Invalid length: " + length);
            }

            return(BufferHelper.ReadBufferUInt32(buffer, offset));
        }
コード例 #3
0
        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));
        }