Пример #1
0
        public bool TryFind(T record, out T resultRecord)
        {
            _currentDepth = 0;
            resultRecord  = default(T);
            var node = FindExternalNode(record.GetHashCode());

            if (node.IndexOfBlock == -1)
            {
                return(false);
            }
            if (node.RecordCount == 0)
            {
                return(false);
            }
            _workingBlock1 = new Block <T>(MaxRecords);
            _workingBlock1.FromByteArray(_blockFile.ReadData(node.IndexOfBlock));
            if (node.RecordCount <= MaxRecords)
            {
                return(_workingBlock1.TryFindRecord(record, out resultRecord));
            }
            else
            {
                return(_workingBlock1.TryFindRecord(record, out resultRecord) || TryFindOverflow(_workingBlock1, record, out resultRecord));
            }
        }
Пример #2
0
        public bool Add(T record, out int index)
        {
            _workingBlock = new Block <T>(MaxRecords);
            if (_partiallyFullBlocks.Count > 0)
            {
                index = _partiallyFullBlocks.Min(i => i);
                _workingBlock.FromByteArray((_file.ReadData(index)));
                if (_workingBlock.AddRecord(record))
                {
                    if (_workingBlock.ValidCount == MaxRecords)
                    {
                        _partiallyFullBlocks.Remove(index);
                    }
                    _file.WriteData(index, _workingBlock.ToByteArray());
                    return(true);
                }
            }
            else if (_freeBlocks.Count > 0)
            {
                index = _freeBlocks.Min(i => i);
                _workingBlock.FromByteArray((_file.ReadData(index)));
                if (_workingBlock.AddRecord(record))
                {
                    _freeBlocks.Remove(index);
                    _partiallyFullBlocks.Add(index);
                    _file.WriteData(index, _workingBlock.ToByteArray());
                    return(true);
                }
            }
            else
            {
                if (_workingBlock.AddRecord(record))
                {
                    index = _file.WriteData(_workingBlock.ToByteArray());
                    if (_workingBlock.ValidCount < MaxRecords)
                    {
                        _partiallyFullBlocks.Add(index);
                    }
                    return(true);
                }
            }

            index = -1;
            return(false);
        }
Пример #3
0
        private bool TryFindOverflow(Block <T> block, T record, out T resultRecord)
        {
            resultRecord = default(T);
            var index = block.NextOverflowBlockIndex;

            if (index == -1)
            {
                throw new Exception($"This should not happened, index of block: {index} and should not be -1");
            }
            while (index != -1)
            {
                _overflowBlock = new Block <T>(MaxRecordsInOverflow);
                _overflowBlock.FromByteArray(_overflowFile.ReadData(index));

                if (_overflowBlock.TryFindRecord(record, out resultRecord))
                {
                    return(true);
                }

                index = _overflowBlock.NextOverflowBlockIndex;
            }

            return(false);
        }