metacrunch-db 1.0.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a2a1b93400bb9101dab9fe5000bcb94a64331aa5
4
+ data.tar.gz: c0f95abc70ce879f8a768bb91574eead74cb2f94
5
+ SHA512:
6
+ metadata.gz: b684069ae65e5adf3d0e4eaf11322184ffb26de4721d973f87e5030d629a0ab5568d6a5d9131ff245c8d8918486a95aeafd918bc3e945edc1da4aa32ba23e517
7
+ data.tar.gz: 303552d24d7f0b132a3e5cdaf4ae53b1aff6d051c0b860c534d1b3be44a3f437dc688faf78e7c8c16d45567fb0a46860ad6e8804314c857f14e74d2c077ed328
data/.gitignore ADDED
@@ -0,0 +1,24 @@
1
+ .DS_Store
2
+ /doc
3
+ *.gem
4
+ *.rbc
5
+ .bundle
6
+ .config
7
+ .yardoc
8
+ Gemfile.lock
9
+ InstalledFiles
10
+ _yardoc
11
+ coverage
12
+ doc/
13
+ lib/bundler/man
14
+ pkg
15
+ rdoc
16
+ spec/reports
17
+ test/tmp
18
+ test/version_tmp
19
+ tmp
20
+ *.bundle
21
+ *.so
22
+ *.o
23
+ *.a
24
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format documentation
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,18 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ group :development do
6
+ gem "bundler", ">= 1.15"
7
+ gem "rake", ">= 12.1"
8
+ gem "rspec", ">= 3.5.0", "< 4.0.0"
9
+ gem "sqlite3", ">= 1.3.13"
10
+
11
+ if !ENV["CI"]
12
+ gem "pry-byebug", ">= 3.5.0"
13
+ end
14
+ end
15
+
16
+ group :test do
17
+ gem "simplecov", ">= 0.15.0"
18
+ end
data/License.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2017 René Sprotte
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "rspec/core/rake_task"
2
+ require "bundler/gem_tasks"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ require "bundler/setup"
3
+ require "metacrunch/db"
4
+
5
+ begin
6
+ require "pry"
7
+ rescue LoadError ; end
8
+
9
+ if defined?(Pry)
10
+ Pry.start
11
+ else
12
+ require "irb"
13
+ IRB.start
14
+ end
@@ -0,0 +1,57 @@
1
+ require "metacrunch/db"
2
+
3
+ module Metacrunch
4
+ class DB::Destination
5
+
6
+ DEFAULT_OPTIONS = {
7
+ use_upsert: false,
8
+ primary_key: :id,
9
+ isolation: :repeatable,
10
+ num_retries: 5
11
+ }
12
+
13
+ def initialize(sequel_dataset, options = {})
14
+ @dataset = sequel_dataset
15
+ @options = DEFAULT_OPTIONS.merge(options)
16
+ end
17
+
18
+ def write(data)
19
+ @dataset.db.transaction(
20
+ isolation: @options[:isolation],
21
+ num_retries: @options[:num_retries]
22
+ ) do
23
+ if data.is_a?(Array)
24
+ data.each{|d| insert_or_upsert(d) }
25
+ else
26
+ insert_or_upsert(data)
27
+ end
28
+ end
29
+ end
30
+
31
+ def close
32
+ @dataset.db.disconnect
33
+ end
34
+
35
+ private
36
+
37
+ def insert_or_upsert(data)
38
+ @use_upsert ? upsert(data) : insert(data)
39
+ end
40
+
41
+ def insert(data)
42
+ @dataset.insert(data) if data
43
+ end
44
+
45
+ def upsert(data)
46
+ if data
47
+ primary_key = @options[:primary_key]
48
+
49
+ rec = @dataset.where(primary_key => data[primary_key])
50
+ if 1 != rec.update(data)
51
+ insert(data)
52
+ end
53
+ end
54
+ end
55
+
56
+ end
57
+ end
@@ -0,0 +1,36 @@
1
+ require "metacrunch/db"
2
+
3
+ module Metacrunch
4
+ class DB::Source
5
+
6
+ DEFAULT_OPTIONS = {
7
+ rows_per_fetch: 1000,
8
+ strategy: :filter,
9
+ filter_values: nil
10
+ }
11
+
12
+ def initialize(sequel_dataset, options = {})
13
+ @dataset = sequel_dataset
14
+ @options = DEFAULT_OPTIONS.merge(options)
15
+
16
+ unless @dataset.opts[:order]
17
+ raise ArgumentError, "The dataset must be ordered."
18
+ end
19
+ end
20
+
21
+ def each(&block)
22
+ return enum_for(__method__) unless block_given?
23
+
24
+ @dataset.paged_each(
25
+ rows_per_fetch: @options[:rows_per_fetch],
26
+ strategy: @options[:strategy],
27
+ filter_values: @options[:filter_values]
28
+ ) do |row|
29
+ yield(row)
30
+ end
31
+
32
+ self
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,5 @@
1
+ module Metacrunch
2
+ module DB
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,10 @@
1
+ require "active_support"
2
+ require "active_support/core_ext"
3
+ require "sequel"
4
+
5
+ module Metacrunch
6
+ module DB
7
+ require_relative "db/source"
8
+ require_relative "db/destination"
9
+ end
10
+ end
@@ -0,0 +1,20 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "metacrunch/db/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "metacrunch-db"
8
+ spec.version = Metacrunch::DB::VERSION
9
+ spec.authors = ["René Sprotte"]
10
+ spec.summary = %q{Database package for the metacrunch ETL toolkit.}
11
+ spec.homepage = "http://github.com/ubpb/metacrunch-db"
12
+ spec.license = "MIT"
13
+
14
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
15
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
16
+ spec.require_paths = ["lib"]
17
+
18
+ spec.add_dependency "activesupport", ">= 5.1.0"
19
+ spec.add_dependency "sequel", ">= 5.0.0"
20
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: metacrunch-db
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - René Sprotte
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-09-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 5.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 5.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: sequel
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 5.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 5.0.0
41
+ description:
42
+ email:
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - ".gitignore"
48
+ - ".rspec"
49
+ - Gemfile
50
+ - License.txt
51
+ - Rakefile
52
+ - bin/console
53
+ - lib/metacrunch/db.rb
54
+ - lib/metacrunch/db/destination.rb
55
+ - lib/metacrunch/db/source.rb
56
+ - lib/metacrunch/db/version.rb
57
+ - metacrunch-db.gemspec
58
+ homepage: http://github.com/ubpb/metacrunch-db
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.6.11
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Database package for the metacrunch ETL toolkit.
82
+ test_files: []