Class: HMap::HMapSaver

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHMapSaver

Returns a new instance of HMapSaver.



13
14
15
16
17
# File 'lib/hmap/hmap_saver.rb', line 13

def initialize
  @string_table = "\0"
  @buckets = []
  @headers = {}
end

Instance Attribute Details

#bucketsObject (readonly)

Returns the value of attribute buckets.



5
6
7
# File 'lib/hmap/hmap_saver.rb', line 5

def buckets
  @buckets
end

#headersObject (readonly)

Returns the value of attribute headers.



5
6
7
# File 'lib/hmap/hmap_saver.rb', line 5

def headers
  @headers
end

#string_tableObject (readonly)

Returns the value of attribute string_table.



5
6
7
# File 'lib/hmap/hmap_saver.rb', line 5

def string_table
  @string_table
end

Class Method Details

.new_from_buckets(buckets) ⇒ Object



7
8
9
10
11
# File 'lib/hmap/hmap_saver.rb', line 7

def self.new_from_buckets(buckets)
  saver = new
  saver.add_to_buckets(buckets)
  saver
end

Instance Method Details

#add_to_bucket(buckets) ⇒ Object



41
42
43
44
45
46
# File 'lib/hmap/hmap_saver.rb', line 41

def add_to_bucket(buckets)
  values = buckets.map { |key| add_to_headers(key) }
  bucket = HMapBucket.new(*values)
  bucket.uuid = Utils.string_downcase_hash(buckets.first)
  @buckets << bucket
end

#add_to_buckets(buckets) ⇒ Object



48
49
50
# File 'lib/hmap/hmap_saver.rb', line 48

def add_to_buckets(buckets)
  buckets.each { |bucket| add_to_bucket(bucket) }
end

#add_to_headers(key) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/hmap/hmap_saver.rb', line 33

def add_to_headers(key)
  if headers[key].nil?
    headers[key] = string_table.length
    add_to_string_table(key)
  end
  headers[key]
end

#add_to_string_table(str) ⇒ Object



29
30
31
# File 'lib/hmap/hmap_saver.rb', line 29

def add_to_string_table(str)
  @string_table += "#{Utils.safe_encode(str, 'ASCII-8BIT')}\0"
end

#header_to_hash(keys, headers, index, buckets) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/hmap/hmap_saver.rb', line 19

def header_to_hash(keys, headers, index, buckets)
  index = index.length
  keys.inject('') do |sum, bucket|
    buckte = HMapBucketStr.new(*bucket)
    string_t = buckte.bucket_to_string(headers, index + sum.length)
    buckets.push(buckte)
    sum + string_t
  end
end

#write_to(path) ⇒ Object



52
53
54
# File 'lib/hmap/hmap_saver.rb', line 52

def write_to(path)
  MapFile.new(@string_table, @buckets).write(path)
end