⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 btreemaxscan.java

📁 derby database source code.good for you.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*   Derby - Class org.apache.derby.impl.store.access.btree.BTreeMaxScan   Copyright 1999, 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.store.access.btree;import org.apache.derby.iapi.reference.SQLState;import org.apache.derby.iapi.services.sanity.SanityManager;import org.apache.derby.iapi.error.StandardException; import org.apache.derby.iapi.store.access.RowUtil;import org.apache.derby.iapi.store.access.Qualifier;import org.apache.derby.iapi.store.access.ScanController;import org.apache.derby.iapi.store.raw.FetchDescriptor;import org.apache.derby.iapi.store.raw.Page;import org.apache.derby.iapi.store.raw.RecordHandle;import org.apache.derby.iapi.types.DataValueDescriptor;import org.apache.derby.iapi.types.RowLocation;import org.apache.derby.iapi.store.access.BackingStoreHashtable;/**  A b-tree scan controller corresponds to an instance of an open b-tree scan.  <P>  <B>Concurrency Notes<\B>  <P>  The concurrency rules are derived from OpenBTree.  <P>  @see OpenBTree**//**A BTreeScan implementation that provides the 90% solution to the max onbtree problem.  If the row is the last row in the btree it works veryefficiently.  This implementation will be removed once backward scan isfully functional.**/public class BTreeMaxScan extends BTreeScan{    /**************************************************************************     * Private methods of This class:     **************************************************************************     */    /**     * Fetch the maximum non-deleted row from the table.     *	 * @exception  StandardException  Standard exception policy.     **/    private boolean fetchMaxRowFromBeginning(    BTreeRowPosition        pos,    DataValueDescriptor[]   fetch_row)        throws StandardException	{        int                 ret_row_count     = 0;        RecordHandle        max_rh            = null;        // we need to scan until we hit the end of the table or until we        // run into a null.  Use this template to probe the "next" row so        // that if we need to finish, fetch_row will have the right value.        DataValueDescriptor[] check_row_template = new DataValueDescriptor[1];        check_row_template[0] = fetch_row[0].getClone();        FetchDescriptor check_row_desc = RowUtil.getFetchDescriptorConstant(1);        // reopen the scan for reading from the beginning of the table.        reopenScan(            (DataValueDescriptor[]) null,            ScanController.NA,            (Qualifier[][]) null,            (DataValueDescriptor[]) null,            ScanController.NA);        positionAtStartForForwardScan(pos);        // At this point:        // current_page is latched.  current_slot is the slot on current_page        // just before the "next" record this routine should process.        // loop through successive leaf pages and successive slots on those        // leaf pages.  Stop when either the last leaf is reached. At any        // time in the scan fetch_row will contain "last" non-deleted row        // seen.        boolean nulls_not_reached = true;		while ((pos.current_leaf != null) && nulls_not_reached)		{			while ((pos.current_slot + 1) < pos.current_leaf.page.recordCount())			{                // unlock the previous row if doing read.                if (pos.current_rh != null)                {                    this.getLockingPolicy().unlockScanRecordAfterRead(                        pos, init_forUpdate);                    // current_rh is used to track which row we need to unlock,                    // at this point no row needs to be unlocked.                    pos.current_rh = null;                }                // move scan current position forward.                pos.current_slot++;                this.stat_numrows_visited++;                // get current record handle for positioning but don't read                // data until we verify it is not deleted.  rh is needed                // for repositioning if we lose the latch.                RecordHandle rh =                     pos.current_leaf.page.fetchFromSlot(                        (RecordHandle) null,                        pos.current_slot,                         check_row_template,                        null,                        true);                // lock the row.                boolean latch_released =                    !this.getLockingPolicy().lockScanRow(                        this, this.getConglomerate(), pos,                        false,                         init_lock_fetch_desc,                        pos.current_lock_template,                        pos.current_lock_row_loc,                        false, init_forUpdate, lock_operation);                // special test to see if latch release code works                if (SanityManager.DEBUG)                {                    latch_released =                         test_errors(                            this,                            "BTreeMaxScan_fetchNextGroup", false,                             this.getLockingPolicy(),                            pos.current_leaf, latch_released);                }                // At this point we have successfully locked this record, so                // remember the record handle so that it can be unlocked if                // necessary.  If the above lock deadlocks, we will not try                // to unlock a lock we never got in close(), because current_rh                // is null until after the lock is granted.                pos.current_rh = rh;                if (latch_released)                {                    // lost latch on page in order to wait for row lock.                    // Because we have scan lock on page, we need only                    // call reposition() which will use the saved record                    // handle to reposition to the same spot on the page.                    // We don't have to search the                    // tree again, as we have the a scan lock on the page                    // which means the current_rh is valid to reposition on.                    if (!reposition(pos, false))                    {                        if (SanityManager.DEBUG)                        {                            // can't fail while with scan lock                            SanityManager.THROWASSERT(                                "can not fail holding scan lock.");                        }                    }                }                if (pos.current_leaf.page.isDeletedAtSlot(pos.current_slot))                {                    this.stat_numdeleted_rows_visited++;                    if (check_row_template[0].isNull())                    {                        // nulls sort at high end and are not to be returned                        // by max scan, so search is over, return whatever is                        // in fetch_row.                        nulls_not_reached = false;                        break;                    }                }                else if (check_row_template[0].isNull())                {                    nulls_not_reached = false;                    break;                }                else                 {                    pos.current_leaf.page.fetchFromSlot(                        pos.current_rh,                        pos.current_slot, fetch_row, init_fetchDesc,                        true);                    stat_numrows_qualified++;                    max_rh = pos.current_rh;                }			}            // Move position of the scan to slot 0 of the next page.  If there            // is no next page current_page will be null.            positionAtNextPage(pos);            this.stat_numpages_visited++;		}        // Reached last leaf of tree.        positionAtDoneScan(pos);        // we need to decrement when we stop scan at the end of the table.        this.stat_numpages_visited--;        return(max_rh != null);	}    /**************************************************************************     * Protected implementation of abstract methods of BTreeScan class:     **************************************************************************     */    /**     * disallow fetchRows on this scan type.     * <p>	 * @exception  StandardException  Standard exception policy.     **/    protected int fetchRows(    BTreeRowPosition        pos,    DataValueDescriptor[][] row_array,    RowLocation[]           rowloc_array,    BackingStoreHashtable   hash_table,    long                    max_rowcnt,    int[]                   key_column_numbers)        throws StandardException    {        throw StandardException.newException(                SQLState.BTREE_UNIMPLEMENTED_FEATURE);    }    /**     * Position scan at "start" position of the scan.     * <p>     * Positions the scan to the slot just after the first record to be      * returned from the backward scan.  Returns the start page latched, and      * sets "current_slot" to the slot number just right of the first slot     * to return.     * <p>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -