Class: Relaton::Db
- Inherits:
-
Object
- Object
- Relaton::Db
- Defined in:
- lib/relaton/db.rb
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(code, year = nil, opts = {}) ⇒ nil, ...
The class of reference requested is determined by the prefix of the code: 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(code, year = nil, opts = {}, &_block) ⇒ Object
Fetch asynchronously.
- #fetch_db(code, year = nil, opts = {}) ⇒ Object
- #fetch_std(code, year = nil, stdclass = nil, opts = {}) ⇒ nil, ...
-
#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.
Constructor Details
#initialize(global_cache, local_cache) ⇒ Db
Returns a new instance of Db.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/relaton/db.rb', line 11 def initialize(global_cache, local_cache) @registry = Relaton::Registry.instance gpath = global_cache && File.(global_cache) @db = open_cache_biblio(gpath, type: :global) lpath = local_cache && File.(local_cache) @local_db = open_cache_biblio(lpath, type: :local) @static_db = open_cache_biblio File.("../relaton/static_cache", __dir__) @queues = {} end |
Instance Method Details
#clear ⇒ Object
Clear global and local databases
36 37 38 39 |
# File 'lib/relaton/db.rb', line 36 def clear @db&.clear @local_db&.clear end |
#docid_type(code) ⇒ Array
The document identifier class corresponding to the given code
147 148 149 150 151 |
# File 'lib/relaton/db.rb', line 147 def docid_type(code) stdclass = standard_class(code) or return [nil, code] _prefix, code = strip_id_wrapper(code, stdclass) [@registry.processors[stdclass].idtype, code] end |
#fetch(code, year = nil, opts = {}) ⇒ nil, ...
The class of reference requested is determined by the prefix of the code: GB Standard for gbbib, IETF for ietfbib, ISO for isobib, IEC or IEV for
iecbib,
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/relaton/db.rb', line 64 def fetch(code, year = nil, opts = {}) stdclass = standard_class(code) || return processor = @registry.processors[stdclass] ref = if processor.respond_to?(:urn_to_code) processor.urn_to_code(code)&.first else code end ref ||= code result = combine_doc ref, year, opts, stdclass result ||= check_bibliocache(ref, year, opts, stdclass) result end |
#fetch_all(text = nil, edition: nil, year: nil) ⇒ Array
fetch all standards from DB
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/relaton/db.rb', line 88 def fetch_all(text = nil, edition: nil, year: nil) result = @static_db.all do |file, yml| search_yml file, yml, text, edition, year end.compact 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(code, year = nil, opts = {}, &_block) ⇒ Object
Fetch asynchronously
102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/relaton/db.rb', line 102 def fetch_async(code, year = nil, opts = {}, &_block) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength stdclass = standard_class code if stdclass unless @queues[stdclass] processor = @registry.processors[stdclass] wp = WorkersPool.new(processor.threads) { |args| yield fetch *args } @queues[stdclass] = { queue: Queue.new, workers_pool: wp } Thread.new { process_queue @queues[stdclass] } end @queues[stdclass][:queue] << [code, year, opts] else yield nil end end |
#fetch_db(code, year = nil, opts = {}) ⇒ Object
78 79 80 81 |
# File 'lib/relaton/db.rb', line 78 def fetch_db(code, year = nil, opts = {}) opts[:fetch_db] = true fetch code, year, opts end |
#fetch_std(code, year = nil, stdclass = nil, opts = {}) ⇒ nil, ...
134 135 136 137 138 139 140 141 142 |
# File 'lib/relaton/db.rb', line 134 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 = standard_class(code) or return nil unless std check_bibliocache(code, year, opts, std) end |
#load_entry(key) ⇒ Hash
155 156 157 158 159 160 161 |
# File 'lib/relaton/db.rb', line 155 def load_entry(key) unless @local_db.nil? entry = @local_db[key] return entry if entry end @db[key] end |
#mv(new_dir, type: :global) ⇒ String?
Move global or local caches to anothe dirs
26 27 28 29 30 31 32 33 |
# File 'lib/relaton/db.rb', line 26 def mv(new_dir, type: :global) case type when :global @db&.mv new_dir when :local @local_db&.mv new_dir end end |
#save_entry(key, value) ⇒ Object
166 167 168 169 |
# File 'lib/relaton/db.rb', line 166 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
173 174 175 176 177 178 179 180 |
# File 'lib/relaton/db.rb', line 173 def to_xml db = @local_db || @db || return Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml| xml.documents do xml.parent.add_child db.all.join(" ") end end.to_xml end |