Class: HMap::HMapSaver

Inherits:
Object
  • Object
show all
Defined in:
lib/hmap/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.



16
17
18
19
20
# File 'lib/hmap/hmap/hmap_saver.rb', line 16

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

Instance Attribute Details

#bucketsObject (readonly)

Returns the value of attribute buckets.



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

def buckets
  @buckets
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#string_tableObject (readonly)

Returns the value of attribute string_table.



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

def string_table
  @string_table
end

Class Method Details

.new_from_buckets(buckets) ⇒ Object



10
11
12
13
14
# File 'lib/hmap/hmap/hmap_saver.rb', line 10

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

Instance Method Details

#add_to_bucket(buckte) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/hmap/hmap/hmap_saver.rb', line 44

def add_to_bucket(buckte)
  key = add_to_headers(buckte.key)
  perfix = add_to_headers(buckte.perfix)
  suffix = add_to_headers(buckte.suffix)
  bucket = HMapBucket.new(key, perfix, suffix)
  bucket.uuid = Utils.string_downcase_hash(buckte.key)
  @buckets << bucket
end

#add_to_buckets(buckets) ⇒ Object



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

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

#add_to_headers(key) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/hmap/hmap/hmap_saver.rb', line 36

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



32
33
34
# File 'lib/hmap/hmap/hmap_saver.rb', line 32

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

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



22
23
24
25
26
27
28
29
30
# File 'lib/hmap/hmap/hmap_saver.rb', line 22

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

#write_to(path) ⇒ Object



57
58
59
# File 'lib/hmap/hmap/hmap_saver.rb', line 57

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