Class: Relaton::Db

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

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



11
12
13
14
15
16
17
# File 'lib/relaton/db.rb', line 11

def initialize(global_cache, local_cache)
  @registry = Relaton::Registry.instance
  @db = open_cache_biblio(global_cache, type: :global)
  @local_db = open_cache_biblio(local_cache, type: :local)
  @static_db = open_cache_biblio File.expand_path("../relaton/static_cache", __dir__)
  @queues = {}
end

Instance Method Details

#clearObject

Clear global and local databases



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

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)


127
128
129
130
131
# File 'lib/relaton/db.rb', line 127

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,

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)

    RelatonBipm::BipmBibliographicItem, RelatonIho::IhoBibliographicItem, RelatonOmg::OmgBibliographicItem RelatinUn::UnBibliographicItem, RelatonW3c::W3cBibliographicItem



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

def fetch(code, year = nil, opts = {})
  stdclass = standard_class(code) || return
  processor = @registry.processors[stdclass]
  ref = processor.respond_to?(:urn_to_code) ? processor.urn_to_code(code)&.first : code
  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

Parameters:

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

    , nil

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

Returns:

  • (Array)


75
76
77
78
79
80
# File 'lib/relaton/db.rb', line 75

def fetch_all(text = nil, edition: nil, year: nil)
  result = @static_db.all { |file, yml| search_yml file, yml, text, edition, year }.compact
  db = @db || @local_db
  result += db.all { |file, xml| search_xml file, xml, text, edition, year }.compact if db
  result
end

#fetch_async(code, year = nil, opts = {}, &_block) ⇒ Object

Fetch asynchronously



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/relaton/db.rb', line 83

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

See Also:



65
66
67
68
# File 'lib/relaton/db.rb', line 65

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

Returns RelatonBipm::BipmBibliographicItem, RelatonIho::IhoBibliographicItem, RelatonOmg::OmgBibliographicItem RelatinUn::UnBibliographicItem, RelatonW3c::W3cBibliographicItem.

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)

    RelatonBipm::BipmBibliographicItem, RelatonIho::IhoBibliographicItem, RelatonOmg::OmgBibliographicItem RelatinUn::UnBibliographicItem, RelatonW3c::W3cBibliographicItem



114
115
116
117
118
119
120
121
122
# File 'lib/relaton/db.rb', line 114

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

Parameters:

  • key (String)

Returns:

  • (Hash)


135
136
137
138
139
140
141
# File 'lib/relaton/db.rb', line 135

def load_entry(key)
  unless @local_db.nil?
    entry = @local_db[key]
    return entry if entry
  end
  @db[key]
end

#mv(new_global_dir, new_local_dir) ⇒ Object

Move global and/or local caches to anothe dirs

Parameters:

  • new_global_dir (String, nil)
  • new_local_dir (String, nil)


22
23
24
25
# File 'lib/relaton/db.rb', line 22

def mv(new_global_dir, new_local_dir)
  @db.mv new_global_dir
  @local_db.mv new_local_dir
end

#save_entry(key, value) ⇒ Object

Parameters:

  • key (String)
  • value (String)

    Bibitem xml serialisation.

Options Hash (value):

  • Bibitem (String)

    xml serialisation.



146
147
148
149
# File 'lib/relaton/db.rb', line 146

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)


153
154
155
156
157
158
159
160
# File 'lib/relaton/db.rb', line 153

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