use of org.teiid.common.buffer.BufferManager in project teiid by teiid.
the class TestLocalBufferService method testStateTransfer.
@Test
public void testStateTransfer() throws Exception {
BufferServiceImpl svc = new BufferServiceImpl();
svc.setDiskDirectory(UnitTestUtil.getTestScratchPath() + "/teiid/1");
svc.setUseDisk(true);
svc.start();
BufferManager mgr = svc.getBufferManager();
List<ElementSymbol> schema = new ArrayList<ElementSymbol>(2);
// $NON-NLS-1$
ElementSymbol es = new ElementSymbol("x");
es.setType(DataTypeManager.getDataTypeClass(DefaultDataTypes.STRING));
schema.add(es);
// $NON-NLS-1$
ElementSymbol es2 = new ElementSymbol("y");
es2.setType(DataTypeManager.getDataTypeClass(DefaultDataTypes.INTEGER));
schema.add(es2);
// $NON-NLS-1$
TupleBuffer buffer = mgr.createTupleBuffer(schema, "cached", TupleSourceType.FINAL);
buffer.setBatchSize(50);
buffer.setId("state_id");
for (int batch = 0; batch < 3; batch++) {
for (int row = 0; row < 50; row++) {
int val = (batch * 50) + row;
buffer.addTuple(Arrays.asList(new Object[] { "String" + val, new Integer(val) }));
}
}
buffer.close();
mgr.distributeTupleBuffer(buffer.getId(), buffer);
FileOutputStream fo = new FileOutputStream(UnitTestUtil.getTestScratchPath() + "/teiid/statetest");
((BufferManagerImpl) mgr).getState(buffer.getId(), fo);
fo.close();
svc.stop();
// now read back
BufferServiceImpl svc2 = new BufferServiceImpl();
svc2.setDiskDirectory(UnitTestUtil.getTestScratchPath() + "/teiid/2");
svc2.setUseDisk(true);
svc2.start();
BufferManagerImpl mgr2 = svc2.getBufferManager();
FileInputStream fis = new FileInputStream(UnitTestUtil.getTestScratchPath() + "/teiid/statetest");
mgr2.setState(buffer.getId(), fis);
fis.close();
String id = "state_id";
buffer = mgr2.getTupleBuffer(id);
for (int batch = 0; batch < 3; batch++) {
TupleBatch tb = buffer.getBatch((batch * 50) + 1);
List[] rows = tb.getAllTuples();
for (int row = 0; row < 50; row++) {
int val = (batch * 50) + row;
assertEquals("String" + val, rows[row].get(0));
assertEquals(val, rows[row].get(1));
}
}
svc2.stop();
}
Aggregations