Class: XGen::Mongo::GridFS::Chunk
- Inherits:
-
Object
- Object
- XGen::Mongo::GridFS::Chunk
- Defined in:
- lib/mongo/gridfs/chunk.rb
Overview
A chunk stores a portion of GridStore data.
Constant Summary collapse
- DEFAULT_CHUNK_SIZE =
1024 * 256
Instance Attribute Summary collapse
-
#chunk_number ⇒ Object
readonly
Returns the value of attribute chunk_number.
-
#data ⇒ Object
Returns the value of attribute data.
-
#object_id ⇒ Object
readonly
Returns the value of attribute object_id.
Instance Method Summary collapse
- #eof? ⇒ Boolean
- #getc ⇒ Object
-
#initialize(file, mongo_object = {}) ⇒ Chunk
constructor
A new instance of Chunk.
- #pos ⇒ Object
- #pos=(pos) ⇒ Object
- #putc(byte) ⇒ Object
- #save ⇒ Object
- #size ⇒ Object (also: #length)
- #to_mongo_object ⇒ Object
-
#truncate ⇒ Object
Erase all data after current position.
Constructor Details
#initialize(file, mongo_object = {}) ⇒ Chunk
Returns a new instance of Chunk.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/mongo/gridfs/chunk.rb', line 18 def initialize(file, mongo_object={}) @file = file @object_id = mongo_object['_id'] || XGen::Mongo::Driver::ObjectID.new @chunk_number = mongo_object['n'] || 0 @data = ByteBuffer.new case mongo_object['data'] when String mongo_object['data'].each_byte { |b| @data.put(b) } when ByteBuffer @data.put_array(mongo_object['data'].to_a) when Array @data.put_array(mongo_object['data']) when nil else raise "illegal chunk format; data is #{mongo_object['data'] ? (' ' + mongo_object['data'].class.name) : 'nil'}" end @data.rewind end |
Instance Attribute Details
#chunk_number ⇒ Object (readonly)
Returns the value of attribute chunk_number.
15 16 17 |
# File 'lib/mongo/gridfs/chunk.rb', line 15 def chunk_number @chunk_number end |
#data ⇒ Object
Returns the value of attribute data.
16 17 18 |
# File 'lib/mongo/gridfs/chunk.rb', line 16 def data @data end |
#object_id ⇒ Object (readonly)
Returns the value of attribute object_id.
15 16 17 |
# File 'lib/mongo/gridfs/chunk.rb', line 15 def object_id @object_id end |
Instance Method Details
#eof? ⇒ Boolean
40 |
# File 'lib/mongo/gridfs/chunk.rb', line 40 def eof?; !@data.more?; end |
#getc ⇒ Object
54 55 56 |
# File 'lib/mongo/gridfs/chunk.rb', line 54 def getc @data.more? ? @data.get : nil end |
#pos ⇒ Object
38 |
# File 'lib/mongo/gridfs/chunk.rb', line 38 def pos; @data.position; end |
#pos=(pos) ⇒ Object
39 |
# File 'lib/mongo/gridfs/chunk.rb', line 39 def pos=(pos); @data.position = pos; end |
#putc(byte) ⇒ Object
58 59 60 |
# File 'lib/mongo/gridfs/chunk.rb', line 58 def putc(byte) @data.put(byte) end |
#save ⇒ Object
62 63 64 65 66 |
# File 'lib/mongo/gridfs/chunk.rb', line 62 def save coll = @file.chunk_collection coll.remove({'_id' => @object_id}) coll.insert(to_mongo_object) end |
#size ⇒ Object Also known as: length
42 |
# File 'lib/mongo/gridfs/chunk.rb', line 42 def size; @data.size; end |
#to_mongo_object ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/mongo/gridfs/chunk.rb', line 68 def to_mongo_object h = OrderedHash.new h['_id'] = @object_id h['files_id'] = @file.files_id h['n'] = @chunk_number h['data'] = data h end |
#truncate ⇒ Object
Erase all data after current position.
46 47 48 49 50 51 52 |
# File 'lib/mongo/gridfs/chunk.rb', line 46 def truncate if @data.position < @data.length curr_data = @data @data = ByteBuffer.new @data.put_array(curr_data.to_a[0...curr_data.position]) end end |