vostok 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.9.3"
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in vostok.gemspec
4
+ gemspec
5
+ gem 'coveralls', require: false
data/Gemfile.lock ADDED
@@ -0,0 +1,46 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ vostok (0.0.1)
5
+ pg
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ colorize (0.5.8)
11
+ coveralls (0.6.3)
12
+ colorize
13
+ multi_json (~> 1.3)
14
+ rest-client
15
+ simplecov (>= 0.7)
16
+ thor
17
+ diff-lcs (1.2.1)
18
+ mime-types (1.21)
19
+ multi_json (1.7.1)
20
+ pg (0.14.1)
21
+ rake (10.0.3)
22
+ rest-client (1.6.7)
23
+ mime-types (>= 1.16)
24
+ rspec (2.13.0)
25
+ rspec-core (~> 2.13.0)
26
+ rspec-expectations (~> 2.13.0)
27
+ rspec-mocks (~> 2.13.0)
28
+ rspec-core (2.13.1)
29
+ rspec-expectations (2.13.0)
30
+ diff-lcs (>= 1.1.3, < 2.0)
31
+ rspec-mocks (2.13.0)
32
+ simplecov (0.7.1)
33
+ multi_json (~> 1.0)
34
+ simplecov-html (~> 0.7.1)
35
+ simplecov-html (0.7.1)
36
+ thor (0.17.0)
37
+
38
+ PLATFORMS
39
+ ruby
40
+
41
+ DEPENDENCIES
42
+ bundler (~> 1.3)
43
+ coveralls
44
+ rake
45
+ rspec
46
+ vostok!
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Valentin Vasilyev
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/README.md ADDED
@@ -0,0 +1,92 @@
1
+ # Vostok
2
+
3
+ [![Build Status](https://travis-ci.org/Valve/vostok.png)](https://travis-ci.org/Valve/vostok)
4
+ [![Coverage Status](https://coveralls.io/repos/Valve/vostok/badge.png?branch=master)](https://coveralls.io/r/Valve/vostok)
5
+
6
+ Sick pg import
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'vostok'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install vostok
21
+
22
+ ## Usage
23
+
24
+ Vostok works directly with PG gem, no other dependencies
25
+
26
+ ```ruby
27
+
28
+ require 'vostok'
29
+
30
+ import = Vostok::Import.new(dbname: 'test', user: 'developer', password: 'r00t')
31
+ data = []
32
+ 1_000.times do
33
+ data << ['String', 99]
34
+ end
35
+
36
+ import.start(:customers, [:name, :balance], data)
37
+
38
+ ```
39
+
40
+ What Vostok does not do:
41
+
42
+ 1. Run validations
43
+ 2. Integrates with your ORM
44
+ 3. Works with other DBs
45
+ 4. Sanitizes your data
46
+
47
+ However, what it does is insert rows at a sick rate.
48
+
49
+ `
50
+ 10_000 rows inserted with AR - 18 seconds
51
+ `
52
+
53
+ `
54
+ 10_000 rows inserted with Vostok - 0.2 seconds
55
+ `
56
+
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it
61
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
62
+ 3. Add your tests and run them with `rspec spec`
63
+ 4. Commit your changes (`git commit -am 'Add some feature'`)
64
+ 5. Push to the branch (`git push origin my-new-feature`)
65
+ 6. Create new Pull Request
66
+
67
+
68
+ ## License
69
+
70
+
71
+ Copyright (c) 2013 Valentin Vasilyev
72
+
73
+ MIT License
74
+
75
+ Permission is hereby granted, free of charge, to any person obtaining
76
+ a copy of this software and associated documentation files (the
77
+ "Software"), to deal in the Software without restriction, including
78
+ without limitation the rights to use, copy, modify, merge, publish,
79
+ distribute, sublicense, and/or sell copies of the Software, and to
80
+ permit persons to whom the Software is furnished to do so, subject to
81
+ the following conditions:
82
+
83
+ The above copyright notice and this permission notice shall be
84
+ included in all copies or substantial portions of the Software.
85
+
86
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
87
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
88
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
89
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
90
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
91
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
92
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new('spec')
5
+
6
+ task(default: :spec)
@@ -0,0 +1,40 @@
1
+ require 'pg'
2
+
3
+ module Vostok
4
+ class Import
5
+ attr_reader :connection, :pg_connection, :table
6
+
7
+ def initialize(connection)
8
+ @connection = connection
9
+ @options = {batch_size: 1000}
10
+ end
11
+
12
+ def start(table, columns, values, options = @options)
13
+ validate_args(columns, values)
14
+ @table = table
15
+ begin
16
+ @pg_connection = PG::Connection.open(@connection)
17
+ values.each_slice(options[:batch_size]) do |slice|
18
+ sql = generate_sql(table, columns, slice)
19
+ @pg_connection.exec(sql)
20
+ end
21
+ values.length
22
+ ensure
23
+ @pg_connection.close
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def validate_args(columns, values)
30
+ raise ArgumentError, 'Columns names must be a non-empty array' if columns.nil? || columns.empty?
31
+ raise ArgumentError, 'Column names must correspond to values' if values.length > 0 && values[0].length != columns.length
32
+ end
33
+
34
+ def generate_sql(table, columns, values_slice)
35
+ columns_sql = columns.join('","')
36
+ values_sql = values_slice.map{|i| "('" + i.join("','") + "')"}.join(',')
37
+ "insert into \"#{table.to_s}\" (\"#{columns_sql}\") values#{values_sql}"
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ module Vostok
2
+ VERSION = "0.0.1"
3
+ end
data/lib/vostok.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'vostok/version'
2
+ require 'vostok/import'
3
+
4
+ module Vostok
5
+ end
@@ -0,0 +1,9 @@
1
+ require 'bundler/setup'
2
+ require_relative '../lib/vostok'
3
+
4
+ require 'coveralls'
5
+ Coveralls.wear!
6
+
7
+
8
+
9
+
@@ -0,0 +1,90 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'vostok' do
4
+ it 'should return version' do
5
+ Vostok::VERSION.should_not be_nil
6
+ end
7
+
8
+ let(:import){import = Vostok::Import.new(host: 'localhost', dbname: 'db1')}
9
+ it 'should take connection hash to constructor' do
10
+ import.should_not be_nil
11
+ end
12
+
13
+ it 'should assign connection' do
14
+ import.connection.should_not be_nil
15
+ end
16
+
17
+ it 'should assign connection with hash' do
18
+ import.connection.should be_instance_of ::Hash
19
+ end
20
+
21
+ context 'importing' do
22
+ it 'should throw argument error when no table name given' do
23
+ ->{import.start(nil, nil, nil)}.should raise_error ArgumentError
24
+ ->{import.start('', nil, nil)}.should raise_error ArgumentError
25
+ end
26
+
27
+ it 'should throw argument error when no column names are given' do
28
+ ->{import.start(:customers, nil, nil)}.should raise_error ArgumentError
29
+ end
30
+
31
+ it 'should throw argument error when column names is empty array' do
32
+ ->{import.start(:customers, [], nil)}.should raise_error ArgumentError
33
+ end
34
+
35
+ it 'should throw argument error when column names are not equal to values' do
36
+ ->{import.start(:customers, [:a, :b], [[1,2,3], [1,2,3]])}.should raise_error ArgumentError
37
+ end
38
+
39
+ it 'should open pg_connection if it is not yet open' do
40
+ mock_connection = mock 'connection', close: true
41
+ mock_connection.stub!(:exec)
42
+ PG::Connection.should_receive(:open).and_return mock_connection
43
+ import.start(:customers, [:a], [[1]])
44
+ end
45
+
46
+ it 'should call PG library with correct sql in one go' do
47
+ sql = <<-eos
48
+ insert into "customers" ("a","b") values('1','2'),('3','4')
49
+ eos
50
+ mock_connection = mock 'connection', close: true
51
+ PG::Connection.stub!(:open).and_return mock_connection
52
+ mock_connection.should_receive(:exec).with(sql.strip)
53
+ import.start(:customers, [:a, :b], [[1,2], [3,4]])
54
+ end
55
+
56
+ it 'should partition sql into subquries per batch_size' do
57
+ data = [[1,2], [3,4], [5,6], [7,8], [9,10]]
58
+ sql1 = <<-eos
59
+ insert into "customers" ("a","b") values('1','2'),('3','4')
60
+ eos
61
+ sql2 = <<-eos
62
+ insert into "customers" ("a","b") values('5','6'),('7','8')
63
+ eos
64
+ sql3 = <<-eos
65
+ insert into "customers" ("a","b") values('9','10')
66
+ eos
67
+ mock_connection = mock 'connection', close: true
68
+ PG::Connection.stub!(:open).and_return mock_connection
69
+ mock_connection.should_receive(:exec).with(sql1.strip).ordered
70
+ mock_connection.should_receive(:exec).with(sql2.strip).ordered
71
+ mock_connection.should_receive(:exec).with(sql3.strip).ordered
72
+ import.start(:customers, [:a, :b], data, batch_size: 2)
73
+ end
74
+
75
+ it 'should return number of inserted rows' do
76
+ mock_connection = mock 'connection', close: true
77
+ PG::Connection.stub!(:open).and_return mock_connection
78
+ mock_connection.stub!(:exec)
79
+ import.start(:customers, [:a, :b], [[1,2]]).should == 1
80
+ end
81
+
82
+ it 'should close the connection' do
83
+ mock_connection = mock 'connection'
84
+ PG::Connection.stub!(:open).and_return mock_connection
85
+ mock_connection.stub!(:exec)
86
+ mock_connection.should_receive(:close)
87
+ import.start(:customers, [:a, :b], [[1,2]])
88
+ end
89
+ end
90
+ end
data/vostok.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'vostok/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "vostok"
8
+ spec.version = Vostok::VERSION
9
+ spec.authors = ["Valentin Vasilyev"]
10
+ spec.email = ["iamvalentin@gmail.com"]
11
+ spec.description = %q{Sick pg import}
12
+ spec.summary = %q{Sick pg import}
13
+ spec.homepage = "https://github.com/Valve/vostok"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "pg"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vostok
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Valentin Vasilyev
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: pg
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.3'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.3'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Sick pg import
79
+ email:
80
+ - iamvalentin@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - .rspec
87
+ - .travis.yml
88
+ - Gemfile
89
+ - Gemfile.lock
90
+ - LICENSE.txt
91
+ - README.md
92
+ - Rakefile
93
+ - lib/vostok.rb
94
+ - lib/vostok/import.rb
95
+ - lib/vostok/version.rb
96
+ - spec/spec_helper.rb
97
+ - spec/vostok_spec.rb
98
+ - vostok.gemspec
99
+ homepage: https://github.com/Valve/vostok
100
+ licenses:
101
+ - MIT
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 1.8.23
121
+ signing_key:
122
+ specification_version: 3
123
+ summary: Sick pg import
124
+ test_files:
125
+ - spec/spec_helper.rb
126
+ - spec/vostok_spec.rb
127
+ has_rdoc: