Class: Relaton::Db

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(global_cache, local_cache) ⇒ Db

Returns a new instance of Db.

Parameters:

  • global_cache (String)

    directory of global DB

  • local_cache (String)

    directory of local DB



5
6
7
8
9
10
11
12
13
# File 'lib/relaton/db.rb', line 5

def initialize(global_cache, local_cache)
  @registry = Relaton::Registry.instance
  gpath = global_cache && File.expand_path(global_cache)
  @db = open_cache_biblio(gpath)
  lpath = local_cache && File.expand_path(local_cache)
  @local_db = open_cache_biblio(lpath)
  @queues = {}
  @semaphore = Mutex.new
end

Class Method Details

.flush_caches(gcache, lcache) ⇒ Object



533
534
535
536
# File 'lib/relaton/db.rb', line 533

def flush_caches(gcache, lcache)
  FileUtils.rm_rf gcache unless gcache.nil?
  FileUtils.rm_rf lcache unless lcache.nil?
end

.global_bibliocache_nameObject



538
539
540
# File 'lib/relaton/db.rb', line 538

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

Parameters:

  • local_cache (String, nil)

    local cache name; “relaton” created if empty or nil

  • global_cache (Boolean, nil)

    create global_cache if true

  • flush_caches (Boolean, nil)

    flush caches if true

Returns:



526
527
528
529
530
531
# File 'lib/relaton/db.rb', line 526

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]
  Relaton::Db.new(globalname, localname)
end

.local_bibliocache_name(cachename) ⇒ Object



542
543
544
545
546
547
# File 'lib/relaton/db.rb', line 542

def local_bibliocache_name(cachename)
  return nil if cachename.nil?

  cachename = "relaton" if cachename.empty?
  "#{cachename}/cache"
end

Instance Method Details

#clearObject

Clear global and local databases



27
28
29
30
# File 'lib/relaton/db.rb', line 27

def clear
  @db&.clear
  @local_db&.clear
end

#docid_type(code) ⇒ Array

The document identifier class corresponding to the given code

Parameters:

  • code (String)

Returns:

  • (Array)


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

def docid_type(code)
  stdclass = @registry.class_by_ref(code) or return [nil, code]
  _, 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,

Parameters:

  • code (String)

    the ISO standard Code to look up (e.g. “ISO 9000”)

  • year (String) (defaults to: nil)

    the year the standard was published (optional)

  • opts (Hash) (defaults to: {})

    options

Options Hash (opts):

  • :all_parts (Boolean)

    If all-parts reference is required

  • :keep_year (Boolean)

    If undated reference should return actual reference with year

  • :retries (Integer) — default: 1

    Number of network retries

Returns:

  • (nil, RelatonBib::BibliographicItem, RelatonIsoBib::IsoBibliographicItem, RelatonItu::ItuBibliographicItem, RelatonIetf::IetfBibliographicItem, RelatonIec::IecBibliographicItem, RelatonIeee::IeeeBibliographicItem, RelatonNist::NistBibliongraphicItem, RelatonGb::GbbibliographicItem, RelatonOgc::OgcBibliographicItem, RelatonCalconnect::CcBibliographicItem, RelatinUn::UnBibliographicItem, RelatonBipm::BipmBibliographicItem, RelatonIho::IhoBibliographicItem, RelatonOmg::OmgBibliographicItem, RelatonW3c::W3cBibliographicItem)


55
56
57
58
59
60
61
62
63
64
65
# File 'lib/relaton/db.rb', line 55

def fetch(code, year = nil, opts = {})
  stdclass = @registry.class_by_ref(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)
end

#fetch_all(text = nil, edition: nil, year: nil) ⇒ Array

fetch all standards from DB

Parameters:

  • test (String, nil)
  • edition (String) (defaults to: nil)

    , nil

  • year (Integer, nil) (defaults to: nil)

Returns:

  • (Array)


78
79
80
81
82
83
84
85
86
87
# File 'lib/relaton/db.rb', line 78

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

Parameters:

  • ref (String)

    reference

  • year (String) (defaults to: nil)

    document yer

  • opts (Hash) (defaults to: {})

    options

Returns:

  • (RelatonBib::BibliographicItem, RelatonBib::RequestError, nil)

    bibitem if document is found, request error if server doesn’t answer, nil if document not found



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/relaton/db.rb', line 99

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.processors[stdclass]
      threads = ENV["RELATON_FETCH_PARALLEL"]&.to_i || processor.threads
      wp = WorkersPool.new(threads) do |args|
        args[3].call fetch(*args[0..2])
      rescue RelatonBib::RequestError => e
        args[3].call e
      rescue StandardError => e
        Util.log "[relaton] ERROR: #{args[0]} -- #{e.message}", :error
        args[3].call nil
      end
      @queues[stdclass] = { queue: Queue.new, 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

See Also:



68
69
70
71
# File 'lib/relaton/db.rb', line 68

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, ...

Parameters:

  • code (String)
  • year (String, NilClass) (defaults to: nil)
  • stdclass (Symbol, NilClass) (defaults to: nil)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :all_parts (Boolean)

    If all-parts reference is required

  • :keep_year (Boolean)

    If undated reference should return actual reference with year

  • :retries (Integer) — default: 1

    Number of network retries

Returns:

  • (nil, RelatonBib::BibliographicItem, RelatonIsoBib::IsoBibliographicItem, RelatonItu::ItuBibliographicItem, RelatonIetf::IetfBibliographicItem, RelatonIec::IecBibliographicItem, RelatonIeee::IeeeBibliographicItem, RelatonNist::NistBibliongraphicItem, RelatonGb::GbbibliographicItem, RelatonOgc::OgcBibliographicItem, RelatonCalconnect::CcBibliographicItem, RelatinUn::UnBibliographicItem, RelatonBipm::BipmBibliographicItem, RelatonIho::IhoBibliographicItem, RelatonOmg::OmgBibliographicItem, RelatonW3c::W3cBibliographicItem)


139
140
141
142
143
144
145
146
147
# File 'lib/relaton/db.rb', line 139

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

Parameters:

  • key (String)

Returns:

  • (Hash)


160
161
162
# File 'lib/relaton/db.rb', line 160

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

Parameters:

  • new_dir (String, nil)
  • type: (Symbol) (defaults to: :global)

Returns:

  • (String, nil)


19
20
21
22
23
24
# File 'lib/relaton/db.rb', line 19

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

Parameters:

  • key (String)
  • value (String)

    Bibitem xml serialisation.

Options Hash (value):

  • Bibitem (String)

    xml serialisation.



167
168
169
170
# File 'lib/relaton/db.rb', line 167

def save_entry(key, value)
  @db.nil? || (@db[key] = value)
  @local_db.nil? || (@local_db[key] = value)
end

#to_xmlString

list all entries as a serialization

Returns:

  • (String)


174
175
176
177
178
179
# File 'lib/relaton/db.rb', line 174

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