class Qwik::BackupBDB

Public Class Methods

new(path) click to toggle source
# File vendor/qwik/lib/qwik/db-b-backup.rb, line 11
def initialize(path)
  @backup_path = path+'.backup'
  @db = open_db(path+'backup.db')
end

Public Instance Methods

close() click to toggle source
# File vendor/qwik/lib/qwik/db-b-backup.rb, line 25
def close
  @db.close
end
each_by_key(key) { |v, time| ... } click to toggle source
# File vendor/qwik/lib/qwik/db-b-backup.rb, line 66
def each_by_key(key)
  ar = []
  # FIXME: use each_by_prefix(key+',') instead
  @db.each {|k, v|
    next if v.nil?
    if /\A#{key},([0-9]+)\z/ =~ k # begin with key?
      time = Time.at($1.to_i)
      ar << [v, time]
    end
  }

  if @backup_path.exist?
    @backup_path.each_entry {|file|
      f = @backup_path+file
      next unless f.file?
      base = file.to_s
      if /\A([0-9]+)_#{key}\z/ =~ base # end with key ?
        time = Time.at($1.to_i)
        v = get(key, time)
        ar << [v, time]
      end
    }
  end

  ar.sort_by {|v, time|
    time
  }.each {|v, time|
    yield(v, time)
  }
end
erase_all() click to toggle source
# File vendor/qwik/lib/qwik/test-module-path.rb, line 55
def erase_all
  bdb_erase_all(@db)
end
exist?(k, time) click to toggle source
# File vendor/qwik/lib/qwik/db-b-backup.rb, line 35
def exist?(k, time)
  v = db_get(k, time)
  return true if v
  path(k, time).exist?
end
get(k, time) click to toggle source
# File vendor/qwik/lib/qwik/db-b-backup.rb, line 41
def get(k, time)
  v = db_get(k, time)
  return v if v
  if  path(k, time).exist?
    str = path(k, time).open {|f| f.read }
    put(k, time, v)
    return str
  end
  ''
end
open_db(path) click to toggle source
# File vendor/qwik/lib/qwik/db-b-backup.rb, line 16
def open_db(path)
  options = 0
  unless path.exist? # don't over write
    options = BDB::CREATE | BDB::EXCL
  end
  db = BDB::Hash.open(path.to_s, nil, options)
  return db
end
put(k, time, v) click to toggle source
# File vendor/qwik/lib/qwik/db-b-backup.rb, line 61
def put(k, time, v)
  @db.put(make_key(k, time), v)
end
Also aliased as: set
set(k, time, v)
Alias for: put

Private Instance Methods

db_get(k, time) click to toggle source
# File vendor/qwik/lib/qwik/db-b-backup.rb, line 52
def db_get(k, time)
  @db.get(make_key(k, time))
end
make_key(k, time) click to toggle source
# File vendor/qwik/lib/qwik/db-b-backup.rb, line 56
def make_key(k, time)
  k+','+time.to_i.to_s
end
path(k, time) click to toggle source
# File vendor/qwik/lib/qwik/db-b-backup.rb, line 29
def path(k, time) # k is String, t is Time
  t = time.to_i.to_s
  @backup_path+(t+'_'+k)
end