theduke 0.2.4-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 +29 -0
- data/Rakefile +30 -0
- data/lib/concurrently.rb +24 -0
- data/lib/duke.rb +24 -0
- data/lib/duke/enumerable.rb +46 -0
- data/lib/duke/iterator.rb +67 -0
- data/lib/duke/version.rb +3 -0
- metadata +57 -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,29 @@
|
|
1
|
+
"Duke", "Duke of York", "Fork"...
|
2
|
+
|
3
|
+
Requirements
|
4
|
+
============
|
5
|
+
- JRuby 1.6.5 +
|
6
|
+
- JDK 7 +
|
7
|
+
|
8
|
+
Environment
|
9
|
+
===========
|
10
|
+
- JRUBY_OPTS="--1.9 --server -Xinvokedynamic.constants=true"
|
11
|
+
- RUBYOPT="rubygems"
|
12
|
+
|
13
|
+
Running Specs
|
14
|
+
=============
|
15
|
+
```ruby
|
16
|
+
rake
|
17
|
+
```
|
18
|
+
|
19
|
+
Usage
|
20
|
+
=====
|
21
|
+
```ruby
|
22
|
+
array.concurrently.each do |object|
|
23
|
+
# Code here to execute in parallel on the object.
|
24
|
+
end
|
25
|
+
|
26
|
+
array.concurrently(100000).each do |object|
|
27
|
+
# Code here to execute in parallel on the object.
|
28
|
+
end
|
29
|
+
```
|
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 "duke/version"
|
8
|
+
|
9
|
+
$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
|
10
|
+
|
11
|
+
task :gem => :build
|
12
|
+
task :build do
|
13
|
+
system "gem build theduke.gemspec"
|
14
|
+
end
|
15
|
+
|
16
|
+
task :install => :build do
|
17
|
+
system "sudo gem install theduke-#{Duke::VERSION}.gem"
|
18
|
+
end
|
19
|
+
|
20
|
+
task :release => :build do
|
21
|
+
system "git tag -a v#{Duke::VERSION} -m 'Tagging #{Duke::VERSION}'"
|
22
|
+
system "git push --tags"
|
23
|
+
system "gem push theduke-#{Duke::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/concurrently.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require "duke/enumerable"
|
2
|
+
require "duke/iterator"
|
3
|
+
require "duke/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 concurrently.
|
11
|
+
# [ 1, 2, 3, 4, 5 ].concurrently.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 [ Duke::Enumerable ] The proxy process.
|
19
|
+
#
|
20
|
+
# @since 0.1.0
|
21
|
+
def concurrently(threshold = 100000)
|
22
|
+
Duke::Enumerable.new(self, threshold)
|
23
|
+
end
|
24
|
+
end
|
data/lib/duke.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require "duke/enumerable"
|
2
|
+
require "duke/iterator"
|
3
|
+
require "duke/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 concurrently.
|
11
|
+
# [ 1, 2, 3, 4, 5 ].concurrently.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 [ Concurrent::Process ] The proxy process.
|
19
|
+
#
|
20
|
+
# @since 0.1.0
|
21
|
+
def concurrently(threshold = 100000)
|
22
|
+
Duke::Enumerable.new(self, threshold)
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require "java"
|
2
|
+
import java.util.concurrent.ForkJoinPool
|
3
|
+
|
4
|
+
module Duke
|
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 [ Duke::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 Duke
|
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 [ Duke::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
|
data/lib/duke/version.rb
ADDED
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: theduke
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.4
|
5
|
+
prerelease:
|
6
|
+
platform: java
|
7
|
+
authors:
|
8
|
+
- Durran Jordan
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-12-03 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/concurrently.rb
|
22
|
+
- lib/duke.rb
|
23
|
+
- lib/duke/enumerable.rb
|
24
|
+
- lib/duke/iterator.rb
|
25
|
+
- lib/duke/version.rb
|
26
|
+
- LICENSE
|
27
|
+
- README.md
|
28
|
+
- Rakefile
|
29
|
+
homepage: http://github.com/durran/duke
|
30
|
+
licenses: []
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options: []
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
segments:
|
40
|
+
- 0
|
41
|
+
hash: 2
|
42
|
+
version: '0'
|
43
|
+
none: false
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: 1.3.6
|
49
|
+
none: false
|
50
|
+
requirements: []
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.8.9
|
53
|
+
signing_key:
|
54
|
+
specification_version: 3
|
55
|
+
summary: Process Array#each blocks concurrently with JDK7 ForkJoinPools.
|
56
|
+
test_files: []
|
57
|
+
...
|