Class: Relaton::DbCache

Inherits:
Object
  • Object
show all
Defined in:
lib/relaton/db_cache.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir, ext = "xml") ⇒ DbCache

Returns a new instance of DbCache.

Parameters:

  • dir (String)

    DB directory



10
11
12
13
14
# File 'lib/relaton/db_cache.rb', line 10

def initialize(dir, ext = "xml")
  @dir = dir
  @ext = ext
  FileUtils::mkdir_p dir
end

Instance Attribute Details

#dirString (readonly)

Returns:

  • (String)


7
8
9
# File 'lib/relaton/db_cache.rb', line 7

def dir
  @dir
end

Class Method Details

.grammar_hash(fdir) ⇒ String

Parameters:

  • fdir (String)

    dir pathe to flover cache

Returns:

  • (String)


153
154
155
156
# File 'lib/relaton/db_cache.rb', line 153

def self.grammar_hash(fdir)
  type = fdir.split("/").last
  Relaton::Registry.instance.by_type(type)&.grammar_hash
end

Instance Method Details

#[](key) ⇒ String

Read item

Parameters:

  • key (String)

Returns:

  • (String)


64
65
66
67
68
69
70
71
# File 'lib/relaton/db_cache.rb', line 64

def [](key)
  value = get(key)
  if (code = redirect? value)
    self[code]
  else
    value
  end
end

#[]=(key, value) ⇒ Object

Save item

Parameters:

  • key (String)
  • value (String)

    Bibitem xml serialization



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/relaton/db_cache.rb', line 39

def []=(key, value)
  if value.nil?
    delete key
    return
  end

  prefix_dir = "#{@dir}/#{prefix(key)}"
  FileUtils::mkdir_p prefix_dir
  set_version prefix_dir
  file_safe_write "#{filename(key)}.#{ext(value)}", value
end

#all(&block) ⇒ Array<String>

Returns all items

Returns:

  • (Array<String>)


103
104
105
106
107
108
# File 'lib/relaton/db_cache.rb', line 103

def all(&block)
  Dir.glob("#{@dir}/**/*.{xml,yml,yaml}").sort.map do |f|
    content = File.read(f, encoding: "utf-8")
    block ? yield(f, content) : content
  end
end

#check_version?(fdir) ⇒ Boolean

Check if version of the DB match to the gem grammar hash.

Parameters:

  • fdir (String)

    dir pathe to flover cache

Returns:

  • (Boolean)


121
122
123
124
125
126
127
# File 'lib/relaton/db_cache.rb', line 121

def check_version?(fdir)
  version_dir = "#{fdir}/version"
  return false unless File.exist? version_dir

  v = File.read version_dir, encoding: "utf-8"
  v.strip == self.class.grammar_hash(fdir)
end

#clearObject

Clear database



32
33
34
# File 'lib/relaton/db_cache.rb', line 32

def clear
  FileUtils.rm_rf Dir.glob "#{dir}/*"
end

#clone_entry(key, db) ⇒ Object

Save entry from cache of ‘db` to this cache.

Parameters:

  • key (String)

    key of the entry

  • db (Relaton::Db)

    database



79
80
81
82
83
84
# File 'lib/relaton/db_cache.rb', line 79

def clone_entry(key, db)
  self[key] ||= db.get(key)
  if (code = redirect? get(key))
    clone_entry code, db
  end
end

#delete(key) ⇒ Object

Delete item

Parameters:

  • key (String)


112
113
114
115
116
# File 'lib/relaton/db_cache.rb', line 112

def delete(key)
  file = filename key
  f = search_ext file
  File.delete f if f
end

#ext(value) ⇒ String

Parameters:

  • value (String)

Returns:

  • (String)


53
54
55
56
57
58
59
# File 'lib/relaton/db_cache.rb', line 53

def ext(value)
  case value
  when /^not_found/ then "notfound"
  when /^redirection/ then "redirect"
  else @ext
  end
end

#fetched(key) ⇒ String

Return fetched date

Parameters:

  • key (String)

Returns:

  • (String)


89
90
91
92
93
94
95
96
97
98
99
# File 'lib/relaton/db_cache.rb', line 89

def fetched(key)
  value = self[key]
  return unless value

  if value.match?(/^not_found/)
    value.match(/\d{4}-\d{2}-\d{2}/).to_s
  else
    doc = Nokogiri::XML value
    doc.at("/bibitem/fetched|bibdata/fetched")&.text
  end
end

#get(key) ⇒ String, NilClass

Reads file by a key

Parameters:

  • key (String)

Returns:

  • (String, NilClass)


144
145
146
147
148
149
# File 'lib/relaton/db_cache.rb', line 144

def get(key)
  file = filename key
  return unless (f = search_ext(file))

  File.read(f, encoding: "utf-8")
end

#mv(new_dir) ⇒ String?

Move caches to anothe dir

Parameters:

  • new_dir (String, nil)

Returns:

  • (String, nil)


19
20
21
22
23
24
25
26
27
28
29
# File 'lib/relaton/db_cache.rb', line 19

def mv(new_dir)
  return unless new_dir && @ext == "xml"

  if File.exist? new_dir
    Util.info "target directory exists `#{new_dir}`"
    return
  end

  FileUtils.mv dir, new_dir
  @dir = new_dir
end

#valid_entry?(key, year) ⇒ Boolean

if cached reference is undated, expire it after 60 days

Parameters:

  • key (String)
  • year (String)

Returns:

  • (Boolean)


132
133
134
135
136
137
138
# File 'lib/relaton/db_cache.rb', line 132

def valid_entry?(key, year)
  datestr = fetched key
  return false unless datestr

  date = Date.parse datestr
  year || Date.today - date < 60
end