123456789_123456789_123456789_123456789_123456789_

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

Relationships & Source Files
Inherits: Object
Defined in: lib/mongo/grid/file/chunk.rb

Overview

Encapsulates behavior around GridFS chunks of file data.

Since:

  • 2.0.0

Constant Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(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

[ GitHub ]

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

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

Class Method Details

.assemble(chunks) ⇒ String

This method is for internal use only.

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

[ GitHub ]

  
# 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 for internal use only.

Split the provided data into multiple chunks.

Examples:

Split the data into chunks.

Chunks.split(data)

Parameters:

  • io (String, IO)

    The raw bytes.

  • file_info (File::Info)

    The files collection file doc.

  • offset (Integer) (defaults to: 0)

    The offset.

Returns:

  • (Array<Chunk>)

    The chunks of the data.

Since:

  • 2.0.0

[ GitHub ]

  
# 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 Attribute Details

#documentBSON::Document (readonly)

Returns:

  • (BSON::Document)

    document The document to store for the chunk.

Since:

  • 2.0.0

[ GitHub ]

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

attr_reader :document

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

[ GitHub ]

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

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

[ GitHub ]

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

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

[ GitHub ]

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

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

[ GitHub ]

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

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

[ GitHub ]

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

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

[ GitHub ]

  
# 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.

Examples:

Convert the chunk to BSON.

chunk.to_bson

Parameters:

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

    The encoded BSON buffer to append to.

  • validating_keys (true, false) (defaults to: nil)

    Whether keys should be validated when serializing. This option is deprecated and will not be used. It will removed in version 3.0.

Returns:

  • (String)

    The raw BSON data.

Since:

  • 2.0.0

[ GitHub ]

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

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