foreach(record);
}
}
private void searchInIndex() {
final OIndex index = database.getMetadata().getIndexManager().getIndex(compiledFilter.getTargetIndex());
if (index == null)
throw new OCommandExecutionException("Target index '" + compiledFilter.getTargetIndex() + "' not found");
if (compiledFilter.getRootCondition() != null) {
if (!"KEY".equalsIgnoreCase(compiledFilter.getRootCondition().getLeft().toString()))
throw new OCommandExecutionException("'Key' field is required for queries against indexes");
final Object right = compiledFilter.getRootCondition().getRight();
final Object keyValue = OSQLHelper.getValue(right);
Collection<OIdentifiable> result = null;
final OQueryOperator indexOperator = compiledFilter.getRootCondition().getOperator();
if (indexOperator instanceof OQueryOperatorBetween) {
final Object[] values = (Object[]) compiledFilter.getRootCondition().getRight();
if (projections != null && projections.size() == 1 && projections.keySet().iterator().next().equalsIgnoreCase("@rid")) {
// SPECIAL CASE
result = index.getValuesBetween(OSQLHelper.getValue(values[0]), OSQLHelper.getValue(values[2]));
for (OIdentifiable e : result)
addResult(e.getIdentity());
} else {
final Collection<ODocument> entries = index.getEntriesBetween(OSQLHelper.getValue(values[0]),
OSQLHelper.getValue(values[2]));
for (OIdentifiable r : entries)
addResult(r);
}
} else if (indexOperator instanceof OQueryOperatorMajor) {
final Object value = compiledFilter.getRootCondition().getRight();
if (projections != null && projections.size() == 1 && projections.keySet().iterator().next().equalsIgnoreCase("@rid")) {
// SPECIAL CASE
result = index.getValuesMajor(OSQLHelper.getValue(value), false);
for (OIdentifiable e : result)
addResult(e.getIdentity());
} else {
final Collection<ODocument> entries = index.getEntriesMajor(OSQLHelper.getValue(value), false);
for (ODocument document : entries)
addResult(document);
}
} else if (indexOperator instanceof OQueryOperatorMajorEquals) {
final Object value = compiledFilter.getRootCondition().getRight();
if (projections != null && projections.size() == 1 && projections.keySet().iterator().next().equalsIgnoreCase("@rid")) {
// SPECIAL CASE
result = index.getValuesMajor(OSQLHelper.getValue(value), true);
for (OIdentifiable e : result)
addResult(e.getIdentity());
} else {
final Collection<ODocument> entries = index.getEntriesMajor(OSQLHelper.getValue(value), true);
for (ODocument document : entries)
addResult(document);
}
} else if (indexOperator instanceof OQueryOperatorMinor) {
final Object value = compiledFilter.getRootCondition().getRight();
if (projections != null && projections.size() == 1 && projections.keySet().iterator().next().equalsIgnoreCase("@rid")) {
// SPECIAL CASE
result = index.getValuesMinor(OSQLHelper.getValue(value), false);
for (OIdentifiable e : result)
addResult(e.getIdentity());
} else {
final Collection<ODocument> entries = index.getEntriesMinor(OSQLHelper.getValue(value), false);
for (ODocument document : entries)
addResult(document);
}
} else if (indexOperator instanceof OQueryOperatorMinorEquals) {
final Object value = compiledFilter.getRootCondition().getRight();
if (projections != null && projections.size() == 1 && projections.keySet().iterator().next().equalsIgnoreCase("@rid")) {
// SPECIAL CASE
result = index.getValuesMinor(OSQLHelper.getValue(value), true);
for (OIdentifiable e : result)
addResult(e.getIdentity());
} else {
final Collection<ODocument> entries = index.getEntriesMinor(OSQLHelper.getValue(value), true);
for (ODocument document : entries)
addResult(document);
}
} else {
result = index.get(keyValue);
for (OIdentifiable r : result)
addResult(createIndexEntryAsDocument(keyValue, r.getIdentity()));
}
} else {
// ADD ALL THE ITEMS AS RESULT
for (Iterator<Entry<Object, Set<OIdentifiable>>> it = index.iterator(); it.hasNext();) {
final Entry<Object, Set<OIdentifiable>> current = it.next();
for (Iterator<OIdentifiable> collIt = ((ORecordLazySet) current.getValue()).rawIterator(); collIt.hasNext();)
addResult(createIndexEntryAsDocument(current.getKey(), collIt.next().getIdentity()));
}
}
if (anyFunctionAggregates) {
for (Entry<String, Object> projection : projections.entrySet()) {
if (projection.getValue() instanceof OSQLFunctionRuntime) {
final OSQLFunctionRuntime f = (OSQLFunctionRuntime) projection.getValue();
f.setResult(index.getSize());
}
}
}
}