Updated Branches: refs/heads/master e8b9d4b2a -> 438e36a38
CRUNCH-142 Delegate cleanup on decorator FilterFns Project: http://git-wip-us.apache.org/repos/asf/incubator-crunch/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-crunch/commit/438e36a3 Tree: http://git-wip-us.apache.org/repos/asf/incubator-crunch/tree/438e36a3 Diff: http://git-wip-us.apache.org/repos/asf/incubator-crunch/diff/438e36a3 Branch: refs/heads/master Commit: 438e36a3801a7d71d08c5422cea5fb6ae7bb0682 Parents: e8b9d4b Author: Gabriel Reid <[email protected]> Authored: Mon Jan 14 21:16:19 2013 +0100 Committer: Gabriel Reid <[email protected]> Committed: Tue Jan 15 06:52:41 2013 +0100 ---------------------------------------------------------------------- .../src/main/java/org/apache/crunch/FilterFn.java | 38 +++++++- .../src/test/java/org/apache/crunch/AndFnTest.java | 77 ++++++++++++++ .../src/test/java/org/apache/crunch/NotFnTest.java | 72 +++++++++++++ .../src/test/java/org/apache/crunch/OrFnTest.java | 78 +++++++++++++++ 4 files changed, 262 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-crunch/blob/438e36a3/crunch/src/main/java/org/apache/crunch/FilterFn.java ---------------------------------------------------------------------- diff --git a/crunch/src/main/java/org/apache/crunch/FilterFn.java b/crunch/src/main/java/org/apache/crunch/FilterFn.java index 010afed..d635b66 100644 --- a/crunch/src/main/java/org/apache/crunch/FilterFn.java +++ b/crunch/src/main/java/org/apache/crunch/FilterFn.java @@ -42,7 +42,20 @@ public abstract class FilterFn<T> extends DoFn<T, T> { emitter.emit(input); } } - + + @Override + public final void cleanup(Emitter<T> emitter) { + cleanup(); + } + + /** + * Called during the cleanup of the MapReduce job this {@code FilterFn} is + * associated with. Subclasses may override this method to do appropriate + * cleanup. + */ + public void cleanup() { + } + @Override public float scaleFactor() { return 0.5f; @@ -80,6 +93,13 @@ public abstract class FilterFn<T> extends DoFn<T, T> { } initialize(); } + + @Override + public void cleanup() { + for (FilterFn<S> fn : fns) { + fn.cleanup(); + } + } @Override public boolean accept(S input) { @@ -90,7 +110,7 @@ public abstract class FilterFn<T> extends DoFn<T, T> { } return true; } - + @Override public float scaleFactor() { float scaleFactor = 1.0f; @@ -133,6 +153,13 @@ public abstract class FilterFn<T> extends DoFn<T, T> { } initialize(); } + + @Override + public void cleanup() { + for (FilterFn<S> fn : fns) { + fn.cleanup(); + } + } @Override public boolean accept(S input) { @@ -143,7 +170,7 @@ public abstract class FilterFn<T> extends DoFn<T, T> { } return false; } - + @Override public float scaleFactor() { float scaleFactor = 0.0f; @@ -184,6 +211,11 @@ public abstract class FilterFn<T> extends DoFn<T, T> { } @Override + public void cleanup() { + base.cleanup(); + } + + @Override public boolean accept(S input) { return !base.accept(input); } http://git-wip-us.apache.org/repos/asf/incubator-crunch/blob/438e36a3/crunch/src/test/java/org/apache/crunch/AndFnTest.java ---------------------------------------------------------------------- diff --git a/crunch/src/test/java/org/apache/crunch/AndFnTest.java b/crunch/src/test/java/org/apache/crunch/AndFnTest.java new file mode 100644 index 0000000..4b00874 --- /dev/null +++ b/crunch/src/test/java/org/apache/crunch/AndFnTest.java @@ -0,0 +1,77 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.crunch; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import org.apache.crunch.FilterFn.AndFn; +import org.apache.hadoop.mapreduce.TaskInputOutputContext; +import org.junit.Before; +import org.junit.Test; + +public class AndFnTest { + + private FilterFn<Integer> fnA; + private FilterFn<Integer> fnB; + private AndFn<Integer> andFn; + + @Before + public void setUp() { + fnA = mock(FilterFn.class); + fnB = mock(FilterFn.class); + andFn = new AndFn(fnA, fnB); + } + + @Test + public void testSetContext() { + TaskInputOutputContext<?, ?, ?, ?> context = mock(TaskInputOutputContext.class); + andFn.setContext(context); + + verify(fnA).setContext(context); + verify(fnB).setContext(context); + } + + @Test + public void testAccept_False() { + when(fnA.accept(1)).thenReturn(true); + when(fnB.accept(1)).thenReturn(false); + + assertFalse(andFn.accept(1)); + } + + @Test + public void testAccept_True() { + when(fnA.accept(1)).thenReturn(true); + when(fnB.accept(1)).thenReturn(true); + + assertTrue(andFn.accept(1)); + } + + @Test + public void testCleanup() { + andFn.cleanup(mock(Emitter.class)); + + verify(fnA).cleanup(); + verify(fnB).cleanup(); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-crunch/blob/438e36a3/crunch/src/test/java/org/apache/crunch/NotFnTest.java ---------------------------------------------------------------------- diff --git a/crunch/src/test/java/org/apache/crunch/NotFnTest.java b/crunch/src/test/java/org/apache/crunch/NotFnTest.java new file mode 100644 index 0000000..8af17a2 --- /dev/null +++ b/crunch/src/test/java/org/apache/crunch/NotFnTest.java @@ -0,0 +1,72 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.crunch; + +import static org.junit.Assert.*; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import org.apache.crunch.FilterFn.NotFn; +import org.apache.hadoop.mapreduce.TaskInputOutputContext; +import org.junit.Before; +import org.junit.Test; + +public class NotFnTest { + + private FilterFn<Integer> base; + private NotFn<Integer> notFn; + + @Before + public void setUp() { + base = mock(FilterFn.class); + notFn = new NotFn(base); + } + + @Test + public void testSetContext() { + TaskInputOutputContext<?, ?, ?, ?> context = mock(TaskInputOutputContext.class); + + notFn.setContext(context); + + verify(base).setContext(context); + } + + @Test + public void testAccept_True() { + when(base.accept(1)).thenReturn(true); + + assertFalse(notFn.accept(1)); + } + + @Test + public void testAccept_False() { + when(base.accept(1)).thenReturn(false); + + assertTrue(notFn.accept(1)); + } + + @Test + public void testCleanupEmitterOfT() { + notFn.cleanup(mock(Emitter.class)); + + verify(base).cleanup(); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-crunch/blob/438e36a3/crunch/src/test/java/org/apache/crunch/OrFnTest.java ---------------------------------------------------------------------- diff --git a/crunch/src/test/java/org/apache/crunch/OrFnTest.java b/crunch/src/test/java/org/apache/crunch/OrFnTest.java new file mode 100644 index 0000000..fde2376 --- /dev/null +++ b/crunch/src/test/java/org/apache/crunch/OrFnTest.java @@ -0,0 +1,78 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.crunch; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import org.apache.crunch.FilterFn.OrFn; +import org.apache.hadoop.mapreduce.TaskInputOutputContext; +import org.junit.Before; +import org.junit.Test; + +public class OrFnTest { + + private FilterFn<Integer> fnA; + private FilterFn<Integer> fnB; + private OrFn<Integer> orFn; + + @Before + public void setUp() { + fnA = mock(FilterFn.class); + fnB = mock(FilterFn.class); + orFn = new OrFn(fnA, fnB); + } + + @Test + public void testSetContext() { + TaskInputOutputContext<?, ?, ?, ?> context = mock(TaskInputOutputContext.class); + + orFn.setContext(context); + + verify(fnA).setContext(context); + verify(fnB).setContext(context); + } + + @Test + public void testAccept_True() { + when(fnA.accept(1)).thenReturn(false); + when(fnB.accept(1)).thenReturn(true); + + assertTrue(orFn.accept(1)); + } + + @Test + public void testAccept_False() { + when(fnA.accept(1)).thenReturn(false); + when(fnB.accept(1)).thenReturn(false); + + assertFalse(orFn.accept(1)); + } + + @Test + public void testCleanupEmitterOfT() { + orFn.cleanup(mock(Emitter.class)); + + verify(fnA).cleanup(); + verify(fnB).cleanup(); + } + +}
