forkit 0.1.0-java
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +7 -0
- data/README.md +33 -0
- data/Rakefile +30 -0
- data/lib/forkit.rb +24 -0
- data/lib/forkit/enumerable.rb +46 -0
- data/lib/forkit/iterator.rb +67 -0
- data/lib/forkit/version.rb +3 -0
- metadata +56 -0
data/LICENSE
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (c) 2011 Durran Jordan
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
Requirements
|
2
|
+
============
|
3
|
+
- JRuby 1.6.5 +
|
4
|
+
- JDK 7 +
|
5
|
+
|
6
|
+
Environment
|
7
|
+
===========
|
8
|
+
- JRUBY_OPTS="--1.9 --server -Xinvokedynamic.constants=true"
|
9
|
+
- RUBYOPT="rubygems"
|
10
|
+
|
11
|
+
Installation
|
12
|
+
============
|
13
|
+
```ruby
|
14
|
+
gem "forkit"
|
15
|
+
```
|
16
|
+
|
17
|
+
Usage
|
18
|
+
=====
|
19
|
+
```ruby
|
20
|
+
array.forked.each do |object|
|
21
|
+
# Code here to execute in parallel on the object.
|
22
|
+
end
|
23
|
+
|
24
|
+
array.forked(100000).each do |object|
|
25
|
+
# Code here to execute in parallel on the object.
|
26
|
+
end
|
27
|
+
```
|
28
|
+
|
29
|
+
Running Specs
|
30
|
+
=============
|
31
|
+
```ruby
|
32
|
+
rake
|
33
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require "bundler"
|
2
|
+
Bundler.setup
|
3
|
+
|
4
|
+
require "rake"
|
5
|
+
require "rspec"
|
6
|
+
require "rspec/core/rake_task"
|
7
|
+
require "forkit/version"
|
8
|
+
|
9
|
+
$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
|
10
|
+
|
11
|
+
task :gem => :build
|
12
|
+
task :build do
|
13
|
+
system "gem build forkit.gemspec"
|
14
|
+
end
|
15
|
+
|
16
|
+
task :install => :build do
|
17
|
+
system "sudo gem install forkit-#{Forkit::VERSION}.gem"
|
18
|
+
end
|
19
|
+
|
20
|
+
task :release => :build do
|
21
|
+
system "git tag -a v#{Forkit::VERSION} -m 'Tagging #{Forkit::VERSION}'"
|
22
|
+
system "git push --tags"
|
23
|
+
system "gem push forkit-#{Forkit::VERSION}-java.gem"
|
24
|
+
end
|
25
|
+
|
26
|
+
RSpec::Core::RakeTask.new("spec") do |spec|
|
27
|
+
spec.pattern = "spec/**/*_spec.rb"
|
28
|
+
end
|
29
|
+
|
30
|
+
task :default => :spec
|
data/lib/forkit.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require "forkit/enumerable"
|
2
|
+
require "forkit/iterator"
|
3
|
+
require "forkit/version"
|
4
|
+
|
5
|
+
class Array
|
6
|
+
|
7
|
+
# We hook into array to be able to easily handle concurrent processing of
|
8
|
+
# each blocks.
|
9
|
+
#
|
10
|
+
# @example Execute each in parallel.
|
11
|
+
# [ 1, 2, 3, 4, 5 ].forked.each do |object|
|
12
|
+
# p object
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# @param [ Integer ] threshold The threshold where to process in a single
|
16
|
+
# thread.
|
17
|
+
#
|
18
|
+
# @return [ Forkit::Enumerable ] The proxy process.
|
19
|
+
#
|
20
|
+
# @since 0.1.0
|
21
|
+
def forked(threshold = 100000)
|
22
|
+
Forkit::Enumerable.new(self, threshold)
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require "java"
|
2
|
+
import java.util.concurrent.ForkJoinPool
|
3
|
+
|
4
|
+
module Forkit
|
5
|
+
|
6
|
+
# The enumerable allows for concurrent processing via JDK7's ForkJoinPool.
|
7
|
+
class Enumerable
|
8
|
+
|
9
|
+
# @attribute [r] objects The array to process.
|
10
|
+
# @attribute [r] threshold The single process threshold count.
|
11
|
+
attr_reader :objects, :threshold
|
12
|
+
|
13
|
+
# Instantiate the new concurrent enumerable.
|
14
|
+
#
|
15
|
+
# @example Create the enumerable.
|
16
|
+
# Enumerable.new([ 1, 2, 3 ], 100000)
|
17
|
+
#
|
18
|
+
# @param [ Array<Object> ] objects The object array.
|
19
|
+
# @param [ Integer ] threshold The single process threshold count.
|
20
|
+
#
|
21
|
+
# @return [ Forkit::Enumerable ] The concurrent enumerable.
|
22
|
+
#
|
23
|
+
# @since 0.1.0
|
24
|
+
def initialize(objects, threshold)
|
25
|
+
@objects, @threshold = objects, threshold
|
26
|
+
end
|
27
|
+
|
28
|
+
# Execute the provided block for each element in the array.
|
29
|
+
#
|
30
|
+
# @example Execute in parallel.
|
31
|
+
# enum.each do |object|
|
32
|
+
# p object
|
33
|
+
# end
|
34
|
+
#
|
35
|
+
# @return [ nil ] nil.
|
36
|
+
#
|
37
|
+
# @since 0.1.0
|
38
|
+
def each(&block)
|
39
|
+
if block_given?
|
40
|
+
ForkJoinPool.new.invoke(Iterator.new(objects, threshold, block))
|
41
|
+
else
|
42
|
+
objects.each
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require "java"
|
2
|
+
import java.util.concurrent.RecursiveAction
|
3
|
+
|
4
|
+
module Forkit
|
5
|
+
|
6
|
+
# The iterator is a recursive action that executes in parallel on a
|
7
|
+
# ForkJoinPool.
|
8
|
+
class Iterator < RecursiveAction
|
9
|
+
|
10
|
+
# @attribute [r] block The block to execute on each object.
|
11
|
+
# @attribute [r] objects The array to process.
|
12
|
+
# @attribute [r] threshold The single process threshold count.
|
13
|
+
attr_reader :block, :objects, :threshold
|
14
|
+
|
15
|
+
# Splits the work into the appropriate chunks and executes in parallel.
|
16
|
+
#
|
17
|
+
# @example Execute in parallel.
|
18
|
+
# iterator.compute
|
19
|
+
#
|
20
|
+
# @return [ nil ] nil.
|
21
|
+
#
|
22
|
+
# @since 0.1.0
|
23
|
+
def compute
|
24
|
+
if objects.size <= threshold
|
25
|
+
objects.each do |object|
|
26
|
+
block.call(object)
|
27
|
+
end
|
28
|
+
else
|
29
|
+
midpoint = objects.size / 2
|
30
|
+
invoke_all(
|
31
|
+
Iterator.new(objects[0, midpoint], threshold, block),
|
32
|
+
Iterator.new(objects[midpoint, objects.size], threshold, block)
|
33
|
+
)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# Instantiate the new iterator.
|
38
|
+
#
|
39
|
+
# @example Create the iterator.
|
40
|
+
# Enumerable.new([ 1, 2, 3 ], 100000)
|
41
|
+
#
|
42
|
+
# @param [ Array<Object> ] objects The object array.
|
43
|
+
# @param [ Integer ] threshold The single process threshold count.
|
44
|
+
#
|
45
|
+
# @return [ Forkit::Iterator ] The concurrent iterator.
|
46
|
+
#
|
47
|
+
# @since 0.1.0
|
48
|
+
def initialize(objects, threshold, block)
|
49
|
+
super()
|
50
|
+
@objects, @threshold, @block = objects, threshold, block
|
51
|
+
end
|
52
|
+
|
53
|
+
# Convenience method for invoking against the class.
|
54
|
+
#
|
55
|
+
# @example Invoke the tasks.
|
56
|
+
# iterator.invoke_all(tasks)
|
57
|
+
#
|
58
|
+
# @param [ Array<RecursiveAction> ] args The tasks to execute.
|
59
|
+
#
|
60
|
+
# @return [ nil ] nil.
|
61
|
+
#
|
62
|
+
# @since 0.1.0
|
63
|
+
def invoke_all(*args)
|
64
|
+
self.class.invoke_all(*args)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: forkit
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: java
|
7
|
+
authors:
|
8
|
+
- Durran Jordan
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-12-05 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: Process Array#each blocks concurrently with JDK7 ForkJoinPools.
|
15
|
+
email:
|
16
|
+
- durran@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/forkit.rb
|
22
|
+
- lib/forkit/enumerable.rb
|
23
|
+
- lib/forkit/iterator.rb
|
24
|
+
- lib/forkit/version.rb
|
25
|
+
- LICENSE
|
26
|
+
- README.md
|
27
|
+
- Rakefile
|
28
|
+
homepage: http://github.com/durran/duke
|
29
|
+
licenses: []
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
segments:
|
39
|
+
- 0
|
40
|
+
hash: 2
|
41
|
+
version: '0'
|
42
|
+
none: false
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 1.3.6
|
48
|
+
none: false
|
49
|
+
requirements: []
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.8.9
|
52
|
+
signing_key:
|
53
|
+
specification_version: 3
|
54
|
+
summary: Process Array#each blocks concurrently with JDK7 ForkJoinPools.
|
55
|
+
test_files: []
|
56
|
+
...
|