Class: Mongo::Grid::File::Chunk
- Inherits:
-
Object
- Object
- Mongo::Grid::File::Chunk
- Defined in:
- lib/mongo/grid/file/chunk.rb
Overview
Encapsulates behavior around GridFS chunks of file data.
Constant Summary collapse
- COLLECTION =
Name of the chunks collection.
'chunks'.freeze
- DEFAULT_SIZE =
Default size for chunks of data.
(255 * 1024).freeze
Instance Attribute Summary collapse
-
#document ⇒ BSON::Document
readonly
Document The document to store for the chunk.
Class Method Summary collapse
-
.assemble(chunks) ⇒ String
private
Takes an array of chunks and assembles them back into the full piece of raw data.
-
.split(io, file_info, offset = 0) ⇒ Array<Chunk>
private
Split the provided data into multiple chunks.
Instance Method Summary collapse
-
#==(other) ⇒ true, false
Check chunk equality.
-
#bson_type ⇒ Integer
Get the BSON type for a chunk document.
-
#data ⇒ BSON::Binary
Get the chunk data.
-
#files_id ⇒ BSON::ObjectId
Get the files id.
-
#id ⇒ BSON::ObjectId
Get the chunk id.
-
#initialize(document) ⇒ Chunk
constructor
Create the new chunk.
-
#n ⇒ Integer
Get the chunk position.
-
#to_bson(buffer = BSON::ByteBuffer.new, validating_keys = BSON::Config.validating_keys?) ⇒ String
Conver the chunk to BSON for storage.
Constructor Details
#initialize(document) ⇒ Chunk
Create the new chunk.
122 123 124 |
# File 'lib/mongo/grid/file/chunk.rb', line 122 def initialize(document) @document = BSON::Document.new(:_id => BSON::ObjectId.new).merge(document) end |
Instance Attribute Details
#document ⇒ BSON::Document (readonly)
Returns document The document to store for the chunk.
36 37 38 |
# File 'lib/mongo/grid/file/chunk.rb', line 36 def document @document end |
Class Method Details
.assemble(chunks) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Takes an array of chunks and assembles them back into the full piece of raw data.
155 156 157 |
# File 'lib/mongo/grid/file/chunk.rb', line 155 def assemble(chunks) chunks.reduce(''){ |data, chunk| data << chunk.data.data } end |
.split(io, file_info, offset = 0) ⇒ Array<Chunk>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Split the provided data into multiple chunks.
172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/mongo/grid/file/chunk.rb', line 172 def split(io, file_info, offset = 0) io = StringIO.new(io) if io.is_a?(String) parts = Enumerator.new { |y| y << io.read(file_info.chunk_size) until io.eof? } parts.map.with_index do |bytes, n| file_info.update_md5(bytes) Chunk.new( data: BSON::Binary.new(bytes), files_id: file_info.id, n: n + offset ) end end |
Instance Method Details
#==(other) ⇒ true, false
Check chunk equality.
48 49 50 51 |
# File 'lib/mongo/grid/file/chunk.rb', line 48 def ==(other) return false unless other.is_a?(Chunk) document == other.document end |
#bson_type ⇒ Integer
Get the BSON type for a chunk document.
61 62 63 |
# File 'lib/mongo/grid/file/chunk.rb', line 61 def bson_type BSON::Hash::BSON_TYPE end |
#data ⇒ BSON::Binary
Get the chunk data.
73 74 75 |
# File 'lib/mongo/grid/file/chunk.rb', line 73 def data document[:data] end |
#files_id ⇒ BSON::ObjectId
Get the files id.
97 98 99 |
# File 'lib/mongo/grid/file/chunk.rb', line 97 def files_id document[:files_id] end |
#id ⇒ BSON::ObjectId
Get the chunk id.
85 86 87 |
# File 'lib/mongo/grid/file/chunk.rb', line 85 def id document[:_id] end |
#n ⇒ Integer
Get the chunk position.
109 110 111 |
# File 'lib/mongo/grid/file/chunk.rb', line 109 def n document[:n] end |
#to_bson(buffer = BSON::ByteBuffer.new, validating_keys = BSON::Config.validating_keys?) ⇒ String
Conver the chunk to BSON for storage.
137 138 139 |
# File 'lib/mongo/grid/file/chunk.rb', line 137 def to_bson(buffer = BSON::ByteBuffer.new, validating_keys = BSON::Config.validating_keys?) document.to_bson(buffer) end |