class Qwik::FileSystemDB

Constants

IGNORE_ON_LAST_ARTICLE_TIME

Public Class Methods

new(path, spath) click to toggle source
# File vendor/qwik/lib/qwik/db-filesystem.rb, line 10
def initialize(path, spath)
  @path = path
  @path_h = {}
  @spath = spath
  @spath_h = {}

  @observer = []
end

Public Instance Methods

add(k, v) click to toggle source
# File vendor/qwik/lib/qwik/db-filesystem.rb, line 83
def add(k, v)
  put(k, get(k)+v)
end
backupdb() click to toggle source

FIXME: Obsolete this method.

# File vendor/qwik/lib/qwik/db-filesystem.rb, line 20
def backupdb
  # FIXME: Check if it is the backupdb.
  return @observer[0]
end
baseexist?(k) click to toggle source
# File vendor/qwik/lib/qwik/db-filesystem.rb, line 51
def baseexist?(k)
  begin
    return true if path(k).exist?
  rescue
    return false
  end
  return false
end
check_integrity(ob) click to toggle source
# File vendor/qwik/lib/qwik/db-filesystem.rb, line 25
def check_integrity(ob)
  self.each {|k|
    v = self.get(k)
    ob.check(k, v)
  }
end
close() click to toggle source
# File vendor/qwik/lib/qwik/db-filesystem.rb, line 37
def close
  # do nothing
end
create(k) click to toggle source
# File vendor/qwik/lib/qwik/db-filesystem.rb, line 60
def create(k)
  touch(k) unless exist?(k)
end
delete(k) click to toggle source
# File vendor/qwik/lib/qwik/db-filesystem.rb, line 108
def delete(k)
  path(k).unlink if path(k).exist?
end
each(with_super = false) { |b| ... } click to toggle source
# File vendor/qwik/lib/qwik/db-filesystem.rb, line 118
def each(with_super = false)
  list(with_super).each {|b|
    yield(b)
  }
end
each_all(&b) click to toggle source
# File vendor/qwik/lib/qwik/db-filesystem.rb, line 124
def each_all(&b)
  each(true, &b)
end
erase_all() click to toggle source
# File vendor/qwik/lib/qwik/test-module-path.rb, line 23
def erase_all
  # do nothing
end
exist?(k) click to toggle source
# File vendor/qwik/lib/qwik/db-filesystem.rb, line 41
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-filesystem.rb, line 64
def get(k)
  if  path(k).exist?
    return path(k).read
  end
  return spath(k).read if spath(k).exist?
  return ''         # Not nil.
end
last_article_time() click to toggle source

QuickML support

# File vendor/qwik/lib/qwik/db-filesystem.rb, line 144
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 ignore_on_last_article_time?(base)

    mt = f.mtime
    if max.nil? || max < mt
      max = mt 
      maxfile = f
    end
  }
  return max # return nil if there is no file.
end
last_page_time() click to toggle source
# File vendor/qwik/lib/qwik/db-filesystem.rb, line 134
def last_page_time # mtime of the newest page
  max = 0
  list().each {|pagename|
    t = mtime(pagename).to_i
    max = t if max < t
  }
  return Time.at(max)
end
list(with_super = false) click to toggle source
# File vendor/qwik/lib/qwik/db-filesystem.rb, line 112
def list(with_super = false)
  ar =  get_dir_list(@path.to_s)
  ar += get_dir_list(@spath.to_s) if with_super
  return ar.sort
end
mtime(k) click to toggle source
# File vendor/qwik/lib/qwik/db-filesystem.rb, line 77
def mtime(k)
  return  path(k).mtime if  path(k).exist?
  return spath(k).mtime if spath(k).exist?
  return Time.at(0)         # Not nil.  The file is already deleted.
end
nu_last_page_time() click to toggle source
# File vendor/qwik/lib/qwik/db-filesystem.rb, line 128
def nu_last_page_time # mtime of the newest page
  t = map {|k| mtime(k) }.max
  t = Time.at(0) if t.nil?
  return t
end
put(k, v, time=nil) click to toggle source
# File vendor/qwik/lib/qwik/db-filesystem.rb, line 87
def put(k, v, time=nil)
  # FIXME: Should make it atomic.  Use mutex.
  path(k).put(v)
  if time
    time = Time.at(time) if time.is_a?(Integer)
    path(k).utime(time, time)
  else
    time = Time.now
  end

# @backupdb.put(k, time, v)
  @observer.each {|ob|
    ob.put(k, v, time)
  }
end
Also aliased as: set
register_observer(ob) click to toggle source
# File vendor/qwik/lib/qwik/db-filesystem.rb, line 32
def register_observer(ob)
  check_integrity(ob)
  @observer << ob
end
set(k, v, time=nil)
Alias for: put
size(k) click to toggle source
# File vendor/qwik/lib/qwik/db-filesystem.rb, line 72
def size(k)
  return  path(k).size if  path(k).exist?
  return 0
end
touch(k) click to toggle source
# File vendor/qwik/lib/qwik/db-filesystem.rb, line 104
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-filesystem.rb, line 183
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')
  return h[k]
end
get_dir_list(dir) click to toggle source

FIXME: I don’t know why this method doesn’t use Pathname.

# File vendor/qwik/lib/qwik/db-filesystem.rb, line 192
def get_dir_list(dir)
  ar = []
  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
    end
  } 
  return ar
end
ignore_on_last_article_time?(base) click to toggle source
# File vendor/qwik/lib/qwik/db-filesystem.rb, line 171
def ignore_on_last_article_time?(base)
  IGNORE_ON_LAST_ARTICLE_TIME.detect { |entry| entry === base }
end
path(k) click to toggle source
# File vendor/qwik/lib/qwik/db-filesystem.rb, line 175
def path(k)
  return gen_path(@path, @path_h, k)
end
spath(k) click to toggle source
# File vendor/qwik/lib/qwik/db-filesystem.rb, line 179
def spath(k)
  return gen_path(@spath, @spath_h, k)
end