EnsureIndexes() public method

Ensures that the proper indexes for GridFS exist (only creates the new indexes if there are fewer than 1000 GridFS files).
public EnsureIndexes ( ) : void
return void
コード例 #1
0
        private void OpenAppend()
        {
            _gridFS.EnsureIndexes();

            _length   = _fileInfo.Length;
            _position = _fileInfo.Length;
        }
コード例 #2
0
 /// <summary>
 /// Deletes a GridFS file.
 /// </summary>
 public void Delete()
 {
     if (Exists)
     {
         using (gridFS.Database.RequestStart()) {
             gridFS.EnsureIndexes();
             gridFS.Files.Remove(Query.EQ("_id", id), gridFS.Settings.SafeMode);
             gridFS.Chunks.Remove(Query.EQ("files_id", id), gridFS.Settings.SafeMode);
         }
     }
 }
コード例 #3
0
        private void OpenAppend()
        {
            EnsureServerInstanceIsPrimary();
            using (_fileInfo.Server.RequestStart(null, _fileInfo.ServerInstance))
            {
                var gridFS = new MongoGridFS(_fileInfo.Server, _fileInfo.DatabaseName, _fileInfo.GridFSSettings);
                gridFS.EnsureIndexes();

                _length   = _fileInfo.Length;
                _position = _fileInfo.Length;
            }
        }
コード例 #4
0
        private void OpenTruncate()
        {
            EnsureServerInstanceIsPrimary();
            using (_fileInfo.Server.RequestStart(null, _fileInfo.ServerInstance))
            {
                var gridFS = new MongoGridFS(_fileInfo.Server, _fileInfo.DatabaseName, _fileInfo.GridFSSettings);
                gridFS.EnsureIndexes();

                _fileIsDirty = true;
                // existing chunks will be overwritten as needed and extra chunks will be removed on Close
                _length   = 0;
                _position = 0;
            }
        }
コード例 #5
0
ファイル: Program.cs プロジェクト: coolya/mongo.GridFS-Import
        public void Run()
        {
            string connectionString = "mongodb://localhost/?safe=true";

            if (!string.IsNullOrEmpty(_server))
            {
                connectionString = string.Format("mongodb://{0}/?safe=true", _server);
            }

            var server = MongoServer.Create(connectionString);
            var db = server.GetDatabase(_database);
            var gridFs = new MongoGridFS(db);
            gridFs.EnsureIndexes();

            var dirInfo = new DirectoryInfo(_dir);
            ImportDirectory(gridFs, dirInfo, _root, _recursive);
        }
コード例 #6
0
        /// <summary>
        /// Deletes a GridFS file.
        /// </summary>
        public void Delete()
        {
            EnsureServerInstanceIsPrimary();
            using (_server.RequestStart(_serverInstance))
            {
                var gridFS = new MongoGridFS(_server, _databaseName, _settings);
                gridFS.EnsureIndexes();

                if (Exists)
                {
                    var database         = gridFS.GetDatabase(ReadPreference.Primary);
                    var filesCollection  = gridFS.GetFilesCollection(database);
                    var chunksCollection = gridFS.GetChunksCollection(database);

                    filesCollection.Remove(Query.EQ("_id", _id), gridFS.Settings.WriteConcern);
                    chunksCollection.Remove(Query.EQ("files_id", _id), gridFS.Settings.WriteConcern);
                }
            }
        }
コード例 #7
0
        private void OpenCreate()
        {
            EnsureServerInstanceIsPrimary();
            using (_fileInfo.Server.RequestStart(null, _fileInfo.ServerInstance))
            {
                var gridFS          = new MongoGridFS(_fileInfo.Server, _fileInfo.DatabaseName, _fileInfo.GridFSSettings);
                var database        = gridFS.GetDatabase(ReadPreference.Primary);
                var filesCollection = gridFS.GetFilesCollection(database);

                gridFS.EnsureIndexes();

                _fileIsDirty = true;
                if (_fileInfo.Id == null)
                {
                    _fileInfo.SetId(ObjectId.GenerateNewId());
                }

                var aliases = (_fileInfo.Aliases != null) ? new BsonArray(_fileInfo.Aliases) : null;
                var file    = new BsonDocument
                {
                    { "_id", _fileInfo.Id },
                    { "filename", _fileInfo.Name, !string.IsNullOrEmpty(_fileInfo.Name) },
                    { "length", 0 },
                    { "chunkSize", _fileInfo.ChunkSize },
                    { "uploadDate", _fileInfo.UploadDate },
                    { "md5", BsonNull.Value },                                                              // will be updated when the file is closed (unless UpdateMD5 is false)
                    { "contentType", _fileInfo.ContentType, !string.IsNullOrEmpty(_fileInfo.ContentType) }, // optional
                    { "aliases", aliases, aliases != null },                                                // optional
                    { "metadata", _fileInfo.Metadata, _fileInfo.Metadata != null } // optional
                };
                filesCollection.Insert(file);

                _length   = 0;
                _position = 0;
            }
        }
コード例 #8
0
        /// <summary>
        /// Initializes a new instance of the MongoGridFSStream class.
        /// </summary>
        /// <param name="fileInfo">The GridFS file info.</param>
        /// <param name="mode">The mode.</param>
        /// <param name="access">The acess.</param>
        public MongoGridFSStream(MongoGridFSFileInfo fileInfo, FileMode mode, FileAccess access)
        {
            _gridFS   = fileInfo.GridFS;
            _fileInfo = fileInfo;
            _mode     = mode;
            _access   = access;

            var    exists = fileInfo.Exists;
            string message;

            switch (mode)
            {
            case FileMode.Append:
                if (exists)
                {
                    OpenAppend();
                }
                else
                {
                    OpenCreate();
                }
                break;

            case FileMode.Create:
                if (exists)
                {
                    OpenTruncate();
                }
                else
                {
                    OpenCreate();
                }
                break;

            case FileMode.CreateNew:
                if (exists)
                {
                    message = string.Format("File '{0}' already exists.", fileInfo.Name);
                    throw new IOException(message);
                }
                else
                {
                    OpenCreate();
                }
                break;

            case FileMode.Open:
                if (exists)
                {
                    OpenExisting();
                }
                else
                {
                    message = string.Format("File '{0}' not found.", fileInfo.Name);
                    throw new FileNotFoundException(message);
                }
                break;

            case FileMode.OpenOrCreate:
                if (exists)
                {
                    OpenExisting();
                }
                else
                {
                    OpenCreate();
                }
                break;

            case FileMode.Truncate:
                if (exists)
                {
                    OpenTruncate();
                }
                else
                {
                    message = string.Format("File '{0}' not found.", fileInfo.Name);
                    throw new FileNotFoundException(message);
                }
                break;

            default:
                message = string.Format("Invalid FileMode {0}.", mode);
                throw new ArgumentException(message, "mode");
            }
            _gridFS.EnsureIndexes();
        }
コード例 #9
0
        private void OpenTruncate()
        {
            EnsureServerInstanceIsPrimary();
            using (_fileInfo.Server.RequestStart(null, _fileInfo.ServerInstance))
            {
                var gridFS = new MongoGridFS(_fileInfo.Server, _fileInfo.DatabaseName, _fileInfo.GridFSSettings);
                gridFS.EnsureIndexes();

                _fileIsDirty = true;
                // existing chunks will be overwritten as needed and extra chunks will be removed on Close
                _length = 0;
                _position = 0;
            }
        }
コード例 #10
0
        private void OpenCreate()
        {
            EnsureServerInstanceIsPrimary();
            using (_fileInfo.Server.RequestStart(null, _fileInfo.ServerInstance))
            {
                var gridFS = new MongoGridFS(_fileInfo.Server, _fileInfo.DatabaseName, _fileInfo.GridFSSettings);
                var database = gridFS.GetDatabase(ReadPreference.Primary);
                var filesCollection = gridFS.GetFilesCollection(database);

                gridFS.EnsureIndexes();

                _fileIsDirty = true;
                if (_fileInfo.Id == null)
                {
                    _fileInfo.SetId(ObjectId.GenerateNewId());
                }

                var aliases = (_fileInfo.Aliases != null) ? new BsonArray(_fileInfo.Aliases) : null;
                var file = new BsonDocument
                {
                    { "_id", _fileInfo.Id },
                    { "filename", _fileInfo.Name, !string.IsNullOrEmpty(_fileInfo.Name) },
                    { "length", 0 },
                    { "chunkSize", _fileInfo.ChunkSize },
                    { "uploadDate", _fileInfo.UploadDate },
                    { "md5", BsonNull.Value }, // will be updated when the file is closed (unless UpdateMD5 is false)
                    { "contentType", _fileInfo.ContentType, !string.IsNullOrEmpty(_fileInfo.ContentType) }, // optional
                    { "aliases", aliases, aliases != null }, // optional
                    { "metadata", _fileInfo.Metadata, _fileInfo.Metadata != null } // optional
                };
                filesCollection.Insert(file);

                _length = 0;
                _position = 0;
            }
        }
コード例 #11
0
        private void OpenAppend()
        {
            EnsureServerInstanceIsPrimary();
            using (_fileInfo.Server.RequestStart(null, _fileInfo.ServerInstance))
            {
                var gridFS = new MongoGridFS(_fileInfo.Server, _fileInfo.DatabaseName, _fileInfo.GridFSSettings);
                gridFS.EnsureIndexes();

                _length = _fileInfo.Length;
                _position = _fileInfo.Length;
            }
        }
コード例 #12
0
        /// <summary>
        /// Initializes a new instance of the MongoGridFSStream class.
        /// </summary>
        /// <param name="fileInfo">The GridFS file info.</param>
        /// <param name="mode">The mode.</param>
        /// <param name="access">The acess.</param>
        public MongoGridFSStream(
            MongoGridFSFileInfo fileInfo,
            FileMode mode,
            FileAccess access
        ) {
            this.gridFS = fileInfo.GridFS;
            this.fileInfo = fileInfo;
            this.mode = mode;
            this.access = access;

            var exists = fileInfo.Exists;
            string message;
            switch (mode) {
                case FileMode.Append:
                    if (exists) {
                        OpenAppend();
                    } else {
                        OpenCreate();
                    }
                    break;
                case FileMode.Create:
                    if (exists) {
                        OpenTruncate();
                    } else {
                        OpenCreate();
                    }
                    break;
                case FileMode.CreateNew:
                    if (exists) {
                        message = string.Format("File '{0}' already exists.", fileInfo.Name);
                        throw new IOException(message);
                    } else {
                        OpenCreate();
                    }
                    break;
                case FileMode.Open:
                    if (exists) {
                        OpenExisting();
                    } else {
                        message = string.Format("File '{0}' not found.", fileInfo.Name);
                        throw new FileNotFoundException(message);
                    }
                    break;
                case FileMode.OpenOrCreate:
                    if (exists) {
                        OpenExisting();
                    } else {
                        OpenCreate();
                    }
                    break;
                case FileMode.Truncate:
                    if (exists) {
                        OpenTruncate();
                    } else {
                        message = string.Format("File '{0}' not found.", fileInfo.Name);
                        throw new FileNotFoundException(message);
                    }
                    break;
                default:
                    message = string.Format("Invalid FileMode {0}.", mode);
                    throw new ArgumentException(message, "mode");
            }
            gridFS.EnsureIndexes();
        }