class DpStmMap::TransactionLog

Public Class Methods

new(log_dir) click to toggle source
# File lib/dp_stm_map/Manager.rb, line 190
def initialize log_dir
  @log_dir=log_dir

  FileUtils.mkdir_p(log_dir) unless File.exist?(log_dir)

  unless File.exist?("#{log_dir}/last_id.txt")
    File.open("#{log_dir}/last_id.txt","w") {|f| f.write("0")}
    @last_id=0
  else
    @last_id=File.open("#{log_dir}/last_id.txt","r") {|f| f.read().to_i}
  end

  @mutex=Mutex.new
  @change=ConditionVariable.new
end

Public Instance Methods

add_listener(current_state) { |arg| ... } click to toggle source
# File lib/dp_stm_map/Manager.rb, line 218
def add_listener current_state, &block
  Thread.new do
    begin
      loop do
        arg=@mutex.synchronize do
          until current_state < last_id
            @change.wait(@mutex)
          end
          current_state+=1
          [current_state, read_tx(current_state)]
        end
        yield arg
      end


    rescue => e
      # puts "Error during processing: #{$!}"
      # puts "Backtrace:\n\t#{e.backtrace.join("\n\t")}"
    end
  end
end
last_id() click to toggle source
# File lib/dp_stm_map/Manager.rb, line 241
def last_id
  @last_id
end
read_tx(id) click to toggle source
# File lib/dp_stm_map/Manager.rb, line 207
def read_tx id
  JSON::parse(File.open("#{@log_dir}/#{id}.json","r") {|f| f.read})
end
store_transaction(references_to_change, values_to_add, values_to_remove) click to toggle source
# File lib/dp_stm_map/Manager.rb, line 245
def store_transaction references_to_change, values_to_add, values_to_remove
  @mutex.synchronize do
    txid=last_id+1
    write_tx txid, [references_to_change, values_to_add, values_to_remove]
    File.open("#{@log_dir}/last_id.txt","w") {|f| f.write(txid.to_s)}
    @change.broadcast
    @last_id=txid
    txid
  end
end
write_tx(id, tx) click to toggle source
# File lib/dp_stm_map/Manager.rb, line 211
def write_tx id, tx

  File.open("#{@log_dir}/#{id}.json","w") {|f| f.write(tx.to_json)}
end