public async Task CompressedAppendToNonEmptyStreamAndDecompress() { var sourceStr1 = "TEST this is TEST"; var sourceStr2 = "THIS IS another TEST"; var utils = new CompressionUtils(); using (var source1 = GetStreamWithContents(sourceStr1)) using (var source2 = GetStreamWithContents(sourceStr2)) using (var target = new MemoryStream()) { var chunk1Position = 0; await utils.CopyWithCompression(source1, target); var chunk2Position = target.Position; await utils.CopyWithCompression(source2, target); var decompressedBytes1 = (await utils.ReadWithDecompression(target, chunk1Position)).ToArray(); var decompressedStr1 = Encoding.GetString(decompressedBytes1); var decompressedBytes2 = (await utils.ReadWithDecompression(target, chunk2Position)).ToArray(); var decompressedStr2 = Encoding.GetString(decompressedBytes2); sourceStr1.Should().Be(decompressedStr1); sourceStr2.Should().Be(decompressedStr2); } }
public async Task CompressedAppendToEmptyStreamAndDecompress() { var sourceStr = "TEST this is TEST"; var utils = new CompressionUtils(); using (var source = GetStreamWithContents(sourceStr)) using (var target = new MemoryStream()) { await utils.CopyWithCompression(source, target); var bytes = (await utils.ReadWithDecompression(target, 0)).ToArray(); var decompressedStr = Encoding.GetString(bytes); sourceStr.Should().BeEquivalentTo(decompressedStr); } }
public async Task <List <Document> > GetChunk(long offset) { var compression = new CompressionUtils(); if (offset >= _file.Length) { // should NEVER happen throw new InvalidOperationException("Specified position doesn't exist in data file"); } using (var file = new FileStream(_filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { var segment = await compression.ReadWithDecompression(file, offset); using (var ms = new MemoryStream(segment.Array, segment.Offset, segment.Count)) { var batch = await DocumentSerializer.DeserializeBatch(ms).ConfigureAwait(false); return(batch); } } }