void MoveValuesContent(AbstractBufferedWriter writer, uint wastefullFileId, uint pvlFileId) { const uint blockSize = 256 * 1024; var wasteFullStream = _keyValueDB.FileCollection.GetFile(wastefullFileId); var totalSize = wasteFullStream.GetSize(); var blocks = (int)((totalSize + blockSize - 1) / blockSize); var wasteInMemory = new byte[blocks][]; var pos = 0UL; var readLimiter = new BytesPerSecondLimiter(_keyValueDB._compactorReadBytesPerSecondLimit); for (var i = 0; i < blocks; i++) { _cancellation.ThrowIfCancellationRequested(); wasteInMemory[i] = new byte[blockSize]; var readSize = totalSize - pos; if (readSize > blockSize) { readSize = blockSize; } wasteFullStream.RandomRead(wasteInMemory[i], 0, (int)readSize, pos, true); pos += readSize; readLimiter.Limit(pos); } _root.Iterate((valueFileId, valueOfs, valueSize) => { if (valueFileId != wastefullFileId) { return; } var size = (uint)Math.Abs(valueSize); _newPositionMap.Add(((ulong)wastefullFileId << 32) | valueOfs, ((ulong)pvlFileId << 32) + (ulong)writer.GetCurrentPosition()); pos = valueOfs; while (size > 0) { _cancellation.ThrowIfCancellationRequested(); var blockId = pos / blockSize; var blockStart = pos % blockSize; var writeSize = (uint)(blockSize - blockStart); if (writeSize > size) { writeSize = size; } writer.WriteBlock(wasteInMemory[blockId], (int)blockStart, (int)writeSize); size -= writeSize; pos += writeSize; _writerBytesPerSecondLimiter.Limit((ulong)writer.GetCurrentPosition()); } }); }
StorageValue StoreContent(ByteBuffer content) { var result = new StorageValue(); result.Compressed = false; result.ContentLength = (uint)content.Length; if (_pureValueFile == null) { StartNewPureValueFile(); } result.FileId = _pureValueFile.Index; result.FileOfs = (uint)_pureValueFileWriter.GetCurrentPosition(); _pureValueFileWriter.WriteBlock(content); if (_pureValueFileWriter.GetCurrentPosition() >= _maxFileSize) { _pureValueFileWriter.FlushBuffer(); StartNewPureValueFile(); } return(result); }
public void Put(ByteBuffer key, ByteBuffer content) { if (key.Length != _keySize) { throw new ArgumentException("Key has wrong Length not equal to KeySize"); } if (content.Length == 0) { throw new ArgumentException("Empty Content cannot be stored"); } var k = new ByteStructs.Key20(key); CacheValue cacheValue; if (_cache.TryGetValue(k, out cacheValue)) { return; } cacheValue.AccessRate = 1; again: var writer = _cacheValueWriter; while (writer == null || writer.GetCurrentPosition() + content.Length > _sizeLimitOfOneValueFile) { StartNewValueFile(); writer = _cacheValueWriter; } lock (writer) { if (writer != _cacheValueWriter) { goto again; } cacheValue.FileId = _cacheValueFileId; cacheValue.FileOfs = (uint)_cacheValueWriter.GetCurrentPosition(); _cacheValueWriter.WriteBlock(content); _cacheValueFile.Flush(); } cacheValue.ContentLength = (uint)content.Length; _cache.TryAdd(k, cacheValue); }
void UpdateTransactionLogInBTreeRoot(IBTreeRootNode btreeRoot) { if (btreeRoot.TrLogFileId != _fileIdWithTransactionLog && btreeRoot.TrLogFileId != 0) { _compactorScheduler?.AdviceRunning(); } btreeRoot.TrLogFileId = _fileIdWithTransactionLog; if (_writerWithTransactionLog != null) { btreeRoot.TrLogOffset = (uint)_writerWithTransactionLog.GetCurrentPosition(); } else { btreeRoot.TrLogOffset = 0; } }