use of org.apache.derby.iapi.services.io.FormatableBitSet in project derby by apache.
the class RenameConstantAction method execGutsRenameColumn.
// do necessary work for rename column at execute time.
private void execGutsRenameColumn(TableDescriptor td, Activation activation) throws StandardException {
ColumnDescriptor columnDescriptor = null;
int columnPosition = 0;
ConstraintDescriptorList constraintDescriptorList;
ConstraintDescriptor constraintDescriptor;
LanguageConnectionContext lcc = activation.getLanguageConnectionContext();
DataDictionary dd = lcc.getDataDictionary();
DependencyManager dm = dd.getDependencyManager();
TransactionController tc = lcc.getTransactionExecute();
/* get the column descriptor for column to be renamed and
* using it's position in the table, set the referenced
* column map of the table indicating which column is being
* renamed. Dependency Manager uses this to find out the
* dependents on the column.
*/
columnDescriptor = td.getColumnDescriptor(oldObjectName);
if (columnDescriptor.isAutoincrement())
columnDescriptor.setAutoinc_create_or_modify_Start_Increment(ColumnDefinitionNode.CREATE_AUTOINCREMENT);
columnPosition = columnDescriptor.getPosition();
FormatableBitSet toRename = new FormatableBitSet(td.getColumnDescriptorList().size() + 1);
toRename.set(columnPosition);
td.setReferencedColumnMap(toRename);
dm.invalidateFor(td, DependencyManager.RENAME, lcc);
// look for foreign key dependency on the column.
constraintDescriptorList = dd.getConstraintDescriptors(td);
for (int index = 0; index < constraintDescriptorList.size(); index++) {
constraintDescriptor = constraintDescriptorList.elementAt(index);
int[] referencedColumns = constraintDescriptor.getReferencedColumns();
int numRefCols = referencedColumns.length;
for (int j = 0; j < numRefCols; j++) {
if ((referencedColumns[j] == columnPosition) && (constraintDescriptor instanceof ReferencedKeyConstraintDescriptor))
dm.invalidateFor(constraintDescriptor, DependencyManager.RENAME, lcc);
}
}
// Drop the column
dd.dropColumnDescriptor(td.getUUID(), oldObjectName, tc);
columnDescriptor.setColumnName(newObjectName);
dd.addDescriptor(columnDescriptor, td, DataDictionary.SYSCOLUMNS_CATALOG_NUM, false, tc);
// Need to do following to reload the cache so that table
// descriptor now has new column name
td = dd.getTableDescriptor(td.getObjectID());
}
use of org.apache.derby.iapi.services.io.FormatableBitSet in project derby by apache.
the class InsertResultSet method emptyIndexes.
/**
* Empty the indexes after doing a bulk insert replace
* where the table has 0 rows after the replace.
* RESOLVE: This method is ugly! Prior to 2.0, we simply
* scanned back across the table to build the indexes. We
* changed this in 2.0 to populate the sorters via a call back
* as we populated the table. Doing a 0 row replace into a
* table with indexes is a degenerate case, hence we allow
* ugly and unoptimized code.
*
* @exception StandardException Thrown on failure
*/
private void emptyIndexes(long newHeapConglom, InsertConstantAction constants, TableDescriptor td, DataDictionary dd, ExecRow fullTemplate) throws StandardException {
int numIndexes = constants.irgs.length;
ExecIndexRow[] idxRows = new ExecIndexRow[numIndexes];
ExecRow baseRows;
ColumnOrdering[][] order = new ColumnOrdering[numIndexes][];
int numColumns = td.getNumberOfColumns();
collation = new int[numIndexes][];
// Create the BitSet for mapping the partial row to the full row
FormatableBitSet bitSet = new FormatableBitSet(numColumns + 1);
// Need to check each index for referenced columns
int numReferencedColumns = 0;
for (int index = 0; index < numIndexes; index++) {
int[] baseColumnPositions = constants.irgs[index].baseColumnPositions();
for (int bcp = 0; bcp < baseColumnPositions.length; bcp++) {
if (!bitSet.get(baseColumnPositions[bcp])) {
bitSet.set(baseColumnPositions[bcp]);
numReferencedColumns++;
}
}
}
// We can finally create the partial base row
baseRows = activation.getExecutionFactory().getValueRow(numReferencedColumns);
// Fill in each base row with nulls of the correct data type
int colNumber = 0;
for (int index = 0; index < numColumns; index++) {
if (bitSet.get(index + 1)) {
colNumber++;
// NOTE: 1-based column numbers
baseRows.setColumn(colNumber, fullTemplate.getColumn(index + 1).cloneValue(false));
}
}
needToDropSort = new boolean[numIndexes];
sortIds = new long[numIndexes];
/* Do the initial set up before scanning the heap.
* For each index, build a single index row and a sorter.
*/
for (int index = 0; index < numIndexes; index++) {
// create a single index row template for each index
idxRows[index] = constants.irgs[index].getIndexRowTemplate();
// Get an index row based on the base row
// (This call is only necessary here because we need to pass a
// template to the sorter.)
constants.irgs[index].getIndexRow(baseRows, rl, idxRows[index], bitSet);
/* For non-unique indexes, we order by all columns + the RID.
* For unique indexes, we just order by the columns.
* We create a unique index observer for unique indexes
* so that we can catch duplicate key
*/
ConglomerateDescriptor cd;
// Get the ConglomerateDescriptor for the index
cd = td.getConglomerateDescriptor(constants.indexCIDS[index]);
int[] baseColumnPositions = constants.irgs[index].baseColumnPositions();
boolean[] isAscending = constants.irgs[index].isAscending();
int numColumnOrderings;
SortObserver sortObserver;
final IndexRowGenerator indDes = cd.getIndexDescriptor();
if (indDes.isUnique() || indDes.isUniqueDeferrable()) {
numColumnOrderings = indDes.isUnique() ? baseColumnPositions.length : baseColumnPositions.length + 1;
String indexOrConstraintName = cd.getConglomerateName();
boolean deferred = false;
boolean uniqueDeferrable = false;
UUID uniqueDeferrableConstraintId = null;
if (cd.isConstraint()) {
// so, the index is backing up a constraint
ConstraintDescriptor conDesc = dd.getConstraintDescriptor(td, cd.getUUID());
indexOrConstraintName = conDesc.getConstraintName();
deferred = lcc.isEffectivelyDeferred(lcc.getCurrentSQLSessionContext(activation), conDesc.getUUID());
uniqueDeferrable = conDesc.deferrable();
uniqueDeferrableConstraintId = conDesc.getUUID();
}
sortObserver = new UniqueIndexSortObserver(lcc, uniqueDeferrableConstraintId, // don't clone rows
false, uniqueDeferrable, deferred, indexOrConstraintName, idxRows[index], true, td.getName());
} else {
numColumnOrderings = baseColumnPositions.length + 1;
sortObserver = new BasicSortObserver(false, false, idxRows[index], true);
}
order[index] = new ColumnOrdering[numColumnOrderings];
for (int ii = 0; ii < isAscending.length; ii++) {
order[index][ii] = new IndexColumnOrder(ii, isAscending[ii]);
}
if (numColumnOrderings > isAscending.length) {
order[index][isAscending.length] = new IndexColumnOrder(isAscending.length);
}
// create the sorters
sortIds[index] = tc.createSort((Properties) null, idxRows[index].getRowArrayClone(), order[index], sortObserver, // not in order
false, // est rows
rowCount, // est row size, -1 means no idea
-1);
needToDropSort[index] = true;
}
// Populate sorters and get the output of each sorter into a row
// source. The sorters have the indexed columns only and the columns
// are in the correct order.
rowSources = new RowLocationRetRowSource[numIndexes];
// Fill in the RowSources
SortController[] sorter = new SortController[numIndexes];
for (int index = 0; index < numIndexes; index++) {
sorter[index] = tc.openSort(sortIds[index]);
sorter[index].completedInserts();
rowSources[index] = tc.openSortRowSource(sortIds[index]);
}
long[] newIndexCongloms = new long[numIndexes];
// Populate each index
for (int index = 0; index < numIndexes; index++) {
ConglomerateController indexCC;
Properties properties = new Properties();
ConglomerateDescriptor cd;
// Get the ConglomerateDescriptor for the index
cd = td.getConglomerateDescriptor(constants.indexCIDS[index]);
// Build the properties list for the new conglomerate
indexCC = tc.openCompiledConglomerate(false, TransactionController.OPENMODE_FORUPDATE, TransactionController.MODE_TABLE, TransactionController.ISOLATION_SERIALIZABLE, constants.indexSCOCIs[index], indexDCOCIs[index]);
// Get the properties on the old index
indexCC.getInternalTablePropertySet(properties);
/* Create the properties that language supplies when creating the
* the index. (The store doesn't preserve these.)
*/
int indexRowLength = idxRows[index].nColumns();
properties.put("baseConglomerateId", Long.toString(newHeapConglom));
if (cd.getIndexDescriptor().isUnique()) {
properties.put("nUniqueColumns", Integer.toString(indexRowLength - 1));
} else {
properties.put("nUniqueColumns", Integer.toString(indexRowLength));
}
if (cd.getIndexDescriptor().isUniqueWithDuplicateNulls() && !cd.getIndexDescriptor().hasDeferrableChecking()) {
properties.put("uniqueWithDuplicateNulls", Boolean.toString(true));
}
properties.put("rowLocationColumn", Integer.toString(indexRowLength - 1));
properties.put("nKeyFields", Integer.toString(indexRowLength));
indexCC.close();
collation[index] = constants.irgs[index].getColumnCollationIds(td.getColumnDescriptorList());
// We can finally drain the sorter and rebuild the index
// Populate the index.
newIndexCongloms[index] = tc.createAndLoadConglomerate("BTREE", idxRows[index].getRowArray(), // default column sort order
null, collation[index], properties, TransactionController.IS_DEFAULT, rowSources[index], (long[]) null);
/* Update the DataDictionary
*
* Update sys.sysconglomerates with new conglomerate #, if the
* conglomerate is shared by duplicate indexes, all the descriptors
* for those indexes need to be updated with the new number.
*/
dd.updateConglomerateDescriptor(td.getConglomerateDescriptors(constants.indexCIDS[index]), newIndexCongloms[index], tc);
// Drop the old conglomerate
tc.dropConglomerate(constants.indexCIDS[index]);
}
}
use of org.apache.derby.iapi.services.io.FormatableBitSet in project derby by apache.
the class RowUtil method shift.
/**
* Shift a FormatableBitSet N bits toward the zero end.
* e.g. shift({2,4}) -> {1,3}.
*
* @param bitSet the bit set
* @param n the number of bits to shift
*
* @return a new FormatableBitSet with the shifted result
*/
public static FormatableBitSet shift(FormatableBitSet bitSet, int n) {
FormatableBitSet out = null;
if (bitSet != null) {
int size = bitSet.size();
out = new FormatableBitSet(size);
for (int i = n; i < size; i++) {
if (bitSet.get(i)) {
out.set(i - n);
}
}
}
return out;
}
use of org.apache.derby.iapi.services.io.FormatableBitSet in project derby by apache.
the class B2I method writeExternal_v10_2.
/**
* Store the stored representation of the column value in the
* stream.
* <p>
* For more detailed description of the ACCESS_B2I_V3_ID format see
* documentation at top of file.
*
* @see java.io.Externalizable#writeExternal
*/
public void writeExternal_v10_2(ObjectOutput out) throws IOException {
super.writeExternal(out);
out.writeLong(baseConglomerateId);
out.writeInt(rowLocationColumn);
// write the columns ascend/descend information as bits
FormatableBitSet ascDescBits = new FormatableBitSet(ascDescInfo.length);
for (int i = 0; i < ascDescInfo.length; i++) {
if (ascDescInfo[i])
ascDescBits.set(i);
}
ascDescBits.writeExternal(out);
}
use of org.apache.derby.iapi.services.io.FormatableBitSet in project derby by apache.
the class B2I method readExternal.
/**
* Restore the in-memory representation from the stream.
* <p>
*
* @exception ClassNotFoundException Thrown if the stored representation
* is serialized and a class named in
* the stream could not be found.
*
* @see java.io.Externalizable#readExternal
*/
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
super.readExternal(in);
baseConglomerateId = in.readLong();
rowLocationColumn = in.readInt();
// read the column sort order info
FormatableBitSet ascDescBits = new FormatableBitSet();
ascDescBits.readExternal(in);
ascDescInfo = new boolean[ascDescBits.getLength()];
for (int i = 0; i < ascDescBits.getLength(); i++) ascDescInfo[i] = ascDescBits.isSet(i);
// In memory maintain a collation id per column in the template.
collation_ids = new int[format_ids.length];
if (SanityManager.DEBUG) {
SanityManager.ASSERT(!hasCollatedTypes);
}
// this is the default and no resetting is necessary.
for (int i = 0; i < format_ids.length; i++) collation_ids[i] = StringDataValue.COLLATION_TYPE_UCS_BASIC;
// initialize the unique with null setting to false, to be reset
// below when read from disk. For version ACCESS_B2I_V3_ID and
// ACCESS_B2I_V4_ID, this is the default and no resetting is necessary.
setUniqueWithDuplicateNulls(false);
if (conglom_format_id == StoredFormatIds.ACCESS_B2I_V4_ID || conglom_format_id == StoredFormatIds.ACCESS_B2I_V5_ID) {
// current format id, read collation info from disk
if (SanityManager.DEBUG) {
// length must include row location column and at least
// one other field.
SanityManager.ASSERT(collation_ids.length >= 2, "length = " + collation_ids.length);
}
hasCollatedTypes = ConglomerateUtil.readCollationIdArray(collation_ids, in);
} else if (conglom_format_id != StoredFormatIds.ACCESS_B2I_V3_ID) {
if (SanityManager.DEBUG) {
SanityManager.THROWASSERT("Unexpected format id: " + conglom_format_id);
}
}
if (conglom_format_id == StoredFormatIds.ACCESS_B2I_V5_ID) {
setUniqueWithDuplicateNulls(in.readBoolean());
}
}
Aggregations