private void RegisterActiveBlock(Dictionary <string, INDArray> block, int blockIndex, IComputationHandler handler) { INDArray firstNamedBlock = block[block.First().Key]; if (IsBlockActive(blockIndex, handler)) { //block already registered as active, nothing to do here return; } RecordBlock recordBlock = new RecordBlock(block, blockIndex, firstNamedBlock.Shape[0], handler.GetSizeBytes(block.Values.ToArray()), handler) { Loaded = true, Active = true }; lock (_activeBlocks) { TotalActiveBlockSizeBytes += recordBlock.EstimatedSizeBytes; TotalActiveRecords += recordBlock.NumberRecords; if (!_activeBlocks.ContainsKey(blockIndex)) { _activeBlocks.Add(blockIndex, new HashSet <RecordBlock>()); } _activeBlocks[blockIndex].Add(recordBlock); } }
public void InsertRecord(params object[] fields) { long insertLocation; //Find available spot to place record.... if (_fileHeader.BlocksInFile == 0) { var block = RecordBlock.CreateNew(_stream, _schema, 0); _fileHeader.BlocksInFile++; WriteFileHeader(); insertLocation = block.Insert(fields); } else { var lastBlock = RecordBlock.LoadFromStream(_stream, _schema, _fileHeader.BlocksInFile - 1); if (lastBlock.BlockHeader.FreeBytes > _schema.GetRecordSize()) { insertLocation = lastBlock.Insert(fields); } else { var newBlock = RecordBlock.CreateNew(_stream, _schema, _fileHeader.BlocksInFile); insertLocation = newBlock.Insert(fields); _fileHeader.BlocksInFile++; WriteFileHeader(); } } _indexManager.AddRecord(fields, insertLocation, _schema); }
public Record[] Query(Func <Record, bool> query) { List <Record> records = new List <Record>(); for (int i = 0; i < this._fileHeader.BlocksInFile; i++) { var block = RecordBlock.LoadFromStream(_stream, _schema, i); records.AddRange(block.Query(query)); } return(records.ToArray()); }
public Record[] ReadAllRecords() { List <Record> records = new List <Record>(); for (int i = 0; i < this._fileHeader.BlocksInFile; i++) { var block = RecordBlock.LoadFromStream(_stream, _schema, i); records.AddRange(block.ReadAll()); } return(records.ToArray()); }
public Record ReadRecordFromPointer(long pointer) { int totalBlockLength = Constants.Lengths.BlockLength + Constants.Lengths.BlockHeaderLength; long pointerInBody = pointer - Constants.Lengths.FileHeaderLength; int blockNumber = (int)Math.Floor((decimal)pointerInBody / totalBlockLength); long remainingBytes = pointerInBody - (blockNumber * totalBlockLength); long trueIndex = (int)(remainingBytes / _schema.GetRecordSize()); var block = RecordBlock.LoadFromStream(_stream, _schema, blockNumber - 1); return(block.Read(trueIndex)); }
public Record ReadRecord(long index) { long totalIndex = 0; for (int i = 0; i < this._fileHeader.BlocksInFile; i++) { var block = RecordBlock.LoadFromStream(_stream, _schema, i); //the index is less than the upper bound of this block if (index < totalIndex + block.BlockHeader.RecordCount) { return(block.Read(index - totalIndex)); } totalIndex += block.BlockHeader.RecordCount; } throw new Exception(); }
private void DeregisterActiveBlock(RecordBlock recordBlock) { if (!IsBlockActive(recordBlock.BlockIndex, recordBlock.Handler)) { //block that should be de-registered is not even registered return; } lock (this) { TotalActiveBlockSizeBytes -= recordBlock.EstimatedSizeBytes; TotalActiveRecords -= recordBlock.NumberRecords; _activeBlocks[recordBlock.BlockIndex].Remove(recordBlock); if (_activeBlocks[recordBlock.BlockIndex].Count == 0) { _activeBlocks.Remove(recordBlock.BlockIndex); } } }
public void FreeBlock(int blockIndex, IComputationHandler handler) { lock (_activeBlocks) { if (!_activeBlocks.ContainsKey(blockIndex)) { _logger.Debug($"Unable to free block with index {blockIndex} for handler {handler} because no block with that information is currently active."); return; } RecordBlock toRemove = null; foreach (RecordBlock block in _activeBlocks[blockIndex]) { if (ReferenceEquals(block.Handler, handler)) { _logger.Debug($"Freeing block with index {blockIndex} for handler {handler}..."); CacheBlockConstrained(block.NamedBlockSections, blockIndex, handler); //_availableBlocksSemaphore.Release(); //_availableBlocksSemaphoreState++; toRemove = block; goto FoundBlock; } } _logger.Debug($"Unable to free block with index {blockIndex} for handler {handler} because no block with that information is currently active."); FoundBlock: DeregisterActiveBlock(toRemove); _logger.Debug($"Done freeing block with index {blockIndex} for handler {handler}."); } }