Class: Relaton::Db
- Inherits:
-
Object
- Object
- Relaton::Db
- Extended by:
- Config
- Defined in:
- lib/relaton/db.rb,
lib/relaton/db/util.rb,
lib/relaton/db/cache.rb,
lib/relaton/db/config.rb,
lib/relaton/db/version.rb,
lib/relaton/db/registry.rb,
lib/relaton/db/workers_pool.rb
Defined Under Namespace
Modules: Config, Util Classes: Cache, Configuration, Registry, WorkersPool
Constant Summary collapse
- VERSION =
"2.2.0.pre.alpha.1".freeze
Class Method Summary collapse
- .flush_caches(gcache, lcache) ⇒ Object
- .global_bibliocache_name ⇒ Object
-
.init_bib_caches(**opts) ⇒ Relaton::Db
Initialse and return relaton instance, with local and global cache names.
- .local_bibliocache_name(cachename) ⇒ Object
Instance Method Summary collapse
-
#clear ⇒ Object
Clear global and local databases.
-
#docid_type(code) ⇒ Array
The document identifier class corresponding to the given code.
-
#fetch(text, year = nil, opts = {}) ⇒ nil, ...
The class of reference requested is determined by the prefix of the reference: GB Standard for gbbib, IETF for ietfbib, ISO for isobib, IEC or IEV for iecbib,.
-
#fetch_all(text = nil, edition: nil, year: nil) ⇒ Array
fetch all standards from DB.
-
#fetch_async(ref, year = nil, opts = {}, &block) ⇒ RelatonBib::BibliographicItem, ...
Fetch asynchronously.
- #fetch_db(code, year = nil, opts = {}) ⇒ Object
- #fetch_std(code, year = nil, stdclass = nil, opts = {}) ⇒ Object
-
#initialize(global_cache, local_cache) ⇒ Db
constructor
A new instance of Db.
- #load_entry(key) ⇒ Hash
-
#mv(new_dir, type: :global) ⇒ String?
Move global or local caches to anothe dirs.
- #save_entry(key, value) ⇒ Object
-
#to_xml ⇒ String
list all entries as a serialization.
Methods included from Config
Constructor Details
#initialize(global_cache, local_cache) ⇒ Db
Returns a new instance of Db.
12 13 14 15 16 17 18 19 20 |
# File 'lib/relaton/db.rb', line 12 def initialize(global_cache, local_cache) @registry = Registry.instance gpath = global_cache && File.(global_cache) @db = open_cache_biblio(gpath) lpath = local_cache && File.(local_cache) @local_db = open_cache_biblio(lpath) @queues = {} @semaphore = Mutex.new end |
Class Method Details
.flush_caches(gcache, lcache) ⇒ Object
446 447 448 449 |
# File 'lib/relaton/db.rb', line 446 def flush_caches(gcache, lcache) FileUtils.rm_rf gcache unless gcache.nil? FileUtils.rm_rf lcache unless lcache.nil? end |
.global_bibliocache_name ⇒ Object
451 452 453 |
# File 'lib/relaton/db.rb', line 451 def global_bibliocache_name "#{Dir.home}/.relaton/cache" end |
.init_bib_caches(**opts) ⇒ Relaton::Db
Initialse and return relaton instance, with local and global cache names
439 440 441 442 443 444 |
# File 'lib/relaton/db.rb', line 439 def init_bib_caches(**opts) # rubocop:disable Metrics/CyclomaticComplexity globalname = global_bibliocache_name if opts[:global_cache] localname = local_bibliocache_name(opts[:local_cache]) flush_caches globalname, localname if opts[:flush_caches] new(globalname, localname) end |
.local_bibliocache_name(cachename) ⇒ Object
455 456 457 458 459 460 |
# File 'lib/relaton/db.rb', line 455 def local_bibliocache_name(cachename) return nil if cachename.nil? cachename = "relaton" if cachename.empty? "#{cachename}/cache" end |
Instance Method Details
#clear ⇒ Object
Clear global and local databases
34 35 36 37 38 39 40 |
# File 'lib/relaton/db.rb', line 34 def clear @db&.clear @local_db&.clear @registry.processors.each_value do |p| p.remove_index_file if p.respond_to? :remove_index_file end end |
#docid_type(code) ⇒ Array
The document identifier class corresponding to the given code
148 149 150 151 152 |
# File 'lib/relaton/db.rb', line 148 def docid_type(code) stdclass = @registry.class_by_ref(code) or return [nil, code] _, code = strip_id_wrapper(code, stdclass) [@registry[stdclass].idtype, code] end |
#fetch(text, year = nil, opts = {}) ⇒ nil, ...
The class of reference requested is determined by the prefix of the reference: GB Standard for gbbib, IETF for ietfbib, ISO for isobib, IEC or IEV for
iecbib,
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/relaton/db.rb', line 65 def fetch(text, year = nil, opts = {}) reference = text.strip stdclass = @registry.class_by_ref(reference) || return processor = @registry[stdclass] ref = if processor.respond_to?(:urn_to_code) processor.urn_to_code(reference)&.first else reference end ref ||= reference result = combine_doc ref, year, opts, stdclass result || check_bibliocache(ref, year, opts, stdclass) end |
#fetch_all(text = nil, edition: nil, year: nil) ⇒ Array
fetch all standards from DB
89 90 91 92 93 94 95 96 97 98 |
# File 'lib/relaton/db.rb', line 89 def fetch_all(text = nil, edition: nil, year: nil) result = [] db = @db || @local_db if db result += db.all do |file, xml| search_xml file, xml, text, edition, year end.compact end result end |
#fetch_async(ref, year = nil, opts = {}, &block) ⇒ RelatonBib::BibliographicItem, ...
Fetch asynchronously
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/relaton/db.rb', line 112 def fetch_async(ref, year = nil, opts = {}, &block) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength stdclass = @registry.class_by_ref ref if stdclass unless @queues[stdclass] processor = @registry[stdclass] threads = ENV["RELATON_FETCH_PARALLEL"]&.to_i || processor.threads wp = WorkersPool.new(threads) do |args| args[3].call fetch(*args[0..2]) rescue Relaton::RequestError => e args[3].call e rescue StandardError => e Util.error "`#{args[0]}` -- #{e.}" args[3].call nil end @queues[stdclass] = { queue: SizedQueue.new(threads * 2), workers_pool: wp } Thread.new { process_queue @queues[stdclass] } end @queues[stdclass][:queue] << [ref, year, opts, block] else yield nil end end |
#fetch_db(code, year = nil, opts = {}) ⇒ Object
79 80 81 82 |
# File 'lib/relaton/db.rb', line 79 def fetch_db(code, year = nil, opts = {}) opts[:fetch_db] = true fetch code, year, opts end |
#fetch_std(code, year = nil, stdclass = nil, opts = {}) ⇒ Object
135 136 137 138 139 140 141 142 143 |
# File 'lib/relaton/db.rb', line 135 def fetch_std(code, year = nil, stdclass = nil, opts = {}) std = nil @registry.processors.each do |name, processor| std = name if processor.prefix == stdclass end std = @registry.class_by_ref(code) or return nil unless std check_bibliocache(code, year, opts, std) end |
#load_entry(key) ⇒ Hash
156 157 158 |
# File 'lib/relaton/db.rb', line 156 def load_entry(key) (@local_db && @local_db[key]) || @db[key] end |
#mv(new_dir, type: :global) ⇒ String?
Move global or local caches to anothe dirs
26 27 28 29 30 31 |
# File 'lib/relaton/db.rb', line 26 def mv(new_dir, type: :global) case type when :global then @db&.mv new_dir when :local then @local_db&.mv new_dir end end |
#save_entry(key, value) ⇒ Object
163 164 165 166 |
# File 'lib/relaton/db.rb', line 163 def save_entry(key, value) @db.nil? || (@db[key] = value) @local_db.nil? || (@local_db[key] = value) end |
#to_xml ⇒ String
list all entries as a serialization
170 171 172 173 174 175 |
# File 'lib/relaton/db.rb', line 170 def to_xml db = @local_db || @db || return Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml| xml.documents { xml.parent.add_child db.all.join(" ") } end.to_xml end |