pg_closure_tree_rebuild 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c64dc28a4a9b38273e37a80b71159be54beae34d
4
+ data.tar.gz: 487daa0b155d4bc97d6f801a6244910b7fb87618
5
+ SHA512:
6
+ metadata.gz: a21ad70e898627c19b5e5bd9e5d37a989c39c1cfb8795feb729fc90a78107a5986e856195b5b80890e5e5e11efd5ff7ba8c5bf7910aa1979f475deca32385d2a
7
+ data.tar.gz: a51a1d60ea86e451a1aa7d790677ffdb9ba2d715c8627eb321beef68ab489f49b0073b0a57d68698e40c8f3b27e0cac0311eda5f6ef356b532c9ebbd1e77293e
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,7 @@
1
+ AllCops:
2
+ Include:
3
+ - ./Gemfile
4
+ Exclude:
5
+ - config/**/*
6
+ Documentation:
7
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1
4
+ - jruby-19mode
5
+ cache: bundler
6
+ sudo: false
7
+
8
+ addons:
9
+ code_climate:
10
+ repo_token: a9262eaad8d352b2cd87a3f67408aac43094fde50bb58b9af94362886874a92c
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pg_closure_tree_rebuild.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Viktor Sokolov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # PgClosureTreeRebuild
2
+
3
+ [![Build Status](https://travis-ci.org/gzigzigzeo/pg_closure_tree_rebuild.svg)](http://travis-ci.org/gzigzigzeo/pg_closure_tree_rebuild)
4
+ [![Code Climate](https://codeclimate.com/github/gzigzigzeo/pg_closure_tree_rebuild/badges/gpa.svg)](https://codeclimate.com/github/gzigzigzeo/pg_closure_tree_rebuild)
5
+ [![Test Coverage](https://codeclimate.com/github/gzigzigzeo/pg_closure_tree_rebuild/badges/coverage.svg)](https://codeclimate.com/github/gzigzigzeo/pg_closure_tree_rebuild)
6
+
7
+
8
+ Quick `#rebuild!` method implementation for [closure_tree](https://github.com/mceachen/closure_tree) on PostgreSQL.
9
+
10
+ Say you have a table with 1M+ hierarchical records and you want to migrate it to closure_tree. Initial rebuild of hierarchies table using the standard #rebuild! method will take a dramatically long time. This library speeds up the process. It makes all the calculations using pure in-memory array and writes the result using COPY FROM STDIN BINARY.
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'pg_closure_tree_rebuild'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install pg_closure_tree_rebuild
27
+
28
+ ## Usage
29
+
30
+ $ bundle exec rake closure_tree:rebuild DATABASE_URL=postgres://localhost/tags TABLE=tags HIERARCHIES=tag_hierarchies PARENT_ID=parent_id ID=id
31
+
32
+ You can bypass all params except database URL and table name.
33
+
34
+ Rebuilding hierarchy for 1162537 records with 4 generations in average takes 2 minutes 58 seconds on my MacBook Pro i7 2.3GHz, while the rebuilding with the standard method takes hours.
35
+
36
+ ## Contributing
37
+
38
+ 1. Fork it ( https://github.com/gzigzigzeo/pg_closure_tree_rebuild/fork )
39
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
40
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
41
+ 4. Push to the branch (`git push origin my-new-feature`)
42
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ load 'tasks/closure_tree.rake'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'pg_closure_tree_rebuild'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,98 @@
1
+ module PgClosureTreeRebuild
2
+ class Builder
3
+ def initialize(table, hierarchies_table_name, options = {})
4
+ @table = table
5
+ @hierarchies_table_name = hierarchies_table_name.to_sym
6
+ @id = options.delete(:id) || :id
7
+ @parent_id = options.delete(:parent_id) || :parent_id
8
+ end
9
+
10
+ def rebuild(db)
11
+ io = StringIO.new
12
+ write_header(io)
13
+ chains.each do |row|
14
+ io.write([row.size].pack('n'))
15
+ row.each { |v| write_integer(v, io) }
16
+ yield if block_given?
17
+ end
18
+ write_close(io)
19
+ copy(db, io)
20
+ end
21
+
22
+ def tuples
23
+ @tuples ||= @table.order(@parent_id).select_map([@id, @parent_id])
24
+ end
25
+
26
+ def chains
27
+ @chains ||= build_chains
28
+ end
29
+
30
+ private
31
+
32
+ def build_chains
33
+ [].tap do |c|
34
+ tuples.each { |tuple| c.concat(chains_for(tuple[0])) }
35
+ end
36
+ end
37
+
38
+ def chains_for(id)
39
+ [].tap do |c|
40
+ c << [id, id, 0]
41
+
42
+ walk_up(id).each.with_index do |parent_id, index|
43
+ c << [parent_id, id, index + 1]
44
+ end
45
+ end
46
+ end
47
+
48
+ def walk_up(id, cref = [])
49
+ parent_id = tuples_hash[id]
50
+ return [] unless parent_id
51
+
52
+ if cref.include?(parent_id)
53
+ fail "Cycle reference detected: #{id} <=> #{parent_id}"
54
+ end
55
+
56
+ [parent_id] + walk_up(parent_id, cref + [parent_id])
57
+ end
58
+
59
+ def tuples_hash
60
+ @tuples_hash ||= index_tuples_by_id
61
+ end
62
+
63
+ def index_tuples_by_id
64
+ {}.tap do |c|
65
+ tuples.each do |(id, parent_id)|
66
+ c[id] = parent_id
67
+ end
68
+ end
69
+ end
70
+
71
+ def write_integer(value, io)
72
+ buf = [value].pack('N')
73
+ io.write([buf.bytesize].pack('N'))
74
+ io.write(buf)
75
+ end
76
+
77
+ def write_header(io)
78
+ io.write("PGCOPY\n\377\r\n\0")
79
+ io.write([0, 0].pack('NN'))
80
+ end
81
+
82
+ def write_close(io)
83
+ io.write([-1].pack('n'))
84
+ io.rewind
85
+ end
86
+
87
+ def copy(db, io)
88
+ db[@hierarchies_table_name].truncate
89
+ db.run('SET client_min_messages TO warning;')
90
+ db.copy_into(
91
+ @hierarchies_table_name,
92
+ columns: COLUMNS, format: :binary, data: io.read
93
+ )
94
+ end
95
+
96
+ COLUMNS = [:ancestor_id, :descendant_id, :generations]
97
+ end
98
+ end
@@ -0,0 +1,7 @@
1
+ module PgClosureTreeRebuild
2
+ class Railtie < Rails::Railtie
3
+ rake_tasks do
4
+ load File.join(File.dirname(__FILE__), '../../tasks/closure_tree.rake')
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module PgClosureTreeRebuild
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,7 @@
1
+ require 'pg_closure_tree_rebuild/version'
2
+ require 'pg_closure_tree_rebuild/builder'
3
+ require 'pg_closure_tree_rebuild/railtie' if defined?(Rails)
4
+
5
+ module PgClosureTreeRebuild
6
+ # Your code goes here...
7
+ end
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pg_closure_tree_rebuild/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'pg_closure_tree_rebuild'
8
+ spec.version = PgClosureTreeRebuild::VERSION
9
+ spec.authors = ['Viktor Sokolov']
10
+ spec.email = ['gzigzigzeo@evilmartians.com']
11
+
12
+ spec.summary = %q{Quick #rebuild! method implementation for closure_tree on PostgreSQL.}
13
+ spec.description = %q{Quick #rebuild! method implementation for closure_tree on PostgreSQL.}
14
+ spec.homepage = "http://github.com/gzigzigzeo/pg_closure_tree_rebuild"
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'sequel'
22
+ spec.add_dependency 'pg'
23
+ spec.add_dependency 'ruby-progressbar'
24
+ spec.add_dependency 'activesupport'
25
+
26
+ spec.add_development_dependency 'bundler'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'rubocop'
29
+ spec.add_development_dependency 'simplecov'
30
+ spec.add_development_dependency 'codeclimate-test-reporter'
31
+ spec.add_development_dependency 'rspec'
32
+ spec.add_development_dependency 'sqlite3'
33
+ end
@@ -0,0 +1,37 @@
1
+ require 'pg_closure_tree_rebuild'
2
+ require 'ruby-progressbar'
3
+ require 'sequel'
4
+ require 'active_support/core_ext/string/inflections'
5
+ require 'benchmark'
6
+
7
+ namespace :closure_tree do
8
+ desc 'Rebuild hierarchy (TABLE, HIERARCHIES, DATABASE_URL, PARENT_ID, ID)'
9
+ task :rebuild do
10
+ puts Benchmark.measure {
11
+ DB = Sequel.connect(ENV['DATABASE_URL'])
12
+
13
+ table = ENV['TABLE']
14
+ hierarchies = ENV['HIERARCHIES'] || "#{table.singularize}_hierarchies"
15
+ parent_id = ENV['PARENT_ID'] || 'parent_id'
16
+ id = ENV['ID'] || 'id'
17
+
18
+ scope = DB[table.to_sym]
19
+ hierarchies = hierarchies.to_sym
20
+
21
+ builder = PgClosureTreeRebuild::Builder.new(
22
+ scope,
23
+ hierarchies.to_sym,
24
+ id: id.to_sym,
25
+ parent_id: parent_id.to_sym
26
+ )
27
+
28
+ puts 'Calculating chains...'
29
+ bar = ProgressBar.create(total: builder.chains.size)
30
+
31
+ puts "Records: #{scope.count}"
32
+
33
+ puts 'Importing...'
34
+ builder.rebuild(DB) { bar.increment }
35
+ }
36
+ end
37
+ end
metadata ADDED
@@ -0,0 +1,215 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pg_closure_tree_rebuild
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Viktor Sokolov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sequel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pg
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: ruby-progressbar
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: codeclimate-test-reporter
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rspec
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: sqlite3
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ description: 'Quick #rebuild! method implementation for closure_tree on PostgreSQL.'
168
+ email:
169
+ - gzigzigzeo@evilmartians.com
170
+ executables: []
171
+ extensions: []
172
+ extra_rdoc_files: []
173
+ files:
174
+ - ".gitignore"
175
+ - ".rspec"
176
+ - ".rubocop.yml"
177
+ - ".travis.yml"
178
+ - CODE_OF_CONDUCT.md
179
+ - Gemfile
180
+ - LICENSE.txt
181
+ - README.md
182
+ - Rakefile
183
+ - bin/console
184
+ - bin/setup
185
+ - lib/pg_closure_tree_rebuild.rb
186
+ - lib/pg_closure_tree_rebuild/builder.rb
187
+ - lib/pg_closure_tree_rebuild/railtie.rb
188
+ - lib/pg_closure_tree_rebuild/version.rb
189
+ - pg_closure_tree_rebuild.gemspec
190
+ - tasks/closure_tree.rake
191
+ homepage: http://github.com/gzigzigzeo/pg_closure_tree_rebuild
192
+ licenses:
193
+ - MIT
194
+ metadata: {}
195
+ post_install_message:
196
+ rdoc_options: []
197
+ require_paths:
198
+ - lib
199
+ required_ruby_version: !ruby/object:Gem::Requirement
200
+ requirements:
201
+ - - ">="
202
+ - !ruby/object:Gem::Version
203
+ version: '0'
204
+ required_rubygems_version: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - ">="
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
209
+ requirements: []
210
+ rubyforge_project:
211
+ rubygems_version: 2.2.2
212
+ signing_key:
213
+ specification_version: 4
214
+ summary: 'Quick #rebuild! method implementation for closure_tree on PostgreSQL.'
215
+ test_files: []