Class: PackedIndex
- Inherits:
-
Object
- Object
- PackedIndex
- Defined in:
- lib/rbbt/packed_index.rb
Constant Summary collapse
- ELEMS =
{ "i" => ["l", 4], "I" => ["q", 8], "f" => ["f", 4], "F" => ["d", 8], }
Instance Attribute Summary collapse
-
#file ⇒ Object
Returns the value of attribute file.
-
#item_size ⇒ Object
Returns the value of attribute item_size.
-
#mask ⇒ Object
Returns the value of attribute mask.
-
#mask_length ⇒ Object
Returns the value of attribute mask_length.
-
#nil_string ⇒ Object
Returns the value of attribute nil_string.
-
#offset ⇒ Object
Returns the value of attribute offset.
-
#stream ⇒ Object
Returns the value of attribute stream.
Class Method Summary collapse
Instance Method Summary collapse
- #<<(payload) ⇒ Object
- #[](position) ⇒ Object
- #close ⇒ Object
-
#initialize(file, write = false, pattern = nil) ⇒ PackedIndex
constructor
A new instance of PackedIndex.
- #persistence_path ⇒ Object
- #persistence_path=(value) ⇒ Object
- #read(force = false) ⇒ Object
- #size ⇒ Object
- #values_at(*positions) ⇒ Object
Constructor Details
#initialize(file, write = false, pattern = nil) ⇒ PackedIndex
Returns a new instance of PackedIndex.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/rbbt/packed_index.rb', line 37 def initialize(file, write = false, pattern = nil) @file = file if write @stream = Open.open(file, :mode => 'wb') @mask, @item_size = PackedIndex.process_mask pattern header = [@mask.length, @item_size].pack("ll") @stream.write(header) @stream.write(mask) @offset = @mask.length + 8 else @stream = Open.open(file, :mode => 'rb') header = @stream.read(8) mask_length, @item_size = header.unpack("ll") @mask = @stream.read(mask_length) @offset = @mask.length + 8 end @nil_string = "NIL" << ("-" * (@item_size - 3)) end |
Instance Attribute Details
#file ⇒ Object
Returns the value of attribute file.
2 3 4 |
# File 'lib/rbbt/packed_index.rb', line 2 def file @file end |
#item_size ⇒ Object
Returns the value of attribute item_size.
2 3 4 |
# File 'lib/rbbt/packed_index.rb', line 2 def item_size @item_size end |
#mask ⇒ Object
Returns the value of attribute mask.
2 3 4 |
# File 'lib/rbbt/packed_index.rb', line 2 def mask @mask end |
#mask_length ⇒ Object
Returns the value of attribute mask_length.
2 3 4 |
# File 'lib/rbbt/packed_index.rb', line 2 def mask_length @mask_length end |
#nil_string ⇒ Object
Returns the value of attribute nil_string.
2 3 4 |
# File 'lib/rbbt/packed_index.rb', line 2 def nil_string @nil_string end |
#offset ⇒ Object
Returns the value of attribute offset.
2 3 4 |
# File 'lib/rbbt/packed_index.rb', line 2 def offset @offset end |
#stream ⇒ Object
Returns the value of attribute stream.
2 3 4 |
# File 'lib/rbbt/packed_index.rb', line 2 def stream @stream end |
Class Method Details
.process_mask(mask) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/rbbt/packed_index.rb', line 11 def self.process_mask(mask) str = "" size = 0 mask.each do |e| if ELEMS.include? e str << ELEMS[e][0] size += ELEMS[e][1] elsif e =~ /^(\d+)s$/ num = $1.to_i str << "a" << num.to_s size += num else e, num = e.split(":") str << e size = (num.nil? ? size + 1 : size + num.to_i) end end [str, size] end |
Instance Method Details
#<<(payload) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/rbbt/packed_index.rb', line 69 def <<(payload) if payload.nil? @stream.write nil_string else @stream.write payload.pack(mask) end end |
#[](position) ⇒ Object
77 78 79 80 81 82 |
# File 'lib/rbbt/packed_index.rb', line 77 def [](position) @stream.seek(position * item_size + offset) encoded = @stream.read(item_size) return nil if encoded.nil? or encoded == nil_string encoded.unpack mask end |
#close ⇒ Object
90 91 92 |
# File 'lib/rbbt/packed_index.rb', line 90 def close stream.close unless stream.closed? end |
#persistence_path ⇒ Object
56 57 58 |
# File 'lib/rbbt/packed_index.rb', line 56 def persistence_path @file end |
#persistence_path=(value) ⇒ Object
60 61 62 |
# File 'lib/rbbt/packed_index.rb', line 60 def persistence_path=(value) @file=value end |
#read(force = false) ⇒ Object
64 65 66 67 |
# File 'lib/rbbt/packed_index.rb', line 64 def read(force = false) close @stream = Open.open(file, :mode => 'rb') end |
#size ⇒ Object
31 32 33 34 35 |
# File 'lib/rbbt/packed_index.rb', line 31 def size @size ||= begin (File.size(file) - offset) / item_size end end |
#values_at(*positions) ⇒ Object
84 85 86 87 88 |
# File 'lib/rbbt/packed_index.rb', line 84 def values_at(*positions) positions.collect{|p| self[p] } end |