-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into jimmy-fixed-join
- Loading branch information
Showing
18 changed files
with
452 additions
and
31 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
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
101 changes: 101 additions & 0 deletions
101
textdb/textdb-dataflow/src/main/java/edu/uci/ics/textdb/dataflow/sink/TupleStreamSink.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,101 @@ | ||
package edu.uci.ics.textdb.dataflow.sink; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import edu.uci.ics.textdb.api.common.ITuple; | ||
import edu.uci.ics.textdb.api.common.Schema; | ||
import edu.uci.ics.textdb.api.dataflow.IOperator; | ||
import edu.uci.ics.textdb.api.dataflow.ISink; | ||
import edu.uci.ics.textdb.api.exception.TextDBException; | ||
import edu.uci.ics.textdb.common.constants.SchemaConstants; | ||
import edu.uci.ics.textdb.common.utils.Utils; | ||
|
||
/** | ||
* TupleStreamSink is a sink that can be used by the caller to get tuples one by one. | ||
* | ||
* @author Zuozhi Wang | ||
* | ||
*/ | ||
public class TupleStreamSink implements ISink { | ||
|
||
private IOperator inputOperator; | ||
|
||
private Schema inputSchema; | ||
private Schema outputSchema; | ||
|
||
private boolean isOpen = false;; | ||
|
||
/** | ||
* TupleStreamSink is a sink that can be used to | ||
* collect tuples to an in-memory list. | ||
* | ||
* TupleStreamSink removes the _id attribute and payload attribute | ||
* from the schema and each tuple. | ||
* | ||
*/ | ||
public TupleStreamSink() { | ||
} | ||
|
||
public void setInputOperator(IOperator inputOperator) { | ||
this.inputOperator = inputOperator; | ||
} | ||
|
||
public IOperator getInputOperator() { | ||
return this.inputOperator; | ||
} | ||
|
||
@Override | ||
public Schema getOutputSchema() { | ||
return outputSchema; | ||
} | ||
|
||
@Override | ||
public void open() throws TextDBException { | ||
if (isOpen) { | ||
return; | ||
} | ||
inputOperator.open(); | ||
inputSchema = inputOperator.getOutputSchema(); | ||
outputSchema = Utils.removeAttributeFromSchema(inputSchema, SchemaConstants._ID, SchemaConstants.PAYLOAD); | ||
isOpen = true; | ||
} | ||
|
||
@Override | ||
public void processTuples() throws TextDBException { | ||
return; | ||
} | ||
|
||
@Override | ||
public ITuple getNextTuple() throws TextDBException { | ||
ITuple tuple = inputOperator.getNextTuple(); | ||
if (tuple == null) { | ||
return null; | ||
} | ||
return Utils.removeFields(tuple, SchemaConstants._ID, SchemaConstants.PAYLOAD); | ||
} | ||
|
||
@Override | ||
public void close() throws TextDBException { | ||
if (! isOpen) { | ||
} | ||
inputOperator.close(); | ||
isOpen = false; | ||
} | ||
|
||
/** | ||
* Collects ALL the tuples to an in-memory list. | ||
* | ||
* @return a list of tuples | ||
* @throws TextDBException | ||
*/ | ||
public List<ITuple> collectAllTuples() throws TextDBException { | ||
ArrayList<ITuple> results = new ArrayList<>(); | ||
ITuple tuple; | ||
while ((tuple = inputOperator.getNextTuple()) != null) { | ||
results.add(Utils.removeFields(tuple, SchemaConstants._ID, SchemaConstants.PAYLOAD)); | ||
} | ||
return results; | ||
} | ||
|
||
} |
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
13 changes: 13 additions & 0 deletions
13
...flow/src/main/java/edu/uci/ics/textdb/plangen/operatorbuilder/TupleStreamSinkBuilder.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,13 @@ | ||
package edu.uci.ics.textdb.plangen.operatorbuilder; | ||
|
||
import java.util.Map; | ||
|
||
import edu.uci.ics.textdb.dataflow.sink.TupleStreamSink; | ||
|
||
public class TupleStreamSinkBuilder { | ||
|
||
public static TupleStreamSink buildTupleStreamSink(Map<String, String> operatorProperties) { | ||
return new TupleStreamSink(); | ||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
...b/textdb-dataflow/src/test/java/edu/uci/ics/textdb/dataflow/sink/TupleStreamSinkTest.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,72 @@ | ||
package edu.uci.ics.textdb.dataflow.sink; | ||
|
||
import java.io.FileNotFoundException; | ||
|
||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
|
||
import edu.uci.ics.textdb.api.common.Attribute; | ||
import edu.uci.ics.textdb.api.common.FieldType; | ||
import edu.uci.ics.textdb.api.common.ITuple; | ||
import edu.uci.ics.textdb.api.common.Schema; | ||
import edu.uci.ics.textdb.api.dataflow.IOperator; | ||
import edu.uci.ics.textdb.common.constants.SchemaConstants; | ||
import junit.framework.Assert; | ||
|
||
public class TupleStreamSinkTest { | ||
|
||
private TupleStreamSink tupleStreamSink; | ||
private IOperator inputOperator; | ||
private Schema inputSchema = new Schema( | ||
SchemaConstants._ID_ATTRIBUTE, new Attribute("content", FieldType.TEXT), SchemaConstants.PAYLOAD_ATTRIBUTE); | ||
|
||
@Before | ||
public void setUp() throws FileNotFoundException { | ||
inputOperator = Mockito.mock(IOperator.class); | ||
Mockito.when(inputOperator.getOutputSchema()).thenReturn(inputSchema); | ||
|
||
tupleStreamSink = new TupleStreamSink(); | ||
tupleStreamSink.setInputOperator(inputOperator); | ||
} | ||
|
||
@After | ||
public void cleanUp() { | ||
} | ||
|
||
@Test | ||
public void testOpen() throws Exception { | ||
tupleStreamSink.open(); | ||
// verify that inputOperator called open() method | ||
Mockito.verify(inputOperator).open(); | ||
// assert that the tuple stream sink removes the _ID and PAYLOAD attribute | ||
Assert.assertEquals(new Schema(new Attribute("content", FieldType.TEXT)), tupleStreamSink.getOutputSchema()); | ||
} | ||
|
||
@Test | ||
public void testClose() throws Exception { | ||
tupleStreamSink.close(); | ||
// verify that inputOperator called close() method | ||
Mockito.verify(inputOperator).close(); | ||
} | ||
|
||
@Test | ||
public void testGetNextTuple() throws Exception { | ||
ITuple sampleTuple = Mockito.mock(ITuple.class); | ||
Mockito.when(sampleTuple.toString()).thenReturn("Sample Tuple"); | ||
Mockito.when(sampleTuple.getSchema()).thenReturn(inputSchema); | ||
// Set the behavior for inputOperator, | ||
// first it returns some non-null tuple and second time it returns null | ||
Mockito.when(inputOperator.getNextTuple()).thenReturn(sampleTuple).thenReturn(null); | ||
|
||
tupleStreamSink.open(); | ||
tupleStreamSink.getNextTuple(); | ||
|
||
// Verify that input operator's getNextTuple is called | ||
Mockito.verify(inputOperator, Mockito.times(1)).getNextTuple(); | ||
|
||
tupleStreamSink.close(); | ||
} | ||
|
||
} |
Oops, something went wrong.