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

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

Overview

Encapsulates behaviour 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)

Parameters:

  • document (BSON::Document)

    The document to create the chunk from.

Since:

  • 2.0.0



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

#documentBSON::Document (readonly)

Returns document The document to store for the chunk.

Returns:

  • (BSON::Document)

    document The document to store for the chunk.

Since:

  • 2.0.0



36
37
38
# File 'lib/mongo/grid/file/chunk.rb', line 36

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)

Parameters:

  • chunks (Array<Chunk>)

    The chunks.

Returns:

  • (String)

    The assembled data.

Since:

  • 2.0.0



153
154
155
# File 'lib/mongo/grid/file/chunk.rb', line 153

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

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

Split the provided data into multiple chunks.

Examples:

Split the data into chunks.

Chunks.split(data)

Parameters:

  • data (String)

    The raw bytes.

  • file_info (File::Info)

    The files collection file doc.

Returns:

  • (Array<Chunk>)

    The chunks of the data.

Since:

  • 2.0.0



168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/mongo/grid/file/chunk.rb', line 168

def split(data, file_info, offset = 0)
  chunks, index, n = [], 0, offset
  while index < data.length
    bytes = data.slice(index, file_info.chunk_size)
    file_info.md5.update(bytes)
    chunk = Chunk.new(:data => BSON::Binary.new(bytes), :files_id => file_info.id, :n => n)
    chunks.push(chunk)
    index += bytes.length
    n += 1
  end
  chunks
end

Instance Method Details

#==(other) ⇒ true, false

Check chunk equality.

Examples:

Check chunk equality.

chunk == other

Parameters:

  • other (Object)

    The object ot compare to.

Returns:

  • (true, false)

    If the objects are equal.

Since:

  • 2.0.0



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_typeInteger

Get the BSON type for a chunk document.

Examples:

Get the BSON type.

chunk.bson_type

Returns:

  • (Integer)

    The BSON type.

Since:

  • 2.0.0



61
62
63
# File 'lib/mongo/grid/file/chunk.rb', line 61

def bson_type
  BSON::Hash::BSON_TYPE
end

#dataBSON::Binary

Get the chunk data.

Examples:

Get the chunk data.

chunk.data

Returns:

  • (BSON::Binary)

    The chunk data.

Since:

  • 2.0.0



73
74
75
# File 'lib/mongo/grid/file/chunk.rb', line 73

def data
  document[:data]
end

#files_idBSON::ObjectId

Get the files id.

Examples:

Get the files id.

chunk.files_id

Returns:

  • (BSON::ObjectId)

    The files id.

Since:

  • 2.0.0



97
98
99
# File 'lib/mongo/grid/file/chunk.rb', line 97

def files_id
  document[:files_id]
end

#idBSON::ObjectId

Get the chunk id.

Examples:

Get the chunk id.

chunk.id

Returns:

  • (BSON::ObjectId)

    The chunk id.

Since:

  • 2.0.0



85
86
87
# File 'lib/mongo/grid/file/chunk.rb', line 85

def id
  document[:_id]
end

#nInteger

Get the chunk position.

Examples:

Get the chunk position.

chunk.n

Returns:

  • (Integer)

    The chunk position.

Since:

  • 2.0.0



109
110
111
# File 'lib/mongo/grid/file/chunk.rb', line 109

def n
  document[:n]
end

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

Conver the chunk to BSON for storage.

Examples:

Convert the chunk to BSON.

chunk.to_bson

Parameters:

  • buffer (String) (defaults to: BSON::ByteBuffer.new)

    The encoded data buffer to append to.

Returns:

  • (String)

    The raw BSON data.

Since:

  • 2.0.0



136
137
138
# File 'lib/mongo/grid/file/chunk.rb', line 136

def to_bson(buffer = BSON::ByteBuffer.new)
  document.to_bson(buffer)
end