use of org.apache.geode.cache.query.IndexInvalidException in project geode by apache.
the class PrimaryKeyIndexCreationHelper method prepareIndexExpression.
private void prepareIndexExpression(String indexedExpression) throws IndexInvalidException {
List indexedExprs = this.compiler.compileProjectionAttributes(indexedExpression);
if (indexedExprs == null || indexedExprs.size() != 1) {
throw new IndexInvalidException(LocalizedStrings.PrimaryKeyIndexCreationHelper_INVALID_INDEXED_EXPRESSOION_0.toLocalizedString(indexedExpression));
}
CompiledValue expr = (CompiledValue) ((Object[]) indexedExprs.get(0))[1];
if (expr.getType() == CompiledValue.LITERAL)
throw new IndexInvalidException(LocalizedStrings.PrimaryKeyIndexCreationHelper_INVALID_INDEXED_EXPRESSOION_0.toLocalizedString(indexedExpression));
try {
StringBuilder sb = new StringBuilder();
expr.generateCanonicalizedExpression(sb, context);
this.indexedExpression = sb.toString();
} catch (Exception e) {
throw new IndexInvalidException(LocalizedStrings.PrimaryKeyIndexCreationHelper_INVALID_INDEXED_EXPRESSOION_0_N_1.toLocalizedString(new Object[] { indexedExpression, e.getMessage() }));
}
}
use of org.apache.geode.cache.query.IndexInvalidException in project geode by apache.
the class FunctionalIndexCreationHelper method prepareIndexExpression.
/**
* This function is modified so that if the indexed expression has any dependency on the 0th
* iterator, then it needs to modified by using the missing link so that it is derivable from the
* 0th iterator.
* <p>
* TODO: refactor large method prepareIndexExpression
*/
private void prepareIndexExpression(String indexedExpression) throws IndexInvalidException {
CompiledValue expr = this.compiler.compileQuery(indexedExpression);
if (expr == null) {
throw new IndexInvalidException(LocalizedStrings.FunctionalIndexCreationHelper_INVALID_INDEXED_EXPRESSION_0.toLocalizedString(indexedExpression));
}
if (expr instanceof CompiledUndefined || expr instanceof CompiledLiteral || expr instanceof CompiledComparison || expr instanceof CompiledBindArgument || expr instanceof CompiledNegation) {
throw new IndexInvalidException(LocalizedStrings.FunctionalIndexCreationHelper_INVALID_INDEXED_EXPRESSION_0.toLocalizedString(indexedExpression));
}
try {
StringBuilder sb = new StringBuilder();
if (expr instanceof MapIndexable) {
MapIndexable mi = (MapIndexable) expr;
List<CompiledValue> indexingKeys = mi.getIndexingKeys();
if (indexingKeys.size() == 1 && indexingKeys.get(0) == CompiledValue.MAP_INDEX_ALL_KEYS) {
this.isMapTypeIndex = true;
this.isAllKeys = true;
// Strip the index operator
expr = mi.getRecieverSansIndexArgs();
expr.generateCanonicalizedExpression(sb, this.context);
sb.append('[').append('*').append(']');
} else if (indexingKeys.size() == 1) {
expr.generateCanonicalizedExpression(sb, this.context);
} else {
this.isMapTypeIndex = true;
this.multiIndexKeysPattern = new String[indexingKeys.size()];
this.mapKeys = new Object[indexingKeys.size()];
expr = mi.getRecieverSansIndexArgs();
expr.generateCanonicalizedExpression(sb, this.context);
sb.append('[');
String prefixStr = sb.toString();
StringBuilder sb2 = new StringBuilder();
int size = indexingKeys.size();
for (int j = 0; j < size; ++j) {
CompiledValue cv = indexingKeys.get(size - j - 1);
this.mapKeys[size - j - 1] = cv.evaluate(this.context);
StringBuilder sb3 = new StringBuilder();
cv.generateCanonicalizedExpression(sb3, this.context);
sb3.insert(0, prefixStr);
sb3.append(']');
this.multiIndexKeysPattern[j] = sb3.toString();
cv.generateCanonicalizedExpression(sb2, this.context);
sb2.insert(0, ',');
}
sb2.deleteCharAt(0);
sb.append(sb2);
sb.append(']');
}
} else {
expr.generateCanonicalizedExpression(sb, this.context);
}
this.indexedExpression = sb.toString();
this.modifiedIndexExpr = expr;
if (!this.isFirstIteratorRegionEntry && this.indexedExpression.contains(this.canonicalizedIteratorNames[0])) {
this.modifiedIndexExpr = getModifiedDependentCompiledValue(this.context, -1, expr, true);
}
} catch (Exception e) {
throw new IndexInvalidException(LocalizedStrings.FunctionalIndexCreationHelper_INVALID_INDEXED_EXPRESSION_0.toLocalizedString(indexedExpression), e);
}
this.indexedExpr = expr;
}
use of org.apache.geode.cache.query.IndexInvalidException in project geode by apache.
the class FunctionalIndexCreationHelper method modifyIterDefToSuiteIMQ.
private Object[] modifyIterDefToSuiteIMQ(CompiledIteratorDef iterDef) throws IndexInvalidException {
Object[] retValues = { null, null };
try {
CompiledValue def = iterDef.getCollectionExpr();
if (def instanceof CompiledRegion) {
CompiledBindArgument bindArg = new CompiledBindArgument(1);
CompiledIteratorDef newDef = new CompiledIteratorDef(iterDef.getName(), null, bindArg);
retValues[0] = def.evaluate(this.context);
retValues[1] = newDef;
return retValues;
}
if (def instanceof CompiledPath || def instanceof CompiledOperation || def instanceof CompiledIndexOperation) {
CompiledValue cv = def;
List reconstruct = new ArrayList();
while (!(cv instanceof CompiledRegion)) {
CompiledValue prevCV = cv;
if (cv instanceof CompiledOperation) {
reconstruct.add(0, ((CompiledOperation) cv).getArguments());
reconstruct.add(0, ((CompiledOperation) cv).getMethodName());
cv = ((CompiledOperation) cv).getReceiver(this.context);
} else if (cv instanceof CompiledPath) {
reconstruct.add(0, ((CompiledPath) cv).getTailID());
cv = ((CompiledPath) cv).getReceiver();
} else if (cv instanceof CompiledIndexOperation) {
reconstruct.add(0, ((CompiledIndexOperation) cv).getExpression());
cv = ((CompiledIndexOperation) cv).getReceiver();
} else {
throw new IndexInvalidException(LocalizedStrings.FunctionalIndexCreationHelper_FUNCTIONALINDEXCREATIONHELPERPREPAREFROMCLAUSEFROM_CLAUSE_IS_NEITHER_A_COMPILEDPATH_NOR_COMPILEDOPERATION.toLocalizedString());
}
reconstruct.add(0, prevCV.getType());
}
CompiledValue v = cv;
cv = new CompiledBindArgument(1);
int len = reconstruct.size();
for (int j = 0; j < len; ++j) {
Object obj = reconstruct.get(j);
if (obj instanceof Integer) {
int tokenType = (Integer) obj;
if (tokenType == CompiledValue.PATH) {
cv = new CompiledPath(cv, (String) reconstruct.get(++j));
} else if (tokenType == OQLLexerTokenTypes.TOK_LBRACK) {
cv = new CompiledIndexOperation(cv, (CompiledValue) reconstruct.get(++j));
} else if (tokenType == OQLLexerTokenTypes.METHOD_INV) {
cv = new CompiledOperation(cv, (String) reconstruct.get(++j), (List) reconstruct.get(++j));
}
}
}
CompiledIteratorDef newDef = new CompiledIteratorDef(iterDef.getName(), null, cv);
retValues[0] = v.evaluate(this.context);
retValues[1] = newDef;
return retValues;
}
} catch (Exception e) {
throw new IndexInvalidException(e);
}
return retValues;
}
use of org.apache.geode.cache.query.IndexInvalidException in project geode by apache.
the class CreateIndexFunction method execute.
@Override
public void execute(FunctionContext context) {
final IndexInfo indexInfo = (IndexInfo) context.getArguments();
String memberId = null;
try {
Cache cache = CacheFactory.getAnyInstance();
memberId = cache.getDistributedSystem().getDistributedMember().getId();
QueryService queryService = cache.getQueryService();
String indexName = indexInfo.getIndexName();
String indexedExpression = indexInfo.getIndexedExpression();
String fromClause = indexInfo.getRegionPath();
// Check to see if the region path contains an alias e.g "/region1 r1"
// Then the first string will be the regionPath
String[] regionPathTokens = fromClause.trim().split(" ");
String regionPath = regionPathTokens[0];
switch(indexInfo.getIndexType()) {
case IndexInfo.RANGE_INDEX:
queryService.createIndex(indexName, indexedExpression, fromClause);
break;
case IndexInfo.KEY_INDEX:
queryService.createKeyIndex(indexName, indexedExpression, fromClause);
break;
case IndexInfo.HASH_INDEX:
queryService.createHashIndex(indexName, indexedExpression, fromClause);
break;
default:
queryService.createIndex(indexName, indexedExpression, fromClause);
}
regionPath = getValidRegionName(cache, regionPath);
setResultInSender(context, indexInfo, memberId, cache, regionPath);
} catch (IndexExistsException e) {
String message = CliStrings.format(CliStrings.CREATE_INDEX__INDEX__EXISTS, indexInfo.getIndexName());
context.getResultSender().lastResult(new CliFunctionResult(memberId, false, message));
} catch (IndexNameConflictException e) {
String message = CliStrings.format(CliStrings.CREATE_INDEX__NAME__CONFLICT, indexInfo.getIndexName());
context.getResultSender().lastResult(new CliFunctionResult(memberId, false, message));
} catch (RegionNotFoundException e) {
String message = CliStrings.format(CliStrings.CREATE_INDEX__INVALID__REGIONPATH, indexInfo.getRegionPath());
context.getResultSender().lastResult(new CliFunctionResult(memberId, false, message));
} catch (IndexInvalidException e) {
context.getResultSender().lastResult(new CliFunctionResult(memberId, e, e.getMessage()));
} catch (Exception e) {
String exceptionMessage = CliStrings.format(CliStrings.EXCEPTION_CLASS_AND_MESSAGE, e.getClass().getName(), e.getMessage());
context.getResultSender().lastResult(new CliFunctionResult(memberId, e, e.getMessage()));
}
}
use of org.apache.geode.cache.query.IndexInvalidException in project geode by apache.
the class RegionProvider method getOrCreateRegion0.
private Region<?, ?> getOrCreateRegion0(ByteArrayWrapper key, RedisDataType type, ExecutionHandlerContext context, boolean addToMeta) {
checkDataType(key, type);
Region<?, ?> r = this.regions.get(key);
if (r != null && r.isDestroyed()) {
removeKey(key, type);
r = null;
}
if (r == null) {
String stringKey = key.toString();
Lock lock = this.locks.get(stringKey);
if (lock == null) {
this.locks.putIfAbsent(stringKey, new ReentrantLock());
lock = this.locks.get(stringKey);
}
try {
lock.lock();
r = regions.get(key);
if (r == null) {
// Can create
boolean hasTransaction = context != null && context.hasTransaction();
// without context
CacheTransactionManager txm = null;
TransactionId transactionId = null;
try {
if (hasTransaction) {
txm = cache.getCacheTransactionManager();
transactionId = txm.suspend();
}
Exception concurrentCreateDestroyException = null;
do {
concurrentCreateDestroyException = null;
r = createRegionGlobally(stringKey);
try {
if (type == RedisDataType.REDIS_LIST) {
doInitializeList(key, r);
} else if (type == RedisDataType.REDIS_SORTEDSET) {
try {
doInitializeSortedSet(key, r);
} catch (RegionNotFoundException | IndexInvalidException e) {
concurrentCreateDestroyException = e;
}
}
} catch (QueryInvalidException e) {
if (e.getCause() instanceof RegionNotFoundException) {
concurrentCreateDestroyException = e;
}
}
} while (concurrentCreateDestroyException != null);
this.regions.put(key, r);
if (addToMeta) {
RedisDataType existingType = metaPutIfAbsent(key, type);
if (existingType != null && existingType != type)
throw new RedisDataTypeMismatchException("The key name \"" + key + "\" is already used by a " + existingType.toString());
}
} finally {
if (hasTransaction)
txm.resume(transactionId);
}
}
} finally {
lock.unlock();
}
}
return r;
}
Aggregations