use of org.apache.phoenix.parse.AlterIndexStatement in project phoenix by apache.
the class MetaDataClient method buildIndex.
/**
* For new mutations only should not be used if there are deletes done in the data table between start time and end
* time passed to the method.
*/
public MutationState buildIndex(PTable index, TableRef dataTableRef, long startTime, long EndTime) throws SQLException {
boolean wasAutoCommit = connection.getAutoCommit();
try {
AlterIndexStatement indexStatement = FACTORY.alterIndex(FACTORY.namedTable(null, TableName.create(index.getSchemaName().getString(), index.getTableName().getString())), dataTableRef.getTable().getTableName().getString(), false, PIndexState.INACTIVE);
alterIndex(indexStatement);
connection.setAutoCommit(true);
MutationPlan mutationPlan = getMutationPlanForBuildingIndex(index, dataTableRef);
Scan scan = mutationPlan.getContext().getScan();
try {
scan.setTimeRange(startTime, EndTime);
} catch (IOException e) {
throw new SQLException(e);
}
MutationState state = connection.getQueryServices().updateData(mutationPlan);
indexStatement = FACTORY.alterIndex(FACTORY.namedTable(null, TableName.create(index.getSchemaName().getString(), index.getTableName().getString())), dataTableRef.getTable().getTableName().getString(), false, PIndexState.ACTIVE);
alterIndex(indexStatement);
return state;
} finally {
connection.setAutoCommit(wasAutoCommit);
}
}
use of org.apache.phoenix.parse.AlterIndexStatement in project phoenix by apache.
the class MetaDataClient method buildIndex.
private MutationState buildIndex(PTable index, TableRef dataTableRef) throws SQLException {
AlterIndexStatement indexStatement = null;
boolean wasAutoCommit = connection.getAutoCommit();
try {
connection.setAutoCommit(true);
MutationPlan mutationPlan = getMutationPlanForBuildingIndex(index, dataTableRef);
Scan scan = mutationPlan.getContext().getScan();
Long scn = connection.getSCN();
try {
if (ScanUtil.isDefaultTimeRange(scan.getTimeRange())) {
if (scn == null) {
scn = mutationPlan.getContext().getCurrentTime();
}
scan.setTimeRange(dataTableRef.getLowerBoundTimeStamp(), scn);
}
} catch (IOException e) {
throw new SQLException(e);
}
// execute index population upsert select
long startTime = EnvironmentEdgeManager.currentTimeMillis();
MutationState state = connection.getQueryServices().updateData(mutationPlan);
long firstUpsertSelectTime = EnvironmentEdgeManager.currentTimeMillis() - startTime;
// for global indexes on non transactional tables we might have to
// run a second index population upsert select to handle data rows
// that were being written on the server while the index was created.
// TODO: this sleep time is really arbitrary. If any query is in progress
// while the index is being built, we're depending on this sleep
// waiting them out. Instead we should have a means of waiting until
// all in progress queries are complete (though I'm not sure that's
// feasible). See PHOENIX-4092.
long sleepTime = connection.getQueryServices().getProps().getLong(QueryServices.INDEX_POPULATION_SLEEP_TIME, QueryServicesOptions.DEFAULT_INDEX_POPULATION_SLEEP_TIME);
if (!dataTableRef.getTable().isTransactional() && sleepTime > 0) {
long delta = sleepTime - firstUpsertSelectTime;
if (delta > 0) {
try {
Thread.sleep(delta);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new SQLExceptionInfo.Builder(SQLExceptionCode.INTERRUPTED_EXCEPTION).setRootCause(e).build().buildException();
}
}
// set the min timestamp of second index upsert select some time before the index
// was created
long minTimestamp = index.getTimeStamp() - firstUpsertSelectTime;
try {
// TODO: Use scn or LATEST_TIMESTAMP here? It's possible that a DML statement
// ran and ended up with timestamps later than this time. If we use a later
// timestamp, we'll need to run the partial index rebuilder here as it's
// possible that the updates to the table were made (such as deletes) after
// the scn which would not be properly reflected correctly this mechanism.
// See PHOENIX-4092.
mutationPlan.getContext().getScan().setTimeRange(minTimestamp, scn);
} catch (IOException e) {
throw new SQLException(e);
}
MutationState newMutationState = connection.getQueryServices().updateData(mutationPlan);
state.join(newMutationState);
}
indexStatement = FACTORY.alterIndex(FACTORY.namedTable(null, TableName.create(index.getSchemaName().getString(), index.getTableName().getString())), dataTableRef.getTable().getTableName().getString(), false, PIndexState.ACTIVE);
alterIndex(indexStatement);
return state;
} finally {
connection.setAutoCommit(wasAutoCommit);
}
}
Aggregations