📄 temporaryrowholderresultset.java
字号:
/* Derby - Class org.apache.derby.impl.sql.execute.TemporaryRowHolderResultSet Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */package org.apache.derby.impl.sql.execute;import org.apache.derby.iapi.services.sanity.SanityManager;import org.apache.derby.iapi.error.StandardException;import org.apache.derby.iapi.store.access.ConglomerateController;import org.apache.derby.iapi.store.access.ScanController;import org.apache.derby.iapi.store.access.TransactionController;import org.apache.derby.iapi.reference.SQLState;import org.apache.derby.iapi.sql.execute.CursorResultSet;import org.apache.derby.iapi.sql.execute.ExecRow;import org.apache.derby.iapi.sql.execute.ExecutionContext;import org.apache.derby.iapi.sql.execute.NoPutResultSet;import org.apache.derby.iapi.sql.execute.ExecutionFactory;import org.apache.derby.iapi.sql.execute.TargetResultSet;import org.apache.derby.iapi.sql.ResultDescription;import org.apache.derby.iapi.sql.ResultSet;import org.apache.derby.iapi.sql.Row;import org.apache.derby.iapi.types.DataValueDescriptor;import org.apache.derby.iapi.types.RowLocation;import org.apache.derby.iapi.sql.execute.ExecIndexRow;import org.apache.derby.iapi.types.SQLLongint;import org.apache.derby.iapi.services.io.FormatableBitSet;import java.sql.Timestamp;/** * A result set to scan temporary row holders. Ultimately, this * may be returned to users, hence the extra junk from the ResultSet * interface. * * @author jamie */public class TemporaryRowHolderResultSet implements CursorResultSet, NoPutResultSet, Cloneable{ private ExecRow[] rowArray; private int numRowsOut; private ScanController scan; private TransactionController tc; private boolean isOpen; private boolean finished; private ExecRow currentRow; private ResultDescription resultDescription; private ExecutionFactory ef; private boolean isAppendable = false; private long positionIndexConglomId; private boolean isVirtualMemHeap; private boolean currRowFromMem; private TemporaryRowHolderImpl holder; // the following is used by position based scan, as well as virtual memory style heap ConglomerateController heapCC; private RowLocation baseRowLocation; /** * Constructor * * @param tc the xact controller * @param rowArray the row array * @param lastArraySlot the last array slot in use * @param conglomId 0 if no conglom used * @param resultDescription value returned by getResultDescription() */ public TemporaryRowHolderResultSet ( TransactionController tc, ExecRow[] rowArray, ResultDescription resultDescription, boolean isVirtualMemHeap, TemporaryRowHolderImpl holder ) { this(tc, rowArray, resultDescription, isVirtualMemHeap, false, 0, holder); } /** * Constructor * * @param tc the xact controller * @param rowArray the row array * @param lastArraySlot the last array slot in use * @param conglomId 0 if no conglom used * @param resultDescription value returned by getResultDescription() * @param isAppendable true,if we can insert rows after this result is created * @param positionIndexConglomId conglomId of the index which has order rows * are inserted and their row location */ public TemporaryRowHolderResultSet ( TransactionController tc, ExecRow[] rowArray, ResultDescription resultDescription, boolean isVirtualMemHeap, boolean isAppendable, long positionIndexConglomId, TemporaryRowHolderImpl holder ) { this.tc = tc; this.rowArray = rowArray; this.resultDescription = resultDescription; this.numRowsOut = 0; isOpen = false; finished = false; this.isVirtualMemHeap = isVirtualMemHeap; this.isAppendable = isAppendable; this.positionIndexConglomId = positionIndexConglomId; if (SanityManager.DEBUG) { SanityManager.ASSERT(rowArray != null, "rowArray is null"); SanityManager.ASSERT(rowArray.length > 0, "rowArray has no elements, need at least one"); } this.holder = holder; } /** * Reset the exec row array and reinitialize * * @param rowArray the row array */ public void reset(ExecRow[] rowArray) { this.rowArray = rowArray; this.numRowsOut = 0; isOpen = false; finished = false; if (SanityManager.DEBUG) { SanityManager.ASSERT(rowArray != null, "rowArray is null"); SanityManager.ASSERT(rowArray.length > 0, "rowArray has no elements, need at least one"); } } /** * postion scan to start from after where we stopped earlier */ public void reStartScan(long currentConglomId, long pconglomId) throws StandardException { if(isAppendable) { holder.CID = currentConglomId; positionIndexConglomId = pconglomId; setupPositionBasedScan(numRowsOut); }else { numRowsOut--; } } /** * Whip up a new Temp ResultSet that has a single * row, the current row of this result set. * * @param tc the xact controller * @param rs the result set * * @return a single row result set * * @exception StandardException on error */ public static TemporaryRowHolderResultSet getNewRSOnCurrentRow ( TransactionController tc, CursorResultSet rs ) throws StandardException { TemporaryRowHolderImpl singleRow = new TemporaryRowHolderImpl(tc, null, rs.getResultDescription()); singleRow.insert(rs.getCurrentRow()); return (TemporaryRowHolderResultSet) singleRow.getResultSet(); } ///////////////////////////////////////////////////////// // // NoPutResultSet // ///////////////////////////////////////////////////////// /** * Mark the ResultSet as the topmost one in the ResultSet tree. * Useful for closing down the ResultSet on an error. * * @return Nothing. */ public void markAsTopResultSet() { } /** * Open the scan and evaluate qualifiers and the like. * For us, there are no qualifiers, this is really a * noop. */ public void openCore() throws StandardException { this.numRowsOut = 0; isOpen = true; currentRow = null; if(isAppendable) setupPositionBasedScan(numRowsOut); } /** * Reopen the scan. Typically faster than open()/close() * * @exception StandardException on error */ public void reopenCore() throws StandardException { numRowsOut = 0; isOpen = true; currentRow = null; if(isAppendable) { setupPositionBasedScan(numRowsOut); return; } if (scan != null) { scan.reopenScan( (DataValueDescriptor[]) null, // start key value 0, // start operator null, // qualifier (DataValueDescriptor[]) null, // stop key value 0); // stop operator } } /** * Get the next row. * * @return the next row, or null if none * * @exception StandardException on error */ public ExecRow getNextRowCore() throws StandardException { if (!isOpen) { return (ExecRow)null; } if(isAppendable) { return getNextAppendedRow() ; } if (isVirtualMemHeap && holder.lastArraySlot >= 0) { numRowsOut++; currentRow = rowArray[holder.lastArraySlot]; currRowFromMem = true; return currentRow; } else if (numRowsOut++ <= holder.lastArraySlot) { currentRow = rowArray[numRowsOut-1]; return currentRow; } if (holder.CID == 0) { return (ExecRow)null; } /* ** Advance in the temporary conglomerate */ if (scan == null) { scan = tc.openScan( holder.CID, false, // hold 0, // open read only TransactionController.MODE_TABLE, TransactionController.ISOLATION_SERIALIZABLE, (FormatableBitSet) null, (DataValueDescriptor[]) null, // start key value 0, // start operator null, // qualifier (DataValueDescriptor[]) null, // stop key value 0); // stop operator } else if (isVirtualMemHeap && holder.state == TemporaryRowHolderImpl.STATE_INSERT) { holder.state = TemporaryRowHolderImpl.STATE_DRAIN; scan.reopenScan( (DataValueDescriptor[]) null, // start key value 0, // start operator null, // qualifier (DataValueDescriptor[]) null, // stop key value 0); // stop operator } if (scan.next()) { currentRow = rowArray[0].getNewNullRow(); scan.fetch(currentRow.getRowArray()); currRowFromMem = false; return currentRow; } return (ExecRow)null; } public void deleteCurrentRow() throws StandardException { if (SanityManager.DEBUG) { SanityManager.ASSERT(isVirtualMemHeap, "deleteCurrentRow is not implemented"); } if (currRowFromMem) { if (holder.lastArraySlot > 0) // 0 is kept for template rowArray[holder.lastArraySlot] = null; // erase reference holder.lastArraySlot--; } else { if (baseRowLocation == null) baseRowLocation = scan.newRowLocationTemplate(); scan.fetchLocation(baseRowLocation); if(heapCC == null) { heapCC = tc.openConglomerate( holder.CID, false, TransactionController.OPENMODE_FORUPDATE, TransactionController.MODE_TABLE, TransactionController.ISOLATION_SERIALIZABLE); } heapCC.delete(baseRowLocation); } } //following variables are specific to the position based scans. DataValueDescriptor[] indexRow; ScanController indexsc; //open the scan of the temporary heap and the position index private void setupPositionBasedScan(long position) throws StandardException { //incase nothing is inserted yet into the temporary row holder if(holder.CID ==0) return; if(heapCC == null)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -