/// <summary> /// Decrement the reference count of a chunk by its key. If the reference count reaches zero, the chunk is deleted. /// </summary> /// <param name="chunkKey">Chunk key.</param> /// <returns>Boolean indicating if the chunk should be garbage collected.</returns> public override bool DecrementChunkRefcount(string chunkKey) { if (String.IsNullOrEmpty(chunkKey)) { throw new ArgumentNullException(nameof(chunkKey)); } chunkKey = DedupeCommon.SanitizeString(chunkKey); DedupeChunk chunk = GetChunkMetadata(chunkKey); if (chunk == null) { return(false); } chunk.RefCount = chunk.RefCount - 1; if (chunk.RefCount < 1) { _ORM.Delete <DedupeChunk>(chunk); return(true); } else { _ORM.Update <DedupeChunk>(chunk); return(false); } }
/// <summary> /// Increment reference count for a chunk by its key. If the chunk does not exist, it is created. /// </summary> /// <param name="chunkKey">Chunk key.</param> /// <param name="length">The chunk length, used when creating the chunk.</param> public override void IncrementChunkRefcount(string chunkKey, int length) { if (String.IsNullOrEmpty(chunkKey)) { throw new ArgumentNullException(nameof(chunkKey)); } chunkKey = DedupeCommon.SanitizeString(chunkKey); lock (_ChunkLock) { DedupeChunk chunk = GetChunkMetadata(chunkKey); if (chunk != null) { chunk.RefCount = chunk.RefCount + 1; _ORM.Update <DedupeChunk>(chunk); } else { chunk = new DedupeChunk(chunkKey, length, 1); _ORM.Insert <DedupeChunk>(chunk); } } }
static void WriteChunk(DedupeChunk data) { using (var fs = new FileStream( _ChunkDir + data.Key, FileMode.Create, FileAccess.Write, FileShare.None, 0x1000, FileOptions.WriteThrough)) { fs.Write(data.Data, 0, data.Data.Length); } }
static void WriteChunk(DedupeChunk data) { // File.WriteAllBytes("Chunks\\" + data.Key, data.Value); using (var fs = new FileStream( "Chunks\\" + data.Key, FileMode.Create, FileAccess.Write, FileShare.None, 0x1000, FileOptions.WriteThrough)) { fs.Write(data.Data, 0, data.Data.Length); } }
/// <summary> /// Retrieve metadata for a given chunk by its key. /// </summary> /// <param name="chunkKey">Chunk key.</param> /// <returns>Chunk metadata.</returns> public override DedupeChunk GetChunkMetadata(string chunkKey) { if (String.IsNullOrEmpty(chunkKey)) { throw new ArgumentNullException(nameof(chunkKey)); } chunkKey = DedupeCommon.SanitizeString(chunkKey); DbExpression e = new DbExpression( _ORM.GetColumnName <DedupeChunk>(nameof(DedupeChunk.Key)), DbOperators.Equals, chunkKey); DedupeChunk ret = _ORM.SelectFirst <DedupeChunk>(e); return(ret); }
// Called during store operations, consider using FileStream with FileOptions.WriteThrough to ensure crash consistency static void WriteChunk(DedupeChunk data) { File.WriteAllBytes("Chunks\\" + data.Key, data.Data); }