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

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo/grid/file/chunk.rb

Overview

Encapsulates behavior around GridFS chunks of file data.

Since:

  • 2.0.0

Constant Summary collapse

COLLECTION =

Name of the chunks collection.

Since:

  • 2.0.0

'chunks'.freeze
DEFAULT_SIZE =

Default size for chunks of data.

Since:

  • 2.0.0

(255 * 1024).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ Chunk

Create the new chunk.

Examples:

Create the chunk.

Chunk.new(document)

Since:

  • 2.0.0



124
125
126
# File 'lib/mongo/grid/file/chunk.rb', line 124

def initialize(document)
  @document = BSON::Document.new(:_id => BSON::ObjectId.new).merge(document)
end

Instance Attribute Details

#documentBSON::Document (readonly)

Returns document The document to store for the chunk.

Since:

  • 2.0.0



38
39
40
# File 'lib/mongo/grid/file/chunk.rb', line 38

def document
  @document
end

Class Method Details

.assemble(chunks) ⇒ String

Takes an array of chunks and assembles them back into the full piece of raw data.

Examples:

Assemble the chunks.

Chunk.assemble(chunks)

Since:

  • 2.0.0



156
157
158
# File 'lib/mongo/grid/file/chunk.rb', line 156

def assemble(chunks)
  chunks.reduce(''){ |data, chunk| data << chunk.data.data }
end

.split(io, file_info, offset = 0) ⇒ Array<Chunk>

Split the provided data into multiple chunks.

Examples:

Split the data into chunks.

Chunks.split(data)

Since:

  • 2.0.0



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.

Examples:

Check chunk equality.

chunk == other

Since:

  • 2.0.0



50
51
52
53
# File 'lib/mongo/grid/file/chunk.rb', line 50

def ==(other)
  return false unless other.is_a?(Chunk)
  document == other.document
end

#bson_typeInteger

Get the BSON type for a chunk document.

Examples:

Get the BSON type.

chunk.bson_type

Since:

  • 2.0.0



63
64
65
# File 'lib/mongo/grid/file/chunk.rb', line 63

def bson_type
  BSON::Hash::BSON_TYPE
end

#dataBSON::Binary

Get the chunk data.

Examples:

Get the chunk data.

chunk.data

Since:

  • 2.0.0



75
76
77
# File 'lib/mongo/grid/file/chunk.rb', line 75

def data
  document[:data]
end

#files_idBSON::ObjectId

Get the files id.

Examples:

Get the files id.

chunk.files_id

Since:

  • 2.0.0



99
100
101
# File 'lib/mongo/grid/file/chunk.rb', line 99

def files_id
  document[:files_id]
end

#idBSON::ObjectId

Get the chunk id.

Examples:

Get the chunk id.

chunk.id

Since:

  • 2.0.0



87
88
89
# File 'lib/mongo/grid/file/chunk.rb', line 87

def id
  document[:_id]
end

#nInteger

Get the chunk position.

Examples:

Get the chunk position.

chunk.n

Since:

  • 2.0.0



111
112
113
# File 'lib/mongo/grid/file/chunk.rb', line 111

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.

Examples:

Convert the chunk to BSON.

chunk.to_bson

Since:

  • 2.0.0



139
140
141
# File 'lib/mongo/grid/file/chunk.rb', line 139

def to_bson(buffer = BSON::ByteBuffer.new, validating_keys = BSON::Config.validating_keys?)
  document.to_bson(buffer)
end