Chunk — Documentation by YARD 0.9.37 (original) (raw)

Class: Mongo::Grid::File::Chunk

Inherits:

Object

Defined in:

lib/mongo/grid/file/chunk.rb

Overview

Encapsulates behavior around GridFS chunks of file data.

Constant Summarycollapse

COLLECTION =

Name of the chunks collection.

'chunks'.freeze

DEFAULT_SIZE =

Default size for chunks of data.

(255 * 1024).freeze

Instance Attribute Summary collapse

Class Method Summarycollapse

Instance Method Summarycollapse

Constructor Details

#initialize(document) ⇒ Chunk

125 126 127 # File 'lib/mongo/grid/file/chunk.rb', line 125 def initialize(document) @document = BSON::Document.new(:_id => BSON::ObjectId.new).merge(document) end

Instance Attribute Details

#document ⇒ BSON::Document

Returns document The document to store for the chunk.

39 40 41 # File 'lib/mongo/grid/file/chunk.rb', line 39 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.

| 159 160 161 | # File 'lib/mongo/grid/file/chunk.rb', line 159 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.

| 176 177 178 179 180 181 182 183 184 185 186 187 | # File 'lib/mongo/grid/file/chunk.rb', line 176 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

51 52 53 54 # File 'lib/mongo/grid/file/chunk.rb', line 51 def ==(other) return false unless other.is_a?(Chunk) document == other.document end

#bson_type ⇒ Integer

Get the BSON type for a chunk document.

64 65 66 # File 'lib/mongo/grid/file/chunk.rb', line 64 def bson_type BSON::Hash::BSON_TYPE end

#data ⇒ BSON::Binary

76 77 78 # File 'lib/mongo/grid/file/chunk.rb', line 76 def data document[:data] end

#files_id ⇒ BSON::ObjectId

100 101 102 # File 'lib/mongo/grid/file/chunk.rb', line 100 def files_id document[:files_id] end

#id ⇒ BSON::ObjectId

88 89 90 # File 'lib/mongo/grid/file/chunk.rb', line 88 def id document[:_id] end

#n ⇒ Integer

112 113 114 # File 'lib/mongo/grid/file/chunk.rb', line 112 def n document[:n] end

#to_bson(buffer = BSON::ByteBuffer.new, validating_keys = nil) ⇒ String

Conver the chunk to BSON for storage.

141 142 143 # File 'lib/mongo/grid/file/chunk.rb', line 141 def to_bson(buffer = BSON::ByteBuffer.new, validating_keys = nil) document.to_bson(buffer) end