Class: HMap::HMapData

Inherits:
Object
  • Object
show all
Defined in:
lib/hmap/hmap_struct.rb

Overview

HMap blobs.

Instance Method Summary collapse

Constructor Details

#initialize(buckets) ⇒ HMapData

Returns a new instance of HMapData.



153
154
155
156
157
158
159
160
# File 'lib/hmap/hmap_struct.rb', line 153

def initialize(buckets)
  super()
  count = buckets.count
  nums = num_buckets(count, Utils.next_power_of_two(count))
  entries = entries(count, nums)
  @header = populate_hmap_header(nums, entries)
  @buckets = add_bucket(buckets, nums)
end

Instance Method Details

#entries(count, nums) ⇒ Object



173
174
175
176
177
178
179
# File 'lib/hmap/hmap_struct.rb', line 173

def entries(count, nums)
  return count if nums == 8

  last_pow = nums >> 1
  index = last_pow < 1024 ? 3 : -2
  count - (last_pow + 1 + index) / 3 + 1 + last_pow
end

#num_buckets(count, pow2) ⇒ Object



162
163
164
165
166
167
168
169
170
171
# File 'lib/hmap/hmap_struct.rb', line 162

def num_buckets(count, pow2)
  if count < 8
    pow2 <<= 1 if count * 4 >= pow2 * 3
    pow2 < 8 ? 8 : pow2
  else
    index = count > 341 ? 2 : -3
    padding = count / 85 % 7 + index
    Utils.next_power_of_two(count * 3 + padding)
  end
end

#serializeString

Returns the serialized fields of the mafile.

Returns:

  • the serialized fields of the mafile



182
183
184
185
186
187
188
189
190
191
192
# File 'lib/hmap/hmap_struct.rb', line 182

def serialize
  @header.serialize + @buckets.inject('') do |sum, bucket|
    sum += if bucket.nil?
             empty_b = [HEADER_CONST[:HMAP_EMPTY_BUCKT_KEY]] * 3
             empty_b.pack('L<3')
           else
             bucket
           end
    sum
  end
end