Created
December 2, 2011 14:46
-
-
Save durran/1423480 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "java" | |
import java.util.concurrent.RecursiveAction | |
class Scorer < RecursiveAction | |
attr_reader :documents | |
def compute | |
if documents.size <= 10000 | |
score_each | |
else | |
invokeAll( | |
Scorer.new(documents.slice!(0, documents.length / 2)), | |
Scorer.new(documents) | |
) | |
end | |
nil | |
end | |
def initialize(documents) | |
super() | |
@documents = documents | |
end | |
def score_each | |
documents.each do |doc| | |
# Just set to one for testing purposes to see if this fork/join works. | |
doc[:score] = 1 | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "java" | |
import java.util.concurrent.ForkJoinPool | |
require "spec_helper" | |
describe Engine do | |
let(:documents) do | |
(1...1000000).reduce([]) do |docs, n| | |
docs.tap { |d| d.push({ score: n }) } | |
end | |
end | |
let(:pool) do | |
ForkJoinPool.new | |
end | |
let!(:results) do | |
pool.invoke(Scorer.new(documents)) | |
end | |
it "scores each of the documents" do | |
documents.first["score"].should eq(1) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment