class KeyValueChain::MemKVChain
Public Class Methods
new(path)
click to toggle source
# File lib/key_value_chain.rb, line 82 def initialize(path) @blocks = [] @hashtable = Hash.new("") @path = path self.loadfile end
Public Instance Methods
[](key)
click to toggle source
# File lib/key_value_chain.rb, line 117 def [](key) @hashtable[key] end
[]=(key, value)
click to toggle source
# File lib/key_value_chain.rb, line 120 def []=(key, value) self.add_block(KVPair.new(self.last_block_hash, key, value)) end
add_block(pair)
click to toggle source
# File lib/key_value_chain.rb, line 97 def add_block(pair) if add_block_to_hash(pair) open(@path, 'a') { |f| f.puts pair.to_json } return true else return false end end
add_block_to_hash(pair)
click to toggle source
# File lib/key_value_chain.rb, line 88 def add_block_to_hash(pair) if pair.is_valid? @blocks << pair @hashtable[pair.key] = pair.value return true end false end
last_block_hash()
click to toggle source
# File lib/key_value_chain.rb, line 110 def last_block_hash if @blocks.any? @blocks[-1].to_hash else "0000000000000000000000000000000000000000" end end
last_block_json()
click to toggle source
# File lib/key_value_chain.rb, line 107 def last_block_json @blocks[-1].to_json end
loadfile(path = @path)
click to toggle source
# File lib/key_value_chain.rb, line 123 def loadfile(path = @path) #f = File.open(path, "a+") #f.foreach(path).with_index do |line, line_num| # self.add_block(pair_from_json(line)) #end File.open(path, "a+") do |f| f.each_line do |line| self.add_block_to_hash(pair_from_json(line)) end end end