relaton 1.8.pre3 → 1.8.pre4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/relaton/config.rb +1 -2
- data/lib/relaton/db.rb +4 -13
- data/lib/relaton/db_cache.rb +62 -19
- data/lib/relaton/version.rb +1 -1
- data/relaton.gemspec +0 -1
- data/spec/relaton/db_spec.rb +16 -16
- metadata +2 -18
- data/lib/relaton/storage.rb +0 -186
- data/spec/relaton/storage_spec.rb +0 -130
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e0872533d5f93e35d777c6a84a4f38a5698b64cc00d9d103b77f7e10fc34738b
|
|
4
|
+
data.tar.gz: 3f1cc128d958996cd8ea6eb31bacc530bba464a8d8b5cb855cd4e204214ef7d1
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1862b4eb76509da3ae2ac959d1ffd7bf6e785bc45033c5123236f5c3f9dc5cafc42940a7cba78222960a59ad542fb0786f2a0be8e5da926bbb4b3aa886278a5e
|
|
7
|
+
data.tar.gz: b21c4797a9003696de1e5d79f017d19c417a9db72e549eeaab7edaa935e26ce9f1b009fdea83bf8896100ed30c63d5b7df730fbeaacedfc91243468cbc54a39b
|
data/lib/relaton/config.rb
CHANGED
|
@@ -12,14 +12,13 @@ module Relaton
|
|
|
12
12
|
end
|
|
13
13
|
|
|
14
14
|
class Configuration
|
|
15
|
-
attr_accessor :logs, :use_api, :api_host
|
|
15
|
+
attr_accessor :logs, :use_api, :api_host
|
|
16
16
|
|
|
17
17
|
def initialize
|
|
18
18
|
@logs = %i(info error) # allowed values: :info, :warning, :error, :debug
|
|
19
19
|
|
|
20
20
|
# @TODO change to true when we start using api.relaton.org
|
|
21
21
|
@use_api = true
|
|
22
|
-
@api_mode = false
|
|
23
22
|
@api_host = "https://api.relaton.org/api/v1"
|
|
24
23
|
end
|
|
25
24
|
end
|
data/lib/relaton/db.rb
CHANGED
|
@@ -1,18 +1,11 @@
|
|
|
1
1
|
module Relaton
|
|
2
|
-
# class RelatonError < StandardError; end
|
|
3
|
-
|
|
4
2
|
class Db
|
|
5
3
|
# @param global_cache [String] directory of global DB
|
|
6
4
|
# @param local_cache [String] directory of local DB
|
|
7
|
-
def initialize(global_cache, local_cache)
|
|
5
|
+
def initialize(global_cache, local_cache)
|
|
8
6
|
@registry = Relaton::Registry.instance
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
lpath = local_cache
|
|
12
|
-
else
|
|
13
|
-
gpath = global_cache && File.expand_path(global_cache)
|
|
14
|
-
lpath = local_cache && File.expand_path(local_cache)
|
|
15
|
-
end
|
|
7
|
+
gpath = global_cache && File.expand_path(global_cache)
|
|
8
|
+
lpath = local_cache && File.expand_path(local_cache)
|
|
16
9
|
@db = open_cache_biblio(gpath, type: :global)
|
|
17
10
|
@local_db = open_cache_biblio(lpath, type: :local)
|
|
18
11
|
@static_db = open_cache_biblio File.expand_path "../relaton/static_cache", __dir__
|
|
@@ -525,12 +518,10 @@ module Relaton
|
|
|
525
518
|
end
|
|
526
519
|
|
|
527
520
|
def global_bibliocache_name
|
|
528
|
-
|
|
521
|
+
"#{Dir.home}/.relaton/cache"
|
|
529
522
|
end
|
|
530
523
|
|
|
531
524
|
def local_bibliocache_name(cachename)
|
|
532
|
-
return nil if Relaton.configuration.api_mode || cachename.nil?
|
|
533
|
-
|
|
534
525
|
cachename = "relaton" if cachename.empty?
|
|
535
526
|
"#{cachename}/cache"
|
|
536
527
|
end
|
data/lib/relaton/db_cache.rb
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
require "fileutils"
|
|
2
2
|
require "timeout"
|
|
3
|
-
require "relaton/storage"
|
|
4
3
|
|
|
5
4
|
module Relaton
|
|
6
5
|
class DbCache
|
|
@@ -11,15 +10,14 @@ module Relaton
|
|
|
11
10
|
def initialize(dir, ext = "xml")
|
|
12
11
|
@dir = dir
|
|
13
12
|
@ext = ext
|
|
14
|
-
|
|
15
|
-
FileUtils::mkdir_p @dir unless Relaton.configuration.api_mode
|
|
13
|
+
FileUtils::mkdir_p dir
|
|
16
14
|
end
|
|
17
15
|
|
|
18
16
|
# Move caches to anothe dir
|
|
19
17
|
# @param new_dir [String, nil]
|
|
20
18
|
# @return [String, nil]
|
|
21
19
|
def mv(new_dir)
|
|
22
|
-
return unless new_dir && @ext == "xml"
|
|
20
|
+
return unless new_dir && @ext == "xml"
|
|
23
21
|
|
|
24
22
|
if File.exist? new_dir
|
|
25
23
|
warn "[relaton] WARNING: target directory exists \"#{new_dir}\""
|
|
@@ -32,9 +30,7 @@ module Relaton
|
|
|
32
30
|
|
|
33
31
|
# Clear database
|
|
34
32
|
def clear
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
FileUtils.rm_rf Dir.glob "#{dir}/*" if @ext == "xml" # if it's static DB
|
|
33
|
+
FileUtils.rm_rf Dir.glob "#{dir}/*" if @ext == "xml" # if it isn't a static DB
|
|
38
34
|
end
|
|
39
35
|
|
|
40
36
|
# Save item
|
|
@@ -47,8 +43,9 @@ module Relaton
|
|
|
47
43
|
end
|
|
48
44
|
/^(?<pref>[^(]+)(?=\()/ =~ key.downcase
|
|
49
45
|
prefix_dir = "#{@dir}/#{pref}"
|
|
50
|
-
|
|
51
|
-
|
|
46
|
+
FileUtils::mkdir_p prefix_dir unless Dir.exist? prefix_dir
|
|
47
|
+
set_version prefix_dir
|
|
48
|
+
file_safe_write "#{filename(key)}.#{ext(value)}", value
|
|
52
49
|
end
|
|
53
50
|
|
|
54
51
|
# Read item
|
|
@@ -88,20 +85,29 @@ module Relaton
|
|
|
88
85
|
# Returns all items
|
|
89
86
|
# @return [Array<String>]
|
|
90
87
|
def all(&block)
|
|
91
|
-
|
|
88
|
+
Dir.glob("#{@dir}/**/*.{xml,yml,yaml}").sort.map do |f|
|
|
89
|
+
content = File.read(f, encoding: "utf-8")
|
|
90
|
+
block ? yield(f, content) : content
|
|
91
|
+
end
|
|
92
92
|
end
|
|
93
93
|
|
|
94
94
|
# Delete item
|
|
95
95
|
# @param key [String]
|
|
96
96
|
def delete(key)
|
|
97
|
-
|
|
97
|
+
file = filename key
|
|
98
|
+
f = search_ext file
|
|
99
|
+
File.delete f if f
|
|
98
100
|
end
|
|
99
101
|
|
|
100
102
|
# Check if version of the DB match to the gem grammar hash.
|
|
101
103
|
# @param fdir [String] dir pathe to flover cache
|
|
102
104
|
# @return [Boolean]
|
|
103
105
|
def check_version?(fdir)
|
|
104
|
-
|
|
106
|
+
version_file = "#{fdir}/version"
|
|
107
|
+
return false unless File.exist? version_file
|
|
108
|
+
|
|
109
|
+
v = File.read version_file, encoding: "utf-8"
|
|
110
|
+
v.strip == self.class.grammar_hash(fdir)
|
|
105
111
|
end
|
|
106
112
|
|
|
107
113
|
# if cached reference is undated, expire it after 60 days
|
|
@@ -120,7 +126,17 @@ module Relaton
|
|
|
120
126
|
# @param key [String]
|
|
121
127
|
# @return [String, NilClass]
|
|
122
128
|
def get(key)
|
|
123
|
-
|
|
129
|
+
file = filename key
|
|
130
|
+
return unless (f = search_ext(file))
|
|
131
|
+
|
|
132
|
+
File.read(f, encoding: "utf-8")
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# @param fdir [String] dir pathe to flover cache
|
|
136
|
+
# @return [String]
|
|
137
|
+
def self.grammar_hash(fdir)
|
|
138
|
+
type = fdir.split("/").last
|
|
139
|
+
Relaton::Registry.instance.by_type(type)&.grammar_hash
|
|
124
140
|
end
|
|
125
141
|
|
|
126
142
|
private
|
|
@@ -135,6 +151,31 @@ module Relaton
|
|
|
135
151
|
end
|
|
136
152
|
end
|
|
137
153
|
|
|
154
|
+
#
|
|
155
|
+
# Checks if there is file with xml or txt extension and return filename with
|
|
156
|
+
# the extension.
|
|
157
|
+
#
|
|
158
|
+
# @param file [String]
|
|
159
|
+
# @return [String, NilClass]
|
|
160
|
+
def search_ext(file)
|
|
161
|
+
if File.exist?("#{file}.#{@ext}")
|
|
162
|
+
"#{file}.#{@ext}"
|
|
163
|
+
elsif File.exist? "#{file}.notfound"
|
|
164
|
+
"#{file}.notfound"
|
|
165
|
+
elsif File.exist? "#{file}.redirect"
|
|
166
|
+
"#{file}.redirect"
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
# Set version of the DB to the gem grammar hash.
|
|
171
|
+
# @param fdir [String] dir pathe to flover cache
|
|
172
|
+
def set_version(fdir)
|
|
173
|
+
file_version = "#{fdir}/version"
|
|
174
|
+
unless File.exist? file_version
|
|
175
|
+
file_safe_write file_version, self.class.grammar_hash(fdir)
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
|
|
138
179
|
# Return item's file name
|
|
139
180
|
# @param key [String]
|
|
140
181
|
# @return [String]
|
|
@@ -158,11 +199,13 @@ module Relaton
|
|
|
158
199
|
code
|
|
159
200
|
end
|
|
160
201
|
|
|
161
|
-
#
|
|
162
|
-
# @
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
202
|
+
# @param file [String]
|
|
203
|
+
# @content [String]
|
|
204
|
+
def file_safe_write(file, content)
|
|
205
|
+
File.open file, File::RDWR | File::CREAT, encoding: "UTF-8" do |f|
|
|
206
|
+
Timeout.timeout(10) { f.flock File::LOCK_EX }
|
|
207
|
+
f.write content
|
|
208
|
+
end
|
|
209
|
+
end
|
|
167
210
|
end
|
|
168
211
|
end
|
data/lib/relaton/version.rb
CHANGED
data/relaton.gemspec
CHANGED
|
@@ -30,7 +30,6 @@ Gem::Specification.new do |spec|
|
|
|
30
30
|
spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
|
|
31
31
|
|
|
32
32
|
# spec.add_dependency "algoliasearch"
|
|
33
|
-
spec.add_dependency "aws-sdk-s3", "~> 1.96.0"
|
|
34
33
|
spec.add_dependency "relaton-bipm", "~> 1.8.0"
|
|
35
34
|
spec.add_dependency "relaton-bsi", "~> 1.8.0"
|
|
36
35
|
spec.add_dependency "relaton-calconnect", "~> 1.8.0"
|
data/spec/relaton/db_spec.rb
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
RSpec.describe Relaton::Db do
|
|
2
2
|
before(:each) { FileUtils.rm_rf %w[testcache testcache2] }
|
|
3
3
|
|
|
4
|
-
context "initialization in API mode" do
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
end
|
|
4
|
+
# context "initialization in API mode" do
|
|
5
|
+
# before(:each) { Relaton.configure { |c| c.api_mode = true } }
|
|
6
|
+
# after(:each) { Relaton.configure { |c| c.api_mode = false } }
|
|
7
|
+
|
|
8
|
+
# it do
|
|
9
|
+
# db = Relaton::Db.new "cache", nil
|
|
10
|
+
# cache = db.instance_variable_get :@db
|
|
11
|
+
# expect(cache.dir).to eq "cache"
|
|
12
|
+
# end
|
|
13
|
+
|
|
14
|
+
# it "with default cache directory" do
|
|
15
|
+
# db = Relaton::Db.init_bib_caches global_cache: true
|
|
16
|
+
# cache = db.instance_variable_get :@db
|
|
17
|
+
# expect(cache.dir).to eq "cache"
|
|
18
|
+
# end
|
|
19
|
+
# end
|
|
20
20
|
|
|
21
21
|
context "modifing database" do
|
|
22
22
|
let(:db) { Relaton::Db.new "testcache", "testcache2" }
|
metadata
CHANGED
|
@@ -1,29 +1,15 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: relaton
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.8.
|
|
4
|
+
version: 1.8.pre4
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ribose Inc.
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2021-07-
|
|
11
|
+
date: 2021-07-16 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
|
-
- !ruby/object:Gem::Dependency
|
|
14
|
-
name: aws-sdk-s3
|
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
|
16
|
-
requirements:
|
|
17
|
-
- - "~>"
|
|
18
|
-
- !ruby/object:Gem::Version
|
|
19
|
-
version: 1.96.0
|
|
20
|
-
type: :runtime
|
|
21
|
-
prerelease: false
|
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
-
requirements:
|
|
24
|
-
- - "~>"
|
|
25
|
-
- !ruby/object:Gem::Version
|
|
26
|
-
version: 1.96.0
|
|
27
13
|
- !ruby/object:Gem::Dependency
|
|
28
14
|
name: relaton-bipm
|
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -512,7 +498,6 @@ files:
|
|
|
512
498
|
- lib/relaton/static_cache/iso/iso_iec_dir_2_iso.yml
|
|
513
499
|
- lib/relaton/static_cache/iso/iso_iec_dir_iec_sup.yml
|
|
514
500
|
- lib/relaton/static_cache/iso/iso_iec_dir_jtc_1_sup.yml
|
|
515
|
-
- lib/relaton/storage.rb
|
|
516
501
|
- lib/relaton/util.rb
|
|
517
502
|
- lib/relaton/version.rb
|
|
518
503
|
- lib/relaton/workers_pool.rb
|
|
@@ -522,7 +507,6 @@ files:
|
|
|
522
507
|
- spec/relaton/db_spec.rb
|
|
523
508
|
- spec/relaton/processor_spec.rb
|
|
524
509
|
- spec/relaton/regirtry_spec.rb
|
|
525
|
-
- spec/relaton/storage_spec.rb
|
|
526
510
|
- spec/relaton_spec.rb
|
|
527
511
|
- spec/spec_helper.rb
|
|
528
512
|
- spec/support/gb_t_20223_2006.xml
|
data/lib/relaton/storage.rb
DELETED
|
@@ -1,186 +0,0 @@
|
|
|
1
|
-
require "aws-sdk-s3"
|
|
2
|
-
|
|
3
|
-
module Relaton
|
|
4
|
-
#
|
|
5
|
-
# Man
|
|
6
|
-
#
|
|
7
|
-
class Storage
|
|
8
|
-
include Singleton
|
|
9
|
-
|
|
10
|
-
def initialize
|
|
11
|
-
if Relaton.configuration.api_mode
|
|
12
|
-
@s3 = Aws::S3::Client.new
|
|
13
|
-
end
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
#
|
|
17
|
-
# Save file to storage
|
|
18
|
-
#
|
|
19
|
-
# @param dir [String]
|
|
20
|
-
# @param key [String]
|
|
21
|
-
# @param value [String]
|
|
22
|
-
#
|
|
23
|
-
def save(dir, key, value)
|
|
24
|
-
set_version dir
|
|
25
|
-
if Relaton.configuration.api_mode then s3_write key, value
|
|
26
|
-
else file_safe_write key, value
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
#
|
|
31
|
-
# Returns all items
|
|
32
|
-
#
|
|
33
|
-
# @param dir [String]
|
|
34
|
-
#
|
|
35
|
-
# @return [Array<String>]
|
|
36
|
-
#
|
|
37
|
-
def all(dir, &block)
|
|
38
|
-
return all_s3 dir, &block if Relaton.configuration.api_mode
|
|
39
|
-
|
|
40
|
-
Dir.glob("#{dir}/**/*.{xml,yml,yaml}").sort.map do |f|
|
|
41
|
-
content = File.read(f, encoding: "utf-8")
|
|
42
|
-
block ? yield(f, content) : content
|
|
43
|
-
end
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
# Delete item
|
|
47
|
-
# @param key [String] path to file without extension
|
|
48
|
-
def delete(key)
|
|
49
|
-
f = search_ext(key)
|
|
50
|
-
if Relaton.configuration.api_mode && f
|
|
51
|
-
@s3.delete_object bucket: ENV["AWS_BUCKET"], key: f
|
|
52
|
-
elsif f then File.delete f
|
|
53
|
-
end
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
# Check if version of the DB match to the gem grammar hash.
|
|
57
|
-
# @param fdir [String] dir pathe to flavor cache
|
|
58
|
-
# @return [Boolean]
|
|
59
|
-
def check_version?(fdir)
|
|
60
|
-
file_version = "#{fdir}/version"
|
|
61
|
-
if Relaton.configuration.api_mode
|
|
62
|
-
check_version_s3? file_version, fdir
|
|
63
|
-
else
|
|
64
|
-
return false unless File.exist? fdir
|
|
65
|
-
|
|
66
|
-
v = File.read file_version, encoding: "utf-8"
|
|
67
|
-
v.strip == grammar_hash(fdir)
|
|
68
|
-
end
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
# Reads file by a key
|
|
72
|
-
#
|
|
73
|
-
# @param key [String]
|
|
74
|
-
# @return [String, NilClass]
|
|
75
|
-
def get(key, static: false)
|
|
76
|
-
return unless (f = search_ext(key, static: static))
|
|
77
|
-
|
|
78
|
-
if Relaton.configuration.api_mode && !static
|
|
79
|
-
s3_read f
|
|
80
|
-
else
|
|
81
|
-
File.read(f, encoding: "utf-8")
|
|
82
|
-
end
|
|
83
|
-
end
|
|
84
|
-
|
|
85
|
-
private
|
|
86
|
-
|
|
87
|
-
# Check if version of the DB match to the gem grammar hash.
|
|
88
|
-
# @param file_version [String] dir pathe to flover cache
|
|
89
|
-
# @param dir [String] fdir pathe to flavor cache
|
|
90
|
-
# @return [Boolean]
|
|
91
|
-
def check_version_s3?(file_version, fdir)
|
|
92
|
-
s3_read(file_version) == grammar_hash(fdir)
|
|
93
|
-
rescue Aws::S3::Errors::NoSuchKey
|
|
94
|
-
false
|
|
95
|
-
end
|
|
96
|
-
|
|
97
|
-
#
|
|
98
|
-
# Read file form AWS S#
|
|
99
|
-
#
|
|
100
|
-
# @param [String] key file name
|
|
101
|
-
#
|
|
102
|
-
# @return [String] content
|
|
103
|
-
#
|
|
104
|
-
def s3_read(key)
|
|
105
|
-
obj = @s3.get_object bucket: ENV["AWS_BUCKET"], key: key
|
|
106
|
-
obj.body.read
|
|
107
|
-
end
|
|
108
|
-
|
|
109
|
-
#
|
|
110
|
-
# Write file to AWS S3
|
|
111
|
-
#
|
|
112
|
-
# @param [String] key
|
|
113
|
-
# @param [String] value
|
|
114
|
-
#
|
|
115
|
-
def s3_write(key, value)
|
|
116
|
-
@s3.put_object(bucket: ENV["AWS_BUCKET"], key: key, body: value,
|
|
117
|
-
content_type: "text/plain; charset=utf-8")
|
|
118
|
-
end
|
|
119
|
-
|
|
120
|
-
#
|
|
121
|
-
# Returns all items
|
|
122
|
-
#
|
|
123
|
-
# @param dir [String]
|
|
124
|
-
#
|
|
125
|
-
# @return [Array<String>]
|
|
126
|
-
#
|
|
127
|
-
def all_s3(dir)
|
|
128
|
-
list = @s3.list_objects_v2 bucket: ENV["AWS_BUCKET"], prefix: dir
|
|
129
|
-
list.contents.select { |i| i.key.match?(/\.xml$/) }.sort_by(&:key).map do |item|
|
|
130
|
-
content = s3_read item.key
|
|
131
|
-
block_given? ? yield(item.key, content) : content
|
|
132
|
-
end
|
|
133
|
-
end
|
|
134
|
-
|
|
135
|
-
#
|
|
136
|
-
# Checks if there is file with xml or txt extension and return filename with
|
|
137
|
-
# the extension.
|
|
138
|
-
#
|
|
139
|
-
# @param file [String]
|
|
140
|
-
# @return [String, NilClass]
|
|
141
|
-
def search_ext(file, static: false)
|
|
142
|
-
if Relaton.configuration.api_mode && !static
|
|
143
|
-
fs = @s3.list_objects_v2 bucket: ENV["AWS_BUCKET"], prefix: "#{file}."
|
|
144
|
-
fs.contents.first&.key
|
|
145
|
-
else
|
|
146
|
-
Dir["#{file}.*"].first
|
|
147
|
-
end
|
|
148
|
-
end
|
|
149
|
-
|
|
150
|
-
# Set version of the DB to the gem grammar hash.
|
|
151
|
-
# @param fdir [String] dir pathe to flover cache
|
|
152
|
-
def set_version(fdir)
|
|
153
|
-
file_version = "#{fdir}/version"
|
|
154
|
-
return set_version_s3 file_version, fdir if Relaton.configuration.api_mode
|
|
155
|
-
|
|
156
|
-
FileUtils::mkdir_p fdir unless Dir.exist?(fdir)
|
|
157
|
-
unless File.exist? file_version
|
|
158
|
-
file_safe_write file_version, grammar_hash(fdir)
|
|
159
|
-
end
|
|
160
|
-
end
|
|
161
|
-
|
|
162
|
-
# Set version of the DB to the gem grammar hash.
|
|
163
|
-
# @param fdir [String] dir pathe to flover cache
|
|
164
|
-
def set_version_s3(fver, dir)
|
|
165
|
-
@s3.head_object bucket: ENV["AWS_BUCKET"], key: fver
|
|
166
|
-
rescue Aws::S3::Errors::NotFound
|
|
167
|
-
s3_write fver, grammar_hash(dir)
|
|
168
|
-
end
|
|
169
|
-
|
|
170
|
-
# @param file [String]
|
|
171
|
-
# @content [String]
|
|
172
|
-
def file_safe_write(file, content)
|
|
173
|
-
File.open file, File::RDWR | File::CREAT, encoding: "UTF-8" do |f|
|
|
174
|
-
Timeout.timeout(10) { f.flock File::LOCK_EX }
|
|
175
|
-
f.write content
|
|
176
|
-
end
|
|
177
|
-
end
|
|
178
|
-
|
|
179
|
-
# @param fdir [String] dir pathe to flover cache
|
|
180
|
-
# @return [String]
|
|
181
|
-
def grammar_hash(fdir)
|
|
182
|
-
type = fdir.split("/").last
|
|
183
|
-
Relaton::Registry.instance.by_type(type)&.grammar_hash
|
|
184
|
-
end
|
|
185
|
-
end
|
|
186
|
-
end
|
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
RSpec.describe Relaton::Storage do
|
|
2
|
-
context "API mode" do
|
|
3
|
-
before(:each) do
|
|
4
|
-
Relaton.configure do |config|
|
|
5
|
-
config.api_mode = true
|
|
6
|
-
end
|
|
7
|
-
Singleton.__init__ Relaton::Storage
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
after(:each) do
|
|
11
|
-
Relaton.configure do |config|
|
|
12
|
-
config.api_mode = false
|
|
13
|
-
end
|
|
14
|
-
Singleton.__init__ Relaton::Storage
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
let(:storage) { Relaton::Storage.instance }
|
|
18
|
-
let(:bucket) { "test-bucket" }
|
|
19
|
-
|
|
20
|
-
it "save document to S3" do
|
|
21
|
-
dir = "cahche/iso"
|
|
22
|
-
# file = "doc.xml"
|
|
23
|
-
body = "<doc>Test doc</doc>"
|
|
24
|
-
key = File.join dir, "doc.xml"
|
|
25
|
-
client = double "AwsClient"
|
|
26
|
-
expect(ENV).to receive(:[]).with("AWS_BUCKET").and_return(bucket).twice
|
|
27
|
-
expect(client).to receive(:head_object).with(bucket: bucket, key: "cahche/iso/version")
|
|
28
|
-
expect(client).to receive(:put_object).with(
|
|
29
|
-
bucket: bucket, key: key, body: body,
|
|
30
|
-
content_type: "text/plain; charset=utf-8"
|
|
31
|
-
)
|
|
32
|
-
expect(Aws::S3::Client).to receive(:new).and_return client
|
|
33
|
-
storage.save dir, key, body
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
it "read document from S3" do
|
|
37
|
-
dir = "cahche/iso"
|
|
38
|
-
# file = "doc.xml"
|
|
39
|
-
body = "<doc>Test doc</doc>"
|
|
40
|
-
key = File.join dir, "doc"
|
|
41
|
-
client = double "AwsClient"
|
|
42
|
-
item = double "Item", key: "#{key}.xml"
|
|
43
|
-
list = double "List", contents: [item]
|
|
44
|
-
obj_body = double "Body", read: body
|
|
45
|
-
obj = double "Object", body: obj_body
|
|
46
|
-
expect(ENV).to receive(:[]).with("AWS_BUCKET").and_return(bucket).twice
|
|
47
|
-
expect(client).to receive(:list_objects_v2).with(bucket: bucket, prefix: "#{key}.").and_return list
|
|
48
|
-
expect(client).to receive(:get_object).with(bucket: bucket, key: "#{key}.xml").and_return obj
|
|
49
|
-
expect(Aws::S3::Client).to receive(:new).and_return client
|
|
50
|
-
expect(storage.get(key)).to eq body
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
it "delete document from S3" do
|
|
54
|
-
key = "cache/iso/doc"
|
|
55
|
-
client = double "AwsClient"
|
|
56
|
-
item = double "Item", key: "#{key}.xml"
|
|
57
|
-
list = double "List", contents: [item]
|
|
58
|
-
expect(ENV).to receive(:[]).with("AWS_BUCKET").and_return(bucket).twice
|
|
59
|
-
expect(client).to receive(:list_objects_v2).with(bucket: bucket, prefix: "#{key}.").and_return list
|
|
60
|
-
expect(client).to receive(:delete_object).with(bucket: bucket, key: "#{key}.xml")
|
|
61
|
-
expect(Aws::S3::Client).to receive(:new).and_return client
|
|
62
|
-
storage.delete key
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
it "return all the documents from S3" do
|
|
66
|
-
dir = "cahche/iso"
|
|
67
|
-
# file = "doc.xml"
|
|
68
|
-
body = "<doc>Test doc</doc>"
|
|
69
|
-
key = File.join dir, "doc.xml"
|
|
70
|
-
client = double "AwsClient"
|
|
71
|
-
expect(ENV).to receive(:[]).with("AWS_BUCKET").and_return(bucket).twice
|
|
72
|
-
item = double "Item", key: key
|
|
73
|
-
list = double "List", contents: [item]
|
|
74
|
-
obj_body = double "Body", read: body
|
|
75
|
-
obj = double "Object", body: obj_body
|
|
76
|
-
expect(client).to receive(:list_objects_v2).with(bucket: bucket, prefix: dir).and_return list
|
|
77
|
-
expect(client).to receive(:get_object).with(bucket: bucket, key: key).and_return obj
|
|
78
|
-
expect(Aws::S3::Client).to receive(:new).and_return client
|
|
79
|
-
contents = storage.all dir
|
|
80
|
-
expect(contents).to eq [body]
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
it "set version" do
|
|
84
|
-
dir = "cahche/iso"
|
|
85
|
-
key = File.join dir, "version"
|
|
86
|
-
hash = Relaton::Registry.instance.by_type("iso").grammar_hash
|
|
87
|
-
client = double "AwsClient"
|
|
88
|
-
expect(ENV).to receive(:[]).with("AWS_BUCKET").and_return(bucket).twice
|
|
89
|
-
# expect(ENV).to receive(:[]).and_call_original
|
|
90
|
-
error = Aws::S3::Errors::NotFound.new Seahorse::Client::RequestContext.new, ""
|
|
91
|
-
expect(client).to receive(:head_object).with(bucket: bucket, key: key)
|
|
92
|
-
.and_raise error
|
|
93
|
-
expect(client).to receive(:put_object).with(
|
|
94
|
-
bucket: bucket, key: key, body: hash,
|
|
95
|
-
content_type: "text/plain; charset=utf-8"
|
|
96
|
-
)
|
|
97
|
-
expect(Aws::S3::Client).to receive(:new).and_return client
|
|
98
|
-
storage.send :set_version, dir
|
|
99
|
-
end
|
|
100
|
-
|
|
101
|
-
context "check version" do
|
|
102
|
-
it "valid" do
|
|
103
|
-
dir = "cahche/iso"
|
|
104
|
-
key = File.join dir, "version"
|
|
105
|
-
hash = Relaton::Registry.instance.by_type("iso").grammar_hash
|
|
106
|
-
client = double "AwsClient"
|
|
107
|
-
obj_body = double "Body", read: hash
|
|
108
|
-
obj = double "Object", body: obj_body
|
|
109
|
-
expect(client).to receive(:get_object).with(bucket: bucket, key: key).and_return obj
|
|
110
|
-
expect(Aws::S3::Client).to receive(:new).and_return client
|
|
111
|
-
expect(ENV).to receive(:[]).with("AWS_BUCKET").and_return(bucket)
|
|
112
|
-
expect(storage.check_version?(dir)).to be true
|
|
113
|
-
end
|
|
114
|
-
|
|
115
|
-
it "version file doesn't exist" do
|
|
116
|
-
dir = "cahche/iso"
|
|
117
|
-
key = File.join dir, "version"
|
|
118
|
-
# hash = "invalid"
|
|
119
|
-
client = double "AwsClient"
|
|
120
|
-
# obj_body = double "Body", read: hash
|
|
121
|
-
# obj = double "Object", body: obj_body
|
|
122
|
-
error = Aws::S3::Errors::NoSuchKey.new Seahorse::Client::RequestContext.new, ""
|
|
123
|
-
expect(client).to receive(:get_object).with(bucket: bucket, key: key).and_raise error
|
|
124
|
-
expect(Aws::S3::Client).to receive(:new).and_return client
|
|
125
|
-
expect(ENV).to receive(:[]).with("AWS_BUCKET").and_return(bucket)
|
|
126
|
-
expect(storage.check_version?(dir)).to be false
|
|
127
|
-
end
|
|
128
|
-
end
|
|
129
|
-
end
|
|
130
|
-
end
|