<commit_before>require 'rubygems'
require 'bud'

require 'voting/voting'

module PaxosPrepare 
  include MajorityVotingMaster

  state {
    table :local_aru, [], ['host', 'aru']
    scratch :leader_change, ['host'], ['leader', 'view']
  
    scratch :prepare, ['view', 'aru']
    table :quorum, ['view', 'aru']
  }

  declare 
  def prep1
    # bootstrap these.
    #local_aru << [@myloc, 0] if global_history.empty?
    #last_installed << [0] if global_history.empty?

    prepare <= join([leader_change, local_aru]).map do |c, a|
      if c.leader == c.host
        print "prepare!\n" or [c.view, a.aru]
      end
    end

    begin_vote <+ prepare.map{|p| print "put in ballot : " + p.inspect + "\n" or [p.view, p.aru]}
  end

  declare 
  def establish_quorum
    quorum <= vote_status.map do |v|
      if v.response.class == Array 
        [ v.response.fetch(0), v.response.fetch(1) ] if v.response.fetch(4) == 'bottom'
      end
    end
  end
end

module PaxosPrepareAgent 
  include VotingAgent

  state {
    table :datalist, ['view', 'aru_requested', 'seq', 'update', 'dltype']
    table :datalist_length, ['aru', 'len']
    table :global_history, ['host', 'seqno'], ['requestor', 'update']
    table :last_installed, [], ['view']
    table :accept, ['view', 'seq', 'update']
  }

  declare
  def build_reply
    datalist <= join([ballot, last_installed]).map do |d, l|
      if d.id > l.view
        print "AROO\n" or [d.id, d.content, -1, "none", "bottom"]
      else 
        print "ACHOO " + d.inspect + ":: " + l.inspect + " vs. " +d.id.to_s + "\n"
      end
    end

    datalist <= join([datalist, global_history]).map do |d, g|<commit_msg>modernize paxos prepare implementation<commit_after>require 'rubygems'
require 'bud'

require 'voting/voting'

module PaxosPrepare 
  include BudModule
  include MajorityVotingMaster

  state do
    table :local_aru, [] => [:host, :aru]
    scratch :leader_change, [:host] => [:leader, :view]
  
    scratch :prepare, [:view, :aru]
    table :quorum, [:view, :aru]
  end


  bootstrap do
    local_aru << [@myloc, 0] #if global_history.empty?
  end

  declare 
  def prep1
    # bootstrap these.
    #local_aru << [@myloc, 0] if global_history.empty?
    #last_installed << [0] if global_history.empty?

    prepare <= join([leader_change, local_aru]).map do |c, a|
      if c.leader == c.host
        print "prepare!\n" or [c.view, a.aru]
      end
    end

    begin_vote <+ prepare.map{|p| print "put in ballot : " + p.inspect + "\n" or [p.view, p.aru]}
  end

  declare 
  def establish_quorum
    quorum <= vote_status.map do |v|
      puts "VOTE_STATUS: #{v.inspect}"
      if v.response.class == Array 
        [ v.response.fetch(0), v.response.fetch(1) ] if v.response.fetch(4) == 'bottom'
      end
    end

    stdio <~ quorum.map{|q| ["QUORUM: #{q.inspect}"] }
  end
  
end

module PaxosPrepareAgent 
  include VotingAgent

  state do
    table :datalist, [:view, :aru_requested, :seq, :update, :dltype]
    table :datalist_length, [:aru, :len]
    table :global_history, [:host, :seqno] => [:requestor, :update]
    table :last_installed, [] => [:view]
    table :accept, [:view, :seq, :update]
  end

  bootstrap do
    last_installed << [0] if global_history.empty?
  end

  declare
  def build_reply
    stdio <~ ballot.map{|b| ["got ballot: #{b.inspect}"] }
    datalist <= join([ballot, last_installed]).map do |d, l|
      if d.ident > l.view
        print "AROO\n" or [d.ident, d.content, -1, "none", "bottom"]
      else 
        print "ACHOO " + d.inspect + ":: " + l.inspect + " vs. " +d.ident.to_s + "\n"
      end
    end

    datalist <= join([datalist, global_history]).map do |d, g|
{'fraction_non_alphanumeric': 0.15930169121658483, 'fraction_numerical': 0.00436442989634479, 'mean_word_length': 2.8804232804232806, 'pattern_counts': {'":': 2, '<': 27, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5239360', 'n_tokens_mistral': 1393, 'n_tokens_neox': 1326, 'n_words': 345}