class Qwik::BerkeleyDB

Attributes

backupdb[R]

Public Class Methods

new(path, spath) click to toggle source
# File vendor/qwik/lib/qwik/db-berkeley.rb, line 20
def initialize(path, spath)
  @path = path
  raise 'No such dir. '+@path.to_s unless @path.directory?
  @path_h = {}
  @spath = spath
  @spath_h = {}

  @db = open_db(@path+'pages.db')
  @mtime_db = open_db(@path+'mtime.db')

  @backupdb = BackupBDB.new(@path)
end

Public Instance Methods

add(k, v, time=Time.now) click to toggle source
# File vendor/qwik/lib/qwik/db-berkeley.rb, line 119
def add(k, v, time=Time.now)
  # FIXME: use @db.add(k, v) instead
  put(k, get(k)+v, time)
end
baseexist?(k) click to toggle source
# File vendor/qwik/lib/qwik/db-berkeley.rb, line 71
def baseexist?(k)
  v = db_get(k)
  return true if v
  begin
    return true if path(k).exist?
  rescue
    return false
  end
  return false
end
close() click to toggle source
# File vendor/qwik/lib/qwik/db-berkeley.rb, line 55
def close
  @backupdb.close
  @db.close
  @mtime_db.close
end
create(k) click to toggle source
# File vendor/qwik/lib/qwik/db-berkeley.rb, line 82
def create(k)
  touch(k) unless exist?(k)
end
db_get(k) click to toggle source
# File vendor/qwik/lib/qwik/db-berkeley.rb, line 99
def db_get(k)
  begin
    @db.get(k)
  rescue BDB::Fatal => e
    return if e.message == 'closed DB'
    raise e
  end
end
db_put(k, v, mtime) click to toggle source
# File vendor/qwik/lib/qwik/db-berkeley.rb, line 131
def db_put(k, v, mtime)
  @db.put(k, v)
  @mtime_db.put(k, mtime.to_i.to_s)
end
delete(k) click to toggle source
# File vendor/qwik/lib/qwik/db-berkeley.rb, line 140
def delete(k)
  put(k, nil)
  path(k).unlink if path(k).exist?
end
each() { |k| ... } click to toggle source
# File vendor/qwik/lib/qwik/db-berkeley.rb, line 145
def each
  ar = []
  @db.each {|k, v|
    next if v.nil?
    ar << k
  }
  dir = @path.to_s
  Dir.foreach(dir) {|file|
    f = dir+'/'+file
    next unless FileTest.file?(f)
    base = file.to_s
    if /\A([_A-Za-z0-9]+)\.txt\z/ =~ base
      b = $1
      ar << b if !ar.include?(b)
    end
  }
  ar.sort.each {|k|
    yield(k)
  }
end
erase_all() click to toggle source
# File vendor/qwik/lib/qwik/test-module-path.rb, line 45
def erase_all
  @backupdb.erase_all
  bdb_erase_all(@db)
  @mtime_db.erase_all
end
exist?(k) click to toggle source
# File vendor/qwik/lib/qwik/db-berkeley.rb, line 61
def exist?(k)
  begin
    return true if baseexist?(k)
    return true if spath(k).exist?
  rescue
    return false
  end
  return false
end
get(k) click to toggle source
# File vendor/qwik/lib/qwik/db-berkeley.rb, line 86
def get(k)
  v = db_get(k)
  return v if v
  if  path(k).exist?
    str = path(k).get
    mtime = path(k).mtime
    db_put(k, str, mtime)
    return str
  end
  return spath(k).get if spath(k).exist?
  ''
end
last_article_time() click to toggle source

QuickML support

# File vendor/qwik/lib/qwik/db-berkeley.rb, line 173
def last_article_time # mtime of the newest file
  max = maxfile = nil
  @path.each_entry {|file|
    f = @path+file
    next unless f.file?
    base = file.to_s
    next if /\A\./ =~ base ||
      base == ',config' ||
      base == '_GroupConfig.txt' ||
      base == ',charset' ||
      base == '_GroupCharset.txt'
    next if /\.db\z/ =~ base
    mt = f.mtime
    if max.nil? || max < mt
      max = mt 
      maxfile = f
    end
  }
  max # return nil if there is no file.
end
last_page_time() click to toggle source
# File vendor/qwik/lib/qwik/db-berkeley.rb, line 166
def last_page_time # mtime of the newest page
  t = map {|k| mtime(k) }.max
  t = Time.at(0) if t.nil?
  t
end
mtime(k) click to toggle source
# File vendor/qwik/lib/qwik/db-berkeley.rb, line 108
def mtime(k)
  v = db_get(k)
  if v
    num = @mtime_db.get(k)
    return Time.at(num.to_i)
  end
  return  path(k).mtime if  path(k).exist?
  return spath(k).mtime if spath(k).exist?
  Time.at(0) # the file is already deleted.
end
open_db(path) click to toggle source
# File vendor/qwik/lib/qwik/db-berkeley.rb, line 34
def open_db(path)
  options = 0
  options = BDB::CREATE | BDB::EXCL unless path.exist? # don't over write
  begin
    db = BDB::Hash.open(path.to_s, nil, options)
  rescue => e
    p 'e is '+e
    @path.each_entry {|pa|
      if /\.db\z/ =~ pa.to_s
        p pa
        (@path+pa).unlink
      end
    }
    $try ||= 0
    $try += 1
    retry if $try < 5
    raise e
  end
  return db
end
put(k, v, time=Time.now) click to toggle source
# File vendor/qwik/lib/qwik/db-berkeley.rb, line 124
def put(k, v, time=Time.now)
  db_put(k, v, time)
  @backupdb.put(k, time, v)
  path(k).open('wb'){|f| f.print(v) } # write through
end
Also aliased as: set
set(k, v, time=Time.now)
Alias for: put
touch(k) click to toggle source
# File vendor/qwik/lib/qwik/db-berkeley.rb, line 136
def touch(k)
  add(k, '') # add null String
end

Private Instance Methods

gen_path(path, h, k) click to toggle source
# File vendor/qwik/lib/qwik/db-berkeley.rb, line 204
def gen_path(path, h, k)
  pa = h[k]
  return pa if pa
  raise 'Character type error '+k unless /\A([_A-Za-z0-9]+)\z/ =~ k
  h[k] = path+(k+'.txt')
  h[k]
end
path(k) click to toggle source
# File vendor/qwik/lib/qwik/db-berkeley.rb, line 196
def path(k)
  gen_path(@path, @path_h, k)
end
spath(k) click to toggle source
# File vendor/qwik/lib/qwik/db-berkeley.rb, line 200
def spath(k)
  gen_path(@spath, @spath_h, k)
end