Beispiel #1
0
        public static void Create(EncodingDefinition encodingMethod, BinaryStreamPointerBase stream, int blockSize, byte level, uint startingNodeIndex, Func <uint> getNextNewNodeIndex, SparseIndexWriter <TKey> sparseIndex, TreeStream <TKey, TValue> treeStream)
        {
            NodeHeader <TKey> header = new NodeHeader <TKey>(level, blockSize);
            PairEncodingBase <TKey, TValue> encoding = Library.Encodings.GetEncodingMethod <TKey, TValue>(encodingMethod);

            SparseIndexWriter <TKey> sparseIndex1 = sparseIndex;
            Func <uint> getNextNewNodeIndex1      = getNextNewNodeIndex;
            int         maximumStorageSize        = encoding.MaxCompressionSize;

            byte[] buffer1 = new byte[maximumStorageSize];
            if ((header.BlockSize - header.HeaderSize) / maximumStorageSize < 4)
            {
                throw new Exception("Tree must have at least 4 records per node. Increase the block size or decrease the size of the records.");
            }

            //InsideNodeBoundary = m_BoundsFalse;
            header.NodeIndex             = startingNodeIndex;
            header.RecordCount           = 0;
            header.ValidBytes            = (ushort)header.HeaderSize;
            header.LeftSiblingNodeIndex  = uint.MaxValue;
            header.RightSiblingNodeIndex = uint.MaxValue;
            header.LowerKey.SetMin();
            header.UpperKey.SetMax();

            byte *writePointer = stream.GetWritePointer(blockSize * header.NodeIndex, blockSize);

            fixed(byte *buffer = buffer1)
            {
                TKey   key1   = new TKey();
                TKey   key2   = new TKey();
                TValue value1 = new TValue();
                TValue value2 = new TValue();

                key1.Clear();
                key2.Clear();
                value1.Clear();
                value2.Clear();

Read1:
                //Read part 1.
                if (treeStream.Read(key1, value1))
                {
                    if (header.RemainingBytes < maximumStorageSize)
                    {
                        if (header.RemainingBytes < encoding.Encode(buffer, key2, value2, key1, value1))
                        {
                            NewNodeThenInsert(header, sparseIndex1, getNextNewNodeIndex1(), writePointer, key1);
                            key2.Clear();
                            value2.Clear();
                            writePointer = stream.GetWritePointer(blockSize * header.NodeIndex, blockSize);
                        }
                    }

                    byte *stream1 = writePointer + header.ValidBytes;
                    header.ValidBytes += (ushort)encoding.Encode(stream1, key2, value2, key1, value1);
                    header.RecordCount++;

                    //Read part 2.
                    if (treeStream.Read(key2, value2))
                    {
                        if (header.RemainingBytes < maximumStorageSize)
                        {
                            if (header.RemainingBytes < encoding.Encode(buffer, key1, value1, key2, value2))
                            {
                                NewNodeThenInsert(header, sparseIndex1, getNextNewNodeIndex1(), writePointer, key2);
                                key1.Clear();
                                value1.Clear();
                                writePointer = stream.GetWritePointer(blockSize * header.NodeIndex, blockSize);
                            }
                        }
                        byte *stream2 = writePointer + header.ValidBytes;
                        header.ValidBytes += (ushort)encoding.Encode(stream2, key1, value1, key2, value2);
                        header.RecordCount++;

                        goto Read1;
                    }
                }
            }

            header.Save(writePointer);
        }