Class: XGen::Mongo::GridFS::Chunk

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(file, mongo_object = {}) ⇒ Chunk

Returns a new instance of Chunk.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/mongo/gridfs/chunk.rb', line 34

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_numberObject (readonly)

Returns the value of attribute chunk_number.



31
32
33
# File 'lib/mongo/gridfs/chunk.rb', line 31

def chunk_number
  @chunk_number
end

#dataObject

Returns the value of attribute data.



32
33
34
# File 'lib/mongo/gridfs/chunk.rb', line 32

def data
  @data
end

#object_idObject (readonly)

Returns the value of attribute object_id.



31
32
33
# File 'lib/mongo/gridfs/chunk.rb', line 31

def object_id
  @object_id
end

Instance Method Details

#eof?Boolean

Returns:

  • (Boolean)


56
# File 'lib/mongo/gridfs/chunk.rb', line 56

def eof?; !@data.more?; end

#getcObject



70
71
72
# File 'lib/mongo/gridfs/chunk.rb', line 70

def getc
  @data.more? ? @data.get : nil
end

#posObject



54
# File 'lib/mongo/gridfs/chunk.rb', line 54

def pos; @data.position; end

#pos=(pos) ⇒ Object



55
# File 'lib/mongo/gridfs/chunk.rb', line 55

def pos=(pos); @data.position = pos; end

#putc(byte) ⇒ Object



74
75
76
# File 'lib/mongo/gridfs/chunk.rb', line 74

def putc(byte)
  @data.put(byte)
end

#saveObject



78
79
80
81
82
# File 'lib/mongo/gridfs/chunk.rb', line 78

def save
  coll = @file.chunk_collection
  coll.remove({'_id' => @object_id})
  coll.insert(to_mongo_object)
end

#sizeObject Also known as: length



58
# File 'lib/mongo/gridfs/chunk.rb', line 58

def size; @data.size; end

#to_mongo_objectObject



84
85
86
87
88
89
90
91
# File 'lib/mongo/gridfs/chunk.rb', line 84

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

#truncateObject

Erase all data after current position.



62
63
64
65
66
67
68
# File 'lib/mongo/gridfs/chunk.rb', line 62

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