//todo: add XMLDOC
        public bool TryAddNonIndexedValue(byte[] key, byte[] value)
        {
            //todo: implement correctly (depends on record being class or struct)
            Record record = new Record(key, value);
            int recordIndex;
            if (nonIndexedRecordsByKey.TryGetValue(key, out recordIndex))
            {
                nonIndexedRecords[recordIndex] = record;
                MarkNonIndexedValueDirty(recordIndex);
                return true;
            }

            recordIndex = nonIndexedRecords.Count;

            if (recordIndex < maxNonIndexedRecordsCount)
            {
                nonIndexedRecords.Add(record);
                nonIndexedRecordsByKey.Add(key, recordIndex);
                MarkNonIndexedValueDirty(recordIndex);
                return true;
            }

            return false;
        }
        public Block ReadBlock(long offset)
        {
            stream.Position = offset;
            int count = reader.ReadInt16();

            byte[] blockData = reader.ReadBytes(count * (keySize + valueSize));
            Record[] records = new Record[count];

            for (int i = 0, recordOffset = 0; i < count; i++, recordOffset += keySize + valueSize)
            {
                ByteArrayRef key = new ByteArrayRef(blockData, recordOffset, keySize);
                ByteArrayRef value = new ByteArrayRef(blockData, recordOffset + keySize, valueSize);

                Record record = new Record(key, value);
                records[i] = record;
            }

            return new Block(records);
        }
        private void FindRecords(Dictionary<byte[], byte[]> res, Record[] recordsA, List<Record> recordsB, int firstByte)
        {
            int ofsA = 0, ofsB = 0;

            while (ofsA < recordsA.Length && ofsB < recordsB.Count)
            {
                Record recordA = recordsA[ofsA];
                Record recordB = recordsB[ofsB];

                int diff = CompareKeys(recordA.Key, recordB.Key, firstByte);

                if (diff < 0)
                {
                    ofsA++;
                }
                else if (diff > 0)
                {
                    ofsB++;
                }
                else
                {
                    //todo: copy value to avoid rewrite by client (see ByteArrayRef implementation)?
                    res.Add(recordB.Key.ToArray(), recordA.Value.ToArray());
                    ofsA++;
                    ofsB++;
                }
            }
        }
        private List<Record> MergeRecords(Record[] recordsA, List<Record> recordsB, int firstByte)
        {
            List<Record> res = new List<Record>(recordsA.Length + recordsB.Count);

            int ofsA = 0, ofsB = 0;

            while (ofsA < recordsA.Length && ofsB < recordsB.Count)
            {
                Record recordA = recordsA[ofsA];
                Record recordB = recordsB[ofsB];

                int diff = CompareKeys(recordA.Key, recordB.Key, firstByte);

                if (diff < 0)
                {
                    res.Add(recordA);
                    ofsA++;
                }
                else if (diff > 0)
                {
                    res.Add(recordB);
                    ofsB++;
                }
                else
                {
                    res.Add(recordB);
                    ofsA++;
                    ofsB++;
                }
            }
            while (ofsA < recordsA.Length)
            {
                res.Add(recordsA[ofsA++]);
            }
            while (ofsB < recordsB.Count)
            {
                res.Add(recordsB[ofsB++]);
            }

            return res;
        }
Example #5
0
 public Block(Record[] records)
 {
     this.records = records;
 }