Permalink
Browse files

refactor the stream compression

  • Loading branch information...
1 parent 58e672d commit 35fd0b316321a87c7f37869648530e2330bff07f @richorama richorama committed Nov 27, 2013
Showing with 37 additions and 33 deletions.
  1. +36 −32 AzureDirectory/AzureIndexOutput.cs
  2. +1 −1 AzureDirectory/AzureLock.cs
@@ -64,38 +64,7 @@ protected override void Dispose(bool disposing)
// optionally put a compressor around the blob stream
if (_azureDirectory.ShouldCompressFile(_name))
{
- // unfortunately, deflate stream doesn't allow seek, and we need a seekable stream
- // to pass to the blob storage stuff, so we compress into a memory stream
- MemoryStream compressedStream = new MemoryStream();
-
- try
- {
- using (var indexInput = CacheDirectory.OpenInput(fileName))
- using (DeflateStream compressor = new DeflateStream(compressedStream, CompressionMode.Compress, true))
- {
- // compress to compressedOutputStream
- byte[] bytes = new byte[indexInput.Length()];
- indexInput.ReadBytes(bytes, 0, (int)bytes.Length);
- compressor.Write(bytes, 0, (int)bytes.Length);
- }
-
- // seek back to beginning of comrpessed stream
- compressedStream.Seek(0, SeekOrigin.Begin);
-
- Debug.WriteLine(string.Format("COMPRESSED {0} -> {1} {2}% to {3}",
- originalLength,
- compressedStream.Length,
- ((float)compressedStream.Length / (float)originalLength) * 100,
- _name));
- }
- catch
- {
- // release the compressed stream resources if an error occurs
- compressedStream.Dispose();
- throw;
- }
-
- blobStream = compressedStream;
+ blobStream = CompressStream(fileName, originalLength);
}
else
{
@@ -134,6 +103,41 @@ protected override void Dispose(bool disposing)
}
}
+ private MemoryStream CompressStream(string fileName, long originalLength)
+ {
+ // unfortunately, deflate stream doesn't allow seek, and we need a seekable stream
+ // to pass to the blob storage stuff, so we compress into a memory stream
+ MemoryStream compressedStream = new MemoryStream();
+
+ try
+ {
+ using (var indexInput = CacheDirectory.OpenInput(fileName))
+ using (var compressor = new DeflateStream(compressedStream, CompressionMode.Compress, true))
+ {
+ // compress to compressedOutputStream
+ byte[] bytes = new byte[indexInput.Length()];
+ indexInput.ReadBytes(bytes, 0, (int)bytes.Length);
+ compressor.Write(bytes, 0, (int)bytes.Length);
+ }
+
+ // seek back to beginning of comrpessed stream
+ compressedStream.Seek(0, SeekOrigin.Begin);
+
+ Debug.WriteLine(string.Format("COMPRESSED {0} -> {1} {2}% to {3}",
+ originalLength,
+ compressedStream.Length,
+ ((float)compressedStream.Length / (float)originalLength) * 100,
+ _name));
+ }
+ catch
+ {
+ // release the compressed stream resources if an error occurs
+ compressedStream.Dispose();
+ throw;
+ }
+ return compressedStream;
+ }
+
public override long Length
{
get
@@ -28,7 +28,7 @@ override public bool IsLocked()
var blob = _azureDirectory.BlobContainer.GetBlobReferenceFromServer(_lockFile);
try
{
- Debug.Print("IsLockeD() : {0}", _leaseid);
+ Debug.Print("IsLocked() : {0}", _leaseid);
if (String.IsNullOrEmpty(_leaseid))
{
var tempLease = blob.AcquireLease(TimeSpan.FromSeconds(60), _leaseid);

0 comments on commit 35fd0b3

Please sign in to comment.