Class: PackedIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/scout/persist/engine/packed_index.rb

Constant Summary collapse

ELEMS =
{
  "i" => ["l", 4],
  "I" => ["q", 8],
  "f" => ["f", 4],
  "F" => ["d", 8],
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(persistence_path, 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/scout/persist/engine/packed_index.rb', line 37

def initialize(persistence_path, write = false, pattern = nil)
  @persistence_path = persistence_path
  if write
    @stream = Open.open(persistence_path, :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(persistence_path, :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

#item_sizeObject

Returns the value of attribute item_size.



2
3
4
# File 'lib/scout/persist/engine/packed_index.rb', line 2

def item_size
  @item_size
end

#maskObject

Returns the value of attribute mask.



2
3
4
# File 'lib/scout/persist/engine/packed_index.rb', line 2

def mask
  @mask
end

#mask_lengthObject

Returns the value of attribute mask_length.



2
3
4
# File 'lib/scout/persist/engine/packed_index.rb', line 2

def mask_length
  @mask_length
end

#nil_stringObject

Returns the value of attribute nil_string.



2
3
4
# File 'lib/scout/persist/engine/packed_index.rb', line 2

def nil_string
  @nil_string
end

#offsetObject

Returns the value of attribute offset.



2
3
4
# File 'lib/scout/persist/engine/packed_index.rb', line 2

def offset
  @offset
end

#persistence_pathObject

Returns the value of attribute persistence_path.



2
3
4
# File 'lib/scout/persist/engine/packed_index.rb', line 2

def persistence_path
  @persistence_path
end

#streamObject

Returns the value of attribute stream.



2
3
4
# File 'lib/scout/persist/engine/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/scout/persist/engine/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



74
75
76
77
78
79
80
# File 'lib/scout/persist/engine/packed_index.rb', line 74

def <<(payload)
  if payload.nil?
    @stream.write nil_string
  else
    @stream.write payload.pack(mask)
  end
end

#closeObject



60
61
62
# File 'lib/scout/persist/engine/packed_index.rb', line 60

def close
  @stream.close
end

#fileObject



56
57
58
# File 'lib/scout/persist/engine/packed_index.rb', line 56

def file
  @persistence_path
end

#get_position(position) ⇒ Object Also known as: []



82
83
84
85
86
87
# File 'lib/scout/persist/engine/packed_index.rb', line 82

def get_position(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

#read(force = false) ⇒ Object



69
70
71
72
# File 'lib/scout/persist/engine/packed_index.rb', line 69

def read(force = false)
  close
  @stream = Open.open(persistence_path, :mode => 'rb')
end

#sizeObject



31
32
33
34
35
# File 'lib/scout/persist/engine/packed_index.rb', line 31

def size
  @size ||= begin
              (File.size(persistence_path) - offset) / item_size
            end
end

#values_at(*positions) ⇒ Object



91
92
93
94
95
# File 'lib/scout/persist/engine/packed_index.rb', line 91

def values_at(*positions)
  positions.collect{|p|
    get_position[p]
  }
end

#write(force = false) ⇒ Object



64
65
66
67
# File 'lib/scout/persist/engine/packed_index.rb', line 64

def write(force = false)
  close
  @stream = Open.open(persistence_path, :mode => 'wb')
end