/// <summary> /// Compacts the specified from buffer. /// </summary> /// <param name="fromBuffer">From buffer.</param> /// <param name="indexs">The indexes.</param> public static void Compact(ref InternalStorageBuffer fromBuffer, ref ConcurrentDictionary <string, BinaryStorageKey> indexs) { scrubBuffer.ResetBuffer(); foreach (var storageKey in indexs.Keys) { // Scrub out old data ;) BinaryStorageKey tmpKey; if (indexs.TryRemove(storageKey, out tmpKey)) { long pos = tmpKey.Position; int len = tmpKey.Length; var bytes = fromBuffer.FetchFromBuffer(pos, len); var idx = scrubBuffer.InsertSegment(bytes); tmpKey.Position = idx; indexs[storageKey] = tmpKey; } } // now copy over the data ;) fromBuffer.ResetBuffer(); scrubBuffer.TransferTo(ref fromBuffer); }
private void MoveBufferToFile() { // time to dump buffer var offSet = _file.Length; _file.Seek(offSet, SeekOrigin.Begin); _file.Write(_internalBuffer.Buffer, 0, _internalBuffer.UsedStorage); foreach (var tmpKey in _bufferIndexes.Keys) { BinaryStorageKey bufferKeyValue; if (!_bufferIndexes.TryRemove(tmpKey, out bufferKeyValue)) { throw new Exception("Invalid Buffer State [ " + tmpKey + " ]"); } var newPos = offSet + bufferKeyValue.Position; var newLen = bufferKeyValue.Length; BinaryStorageKey tmp; if (_lstIndexes.TryGetValue(tmpKey, out tmp)) { // adjust pos to reflect location in file ;) tmp.Length = newLen; tmp.Position = newPos; _lstIndexes[tmpKey] = tmp; } else { tmp = new BinaryStorageKey { Length = newLen, Position = newPos }; _lstIndexes.TryAdd(tmpKey, tmp); } } // reset buffer and try again ;) _internalBuffer.ResetBuffer(); }