类:Mongo::Grid::FSBucket
- 继承:
-
对象
- 对象
- Mongo::Grid::FSBucket
- 扩展方式:
- 可转发
- 定义于:
- lib/ Mongo/grid/fs_bucket.rb 、
lib/ Mongo/grid/ 流.rb、
lib/ Mongo/grid/ 流/read.rb、
lib/ Mongo/grid/ 流/ 写入.rb
Overview
表示数据库中 GridFS 的视图。
在命名空间下定义
模块: Stream
常量摘要折叠
- DEFAULT_ROOT =
默认根前缀。
' fs '.冻结
- CHUNKS_INDEX =
数据段集合索引的规范。
{ :files_id => 1, :n => 1 }.冻结
- FILES_INDEX =
文件集合索引的规范。
{ 文件名: 1, uploadDate: 1 }.冻结
实例属性摘要折叠
-
#chunks_collection ⇒ Collection
只读
chunks_collection 数据段集合。
-
#database ⇒ Database
只读
Database The database.
-
#files_collection ⇒ Collection
只读
Files_collection 文件集合。
-
# options ⇒ 哈希
只读
选项 FSBucket 选项。
实例方法摘要折叠
-
# 删除 (ID, opts = {}) ⇒ 结果
从 GridFS 中删除由其ID标识的单个文件。
-
# delete_one (文件, opts = {}) ⇒ 结果
从GridFS中删除单个文件。
-
# download_to_stream ( ID , io) ⇒ 对象
下载量ID指定的文件内容并将其写入目标 io对象。
-
# download_to_stream_by_name (filename, io, opts = {}) ⇒ 对象
下载量由 filename 和 options 中的修订版本指定的存储文件的内容,并将内容写入目标 io对象。
-
# drop (opts = {}) ⇒ 对象
删除实现此存储桶的集合。
-
# find (selector = nil, options = {}) ⇒ CollectionView
查找与给定选择器匹配的文件集合文档。
-
# find_one (selector = nil) ⇒ Grid::File
已弃用
已弃用。
请改用 #find,限制为 - 1 。 将在版本3.0中删除。
-
#initialize(database, options = {}) ⇒ FSBucket
构造函数
创建GridFS。
-
#insert_one(file) ⇒ BSON::ObjectId
已弃用
已弃用。
请改用 #upload_from_stream 或 #open_upload_stream。 将在版本3.0中删除。
-
# open_download_stream ( ID , options = nil) {|The| ... } ="Stream::Read"
打开可从中下载文件的流,文件由ID指定。
-
# open_download_stream_by_name (filename, opts = {}) {|The| ... } ="Stream::Read"
打开一个流,应用程序可以从中读取由文件名和选项中的修订版本指定的存储文件的内容。
-
# open_upload_stream (filename, opts = {}) {|The| ... } ="Stream::Write"
打开到 GridFS 的上传流,可以向其中写入文件或 blob 的内容。
-
# prefix ⇒ string
获取GridFS的前缀。
-
# read_preference ⇒ BSON::Document
获取读取偏好。
-
# upload_from_stream (filename, io, opts = {}) ⇒ BSON::ObjectId
将用户文件上传到GridFS存储桶。
-
# write_concern ⇒ Mongo::WriteConcern
获取写关注(write concern)。
构造函数详情
#initialize(database, options = {}) ⇒ FSBucket
创建GridFS。
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/ Mongo/grid/fs_bucket.rb', line 70 def 初始化(database, = {}) @database = database @options = .dup =begin WriteConcern 对象支持 if @options[:write_concern].is_a?(WriteConcern::Base) # 缓存实例,以便我们不会不必要地重建它。 @write_concern = @options[:write_concern] @options[:write_concern] = @write_concern.options end =end @options.冻结 @chunks_collection = database[chunks_name] @files_collection = database[files_name] end |
实例属性详细信息
# chunks_collection ⇒集合(只读)
返回 chunks_collection 数据段集合。
88 89 90 |
# File 'lib/ Mongo/grid/fs_bucket.rb', line 88 def chunks_collection @chunks_collection end |
#database ⇒ Database (readonly)
返回数据库 数据库。
93 94 95 |
# File 'lib/ Mongo/grid/fs_bucket.rb', line 93 def database @database end |
# files_collection ⇒集合(只读)
返回 files_collection 文件集合。
98 99 100 |
# File 'lib/ Mongo/grid/fs_bucket.rb', line 98 def files_collection @files_collection end |
# options ⇒哈希(只读)
返回 options FSBucket 选项。
103 104 105 |
# File 'lib/ Mongo/grid/fs_bucket.rb', line 103 def @options end |
实例方法详细信息
# 删除 (ID, opts = {}) ⇒ 结果
从 GridFS 中删除由其ID标识的单个文件。
220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/ Mongo/grid/fs_bucket.rb', line 220 def 删除(id, opts = {}) timeout_holder = CsotTimeoutHolder.new(operation_timeups: operation_timeups(opts)) 结果 = files_collection .find({ :_id => id }, @options.合并(merge)(timeout_ms: timeout_holder.剩余超时毫秒)) .delete_one(timeout_ms: timeout_holder.剩余超时毫秒) chunks_collection .find({ :files_id => id }, @options.合并(merge)(timeout_ms: timeout_holder.剩余超时毫秒)) .delete_many(timeout_ms: timeout_holder.剩余超时毫秒) 提高 错误::FileNotFound.new(id, :id) if 结果.n == 0 结果 end |
# delete_one (文件, opts = {}) ⇒ 结果
从GridFS中删除单个文件。
204 205 206 |
# File 'lib/ Mongo/grid/fs_bucket.rb', line 204 def delete_one(文件, opts = {}) 删除(文件.id, opts) end |
# download_to_stream ( ID , io) ⇒对象
下载量ID指定的文件内容并将其写入目标 io对象。
271 272 273 274 275 276 277 |
# File 'lib/ Mongo/grid/fs_bucket.rb', line 271 def download_to_stream(id, io) open_download_stream(id) do |流| 流.每 do |数据段| io << 数据段 end end end |
# download_to_stream_by_name (filename, io, opts = {}) ⇒对象
下载量由 filename 和 options 中的修订版本指定的存储文件的内容,并将内容写入目标 io对象。
修订号定义如下: 0 = 原始存储的文件1 = 第一个修订2 = 第二个修订,依此类推 - 2 = 第二个最新修订 - 1 = 最新修订
# @example 下载原始文件。
fs.download_to_stream_by_name('some-file.txt', io, revision: 0)
364 365 366 |
# File 'lib/ Mongo/grid/fs_bucket.rb', line 364 def download_to_stream_by_name(文件名, io, opts = {}) download_to_stream(open_download_stream_by_name(文件名, opts).file_id, io) end |
# drop (opts = {}) ⇒ 对象
删除实现此存储桶的集合。
493 494 495 496 497 |
# File 'lib/ Mongo/grid/fs_bucket.rb', line 493 def 删除(opts = {}) 上下文 = 操作::上下文.new(operation_timeups: operation_timeups(opts)) files_collection.删除(timeout_ms: 上下文.剩余超时毫秒) chunks_collection.删除(timeout_ms: 上下文.剩余超时毫秒) end |
# find (selector = nil, options = {}) ⇒ CollectionView
查找与给定选择器匹配的文件集合文档。
134 135 136 137 |
# File 'lib/ Mongo/grid/fs_bucket.rb', line 134 def find(选择器 = nil, = {}) opts = .合并(merge)(读取: read_preference) if read_preference files_collection.find(选择器, opts || ) end |
# find_one (selector = nil) ⇒ Grid::File
请改用 #find,限制为 - 1 。 将在版本3.0中删除。
在GridFS中查找文件。
155 156 157 158 159 160 |
# File 'lib/ Mongo/grid/fs_bucket.rb', line 155 def find_one(选择器 = nil) file_info = files_collection.find(选择器).first return nil 除非 file_info 数据块 = chunks_collection.find(:files_id => file_info[:_id]).sort(:n => 1) 网格::file.new(数据块.to_a, 选项::映射器.转换(Transform)(file_info, 网格::file::信息::mappings.反转)) end |
#insert_one(file) ⇒ BSON::ObjectId
请改用 #upload_from_stream 或 #open_upload_stream。 将在版本3.0中删除。
将单个文件插入 GridFS。
175 176 177 178 179 180 |
# File 'lib/ Mongo/grid/fs_bucket.rb', line 175 def insert_one(文件) @indexes ||= ensure_indexes! chunks_collection.insert_many(文件.数据块) files_collection.insert_one(文件.信息) 文件.id end |
# open_download_stream ( ID , options = nil) {|The| ... } = "Stream::Read"
打开可从中下载文件的流,文件由ID指定。
248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/ Mongo/grid/fs_bucket.rb', line 248 def open_download_stream(id, = nil) = Utils.shallow_symbolize_keys( || {}) read_stream(id, **).点击 do |流| if block_given? 开始 产量 流 确保 流.关闭 end end end end |
# open_download_stream_by_name (filename, opts = {}) {|The| ... } = "Stream::Read"
打开一个流,应用程序可以从中读取由文件名和选项中的修订版本指定的存储文件的内容。
修订号定义如下: 0 = 原始存储的文件1 = 第一个修订2 = 第二个修订,依此类推 - 2 = 第二个最新修订 - 1 = 最新修订
# @example 打开流以下载原始文件。
fs.open_download_stream_by_name('some-file.txt', revision: 0)
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/ Mongo/grid/fs_bucket.rb', line 313 def open_download_stream_by_name(文件名, opts = {}, 和块) 修订 = opts.获取(:revision, -1) if 修订 < 0 跳过 = 修订.abs - 1 sort = { ' uploadDate ' => mongo::Index::降序 } else 跳过 = 修订 sort = { ' uploadDate ' => mongo::Index::升序 } end file_info_doc = files_collection.find({ 文件名: 文件名} , sort: sort, skip: 跳过, limit: -1).first 除非 file_info_doc 提高 错误::FileNotFound.new(文件名, :filename) 除非 opts[:revision] 提高 错误::InvalidFileRevision.new(文件名, opts[:revision]) end open_download_stream(file_info_doc[:_id], file_info_doc: file_info_doc, 和块) end |
# open_upload_stream (filename, opts = {}) {|The| ... } = "Stream::Write"
打开到 GridFS 的上传流,可以向其中写入文件或 blob 的内容。
394 395 396 397 398 399 400 401 402 403 404 405 |
# File 'lib/ Mongo/grid/fs_bucket.rb', line 394 def open_upload_stream(文件名, opts = {}) opts = Utils.shallow_symbolize_keys(opts) write_stream(文件名, **opts).点击 do |流| if block_given? 开始 产量 流 确保 流.关闭 end end end end |
# prefix ⇒ string
获取 GridFS 的前缀
190 191 192 |
# File 'lib/ Mongo/grid/fs_bucket.rb', line 190 def prefix @options[:fs_name] || @options[:bucket_name] || DEFAULT_ROOT end |
# read_preference ⇒ BSON::Document
此方法始终返回BSON::Document实例,即使 FSBucket 构造函数将 :read 的类型指定为哈希,而不是BSON::Document。
获取读取偏好。
465 466 467 468 469 470 471 472 473 474 |
# File 'lib/ Mongo/grid/fs_bucket.rb', line 465 def read_preference @read_preference ||= 开始 pref = [:read] || database.read_preference if BSON::文档 === pref pref else BSON::文档.new(pref) end end end |
#upload_from_stream(filename, io, opts = {}) ⇒ BSON::ObjectId
将用户文件上传到GridFS存储桶。 从流读取用户文件的内容,并将其作为数据段上传到数据段集合中。 上传所有数据段后,它会为 files 集合中的 filename 创建一个文件集合文档。
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 |
# File 'lib/ Mongo/grid/fs_bucket.rb', line 435 def upload_from_stream(文件名, io, opts = {}) open_upload_stream(文件名, opts) do |流| 开始 流.写(io) # IOError 和 SystemCallError 用于读取 io. # Error::SocketError 和 Error::SocketTimeoutError 用于 # 写入 MongoDB。 救援 IOError, SystemCallError, 错误::SocketError, 错误::SocketTimeoutError 开始 流.中止 救援 错误::OperationFailure end 提高 end end.file_id end |
# write_concern ⇒ Mongo::WriteConcern
获取写关注(write concern)。
484 485 486 487 488 489 490 |
# File 'lib/ Mongo/grid/fs_bucket.rb', line 484 def write_concern @write_concern ||= if wco = @options[:write_concern] || @options[:write] writeConcern.获取(wco) else database.write_concern end end |