@Test public void testTruncate() throws Exception { ElementSymbol x = new ElementSymbol("x"); // $NON-NLS-1$ x.setType(DataTypeManager.DefaultDataClasses.INTEGER); List<ElementSymbol> schema = Arrays.asList(x); TupleBuffer tb = BufferManagerFactory.getStandaloneBufferManager() .createTupleBuffer(schema, "x", TupleSourceType.PROCESSOR); // $NON-NLS-1$ tb.setBatchSize(2); for (int i = 0; i < 5; i++) { tb.addTuple(Arrays.asList(1)); } TupleBatch batch = tb.getBatch(1); assertTrue(!batch.getTerminationFlag()); assertEquals(2, batch.getEndRow()); tb.close(); assertEquals(5, tb.getManagedRowCount()); tb.truncateTo(3); assertEquals(3, tb.getManagedRowCount()); assertEquals(3, tb.getRowCount()); batch = tb.getBatch(3); assertTrue(batch.getTerminationFlag()); tb.truncateTo(2); assertEquals(2, tb.getManagedRowCount()); assertEquals(2, tb.getRowCount()); batch = tb.getBatch(2); assertTrue(batch.getTerminationFlag()); }
@Test public void testForwardOnly() throws Exception { ElementSymbol x = new ElementSymbol("x"); // $NON-NLS-1$ x.setType(DataTypeManager.DefaultDataClasses.INTEGER); List<ElementSymbol> schema = Arrays.asList(x); TupleBuffer tb = BufferManagerFactory.getStandaloneBufferManager() .createTupleBuffer(schema, "x", TupleSourceType.PROCESSOR); // $NON-NLS-1$ tb.setForwardOnly(true); tb.addTuple(Arrays.asList(1)); TupleBatch batch = tb.getBatch(1); assertTrue(!batch.getTerminationFlag()); assertEquals(1, batch.getBeginRow()); try { tb.getBatch(1); fail("expected exception"); // $NON-NLS-1$ } catch (AssertionError e) { } tb.addTuple(Arrays.asList(1)); tb.close(); batch = tb.getBatch(2); assertTrue(batch.getTerminationFlag()); assertEquals(2, batch.getBeginRow()); }
/** * Get batch from child node Walk through each row of child batch Bind values to insertCommand * Execute insertCommand Update insertCount When no more data is available, output batch with * single row containing insertCount */ public TupleBatch nextBatchDirect() throws BlockedException, TeiidComponentException, TeiidProcessingException { while (phase == REQUEST_CREATION) { checkExitConditions(); /* If we don't have a batch to work, get the next */ if (currentBatch == null) { if (sourceDone) { phase = RESPONSE_PROCESSING; break; } currentBatch = getChildren()[0].nextBatch(); // can throw BlockedException sourceDone = currentBatch.getTerminationFlag(); this.batchRow = currentBatch.getBeginRow(); // normally we would want to skip a 0 sized batch, but it typically represents the terminal // batch // and for implicit temp tables we need to issue an empty insert if (currentBatch.getRowCount() == 0 && (!currentBatch.getTerminationFlag() || mode != Mode.ITERATOR)) { currentBatch = null; continue; } } int batchSize = currentBatch.getRowCount(); int requests = 1; switch (mode) { case ITERATOR: if (buffer == null) { buffer = getBufferManager() .createTupleBuffer(intoElements, getConnectionID(), TupleSourceType.PROCESSOR); } buffer.addTupleBatch(currentBatch, true); if (currentBatch.getTerminationFlag() && (buffer.getRowCount() != 0 || intoGroup.isImplicitTempGroupSymbol())) { Insert insert = new Insert(intoGroup, intoElements, null); buffer.close(); insert.setTupleSource(buffer.createIndexedTupleSource(true)); // Register insert command against source registerRequest(insert); } else { requests = 0; } break; case BATCH: // Register batched update command against source int endRow = currentBatch.getEndRow(); List<Command> rows = new ArrayList<Command>(endRow - batchRow); for (int rowNum = batchRow; rowNum <= endRow; rowNum++) { Insert insert = new Insert( intoGroup, intoElements, convertValuesToConstants(currentBatch.getTuple(rowNum), intoElements)); rows.add(insert); } registerRequest(new BatchedUpdateCommand(rows)); break; case SINGLE: batchSize = 1; // Register insert command against source // Defect 16036 - submit a new INSERT command to the DataManager. registerRequest( new Insert( intoGroup, intoElements, convertValuesToConstants(currentBatch.getTuple(batchRow), intoElements))); } this.batchRow += batchSize; if (batchRow > currentBatch.getEndRow()) { currentBatch = null; } this.requestsRegistered += requests; } checkExitConditions(); // End this node's work addBatchRow(Arrays.asList(insertCount)); terminateBatches(); return pullBatch(); }