#!/usr/bin/env ruby
# coding: binary
require 'yaml/store'
require 'pstore'
require 'csv'
require 'json'
require 'sqlite3'

finish=nil
if ARGV[0] == 'init'
  db = SQLite3::Database.new("data.sq3")
  keys = db.execute2("select * from d limit 1")[0]	# get column names
  obj = Hash.new
  STDERR.print "Loading..."
  CSV.foreach("dummy.csv") do |row|
    pkey = row[0]
    obj[pkey] = Hash.new
    keys.each do |k|
      obj[pkey][k] = row.shift
    end
  end
  start = Time.now.to_f
  if ENV['PST']
    STDERR.puts "Convert to PStore"
    PStore.new("data.pstore").transaction do |y|
      y["record"] = obj
    end
    finish = Time.now.to_f
  elsif ENV['JSON']
    STDERR.puts "Convert to JSON"
    open("data.json", "w") do |dj|
      dj.write JSON.pretty_generate(obj)
    end
    finish = Time.now.to_f
  elsif ENV['YAML']
    STDERR.puts "Convert to yaml"
    YAML::Store.new("data.yaml").transaction do |y|
      y["record"] = obj
    end
    finish = Time.now.to_f
  elsif ENV['CSV']
    STDERR.puts "Convert to CSV"
    CSV.open("data.csv", "w") do |csv|
      obj.each {|row| csv << row}
    end
    finish = Time.now.to_f
  end
  STDERR.printf("Creating took %.2fs\n", finish-start)
else
  start = Time.now.to_f
  newstr = Time.now.strftime("%S")
  if ENV['PST']
    STDERR.puts 'Loading PStore'
    PStore.new("data.pstore").transaction do |y|
      p y["record"]["C123000"]
      y["record"]["C123000"]["kname"].chop!
      y["record"]["C123000"]["kname"] += newstr
      p y["record"]["C123000"]
    end
    finish = Time.now.to_f
  elsif ENV['CSV']
    STDERR.puts 'Loading CSV'
    open("lock.csv", "w") do |lf|
      begin
        lf.flock(File::LOCK_EX)
        y = CSV.read("data.csv")
        p y.assoc("C123000")
        y.assoc("C123000")[1].chop!
        y.assoc("C123000")[1] += newstr
        p y.assoc("C123000")
        CSV.open("data2.csv", "w") do |csv|
          y.each{|row| csv << row}
        end
      ensure
        lf.flock(File::LOCK_UN)
        File.unlink("lock.csv")
      end
      finish = Time.now.to_f
    end
  elsif ENV['JSON']
    STDERR.puts 'Loading JSON'
    open("lock.json", "w") do |lf|
      begin
        lf.flock(File::LOCK_EX)
        y = nil
        open("data.json", "r") do |rj|
          y = JSON.load(rj)
          p y["C123000"]
          y["C123000"]["kname"].chop!
          y["C123000"]["kname"] += newstr
          p y["C123000"]
        end
        open("data2.json", "w") do |wj|
          wj.write JSON.pretty_generate(y)
        end
      ensure
          lf.flock(File::LOCK_EX)
          File.unlink("lock.json")
      end
    end
    finish = Time.now.to_f
  elsif ENV['SQ3']
    STDERR.puts 'Loading SQLite3'
    db = SQLite3::Database.new("data.sq3")
    #db.results_as_hash = true
    db.transaction do
      if 'sel' == ENV['SQ3']
        y = db.execute("select * from d where uid = 'C123000'")
      else
        y = db.execute("select * from d")
      end
      p y.assoc("C123000")
      y.assoc("C123000")[1].chop!
      y.assoc("C123000")[1] += newstr
      p y.assoc("C123000")
      y = db.execute("update d set kname = ? where uid = 'C123000'",
                     y.assoc("C123000")[1])
    end
    db.close
    finish = Time.now.to_f
  else
    STDERR.puts 'Loading yaml'
    YAML::Store.new("data.yaml").transaction do |y|
      p y["record"]["C123000"]
      y["record"]["C123000"]["kname"].chop!
      y["record"]["C123000"]["kname"] += newstr
      p y["record"]["C123000"]
    end
    finish = Time.now.to_f
  end
  STDERR.printf("Updation took %.2fs\n", finish-start)
end
