@Test public void testRootTraversal() { LeftTupleImpl t0 = new LeftTupleImpl(); LeftTupleImpl t1 = new LeftTupleImpl(t0, null, (PropagationContext) null, true); LeftTupleImpl t2 = new LeftTupleImpl(t0, null, (PropagationContext) null, true); LeftTupleImpl t3 = new LeftTupleImpl(t0, null, (PropagationContext) null, true); LeftTupleImpl t1_1 = new LeftTupleImpl(t1, null, (PropagationContext) null, true); LeftTupleImpl t1_2 = new LeftTupleImpl(t1, null, (PropagationContext) null, true); LeftTupleImpl t1_1_1 = new LeftTupleImpl(t1_1, null, (PropagationContext) null, true); LeftTupleImpl t1_1_1_1 = new LeftTupleImpl(t1_1_1, null, (PropagationContext) null, true); LeftTupleImpl t1_1_1_2 = new LeftTupleImpl(t1_1_1, null, (PropagationContext) null, true); LeftTupleImpl t1_1_1_3 = new LeftTupleImpl(t1_1_1, null, (PropagationContext) null, true); LeftTupleImpl t1_2_1 = new LeftTupleImpl(t1_2, null, (PropagationContext) null, true); LeftTupleImpl t1_2_2 = new LeftTupleImpl(t1_2, null, (PropagationContext) null, true); LeftTupleImpl t1_2_2_1 = new LeftTupleImpl(t1_2_2, null, (PropagationContext) null, true); LeftTupleImpl t1_2_3 = new LeftTupleImpl(t1_2, null, (PropagationContext) null, true); LeftTupleImpl t2_1 = new LeftTupleImpl(t2, null, (PropagationContext) null, true); LeftTupleImpl t2_2 = new LeftTupleImpl(t2, null, (PropagationContext) null, true); LeftTupleImpl t2_3 = new LeftTupleImpl(t2, null, (PropagationContext) null, true); LeftTupleImpl t2_3_1 = new LeftTupleImpl(t2_3, null, (PropagationContext) null, true); LeftTupleImpl t2_3_1_1 = new LeftTupleImpl(t2_3_1, null, (PropagationContext) null, true); LeftTupleImpl t2_3_2 = new LeftTupleImpl(t2_3, null, (PropagationContext) null, true); LeftTupleImpl[] leafs = new LeftTupleImpl[] { t1_1_1_1, t1_1_1_2, t1_1_1_3, t1_2_1, t1_2_2_1, t1_2_3, t2_1, t2_2, t2_3_1_1, t2_3_2, t3 }; final List<LeftTuple> foundLeafs = new ArrayList<LeftTuple>(); TupleIterator iterator = new TupleIterator(); OnLeaf onLeaf = new OnLeaf() { public void execute(LeftTuple leafLeftTuple) { foundLeafs.add(leafLeftTuple); } }; iterator.traverse(t0, t0, onLeaf); assertEquals(leafs.length, foundLeafs.size()); assertEquals(Arrays.asList(leafs), foundLeafs); }
@Override public final void reduce(DatumWrapper<ITuple> key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException { try { Iterator<NullWritable> iterator = values.iterator(); tupleIterator.setIterator(iterator); ITuple currentTuple = key.datum(); ITuple previousKey = key.previousDatum(); int indexMismatch; if (firstRun) { indexMismatch = minDepth; firstRun = false; } else { indexMismatch = indexMismatch(previousKey, currentTuple, 0, maxDepth); if (indexMismatch < minDepth) { indexMismatch = minDepth; } for (int i = maxDepth; i >= indexMismatch; i--) { handler.onCloseGroup( i, groupSchema.getField(i).getName(), previousKey, this.context, collector); } } for (int i = indexMismatch; i <= maxDepth; i++) { handler.onOpenGroup( i, groupSchema.getField(i).getName(), currentTuple, this.context, collector); } // We set a view over the group fields to the method. if (isMultipleSources) { int schemaId = tupleMRConfig.getSchemaIdByName(currentTuple.getSchema().getName()); int[] indexTranslation = serInfo.getGroupSchemaIndexTranslation(schemaId); groupTuple.setContained(currentTuple, indexTranslation); } else { groupTuple.setContained(currentTuple); } handler.reduce(groupTuple, tupleIterator, this.context, collector); // This loop consumes the remaining elements that reduce didn't consume // The goal of this is to correctly set the last element in the next // onCloseGroup() call while (iterator.hasNext()) { iterator.next(); } } catch (TupleMRException e) { throw new RuntimeException(e); } }
@Test public void testMidTraversal() { LeftTupleImpl tm2 = new LeftTupleImpl(); LeftTupleImpl tm1 = new LeftTupleImpl(tm2, null, (PropagationContext) null, true); LeftTuple tm1_1 = new LeftTupleImpl( tm1, null, (PropagationContext) null, true); // this leaf will not be included LeftTupleImpl t0 = new LeftTupleImpl( tm1, null, (PropagationContext) null, true); // insert two nodes before our root traversal position LeftTupleImpl t1 = new LeftTupleImpl(t0, null, (PropagationContext) null, true); LeftTupleImpl t2 = new LeftTupleImpl(t0, null, (PropagationContext) null, true); LeftTupleImpl t3 = new LeftTupleImpl(t0, null, (PropagationContext) null, true); LeftTupleImpl t1_1 = new LeftTupleImpl(t1, null, (PropagationContext) null, true); LeftTupleImpl t1_2 = new LeftTupleImpl(t1, null, (PropagationContext) null, true); LeftTupleImpl t1_1_1 = new LeftTupleImpl(t1_1, null, (PropagationContext) null, true); LeftTupleImpl t1_1_1_1 = new LeftTupleImpl(t1_1_1, null, (PropagationContext) null, true); LeftTupleImpl t1_1_1_2 = new LeftTupleImpl(t1_1_1, null, (PropagationContext) null, true); LeftTupleImpl t1_1_1_3 = new LeftTupleImpl(t1_1_1, null, (PropagationContext) null, true); LeftTupleImpl t1_2_1 = new LeftTupleImpl(t1_2, null, (PropagationContext) null, true); LeftTupleImpl t1_2_2 = new LeftTupleImpl(t1_2, null, (PropagationContext) null, true); LeftTupleImpl t1_2_2_1 = new LeftTupleImpl(t1_2_2, null, (PropagationContext) null, true); LeftTupleImpl t1_2_3 = new LeftTupleImpl(t1_2, null, (PropagationContext) null, true); LeftTupleImpl t2_1 = new LeftTupleImpl(t2, null, (PropagationContext) null, true); LeftTupleImpl t2_2 = new LeftTupleImpl(t2, null, (PropagationContext) null, true); LeftTupleImpl t2_3 = new LeftTupleImpl(t2, null, (PropagationContext) null, true); LeftTupleImpl t2_3_1 = new LeftTupleImpl(t2_3, null, (PropagationContext) null, true); LeftTupleImpl t2_3_1_1 = new LeftTupleImpl(t2_3_1, null, (PropagationContext) null, true); LeftTupleImpl t2_3_2 = new LeftTupleImpl(t2_3, null, (PropagationContext) null, true); LeftTupleImpl[] leafs = new LeftTupleImpl[] { t1_1_1_1, t1_1_1_2, t1_1_1_3, t1_2_1, t1_2_2_1, t1_2_3, t2_1, t2_2, t2_3_1_1, t2_3_2, t3 }; final List<LeftTuple> foundLeafs = new ArrayList<LeftTuple>(); TupleIterator iterator = new TupleIterator(); OnLeaf onLeaf = new OnLeaf() { public void execute(LeftTuple leafLeftTuple) { foundLeafs.add(leafLeftTuple); } }; iterator.traverse(t0, t0, onLeaf); assertEquals(leafs.length, foundLeafs.size()); assertEquals(Arrays.asList(leafs), foundLeafs); }