예제 #1
0
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                Flush();

                cacheOutput.Dispose();

                if (isWritten && isFlushed)
                {
                    var fileInfo = new FileInfo(indexDirectory.GetFullPath(indexFileName));

                    using (var fs = new FileStream(indexDirectory.GetFullPath(indexFileName), FileMode.Open, FileAccess.Read))
                    {
                        var fullName = indexDirectory.GetFullName(indexFileName);

                        var options = new GridFSUploadOptions
                        {
                            Metadata = new BsonDocument
                            {
                                ["WrittenTime"] = fileInfo.LastWriteTimeUtc
                            }
                        };

                        try
                        {
                            indexDirectory.Bucket.UploadFromStream(fullName, indexFileName, fs, options);
                        }
                        catch (MongoBulkWriteException ex) when(ex.WriteErrors.Any(x => x.Code == 11000))
                        {
                            indexDirectory.Bucket.Delete(fullName);
                            indexDirectory.Bucket.UploadFromStream(fullName, indexFileName, fs, options);
                        }
                    }
                }
            }
        }
예제 #2
0
        public MongoIndexInput(MongoDirectory indexDirectory, IOContext context, string indexFileName)
            : base(indexDirectory.GetFullName(indexFileName))
        {
            this.indexDirectory = indexDirectory;
            this.indexFileName  = indexFileName;

            this.context = context;

            try
            {
                var file = indexDirectory.FindFile(indexFileName);

                if (file != null)
                {
                    var fileInfo = new FileInfo(indexDirectory.GetFullPath(indexFileName));

                    var writtenTime = file.Metadata["WrittenTime"].ToUniversalTime();

                    if (!fileInfo.Exists || fileInfo.LastWriteTimeUtc < writtenTime)
                    {
                        using (var fs = new FileStream(fileInfo.FullName, FileMode.Create, FileAccess.Write))
                        {
                            var fullName = indexDirectory.GetFullName(indexFileName);

                            indexDirectory.Bucket.DownloadToStream(fullName, fs);
                        }
                    }
                }
            }
            catch (GridFSFileNotFoundException)
            {
                throw new FileNotFoundException();
            }

            cacheInput = indexDirectory.CacheDirectory.OpenInput(indexFileName, context);
        }