use of org.knime.core.data.RowIterator in project knime-core by knime.
the class SorterNodeModelTest method testExecuteBufferedDataTableArrayExecutionContext.
/**
* Test method for {@link org.knime.base.node.preproc.sorter.SorterNodeModel#execute(org.knime.core.node.BufferedDataTable[], org.knime.core.node.ExecutionContext)}.
* @throws Exception
* @throws CanceledExecutionException
*/
@Test
public final void testExecuteBufferedDataTableArrayExecutionContext() throws CanceledExecutionException, Exception {
// try to sort a table with 1 entry
String[] columnNames = { "col1", "col2", "col3", "col4" };
DataType[] columnTypes = { DoubleCell.TYPE, StringCell.TYPE, IntCell.TYPE, DoubleCell.TYPE };
DataRow[] rows = new DataRow[1];
DataCell[] myRow = new DataCell[4];
myRow[0] = new DoubleCell(2.4325);
myRow[1] = new StringCell("Test");
myRow[2] = new IntCell(7);
myRow[3] = new DoubleCell(32432.324);
rows[0] = new DefaultRow(Integer.toString(1), myRow);
DataTable[] inputTable = { new DefaultTable(rows, columnNames, columnTypes) };
DataTable[] resultTable = { new DefaultTable(rows, columnNames, columnTypes) };
// set settings
String[] includeCols = { "col1", "col2", "col3", "col4" };
m_settings.addStringArray(SorterNodeModel.INCLUDELIST_KEY, includeCols);
boolean[] sortorder = { true, true, true, true };
m_settings.addBooleanArray(SorterNodeModel.SORTORDER_KEY, sortorder);
m_snm.loadValidatedSettingsFrom(m_settings);
resultTable = m_snm.execute(EXEC_CONTEXT.createBufferedDataTables(inputTable, EXEC_CONTEXT), EXEC_CONTEXT);
// test output
RowIterator rowIt = resultTable[0].iterator();
Assert.assertTrue(rowIt.hasNext());
Assert.assertEquals(rows[0], rowIt.next());
Assert.assertFalse(rowIt.hasNext());
m_snm.reset();
// *********************************************//
// try to sort a large array of DataRows
// In this case we generate a unit matrix
// *********************************************//
// start with a little one
int dimension = 50;
// *********************************************//
// set settings
includeCols = new String[dimension];
for (int i = 0; i < dimension; i++) {
includeCols[i] = "col" + i;
}
m_settings.addStringArray(SorterNodeModel.INCLUDELIST_KEY, includeCols);
sortorder = new boolean[dimension];
for (int i = 0; i < dimension; i++) {
sortorder[i] = true;
}
m_settings.addBooleanArray(SorterNodeModel.SORTORDER_KEY, sortorder);
DataTable[] inputTable2 = { generateUnitMatrixTable(dimension) };
m_snm.loadValidatedSettingsFrom(m_settings);
resultTable = m_snm.execute(EXEC_CONTEXT.createBufferedDataTables(inputTable2, EXEC_CONTEXT), EXEC_CONTEXT);
// test output (should have sorted all rows in reverse order)
rowIt = resultTable[0].iterator();
Assert.assertTrue(rowIt.hasNext());
int k = dimension - 1;
while (rowIt.hasNext()) {
RowKey rk = rowIt.next().getKey();
int ic = Integer.parseInt(rk.getString());
Assert.assertEquals(k, ic);
k--;
}
Assert.assertFalse(rowIt.hasNext());
m_snm.reset();
// *********************************************//
// try to sort a very large array of DataRows
// In this case we generate a unit matrix
// *********************************************//
// dimension 300 => 15,8 secs.
// dimension 500 => 49,7 secs.
dimension = 100;
// *********************************************//
// set settings
includeCols = new String[dimension];
for (int i = 0; i < dimension; i++) {
includeCols[i] = "col" + i;
}
m_settings.addStringArray(SorterNodeModel.INCLUDELIST_KEY, includeCols);
sortorder = new boolean[dimension];
for (int i = 0; i < dimension; i++) {
sortorder[i] = true;
}
m_settings.addBooleanArray(SorterNodeModel.SORTORDER_KEY, sortorder);
DataTable[] inputTable3 = { generateUnitMatrixTable(dimension) };
m_snm.loadValidatedSettingsFrom(m_settings);
resultTable = m_snm.execute(EXEC_CONTEXT.createBufferedDataTables(inputTable3, EXEC_CONTEXT), EXEC_CONTEXT);
// test output (should have sorted all rows in reverse order)
rowIt = resultTable[0].iterator();
Assert.assertTrue(rowIt.hasNext());
k = dimension - 1;
while (rowIt.hasNext()) {
RowKey rk = rowIt.next().getKey();
int ic = Integer.parseInt(rk.getString());
Assert.assertEquals(k, ic);
k--;
}
Assert.assertFalse(rowIt.hasNext());
m_snm.reset();
}
use of org.knime.core.data.RowIterator in project knime-core by knime.
the class DataContainerTest method testMemoryAlertAfterCloseWhileReading.
public final void testMemoryAlertAfterCloseWhileReading() throws Exception {
DataContainer container = new DataContainer(SPEC_STR_INT_DBL, true, Integer.MAX_VALUE, false);
int count = 100000;
for (RowIterator it = generateRows(count); it.hasNext(); ) {
container.addRowToTable(it.next());
}
container.close();
RowIterator tableIterator = container.getTable().iterator();
RowIterator it = generateRows(count);
int i;
for (i = 0; i < count / 2; i++) {
assertEquals(it.next(), tableIterator.next());
}
Buffer buffer = container.getBufferedTable().getBuffer();
synchronized (buffer) {
buffer.writeAllRowsFromListToFile();
}
for (; i < count; i++) {
assertEquals(it.next(), tableIterator.next());
}
}
use of org.knime.core.data.RowIterator in project knime-core by knime.
the class DataContainerTest method testRestoreIntoMemory.
// testBigFile()
/**
* Restoring into main memory.
* @see ContainerTable#restoreIntoMemory()
*/
public void testRestoreIntoMemory() {
// with these setting (50, 100) it will write an 250MB cache file
// (the latest data this value was checked: 31. August 2006...)
final int colCount = 50;
final int rowCount = 100;
String[] names = new String[colCount];
DataType[] types = new DataType[colCount];
for (int c = 0; c < colCount; c++) {
names[c] = "Column " + c;
switch(c % 3) {
case 0:
types[c] = DoubleCell.TYPE;
break;
case 1:
types[c] = StringCell.TYPE;
break;
case 2:
types[c] = IntCell.TYPE;
break;
default:
throw new InternalError();
}
}
DataTableSpec spec = new DataTableSpec(names, types);
names = null;
types = null;
DataContainer container = new DataContainer(spec, true, 0);
final ObjectToDataCellConverter conv = new ObjectToDataCellConverter();
final long seed = System.currentTimeMillis();
Random rand = new Random(seed);
for (int i = 0; i < rowCount; i++) {
DataRow row = createRandomRow(i, colCount, rand, conv);
container.addRowToTable(row);
row = null;
}
container.close();
assertTrue(container.getBufferedTable().getBuffer().usesOutFile());
final Throwable[] throwables = new Throwable[1];
final ContainerTable table = container.getBufferedTable();
table.restoreIntoMemory();
// different iterators restore the content, each of which one row
RowIterator[] its = new RowIterator[10];
for (int i = 0; i < its.length; i++) {
its[i] = table.iterator();
for (int count = 0; count < i + 1; count++) {
its[i].next();
}
}
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
int i = 0;
Random rand1 = new Random(seed);
for (RowIterator it = table.iterator(); it.hasNext(); i++) {
DataRow row1 = createRandomRow(i, colCount, rand1, conv);
DataRow row2 = it.next();
assertEquals(row1, row2);
}
assertEquals(i, rowCount);
} catch (Throwable t) {
throwables[0] = t;
}
}
};
// Runnable
// make two threads read the buffer (file) concurrently.
Thread t1 = new Thread(runnable);
Thread t2 = new Thread(runnable);
t1.start();
t2.start();
try {
// seems that the event dispatch thread must not release the
// reference to the table, otherwise it is (I guess!!) garbage
// collected: You comment these lines and see the error message.
t1.join();
t2.join();
} catch (InterruptedException ie) {
ie.printStackTrace();
fail();
}
if (throwables[0] != null) {
throw new RuntimeException(throwables[0]);
}
}
use of org.knime.core.data.RowIterator in project knime-core by knime.
the class DataContainerTest method testAsyncWriteLimits.
public void testAsyncWriteLimits() throws Exception {
Assume.assumeTrue(!DataContainer.SYNCHRONOUS_IO);
final int limit = Platform.ARCH_X86.equals(Platform.getOSArch()) ? 10 : 50;
Assert.assertEquals(limit, DataContainer.MAX_ASYNC_WRITE_THREADS);
RowIterator infinitIterator = generateRows(Integer.MAX_VALUE);
List<DataContainer> containerList = new ArrayList<DataContainer>();
try {
boolean isAsync;
do {
int activeCount = DataContainer.ASYNC_EXECUTORS.getActiveCount();
DataContainer c = new DataContainer(SPEC_STR_INT_DBL, true, 0);
c.addRowToTable(infinitIterator.next());
// no activeCount is incremented by one - so order of two lines is important.
containerList.add(c);
isAsync = activeCount <= limit;
assertEquals("unexpected async write behavior, active thread count is " + activeCount, isAsync, !c.isSynchronousWrite());
} while (isAsync);
} finally {
for (DataContainer c : containerList) {
c.close();
}
}
}
use of org.knime.core.data.RowIterator in project knime-core by knime.
the class DataContainerTest method generateRows.
private static RowIterator generateRows(final int count) {
return new RowIterator() {
private int m_index = 0;
@Override
public DataRow next() {
DefaultRow r = new DefaultRow(RowKey.createRowKey(m_index), new StringCell("String " + m_index), new IntCell(m_index), new DoubleCell(m_index));
m_index++;
return r;
}
@Override
public boolean hasNext() {
return m_index < count;
}
};
}
Aggregations