require 'yaml'
require 'sldb'

DB = SLDB::new { schema 'create table t ( tid, time )'; path 'sldb' }
db = DB::new

#
# multi-processed/multi-threaded applications may simoultaneously access the db 
#

4.times do
  unless fork
    pid = $$
    threads = []
    2.times do |i|
      threads << 
        Thread::new(i, db) do |tid, db|
          sleep rand
          tuple = db.tuple_for 't'
          tuple['tid'] = "#{ pid }:#{ tid }" 
          tuple['time'] = Time::now.to_f
          values = db.quote tuple
          db.transaction{db.execute "insert into t values(#{ values.join ',' })"}
        end
    end
    threads.each{|t| t.join}
    exit!
  end
end

4.times{ Process::wait }

report = Hash::new{|h,k| h[k] = []} 

db.transaction{db.execute("select * from t"){|t| report['t'] << t.to_hash}}

y report
