throw new CacheException("An exception has occurred during query execution", e);
}
}
private byte[] executeQuery(AdvancedCache<byte[], byte[]> cache, byte[] query) throws IOException {
final SerializationContext serCtx = ProtobufMetadataManager.getSerializationContext(cache.getCacheManager());
QueryRequest request = ProtobufUtil.fromByteArray(serCtx, query, 0, query.length, QueryRequest.class);
SearchManager searchManager = Search.getSearchManager(cache);
Query luceneQuery;
List<String> projections;
Class targetEntity;
Descriptors.Descriptor messageDescriptor;
QueryParser queryParser = new QueryParser();
SearchFactoryIntegrator searchFactory = (SearchFactoryIntegrator) searchManager.getSearchFactory();
if (cache.getCacheConfiguration().compatibility().enabled()) {
final QueryInterceptor queryInterceptor = ComponentRegistryUtils.getQueryInterceptor(cache);
EntityNamesResolver entityNamesResolver = new EntityNamesResolver() {
@Override
public Class<?> getClassFromName(String entityName) {
MessageMarshaller messageMarshaller = (MessageMarshaller) serCtx.getMarshaller(entityName);
Class clazz = messageMarshaller.getJavaClass();
return queryInterceptor.isIndexed(clazz) ? clazz : null;
}
};
LuceneProcessingChain processingChain = new LuceneProcessingChain.Builder(searchFactory, entityNamesResolver)
.buildProcessingChainForClassBasedEntities();
LuceneQueryParsingResult parsingResult = queryParser.parseQuery(request.getJpqlString(), processingChain);
MessageMarshaller messageMarshaller = (MessageMarshaller) serCtx.getMarshaller(parsingResult.getTargetEntity());
messageDescriptor = serCtx.getMessageDescriptor(messageMarshaller.getTypeName());
targetEntity = parsingResult.getTargetEntity();
projections = parsingResult.getProjections();
luceneQuery = parsingResult.getQuery();
} else {
EntityNamesResolver entityNamesResolver = new EntityNamesResolver() {
@Override
public Class<?> getClassFromName(String entityName) {
return serCtx.canMarshall(entityName) ? ProtobufValueWrapper.class : null;
}
};
FieldBridgeProvider fieldBridgeProvider = new FieldBridgeProvider() {
@Override
public FieldBridge getFieldBridge(String type, String propertyPath) {
Descriptors.Descriptor md = serCtx.getMessageDescriptor(type);
Descriptors.FieldDescriptor fd = getFieldDescriptor(md, propertyPath);
switch (fd.getType()) {
case DOUBLE:
return new NullEncodingDoubleNumericFieldBridge(NULL_TOKEN);
case FLOAT:
return new NullEncodingFloatNumericFieldBridge(NULL_TOKEN);
case INT64:
case UINT64:
case FIXED64:
case SFIXED64:
case SINT64:
return new NullEncodingLongNumericFieldBridge(NULL_TOKEN);
case INT32:
case FIXED32:
case UINT32:
case SFIXED32:
case SINT32:
case BOOL:
case ENUM:
return new NullEncodingIntegerNumericFieldBridge(NULL_TOKEN);
case STRING:
case BYTES:
case GROUP:
case MESSAGE:
return new NullEncodingTwoWayFieldBridge(BridgeFactory.STRING, NULL_TOKEN);
}
return null;
}
};
LuceneProcessingChain processingChain = new LuceneProcessingChain.Builder(searchFactory, entityNamesResolver)
.buildProcessingChainForDynamicEntities(fieldBridgeProvider);
LuceneQueryParsingResult parsingResult = queryParser.parseQuery(request.getJpqlString(), processingChain);
targetEntity = parsingResult.getTargetEntity();
messageDescriptor = serCtx.getMessageDescriptor(parsingResult.getTargetEntityName());
projections = parsingResult.getProjections();
QueryBuilder qb = searchManager.getSearchFactory().buildQueryBuilder().forEntity(targetEntity).get();
luceneQuery = qb.bool()
.must(qb.keyword().onField(TYPE_FIELD_NAME).ignoreFieldBridge().ignoreAnalyzer().matching(messageDescriptor.getFullName()).createQuery())