Class: GridFS::GridStore Deprecated
- Includes:
- Enumerable
- Defined in:
- lib/mongo/gridfs/grid_store.rb
Overview
GridStore is an IO-like class that provides input and output for streams of data to MongoDB.
Constant Summary collapse
- DEFAULT_ROOT_COLLECTION =
'fs'
- DEFAULT_CONTENT_TYPE =
'text/plain'
- DEPRECATION_WARNING =
"GridFS::GridStore is deprecated. Use either Grid or GridFileSystem."
Instance Attribute Summary collapse
-
#aliases ⇒ Object
Array of strings; may be
nil
. -
#chunk_size ⇒ Object
Returns the value of attribute chunk_size.
-
#content_type ⇒ Object
Default is DEFAULT_CONTENT_TYPE.
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#files_id ⇒ Object
readonly
Returns the value of attribute files_id.
-
#length ⇒ Object
readonly
Size of file in bytes.
-
#lineno ⇒ Object
Returns the value of attribute lineno.
-
#md5 ⇒ Object
readonly
Returns the value of attribute md5.
-
#metadata ⇒ Object
Returns the value of attribute metadata.
-
#upload_date ⇒ Object
readonly
Time that the file was first saved.
Class Method Summary collapse
- .default_root_collection ⇒ Object
- .default_root_collection=(name) ⇒ Object
- .exist?(db, name, root_collection = GridStore.default_root_collection) ⇒ Boolean deprecated Deprecated.
- .list(db, root_collection = GridStore.default_root_collection) ⇒ Array deprecated Deprecated.
- .mv(db, src, dest, root_collection = GridStore.default_root_collection) ⇒ Object deprecated Deprecated.
- .open(db, name, mode, options = {}) ⇒ Object deprecated Deprecated.
- .read(db, name, length = nil, offset = nil) ⇒ String deprecated Deprecated.
- .readlines(db, name, separator = $/) ⇒ Array deprecated Deprecated.
- .unlink(db, *names) ⇒ True (also: delete) deprecated Deprecated.
Instance Method Summary collapse
- #<<(obj) ⇒ Object
-
#chunk_collection ⇒ Mongo::Collection
Get the chunk collection referenced by this GridStore.
-
#close ⇒ Object
— ================ closing ================ +++.
- #closed? ⇒ Boolean
-
#collection ⇒ Mongo::Collection
Get the files collection referenced by this GridStore instance.
- #delete_chunks ⇒ Object
- #each ⇒ Object (also: #each_line)
- #each_byte ⇒ Object
-
#eof ⇒ Object
(also: #eof?)
status ================.
-
#flush ⇒ Object
A no-op.
-
#getc ⇒ Object
reading ================.
- #gets(separator = $/) ⇒ Object
- #initialize(db, name, mode = 'r', options = {}) ⇒ GridStore constructor deprecated Deprecated.
- #print(*objs) ⇒ Object
-
#putc(byte) ⇒ Object
writing ================.
- #puts(*objs) ⇒ Object
- #read(len = nil, buf = nil) ⇒ Object
- #readchar ⇒ Object
- #readline(separator = $/) ⇒ Object
- #readlines(separator = $/) ⇒ Object
-
#rewind ⇒ Object
positioning ================.
- #seek(pos, whence = IO::SEEK_SET) ⇒ Object
- #tell ⇒ Object
- #ungetc(byte) ⇒ Object
- #write(string) ⇒ Object
Constructor Details
#initialize(db, name, mode = 'r', options = {}) ⇒ GridStore
Initialize a GridStore instance for reading, writing, or modifying a given file. Note that it’s often easier to work with the various GridStore class methods (open, read, etc.).
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/mongo/gridfs/grid_store.rb', line 226 def initialize(db, name, mode='r', ={}) warn DEPRECATION_WARNING @db, @filename, @mode = db, name, mode @root = [:root] || GridStore.default_root_collection doc = collection.find({'filename' => @filename}).next_document if doc @files_id = doc['_id'] @content_type = doc['contentType'] @chunk_size = doc['chunkSize'] @upload_date = doc['uploadDate'] @aliases = doc['aliases'] @length = doc['length'] @metadata = doc['metadata'] @md5 = doc['md5'] else @files_id = Mongo::ObjectID.new @content_type = DEFAULT_CONTENT_TYPE @chunk_size = Chunk::DEFAULT_CHUNK_SIZE @length = 0 end case mode when 'r' @curr_chunk = nth_chunk(0) @position = 0 when 'w' chunk_collection.create_index([['files_id', Mongo::ASCENDING], ['n', Mongo::ASCENDING]]) delete_chunks @curr_chunk = Chunk.new(self, 'n' => 0) @content_type = [:content_type] if [:content_type] @chunk_size = [:chunk_size] if [:chunk_size] @metadata = [:metadata] if [:metadata] @position = 0 when 'w+' chunk_collection.create_index([['files_id', Mongo::ASCENDING], ['n', Mongo::ASCENDING]]) @curr_chunk = nth_chunk(last_chunk_number) || Chunk.new(self, 'n' => 0) # might be empty @curr_chunk.pos = @curr_chunk.data.length if @curr_chunk @metadata = [:metadata] if [:metadata] @position = @length else raise "error: illegal mode #{mode}" end @lineno = 0 @pushback_byte = nil end |
Instance Attribute Details
#aliases ⇒ Object
Array of strings; may be nil
63 64 65 |
# File 'lib/mongo/gridfs/grid_store.rb', line 63 def aliases @aliases end |
#chunk_size ⇒ Object
Returns the value of attribute chunk_size.
78 79 80 |
# File 'lib/mongo/gridfs/grid_store.rb', line 78 def chunk_size @chunk_size end |
#content_type ⇒ Object
Default is DEFAULT_CONTENT_TYPE
66 67 68 |
# File 'lib/mongo/gridfs/grid_store.rb', line 66 def content_type @content_type end |
#filename ⇒ Object
Returns the value of attribute filename.
60 61 62 |
# File 'lib/mongo/gridfs/grid_store.rb', line 60 def filename @filename end |
#files_id ⇒ Object (readonly)
Returns the value of attribute files_id.
73 74 75 |
# File 'lib/mongo/gridfs/grid_store.rb', line 73 def files_id @files_id end |
#length ⇒ Object (readonly)
Size of file in bytes
69 70 71 |
# File 'lib/mongo/gridfs/grid_store.rb', line 69 def length @length end |
#lineno ⇒ Object
Returns the value of attribute lineno.
80 81 82 |
# File 'lib/mongo/gridfs/grid_store.rb', line 80 def lineno @lineno end |
#md5 ⇒ Object (readonly)
Returns the value of attribute md5.
82 83 84 |
# File 'lib/mongo/gridfs/grid_store.rb', line 82 def md5 @md5 end |
#metadata ⇒ Object
Returns the value of attribute metadata.
71 72 73 |
# File 'lib/mongo/gridfs/grid_store.rb', line 71 def @metadata end |
#upload_date ⇒ Object (readonly)
Time that the file was first saved.
76 77 78 |
# File 'lib/mongo/gridfs/grid_store.rb', line 76 def upload_date @upload_date end |
Class Method Details
.default_root_collection ⇒ Object
84 85 86 |
# File 'lib/mongo/gridfs/grid_store.rb', line 84 def self.default_root_collection @@default_root_collection ||= DEFAULT_ROOT_COLLECTION end |
.default_root_collection=(name) ⇒ Object
88 89 90 |
# File 'lib/mongo/gridfs/grid_store.rb', line 88 def self.default_root_collection=(name) @@default_root_collection = name end |
.exist?(db, name, root_collection = GridStore.default_root_collection) ⇒ Boolean
Determine whether a given file exists in the GridStore.
100 101 102 103 |
# File 'lib/mongo/gridfs/grid_store.rb', line 100 def self.exist?(db, name, root_collection=GridStore.default_root_collection) warn DEPRECATION_WARNING db.collection("#{root_collection}.files").find({'filename' => name}).next_document != nil end |
.list(db, root_collection = GridStore.default_root_collection) ⇒ Array
List the contents of all GridFS files stored in the given db and root collection.
154 155 156 157 158 159 |
# File 'lib/mongo/gridfs/grid_store.rb', line 154 def self.list(db, root_collection=GridStore.default_root_collection) warn DEPRECATION_WARNING db.collection("#{root_collection}.files").find().map do |f| f['filename'] end end |
.mv(db, src, dest, root_collection = GridStore.default_root_collection) ⇒ Object
Rename a file in this collection. Note that this method uses Collection#update, which means that you will not be notified of the success of the operation.
203 204 205 206 |
# File 'lib/mongo/gridfs/grid_store.rb', line 203 def self.mv(db, src, dest, root_collection=GridStore.default_root_collection) warn DEPRECATION_WARNING db.collection("#{root_collection}.files").update({ :filename => src }, { '$set' => { :filename => dest } }) end |
.open(db, name, mode, options = {}) ⇒ Object
Open a GridFS file for reading, writing, or appending. Note that this method must be used with a block.
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/mongo/gridfs/grid_store.rb', line 118 def self.open(db, name, mode, ={}) gs = self.new(db, name, mode, ) result = nil begin result = yield gs if block_given? ensure gs.close end result end |
.read(db, name, length = nil, offset = nil) ⇒ String
Read a file stored in GridFS.
139 140 141 142 143 144 |
# File 'lib/mongo/gridfs/grid_store.rb', line 139 def self.read(db, name, length=nil, offset=nil) GridStore.open(db, name, 'r') do |gs| gs.seek(offset) if offset gs.read(length) end end |
.readlines(db, name, separator = $/) ⇒ Array
Get each line of data from the specified file as an array of strings.
170 171 172 173 174 |
# File 'lib/mongo/gridfs/grid_store.rb', line 170 def self.readlines(db, name, separator=$/) GridStore.open(db, name, 'r') do |gs| gs.readlines(separator) end end |
.unlink(db, *names) ⇒ True Also known as: delete
Remove one for more files from the given db.
183 184 185 186 187 188 189 |
# File 'lib/mongo/gridfs/grid_store.rb', line 183 def self.unlink(db, *names) names.each do |name| gs = GridStore.new(db, name) gs.delete_chunks gs.collection.remove('_id' => gs.files_id) end end |
Instance Method Details
#<<(obj) ⇒ Object
414 415 416 |
# File 'lib/mongo/gridfs/grid_store.rb', line 414 def <<(obj) write(obj.to_s) end |
#chunk_collection ⇒ Mongo::Collection
Get the chunk collection referenced by this GridStore.
284 285 286 |
# File 'lib/mongo/gridfs/grid_store.rb', line 284 def chunk_collection @db.collection("#{@root}.chunks") end |
#close ⇒ Object
closing ================
+++
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/mongo/gridfs/grid_store.rb', line 495 def close if @mode[0] == ?w if @curr_chunk @curr_chunk.truncate @curr_chunk.save if @curr_chunk.pos > 0 end files = collection if @upload_date files.remove('_id' => @files_id) else @upload_date = Time.now end files.insert(to_mongo_object) end @db = nil end |
#closed? ⇒ Boolean
512 513 514 |
# File 'lib/mongo/gridfs/grid_store.rb', line 512 def closed? @db == nil end |
#collection ⇒ Mongo::Collection
Get the files collection referenced by this GridStore instance.
277 278 279 |
# File 'lib/mongo/gridfs/grid_store.rb', line 277 def collection @db.collection("#{@root}.files") end |
#delete_chunks ⇒ Object
516 517 518 519 |
# File 'lib/mongo/gridfs/grid_store.rb', line 516 def delete_chunks chunk_collection.remove({'files_id' => @files_id}) if @files_id @curr_chunk = nil end |
#each ⇒ Object Also known as: each_line
358 359 360 361 362 363 364 |
# File 'lib/mongo/gridfs/grid_store.rb', line 358 def each line = gets while line yield line line = gets end end |
#each_byte ⇒ Object
367 368 369 370 371 372 373 |
# File 'lib/mongo/gridfs/grid_store.rb', line 367 def each_byte byte = self.getc while byte yield byte byte = self.getc end end |
#eof ⇒ Object Also known as: eof?
status ================
445 446 447 448 |
# File 'lib/mongo/gridfs/grid_store.rb', line 445 def eof raise IOError.new("stream not open for reading") unless @mode[0] == ?r @position >= @length end |
#getc ⇒ Object
reading ================
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/mongo/gridfs/grid_store.rb', line 303 def getc if @pushback_byte byte = @pushback_byte @pushback_byte = nil @position += 1 byte elsif eof? nil else if @curr_chunk.eof? @curr_chunk = nth_chunk(@curr_chunk.chunk_number + 1) end @position += 1 @curr_chunk.getc end end |
#gets(separator = $/) ⇒ Object
320 321 322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/mongo/gridfs/grid_store.rb', line 320 def gets(separator=$/) str = '' byte = self.getc return nil if byte == nil # EOF while byte != nil s = byte.chr str << s break if s == separator byte = self.getc end @lineno += 1 str end |
#print(*objs) ⇒ Object
392 393 394 395 396 397 398 399 |
# File 'lib/mongo/gridfs/grid_store.rb', line 392 def print(*objs) objs = [$_] if objs == nil || objs.empty? objs.each { |obj| str = obj.to_s str.each_byte { |byte| self.putc(byte) } } nil end |
#putc(byte) ⇒ Object
writing ================
382 383 384 385 386 387 388 389 390 |
# File 'lib/mongo/gridfs/grid_store.rb', line 382 def putc(byte) if @curr_chunk.pos == @chunk_size prev_chunk_number = @curr_chunk.chunk_number @curr_chunk.save @curr_chunk = Chunk.new(self, 'n' => prev_chunk_number + 1) end @position += 1 @curr_chunk.putc(byte) end |
#puts(*objs) ⇒ Object
401 402 403 404 405 406 407 408 409 410 411 412 |
# File 'lib/mongo/gridfs/grid_store.rb', line 401 def puts(*objs) if objs == nil || objs.empty? self.putc(10) else print(*objs.collect{ |obj| str = obj.to_s str << "\n" unless str =~ /\n$/ str }) end nil end |
#read(len = nil, buf = nil) ⇒ Object
334 335 336 337 338 339 340 |
# File 'lib/mongo/gridfs/grid_store.rb', line 334 def read(len=nil, buf=nil) if len read_partial(len, buf) else read_all(buf) end end |
#readchar ⇒ Object
342 343 344 345 346 |
# File 'lib/mongo/gridfs/grid_store.rb', line 342 def readchar byte = self.getc raise EOFError.new if byte == nil byte end |
#readline(separator = $/) ⇒ Object
348 349 350 351 352 |
# File 'lib/mongo/gridfs/grid_store.rb', line 348 def readline(separator=$/) line = gets raise EOFError.new if line == nil line end |
#readlines(separator = $/) ⇒ Object
354 355 356 |
# File 'lib/mongo/gridfs/grid_store.rb', line 354 def readlines(separator=$/) read.split(separator).collect { |line| "#{line}#{separator}" } end |
#rewind ⇒ Object
positioning ================
453 454 455 456 457 458 459 460 461 462 463 464 465 |
# File 'lib/mongo/gridfs/grid_store.rb', line 453 def rewind if @curr_chunk.chunk_number != 0 if @mode[0] == ?w delete_chunks @curr_chunk = Chunk.new(self, 'n' => 0) else @curr_chunk == nth_chunk(0) end end @curr_chunk.pos = 0 @lineno = 0 @position = 0 end |
#seek(pos, whence = IO::SEEK_SET) ⇒ Object
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 |
# File 'lib/mongo/gridfs/grid_store.rb', line 467 def seek(pos, whence=IO::SEEK_SET) target_pos = case whence when IO::SEEK_CUR @position + pos when IO::SEEK_END @length + pos when IO::SEEK_SET pos end new_chunk_number = (target_pos / @chunk_size).to_i if new_chunk_number != @curr_chunk.chunk_number @curr_chunk.save if @mode[0] == ?w @curr_chunk = nth_chunk(new_chunk_number) end @position = target_pos @curr_chunk.pos = @position % @chunk_size 0 end |
#tell ⇒ Object
487 488 489 |
# File 'lib/mongo/gridfs/grid_store.rb', line 487 def tell @position end |
#ungetc(byte) ⇒ Object
375 376 377 378 |
# File 'lib/mongo/gridfs/grid_store.rb', line 375 def ungetc(byte) @pushback_byte = byte @position -= 1 end |
#write(string) ⇒ Object
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 |
# File 'lib/mongo/gridfs/grid_store.rb', line 418 def write(string) raise "#@filename not opened for write" unless @mode[0] == ?w # Since Ruby 1.9.1 doesn't necessarily store one character per byte. if string.respond_to?(:force_encoding) string.force_encoding("binary") end to_write = string.length while (to_write > 0) do if @curr_chunk && @curr_chunk.data.position == @chunk_size prev_chunk_number = @curr_chunk.chunk_number @curr_chunk = GridFS::Chunk.new(self, 'n' => prev_chunk_number + 1) end chunk_available = @chunk_size - @curr_chunk.data.position step_size = (to_write > chunk_available) ? chunk_available : to_write @curr_chunk.data.put_array(ByteBuffer.new(string[-to_write,step_size]).to_a) to_write -= step_size @curr_chunk.save end string.length - to_write end |