Search in sources :

Example 11 with ContainerHandle

use of org.apache.derby.iapi.store.raw.ContainerHandle in project derby by apache.

the class HeapCostController method init.

/* Private/Protected methods of This class: */
/**
 * Initialize the cost controller.
 * <p>
 * Let super.init() do it's work and then get the initial stats about the
 * table from raw store.
 *
 * @exception  StandardException  Standard exception policy.
 */
public void init(OpenConglomerate open_conglom) throws StandardException {
    super.init(open_conglom);
    ContainerHandle container = open_conglom.getContainer();
    // look up costs from raw store.
    num_rows = container.getEstimatedRowCount(/*unused flag*/
    0);
    // because of these 0 row based plans.
    if (num_rows == 0)
        num_rows = 1;
    // eliminate the allocation page from the count.
    num_pages = container.getEstimatedPageCount(/* unused flag */
    0);
    Properties prop = new Properties();
    prop.put(Property.PAGE_SIZE_PARAMETER, "");
    container.getContainerProperties(prop);
    page_size = Integer.parseInt(prop.getProperty(Property.PAGE_SIZE_PARAMETER));
    row_size = (num_pages * page_size / num_rows);
    return;
}
Also used : Properties(java.util.Properties) ContainerHandle(org.apache.derby.iapi.store.raw.ContainerHandle)

Example 12 with ContainerHandle

use of org.apache.derby.iapi.store.raw.ContainerHandle in project derby by apache.

the class D_DiagnosticUtil method diag_dump_page.

/**
 * Dump raw contents of a page.
 * <p>
 * A utility routine that can be called from an ij session that will
 * dump the raw contents of a page, in the raw store dump format.
 *
 * @param db_name       name of the database
 * @param segmentid     segmentid of the table (usually 0)
 * @param containerid   containerid of the table (not conglomid)
 * @param pagenumber    pagenumber of page to dump.
 */
public static void diag_dump_page(String db_name, long segmentid, long containerid, long pagenumber) {
    Transaction xact = null;
    try {
        Object module = getModuleFromDbName(db_name);
        RawStoreFactory store_module = (RawStoreFactory) findServiceModule(module, RawStoreFactory.MODULE);
        xact = store_module.startInternalTransaction(FileContainer.getContextService().getCurrentContextManager());
        ContainerKey id = new ContainerKey(segmentid, containerid);
        ContainerHandle container = xact.openContainer(id, ContainerHandle.MODE_READONLY);
        Page page = container.getPage(pagenumber);
        if (page != null) {
            System.out.println(page.toString());
            page.unlatch();
        } else {
            System.out.println("page " + pagenumber + " not found");
        }
        xact.abort();
        xact.close();
        xact = null;
    } catch (StandardException se) {
        se.printStackTrace();
    } finally {
        if (xact != null) {
            try {
                xact.abort();
                xact.close();
            } catch (StandardException se) {
            }
        }
    }
}
Also used : StandardException(org.apache.derby.shared.common.error.StandardException) Transaction(org.apache.derby.iapi.store.raw.Transaction) Page(org.apache.derby.iapi.store.raw.Page) ContainerKey(org.apache.derby.iapi.store.raw.ContainerKey) RawStoreFactory(org.apache.derby.iapi.store.raw.RawStoreFactory) ContainerHandle(org.apache.derby.iapi.store.raw.ContainerHandle)

Example 13 with ContainerHandle

use of org.apache.derby.iapi.store.raw.ContainerHandle in project derby by apache.

the class ReclaimSpaceHelper method reclaimSpace.

/**
 *		Reclaim space based on work.
 */
public static int reclaimSpace(BaseDataFileFactory dataFactory, RawTransaction tran, ReclaimSpace work) throws StandardException {
    if (work.reclaimWhat() == ReclaimSpace.CONTAINER)
        return reclaimContainer(dataFactory, tran, work);
    // Else, not reclaiming container. Get a no-wait shared lock on the
    // container regardless of how the user transaction had the
    // container opened.
    LockingPolicy container_rlock = tran.newLockingPolicy(LockingPolicy.MODE_RECORD, TransactionController.ISOLATION_SERIALIZABLE, true);
    if (SanityManager.DEBUG)
        SanityManager.ASSERT(container_rlock != null);
    ContainerHandle containerHdl = openContainerNW(tran, container_rlock, work.getContainerId());
    if (containerHdl == null) {
        tran.abort();
        if (SanityManager.DEBUG) {
            if (SanityManager.DEBUG_ON(DaemonService.DaemonTrace)) {
                SanityManager.DEBUG(DaemonService.DaemonTrace, " aborted " + work + " because container is locked or dropped");
            }
        }
        if (// retry this for serveral times
        work.incrAttempts() < 3) // it is however, unlikely that three tries will be
        // enough because there is no delay between retries.
        // See DERBY-4059 and DERBY-4055 for details.
        {
            return Serviceable.REQUEUE;
        } else {
            if (SanityManager.DEBUG) {
                if (SanityManager.DEBUG_ON(DaemonService.DaemonTrace)) {
                    SanityManager.DEBUG(DaemonService.DaemonTrace, "  gave up after 3 tries to get container lock " + work);
                }
            }
            return Serviceable.DONE;
        }
    }
    if (work.reclaimWhat() == ReclaimSpace.PAGE) {
        // Reclaiming a page - called by undo of insert which purged the
        // last row off an overflow page. It is safe to reclaim the page
        // without first locking the head row because unlike post commit
        // work, this is post abort work.  Abort is guarenteed to happen
        // and to happen only once, if at all.
        Page p = containerHdl.getPageNoWait(work.getPageId().getPageNumber());
        if (p != null)
            containerHdl.removePage(p);
        tran.commit();
        return Serviceable.DONE;
    }
    // We are reclaiming row space or long column.
    // First get an xlock on the head row piece.
    RecordHandle headRecord = work.getHeadRowHandle();
    if (!container_rlock.lockRecordForWrite(tran, headRecord, false, /* not insert */
    false)) {
        // cannot get the row lock, retry
        tran.abort();
        if (work.incrAttempts() < 3) {
            return Serviceable.REQUEUE;
        } else {
            if (SanityManager.DEBUG) {
                if (SanityManager.DEBUG_ON(DaemonService.DaemonTrace)) {
                    SanityManager.DEBUG(DaemonService.DaemonTrace, "  gave up after 3 tries to get row lock " + work);
                }
            }
            return Serviceable.DONE;
        }
    }
    if (work.reclaimWhat() == ReclaimSpace.ROW_RESERVE) {
        // This row may benefit from compaction.
        containerHdl.compactRecord(headRecord);
        // This work is being done - post commit, there is no user
        // transaction that depends on the commit being sync'd.  It is safe
        // to commitNoSync() This do as one of 2 things will happen:
        // 
        // 1) if any data page associated with this transaction is
        // moved from cache to disk, then the transaction log
        // must be sync'd to the log record for that change and
        // all log records including the commit of this xact must
        // be sync'd before returning.
        // 
        // 2) if the data page is never written then the log record
        // for the commit may never be written, and the xact will
        // never make to disk.  This is ok as no subsequent action
        // depends on this operation being committed.
        // 
        tran.commitNoSync(Transaction.RELEASE_LOCKS);
        return Serviceable.DONE;
    } else {
        if (SanityManager.DEBUG)
            SanityManager.ASSERT(work.reclaimWhat() == ReclaimSpace.COLUMN_CHAIN);
        // Reclaiming a long column chain due to update.  The long column
        // chain being reclaimed is the before image of the update
        // operation.
        // 
        long headPageId = ((PageKey) headRecord.getPageId()).getPageNumber();
        // DERBY-4050 - we wait for the page so we don't have to retry.
        // prior to the 4050 fix, we called getPageNoWait and just
        // retried 3 times.  This left unreclaimed space if we were
        // not successful after three tries.
        StoredPage headRowPage = (StoredPage) containerHdl.getPage(headPageId);
        if (headRowPage == null) {
            if (SanityManager.DEBUG) {
                if (SanityManager.DEBUG_ON(DaemonService.DaemonTrace)) {
                    SanityManager.DEBUG(DaemonService.DaemonTrace, "gave up because hadRowPage was null" + work);
                }
            }
            tran.abort();
            return Serviceable.DONE;
        }
        try {
            headRowPage.removeOrphanedColumnChain(work, containerHdl);
        } finally {
            headRowPage.unlatch();
        }
        // This work is being done - post commit, there is no user
        // transaction that depends on the commit being sync'd.  It is safe
        // to commitNoSync() This do as one of 2 things will happen:
        // 
        // 1) if any data page associated with this transaction is
        // moved from cache to disk, then the transaction log
        // must be sync'd to the log record for that change and
        // all log records including the commit of this xact must
        // be sync'd before returning.
        // 
        // 2) if the data page is never written then the log record
        // for the commit may never be written, and the xact will
        // never make to disk.  This is ok as no subsequent action
        // depends on this operation being committed.
        // 
        tran.commitNoSync(Transaction.RELEASE_LOCKS);
        return Serviceable.DONE;
    }
}
Also used : PageKey(org.apache.derby.iapi.store.raw.PageKey) RecordHandle(org.apache.derby.iapi.store.raw.RecordHandle) Page(org.apache.derby.iapi.store.raw.Page) BasePage(org.apache.derby.impl.store.raw.data.BasePage) LockingPolicy(org.apache.derby.iapi.store.raw.LockingPolicy) RawContainerHandle(org.apache.derby.iapi.store.raw.data.RawContainerHandle) ContainerHandle(org.apache.derby.iapi.store.raw.ContainerHandle)

Example 14 with ContainerHandle

use of org.apache.derby.iapi.store.raw.ContainerHandle in project derby by apache.

the class GenericController method setEstimatedRowCount.

/**
 * Set the total estimated number of rows in the container.
 * <p>
 * Often, after a scan, the client of RawStore has a much better estimate
 * of the number of rows in the container than what store has.  For
 * instance if we implement some sort of update statistics command, or
 * just after a create index a complete scan will have been done of the
 * table.  In this case this interface allows the client to set the
 * estimated row count for the container, and store will use that number
 * for all future references.
 * <p>
 * This call is currently only supported on Heap conglomerates, it
 * will throw an exception if called on btree conglomerates.
 *
 * @param count the estimated number of rows in the container.
 *
 * @exception  StandardException  Standard exception policy.
 */
public void setEstimatedRowCount(long count) throws StandardException {
    ContainerHandle container = open_conglom.getContainer();
    if (container == null)
        open_conglom.reopen();
    // the container could still be null if a DROP TABLE or TRUNCATE TABLE
    // has removed the old conglomerate
    container = open_conglom.getContainer();
    if (container != null) {
        container.setEstimatedRowCount(count, /* unused flag */
        0);
    }
}
Also used : ContainerHandle(org.apache.derby.iapi.store.raw.ContainerHandle)

Example 15 with ContainerHandle

use of org.apache.derby.iapi.store.raw.ContainerHandle in project derby by apache.

the class BaseDataFileFactory method backupDataFiles.

/*
	 *  Find all the all the containers stored in the seg0 directory and 
	 *  backup each container to the specified backup location.
	 */
public void backupDataFiles(Transaction rt, File backupDir) throws StandardException {
    /*
		 * List of containers that needs to be backed up are identified by 
		 * simply reading the list of files in seg0. 
		 * All container that are created after the container list is created 
		 * when backup is in progress are recreated on restore using the
		 * transaction log.
		 */
    String[] files = getContainerNames();
    if (files != null) {
        // No user visible locks are acquired to backup the database. A stable backup
        // is made by latching the pages and internal synchronization
        // mechanisms.
        LockingPolicy lockPolicy = rt.newLockingPolicy(LockingPolicy.MODE_NONE, TransactionController.ISOLATION_NOLOCK, false);
        long segmentId = 0;
        // loop through all the files in seg0 and backup all valid containers.
        for (int f = files.length - 1; f >= 0; f--) {
            long containerId;
            try {
                containerId = Long.parseLong(files[f].substring(1, (files[f].length() - 4)), 16);
            } catch (Throwable t) {
                // next one.
                continue;
            }
            ContainerKey identity = new ContainerKey(segmentId, containerId);
            /* Not necessary to get the container thru the transaction.
				 * Backup opens in container in read only mode , No need to 
				 * transition the transaction to active state. 
				 * 
				 *  dropped container stubs also has to be backed up 
				 *  for restore to work correctly. That is 
				 *  why we are using a open call that let us
				 *  open dropped containers.
				 */
            ContainerHandle containerHdl = openDroppedContainer((RawTransaction) rt, identity, lockPolicy, ContainerHandle.MODE_READONLY);
            if (containerHdl != null) {
                containerHdl.backupContainer(backupDir.getPath());
                containerHdl.close();
            }
        }
    } else {
        if (SanityManager.DEBUG)
            SanityManager.THROWASSERT("backup process is unable to read container names in seg0");
    }
}
Also used : ContainerKey(org.apache.derby.iapi.store.raw.ContainerKey) LockingPolicy(org.apache.derby.iapi.store.raw.LockingPolicy) RawContainerHandle(org.apache.derby.iapi.store.raw.data.RawContainerHandle) ContainerHandle(org.apache.derby.iapi.store.raw.ContainerHandle) StreamContainerHandle(org.apache.derby.iapi.store.raw.StreamContainerHandle)

Aggregations

ContainerHandle (org.apache.derby.iapi.store.raw.ContainerHandle)22 Page (org.apache.derby.iapi.store.raw.Page)10 OpenConglomerate (org.apache.derby.impl.store.access.conglomerate.OpenConglomerate)7 LockingPolicy (org.apache.derby.iapi.store.raw.LockingPolicy)6 DataValueDescriptor (org.apache.derby.iapi.types.DataValueDescriptor)5 ContainerKey (org.apache.derby.iapi.store.raw.ContainerKey)4 RecordHandle (org.apache.derby.iapi.store.raw.RecordHandle)4 RawContainerHandle (org.apache.derby.iapi.store.raw.data.RawContainerHandle)4 Properties (java.util.Properties)2 StreamContainerHandle (org.apache.derby.iapi.store.raw.StreamContainerHandle)2 Transaction (org.apache.derby.iapi.store.raw.Transaction)2 ControlRow (org.apache.derby.impl.store.access.btree.ControlRow)2 FormatableBitSet (org.apache.derby.iapi.services.io.FormatableBitSet)1 DynamicCompiledOpenConglomInfo (org.apache.derby.iapi.store.access.DynamicCompiledOpenConglomInfo)1 Conglomerate (org.apache.derby.iapi.store.access.conglomerate.Conglomerate)1 TransactionManager (org.apache.derby.iapi.store.access.conglomerate.TransactionManager)1 PageKey (org.apache.derby.iapi.store.raw.PageKey)1 RawStoreFactory (org.apache.derby.iapi.store.raw.RawStoreFactory)1 OpenBTree (org.apache.derby.impl.store.access.btree.OpenBTree)1 SearchParameters (org.apache.derby.impl.store.access.btree.SearchParameters)1