Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
nigelduffy
committed
Feb 8, 2014
1 parent
404cd7a
commit 21ac3ab
Showing
15 changed files
with
180 additions
and
12 deletions.
There are no files selected for viewing
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
50 changes: 50 additions & 0 deletions
50
src/main/java/org/nickelproject/nickel/collections/DistributedCollectionReducer.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.nickelproject.nickel.collections; | ||
|
||
import java.util.List; | ||
|
||
import org.nickelproject.nickel.dataflow.Reductor; | ||
import org.nickelproject.nickel.dataflow.Sink; | ||
|
||
import com.google.common.collect.Lists; | ||
|
||
public final class DistributedCollectionReducer<T> implements Sink<T, DistributedCollection<T>> { | ||
private static final long serialVersionUID = 1L; | ||
private final int nodeSize; | ||
|
||
public DistributedCollectionReducer(final int nodeSize) { | ||
this.nodeSize = nodeSize; | ||
} | ||
|
||
@Override | ||
public Reductor<T, DistributedCollection<T>> reductor() { | ||
return new DistributedCollectionReductor<T>(nodeSize); | ||
} | ||
|
||
private static final class DistributedCollectionReductor<T> | ||
implements Reductor<T, DistributedCollection<T>> { | ||
private final List<T> data = Lists.newArrayList(); | ||
private final List<DistributedCollection<T>> nodes = Lists.newArrayList(); | ||
private final int nodeSize; | ||
|
||
public DistributedCollectionReductor(final int nodeSize) { | ||
this.nodeSize = nodeSize; | ||
} | ||
|
||
@Override | ||
public void collect(final T pVal) { | ||
data.add(pVal); | ||
if (data.size() > nodeSize) { | ||
nodes.add(DistributedCollectionUtil.from(data)); | ||
data.clear(); | ||
} | ||
} | ||
|
||
@Override | ||
public DistributedCollection<T> reduce() { | ||
if (data.size() > 0) { | ||
nodes.add(DistributedCollectionUtil.from(data)); | ||
} | ||
return DistributedCollectionUtil.concat(nodes); | ||
} | ||
} | ||
} |
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
60 changes: 60 additions & 0 deletions
60
src/main/java/org/nickelproject/nickel/dataflow/Collector.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package org.nickelproject.nickel.dataflow; | ||
|
||
import com.google.common.base.Function; | ||
import com.google.common.base.Functions; | ||
|
||
public final class Collector<S, T, V> implements CollectorInterface<S, V> { | ||
private static final long serialVersionUID = 1L; | ||
private final Function<S, T> preFunctor; | ||
private final Reducer<T> reducer; | ||
private final Function<T, V> postFunctor; | ||
|
||
private Collector(final Function<S, T> preFunctor, final Reducer<T> reducer, | ||
final Function<T, V> postFunctor) { | ||
this.preFunctor = preFunctor; | ||
this.reducer = reducer; | ||
this.postFunctor = postFunctor; | ||
} | ||
|
||
public static <S, T, V> Collector<S, T, V> create(final Function<S, T> preFunctor, | ||
final Reducer<T> reducer, | ||
final Function<T, V> postFunctor) { | ||
return new Collector<S, T, V>(preFunctor, reducer, postFunctor); | ||
} | ||
|
||
public static <S, T, X, Y, V> Collector<S, X, V> create(final Function<S, T> preFunctor, | ||
final Collector<T, X, Y> collector, | ||
final Function<Y, V> postFunctor) { | ||
final Function<S, X> newPreFunction = Functions.compose(collector.preFunctor, preFunctor); | ||
final Function<X, V> newPostFunction = Functions.compose(postFunctor, collector.postFunctor); | ||
return new Collector<S, X, V>(newPreFunction, collector.reducer, newPostFunction); | ||
} | ||
|
||
@Override | ||
public Reductor<S, V> reductor() { | ||
return new ComposingReductor<S, T, V>(preFunctor, reducer.reductor(), postFunctor); | ||
} | ||
|
||
private static final class ComposingReductor<S, T, V> implements Reductor<S, V> { | ||
private final Function<S, T> preFunction; | ||
private final Function<T, V> postFunction; | ||
private final Reductor<T, T> reductor; | ||
|
||
ComposingReductor(final Function<S, T> preFunction, final Reductor<T, T> reductor, | ||
final Function<T, V> postFunction) { | ||
this.preFunction = preFunction; | ||
this.postFunction = postFunction; | ||
this.reductor = reductor; | ||
} | ||
|
||
@Override | ||
public void collect(final S pVal) { | ||
reductor.collect(preFunction.apply(pVal)); | ||
} | ||
|
||
@Override | ||
public V reduce() { | ||
return postFunction.apply(reductor.reduce()); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/org/nickelproject/nickel/dataflow/CollectorInterface.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright (c) 2013 Nigel Duffy | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.nickelproject.nickel.dataflow; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* Do not extend this class. | ||
* | ||
* @param <T> | ||
* @param <U> | ||
*/ | ||
interface CollectorInterface<T, U> extends Serializable { | ||
Reductor<T, U> reductor(); | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/org/nickelproject/nickel/dataflow/Partitionable.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package org.nickelproject.nickel.dataflow; | ||
|
||
public interface Partitionable<T> { | ||
Source<? extends Source<T>> partition(final int partitionSize); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright (c) 2013 Nigel Duffy | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.nickelproject.nickel.dataflow; | ||
|
||
/** | ||
* A Sink is a Collector that cannot be nested. | ||
* Usually, this should involve output to a file, terminal, database, etc. | ||
* | ||
* @param <T> | ||
* @param <U> | ||
*/ | ||
public interface Sink<T, U> extends CollectorInterface<T, U> { | ||
} |
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
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
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
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
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
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
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
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