public AzureIndexOutput(AzureDirectory azureDirectory, ICloudBlob blob) { if (azureDirectory == null) throw new ArgumentNullException(nameof(azureDirectory)); _azureDirectory = azureDirectory; _fileMutex = SyncMutexManager.GrabMutex(_azureDirectory, _name); _fileMutex.WaitOne(); try { _blobContainer = _azureDirectory.BlobContainer; _blob = blob; _name = blob.Uri.Segments[blob.Uri.Segments.Length - 1]; // create the local cache one we will operate against... _indexOutput = CacheDirectory.CreateOutput(_name); } finally { _fileMutex.ReleaseMutex(); } }
public AzureLock(string lockFile, AzureDirectory directory) { _lockFile = lockFile; _azureDirectory = directory; }
public override void Close() { _fileMutex.WaitOne(); try { #if FULLDEBUG Debug.WriteLine(String.Format("CLOSED READSTREAM local {0}", _name)); #endif _indexInput.Close(); _indexInput = null; _azureDirectory = null; _blobContainer = null; _blob = null; GC.SuppressFinalize(this); } finally { _fileMutex.ReleaseMutex(); } }
public AzureIndexInput(AzureDirectory azuredirectory, ICloudBlob blob) { if (azuredirectory == null) throw new ArgumentNullException(nameof(azuredirectory)); _name = blob.Uri.Segments[blob.Uri.Segments.Length - 1]; _azureDirectory = azuredirectory; #if FULLDEBUG Debug.WriteLine(String.Format("opening {0} ", _name)); #endif _fileMutex = SyncMutexManager.GrabMutex(_azureDirectory, _name); _fileMutex.WaitOne(); try { _blobContainer = azuredirectory.BlobContainer; _blob = blob; var fileName = _name; var fFileNeeded = false; if (!CacheDirectory.FileExists(fileName)) { fFileNeeded = true; } else { long cachedLength = CacheDirectory.FileLength(fileName); string blobLengthMetadata; bool hasMetadataValue = blob.Metadata.TryGetValue("CachedLength", out blobLengthMetadata); long blobLength = blob.Properties.Length; if (hasMetadataValue) long.TryParse(blobLengthMetadata, out blobLength); string blobLastModifiedMetadata; long longLastModified = 0; DateTime blobLastModifiedUTC = blob.Properties.LastModified.Value.UtcDateTime; if (blob.Metadata.TryGetValue("CachedLastModified", out blobLastModifiedMetadata)) { if (long.TryParse(blobLastModifiedMetadata, out longLastModified)) blobLastModifiedUTC = new DateTime(longLastModified).ToUniversalTime(); } if (cachedLength != blobLength) fFileNeeded = true; else { // cachedLastModifiedUTC was not ouputting with a date (just time) and the time was always off long unixDate = CacheDirectory.FileModified(fileName); DateTime start = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); var cachedLastModifiedUTC = start.AddMilliseconds(unixDate).ToUniversalTime(); if (cachedLastModifiedUTC != blobLastModifiedUTC) { var timeSpan = blobLastModifiedUTC.Subtract(cachedLastModifiedUTC); if (timeSpan.TotalSeconds > 1) fFileNeeded = true; else { #if FULLDEBUG Debug.WriteLine(timeSpan.TotalSeconds); #endif // file not needed } } } } // if the file does not exist // or if it exists and it is older then the lastmodified time in the blobproperties (which always comes from the blob storage) if (fFileNeeded) { if (_azureDirectory.ShouldCompressFile(_name)) { InflateStream(fileName); } else { using (var fileStream = _azureDirectory.CreateCachedOutputAsStream(fileName)) { // get the blob _blob.DownloadToStream(fileStream); fileStream.Flush(); Debug.WriteLine(string.Format("GET {0} RETREIVED {1} bytes", _name, fileStream.Length)); } } // and open it as an input _indexInput = CacheDirectory.OpenInput(fileName); } else { #if FULLDEBUG Debug.WriteLine(String.Format("Using cached file for {0}", _name)); #endif // open the file in read only mode _indexInput = CacheDirectory.OpenInput(fileName); } } finally { _fileMutex.ReleaseMutex(); } }
public AzureIndexInput(AzureIndexInput cloneInput) { _fileMutex = SyncMutexManager.GrabMutex(cloneInput._azureDirectory, cloneInput._name); _fileMutex.WaitOne(); try { #if FULLDEBUG Debug.WriteLine(String.Format("Creating clone for {0}", cloneInput._name)); #endif _azureDirectory = cloneInput._azureDirectory; _blobContainer = cloneInput._blobContainer; _blob = cloneInput._blob; _indexInput = cloneInput._indexInput.Clone() as IndexInput; } catch (Exception) { // sometimes we get access denied on the 2nd stream...but not always. I haven't tracked it down yet // but this covers our tail until I do Debug.WriteLine(String.Format("Dagnabbit, falling back to memory clone for {0}", cloneInput._name)); } finally { _fileMutex.ReleaseMutex(); } }