/// <summary> /// Encodes the current key/value to the stream. /// </summary> /// <param name="stream">the stream to write to</param> /// <param name="currentKey">the key to write</param> /// <param name="currentValue">the value to write</param> public override void Encode(BinaryStreamBase stream, TKey currentKey, TValue currentValue) { if (!m_encoding.ContainsEndOfStreamSymbol) { stream.Write((byte)1); } m_encoding.Encode(stream, m_prevKey, m_prevValue, currentKey, currentValue); currentKey.CopyTo(m_prevKey); currentValue.CopyTo(m_prevValue); }
protected override bool InsertUnlessFull(int index, TKey key, TValue value) { if (RecordCount >= m_maxRecordsPerNode) { return(false); } byte *start = GetWritePointerAfterHeader() + index * KeyValueSize; if (index != RecordCount) { WinApi.MoveMemory(start + KeyValueSize, start, (RecordCount - index) * KeyValueSize); } //Insert the data m_encoding.Encode(start, null, null, key, value); //key.Write(start); //value.Write(start + KeySize); //save the header IncrementOneRecord(KeyValueSize); return(true); }
protected int EncodeRecord(byte *stream, TKey prevKey, TValue prevValue, TKey currentKey, TValue currentValue) { return(m_encoding.Encode(stream, prevKey, prevValue, currentKey, currentValue)); }
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); }