parallel_tsort 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
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ vendor
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in parallel_tsort.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Trae Robrock
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,29 @@
1
+ # ParallelTsort
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'parallel_tsort'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install parallel_tsort
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
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,7 @@
1
+ require "parallel_tsort/version"
2
+ require "parallel_tsort/sorter"
3
+
4
+ module ParallelTsort
5
+ class MissingNode < StandardError ; end
6
+ class SelfDependentNode < StandardError ; end
7
+ end
@@ -0,0 +1,59 @@
1
+ require "tsort"
2
+
3
+ module ParallelTsort
4
+ class Sorter
5
+ include TSort
6
+
7
+ def initialize
8
+ @deps = []
9
+ end
10
+
11
+ def add_dependency(name, deps=[])
12
+ raise SelfDependentNode if deps.include?(name)
13
+ @deps << [name, deps]
14
+ end
15
+
16
+ def run(parallel=false)
17
+ result = tsort
18
+ return result unless parallel
19
+
20
+ groups = []
21
+ used = []
22
+ result.each_with_index do |node, i|
23
+ next if used.include?(node)
24
+
25
+ parallel_nodes = result[i..-1].reject do |other_node|
26
+ next if other_node == node
27
+ next if used.include?(other_node)
28
+
29
+ is_dependent? node, other_node
30
+ end
31
+
32
+ used.concat(parallel_nodes)
33
+ groups << parallel_nodes
34
+ end
35
+
36
+ groups
37
+ end
38
+
39
+ private
40
+
41
+ def is_dependent?(node, other_node)
42
+ other_node = @deps.detect { |dep| dep.first == other_node }
43
+ dependencies = other_node[1]
44
+
45
+ dependencies.include?(node) || dependencies.detect { |n| is_dependent?(node, n) }
46
+ end
47
+
48
+ def tsort_each_node(&block)
49
+ @deps.map(&:first).each(&block)
50
+ end
51
+
52
+ def tsort_each_child(node, &block)
53
+ node = @deps.detect { |dep| dep.first == node }
54
+ raise MissingNode if node.nil?
55
+
56
+ node[1].each(&block)
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,3 @@
1
+ module ParallelTsort
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'parallel_tsort/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "parallel_tsort"
8
+ spec.version = ParallelTsort::VERSION
9
+ spec.authors = ["Trae Robrock"]
10
+ spec.email = ["trobrock@gmail.com"]
11
+ spec.description = %q{Simple tool to resolve dependencies that can be run in parallel.}
12
+ spec.summary = %q{Simple tool to resolve dependencies that can be run in parallel.}
13
+ spec.homepage = ""
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_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "pry"
25
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe ParallelTsort do
4
+ it 'should have a version number' do
5
+ ParallelTsort::VERSION.should_not be_nil
6
+ end
7
+ end
@@ -0,0 +1,124 @@
1
+ require 'spec_helper'
2
+ describe ParallelTsort::Sorter do
3
+ let(:sorter) { ParallelTsort::Sorter.new }
4
+
5
+ context "happy path" do
6
+ before do
7
+ sorter.add_dependency :web, [:database, :redis]
8
+ sorter.add_dependency :queue, [:database, :redis]
9
+ sorter.add_dependency :database
10
+ sorter.add_dependency :redis
11
+ end
12
+
13
+ it "should sort in serial" do
14
+ sorter.run.should == [:database, :redis, :web, :queue]
15
+ end
16
+
17
+ it "should sort in parallel" do
18
+ sorter.run(true).should == [[:database, :redis], [:web, :queue]]
19
+ end
20
+ end
21
+
22
+ context "with deps in reverse" do
23
+ before do
24
+ sorter.add_dependency :redis
25
+ sorter.add_dependency :database
26
+ sorter.add_dependency :web, [:database, :redis]
27
+ sorter.add_dependency :queue, [:database, :redis]
28
+ end
29
+
30
+ it "should sort in serial" do
31
+ sorter.run.should == [:redis, :database, :web, :queue]
32
+ end
33
+
34
+ it "should sort in parallel" do
35
+ sorter.run(true).should == [[:redis, :database], [:web, :queue]]
36
+ end
37
+ end
38
+
39
+ context "with only one item" do
40
+ before do
41
+ sorter.add_dependency :redis
42
+ end
43
+
44
+ it "should sort in serial" do
45
+ sorter.run.should == [:redis]
46
+ end
47
+
48
+ it "should sort in parallel" do
49
+ sorter.run(true).should == [[:redis]]
50
+ end
51
+ end
52
+
53
+ context "with nothing added" do
54
+ it "should sort in serial" do
55
+ sorter.run.should == []
56
+ end
57
+
58
+ it "should sort in parallel" do
59
+ sorter.run(true).should == []
60
+ end
61
+ end
62
+
63
+ context "with a dependency that does not exist" do
64
+ before do
65
+ sorter.add_dependency :queue, [:redis]
66
+ end
67
+
68
+ it "should sort in serial" do
69
+ expect {
70
+ sorter.run
71
+ }.to raise_error(ParallelTsort::MissingNode)
72
+ end
73
+
74
+ it "should sort in parallel" do
75
+ expect {
76
+ sorter.run(true)
77
+ }.to raise_error(ParallelTsort::MissingNode)
78
+ end
79
+ end
80
+
81
+ context "with cycle" do
82
+ before do
83
+ sorter.add_dependency :queue, [:redis]
84
+ sorter.add_dependency :redis, [:queue]
85
+ end
86
+
87
+ it "should sort in serial" do
88
+ expect {
89
+ sorter.run
90
+ }.to raise_error(TSort::Cyclic)
91
+ end
92
+
93
+ it "should sort in parallel" do
94
+ expect {
95
+ sorter.run(true)
96
+ }.to raise_error(TSort::Cyclic)
97
+ end
98
+ end
99
+
100
+ context "with node depending on itself" do
101
+ it "should raise an error" do
102
+ expect {
103
+ sorter.add_dependency :queue, [:queue]
104
+ }.to raise_error(ParallelTsort::SelfDependentNode)
105
+ end
106
+ end
107
+
108
+ context "non parallel dependencies" do
109
+ before do
110
+ sorter.add_dependency :web, [:queue]
111
+ sorter.add_dependency :queue, [:database]
112
+ sorter.add_dependency :database, [:redis]
113
+ sorter.add_dependency :redis
114
+ end
115
+
116
+ it "should sort in serial" do
117
+ sorter.run.should == [:redis, :database, :queue, :web]
118
+ end
119
+
120
+ it "should sort in parallel" do
121
+ sorter.run(true).should == [[:redis], [:database], [:queue], [:web]]
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,3 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'pry'
3
+ require 'parallel_tsort'
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: parallel_tsort
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Trae Robrock
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-12-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
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: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
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: pry
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: Simple tool to resolve dependencies that can be run in parallel.
79
+ email:
80
+ - trobrock@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - .rspec
87
+ - .travis.yml
88
+ - Gemfile
89
+ - LICENSE.txt
90
+ - README.md
91
+ - Rakefile
92
+ - lib/parallel_tsort.rb
93
+ - lib/parallel_tsort/sorter.rb
94
+ - lib/parallel_tsort/version.rb
95
+ - parallel_tsort.gemspec
96
+ - spec/parallel_tsort_spec.rb
97
+ - spec/sorter_spec.rb
98
+ - spec/spec_helper.rb
99
+ homepage: ''
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
+ segments:
113
+ - 0
114
+ hash: -3671689608418595975
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ segments:
122
+ - 0
123
+ hash: -3671689608418595975
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 1.8.23
127
+ signing_key:
128
+ specification_version: 3
129
+ summary: Simple tool to resolve dependencies that can be run in parallel.
130
+ test_files:
131
+ - spec/parallel_tsort_spec.rb
132
+ - spec/sorter_spec.rb
133
+ - spec/spec_helper.rb