Method: PureCDB::Reader#values

Defined in:
lib/purecdb/reader.rb

#values(key) ⇒ Object

Returns all values for key in an array



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/purecdb/reader.rb', line 107

def values(key)
  h = hash(key)

  hoff = @hashes[(h % 256)*2]
  hlen = @hashes[(h % 256)*2 + 1]

  return [] if hlen == 0
  off = (h / 256) % hlen

  vals = []

  # FIXME: Is this potentially an infinite loop (if full)?
  # Easy to avoid by exiting if off reaches the same value twice.

  while
      (slot = read(hoff + off * hashref_size .. hoff + off * hashref_size + hashref_size - 1)) &&
      (dslot = ary_unpack(slot,2)) && dslot[1] != 0

    if dslot[0] == h
      pos = dslot[1]

      rkey, value = read_entry(pos)
      if rkey == key
        vals << value
      end
    end
    off = (off + 1) % hlen
  end
  return vals
end