use of org.apache.derby.iapi.store.raw.Transaction in project derby by apache.
the class B2I method create.
/*
** Methods of B2I.
*/
/**
* Create an empty secondary index b-tree, using the generic b-tree to do the
* generic part of the creation process.
*
* This routine opens the newly created container, adds a single page, and
* makes this page the root by inserting a LeafControlRow onto this page
* at slot 0 and marking in that control row that the page is a root page.
*
* The following properties are specific to the b-tree secondary index:
* <UL>
* <LI> "baseConglomerateId" (integer). The conglomerate id of the base
* conglomerate is never actually accessed by the b-tree secondary
* index implementation, it only serves as a namespace for row locks.
* This property is required.
* <LI> "rowLocationColumn" (integer). The zero-based index into the row which
* the b-tree secondary index will assume holds a @see RowLocation of
* the base row in the base conglomerate. This value will be used
* for acquiring locks. In this implementation RowLocationColumn must be
* the last key column.
* This property is required.
* </UL>
*
* A secondary index i (a, b) on table t (a, b, c) would have rows
* which looked like (a, b, row_location). baseConglomerateId is set to the
* conglomerate id of t. rowLocationColumns is set to 2. allowsDuplicates
* would be set to false, @see BTree#create. To create a unique
* secondary index set uniquenessColumns to 2, this means that the btree
* code will compare the key values but not the row id when determing
* uniqueness. To create a nonunique secondary index set uniquenessColumns
* to 3, this would mean that the uniqueness test would include the row
* location and since all row locations will be unique all rows inserted
* into the index will be differentiated (at least) by row location.
*
* @see BTree#create
*
* @exception StandardException Standard exception policy.
*/
public void create(TransactionManager xact_manager, int segmentId, long input_conglomid, DataValueDescriptor[] template, ColumnOrdering[] columnOrder, int[] collationIds, Properties properties, int temporaryFlag) throws StandardException {
String property_value = null;
Transaction rawtran = xact_manager.getRawStoreXact();
if (properties == null) {
throw (StandardException.newException(SQLState.BTREE_PROPERTY_NOT_FOUND, PROPERTY_BASECONGLOMID));
}
// Get baseConglomerateId //
property_value = properties.getProperty(PROPERTY_BASECONGLOMID);
if (property_value == null) {
throw (StandardException.newException(SQLState.BTREE_PROPERTY_NOT_FOUND, PROPERTY_BASECONGLOMID));
}
if (SanityManager.DEBUG) {
if (property_value == null)
SanityManager.THROWASSERT(PROPERTY_BASECONGLOMID + "property not passed to B2I.create()");
}
baseConglomerateId = Long.parseLong(property_value);
// Get rowLocationColumn //
property_value = properties.getProperty(PROPERTY_ROWLOCCOLUMN);
if (SanityManager.DEBUG) {
if (property_value == null)
SanityManager.THROWASSERT(PROPERTY_ROWLOCCOLUMN + "property not passed to B2I.create()");
}
if (property_value == null) {
throw (StandardException.newException(SQLState.BTREE_PROPERTY_NOT_FOUND, PROPERTY_BASECONGLOMID));
}
rowLocationColumn = Integer.parseInt(property_value);
// comparing the columns in the index easier.
if (SanityManager.DEBUG) {
SanityManager.ASSERT(rowLocationColumn == template.length - 1, "rowLocationColumn is not the last column in the index");
SanityManager.ASSERT(template[rowLocationColumn] instanceof RowLocation);
// There must be at least one key column
if (rowLocationColumn < 1)
SanityManager.THROWASSERT("rowLocationColumn (" + rowLocationColumn + ") expected to be >= 1");
}
/* convert the sorting order information into a boolean array map.
* If the sorting order for the columns is not provided, we
* assign the default as Ascending Order.
* array length is equal to template length, because column order
* length changes whether it is unique or is non unique. store assumes
* template length arrays. So, we make template length array and make
* the last column as ascending instead of having lot of execeptions
* code.
*/
ascDescInfo = new boolean[template.length];
for (int i = 0; i < ascDescInfo.length; i++) {
if (columnOrder != null && i < columnOrder.length)
ascDescInfo[i] = columnOrder[i].getIsAscending();
else
// default values - ascending order
ascDescInfo[i] = true;
}
// get collation ids from input collation ids, store it in the
// conglom state.
collation_ids = ConglomerateUtil.createCollationIds(template.length, collationIds);
hasCollatedTypes = hasCollatedColumns(collation_ids);
// Do the generic part of creating the b-tree.
super.create(rawtran, segmentId, input_conglomid, template, properties, getTypeFormatId(), temporaryFlag);
// open the base conglomerate - to get the lock
ConglomerateController base_cc = xact_manager.openConglomerate(baseConglomerateId, false, TransactionController.OPENMODE_FOR_LOCK_ONLY, TransactionController.MODE_TABLE, TransactionController.ISOLATION_SERIALIZABLE);
OpenBTree open_btree = new OpenBTree();
BTreeLockingPolicy b2i_locking_policy = new B2ITableLocking3(rawtran, TransactionController.MODE_TABLE, rawtran.newLockingPolicy(LockingPolicy.MODE_CONTAINER, TransactionController.ISOLATION_SERIALIZABLE, true), base_cc, open_btree);
// The following call will "open" the new btree. Create is
// an interesting case. What we really want is read only table lock
// on the base conglomerate and update locks on the index. For now
// just get the update lock on the base table, this is done by the
// lockTable() call made by base class.
open_btree.init(// current user xact
(TransactionManager) xact_manager, // current xact
(TransactionManager) xact_manager, // have init open the container.
(ContainerHandle) null, rawtran, false, (ContainerHandle.MODE_FORUPDATE), TransactionController.MODE_TABLE, // get table level lock.
b2i_locking_policy, this, // no logical undo necessary, as
(LogicalUndo) null, // rows will not move.
(DynamicCompiledOpenConglomInfo) null);
// Open the newly created container, and insert the first control row.
LeafControlRow.initEmptyBtree(open_btree);
open_btree.close();
base_cc.close();
}
use of org.apache.derby.iapi.store.raw.Transaction 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) {
}
}
}
}
use of org.apache.derby.iapi.store.raw.Transaction in project derby by apache.
the class RAMAccessManager method getAndNameTransaction.
public TransactionController getAndNameTransaction(ContextManager cm, String transName) throws StandardException {
if (cm == null)
// XXX (nat) should throw exception
return null;
// See if there's already a transaction context.
RAMTransactionContext rtc = (RAMTransactionContext) cm.getContext(AccessFactoryGlobals.RAMXACT_CONTEXT_ID);
if (rtc == null) {
// No transaction context. Create or find a raw store transaction,
// make a context for it, and push the context. Note this puts the
// raw store transaction context above the access context, which is
// required for error handling assumptions to be correct.
Transaction rawtran = rawstore.findUserTransaction(cm, transName);
RAMTransaction rt = new RAMTransaction(this, rawtran, null);
rtc = new RAMTransactionContext(cm, AccessFactoryGlobals.RAMXACT_CONTEXT_ID, rt, false);
TransactionController tc = rtc.getTransaction();
if (xactProperties != null) {
rawtran.setup(tc);
tc.commit();
}
rawtran.setDefaultLockingPolicy(system_default_locking_policy);
tc.commit();
return tc;
}
return rtc.getTransaction();
}
use of org.apache.derby.iapi.store.raw.Transaction in project derby by apache.
the class MergeScan method init.
/*
* Methods of MergeScan
*/
/**
* Initialize the scan, returning false if there
* was some error.
*/
public boolean init(TransactionManager tran) throws StandardException {
if (SanityManager.DEBUG) {
// We really expect to have at least one
// merge run.
SanityManager.ASSERT(mergeRuns != null);
SanityManager.ASSERT(mergeRuns.size() > 0);
// This sort scan also expects that the
// caller has ensured that the sort buffer
// capacity will hold a row from all the
// merge runs.
SanityManager.ASSERT(sortBuffer.capacity() >= mergeRuns.size());
}
// Clear the sort buffer.
sortBuffer.reset();
// Create an array to hold a scan controller
// for each merge run.
openScans = new StreamContainerHandle[mergeRuns.size()];
if (openScans == null)
return false;
// Open a scan on each merge run.
int scanindex = 0;
Enumeration<Long> e = mergeRuns.elements();
while (e.hasMoreElements()) {
// get the container id
long id = (e.nextElement()).longValue();
// get raw transaction
Transaction rawTran = tran.getRawStoreXact();
int segmentId = StreamContainerHandle.TEMPORARY_SEGMENT;
openScans[scanindex++] = rawTran.openStreamContainer(segmentId, id, hold);
}
// Load the initial rows.
for (scanindex = 0; scanindex < openScans.length; scanindex++) mergeARow(scanindex);
// Success!
return true;
}
use of org.apache.derby.iapi.store.raw.Transaction in project derby by apache.
the class MergeSort method multiStageMerge.
/* DEBUG (nat)
void printRunInfo(TransactionController tran)
throws StandardException
{
java.util.Enumeration e = mergeRuns.elements();
while (e.hasMoreElements())
{
long conglomid = ((Long) e.nextElement()).longValue();
ScanController sc = tran.openScan(conglomid, false,
false, null, null, 0, null,
null, 0);
System.out.println("Merge run: conglomid=" + conglomid);
while (sc.next())
{
sc.fetch(template);
System.out.println(template);
}
sc.close();
}
}
*/
private void multiStageMerge(TransactionManager tran) throws StandardException {
Enumeration<Long> e;
// int iterations = 0; // DEBUG (nat)
int maxMergeRuns = sortBuffer.capacity();
if (maxMergeRuns > ExternalSortFactory.DEFAULT_MAX_MERGE_RUN)
maxMergeRuns = ExternalSortFactory.DEFAULT_MAX_MERGE_RUN;
Vector<Long> subset;
Vector<Long> leftovers;
while (mergeRuns.size() > maxMergeRuns) {
// Move maxMergeRuns elements from the merge runs
// vector into a subset, leaving the rest.
subset = new Vector<Long>(maxMergeRuns);
leftovers = new Vector<Long>(mergeRuns.size() - maxMergeRuns);
e = mergeRuns.elements();
while (e.hasMoreElements()) {
Long containerId = e.nextElement();
if (subset.size() < maxMergeRuns)
subset.addElement(containerId);
else
leftovers.addElement(containerId);
}
/* DEBUG (nat)
iterations++;
System.out.println(subset.size() + " elements in subset");
System.out.println(leftovers.size() + " elements in leftovers");
System.out.println(mergeRuns.size() + " elements in mergeRuns");
System.out.println("maxMergeRuns is " + maxMergeRuns);
System.out.println("iterations = " + iterations);
if (subset.size() == 0)
{
System.exit(1);
}
*/
mergeRuns = leftovers;
// Open a merge scan on the subset.
MergeScanRowSource msRowSource = new MergeScanRowSource(this, tran, sortBuffer, subset, sortObserver, false);
if (!msRowSource.init(tran)) {
throw StandardException.newException(SQLState.SORT_COULD_NOT_INIT);
}
// Create and open another temporary stream conglomerate
// which will become
// a merge run made up with the merged runs from the subset.
Transaction rawTran = tran.getRawStoreXact();
int segmentId = StreamContainerHandle.TEMPORARY_SEGMENT;
long id = rawTran.addAndLoadStreamContainer(segmentId, properties, msRowSource);
mergeRuns.addElement(id);
// Drop the conglomerates in the merge subset
e = subset.elements();
while (e.hasMoreElements()) {
Long containerId = (Long) e.nextElement();
rawTran.dropStreamContainer(segmentId, containerId.longValue());
}
}
}
Aggregations