use of org.apache.atlas.query.GremlinQuery in project incubator-atlas by apache.
the class GraphBackedDiscoveryService method parseAndTranslateDsl.
private GremlinQuery parseAndTranslateDsl(String dslQuery, QueryParams queryParams) throws DiscoveryException {
CompiledQueryCacheKey entry = new CompiledQueryCacheKey(dslQuery, queryParams);
GremlinQuery gremlinQuery = QueryProcessor.compiledQueryCache().get(entry);
if (gremlinQuery == null) {
Expressions.Expression validatedExpression = parseQuery(dslQuery, queryParams);
//If the final limit is 0, don't launch the query, return with 0 rows
if (validatedExpression instanceof Expressions.LimitExpression && ((Integer) ((Expressions.LimitExpression) validatedExpression).limit().rawValue()) == 0) {
gremlinQuery = new NoopGremlinQuery(validatedExpression.dataType());
} else {
gremlinQuery = new GremlinTranslator(validatedExpression, graphPersistenceStrategy).translate();
if (LOG.isDebugEnabled()) {
LOG.debug("Query = {}", validatedExpression);
LOG.debug("Expression Tree = {}", validatedExpression.treeString());
LOG.debug("Gremlin Query = {}", gremlinQuery.queryStr());
}
}
QueryProcessor.compiledQueryCache().put(entry, gremlinQuery);
}
return gremlinQuery;
}
use of org.apache.atlas.query.GremlinQuery in project incubator-atlas by apache.
the class EntityDiscoveryService method toGremlinQuery.
private GremlinQuery toGremlinQuery(String query, int limit, int offset) throws AtlasBaseException {
QueryParams params = validateSearchParams(limit, offset);
Either<NoSuccess, Expression> either = QueryParser.apply(query, params);
if (either.isLeft()) {
throw new AtlasBaseException(DISCOVERY_QUERY_FAILED, query);
}
Expression expression = either.right().get();
Expression validExpression = QueryProcessor.validate(expression);
GremlinQuery gremlinQuery = new GremlinTranslator(validExpression, graphPersistenceStrategy).translate();
if (LOG.isDebugEnabled()) {
LOG.debug("Translated Gremlin Query: {}", gremlinQuery.queryStr());
}
return gremlinQuery;
}
use of org.apache.atlas.query.GremlinQuery in project incubator-atlas by apache.
the class EntityDiscoveryService method searchUsingDslQuery.
@Override
public AtlasSearchResult searchUsingDslQuery(String dslQuery, int limit, int offset) throws AtlasBaseException {
AtlasSearchResult ret = new AtlasSearchResult(dslQuery, AtlasQueryType.DSL);
GremlinQuery gremlinQuery = toGremlinQuery(dslQuery, limit, offset);
if (LOG.isDebugEnabled()) {
LOG.debug("Executing DSL query: {}", dslQuery);
}
Object result = graph.executeGremlinScript(gremlinQuery.queryStr(), false);
if (result instanceof List && CollectionUtils.isNotEmpty((List) result)) {
List queryResult = (List) result;
Object firstElement = queryResult.get(0);
if (firstElement instanceof AtlasVertex) {
for (Object element : queryResult) {
if (element instanceof AtlasVertex) {
ret.addEntity(entityRetriever.toAtlasEntityHeader((AtlasVertex) element));
} else {
LOG.warn("searchUsingDslQuery({}): expected an AtlasVertex; found unexpected entry in result {}", dslQuery, element);
}
}
} else if (firstElement instanceof Map && (((Map) firstElement).containsKey("theInstance") || ((Map) firstElement).containsKey("theTrait"))) {
for (Object element : queryResult) {
if (element instanceof Map) {
Map map = (Map) element;
if (map.containsKey("theInstance")) {
Object value = map.get("theInstance");
if (value instanceof List && CollectionUtils.isNotEmpty((List) value)) {
Object entry = ((List) value).get(0);
if (entry instanceof AtlasVertex) {
ret.addEntity(entityRetriever.toAtlasEntityHeader((AtlasVertex) entry));
}
}
}
} else {
LOG.warn("searchUsingDslQuery({}): expected a trait result; found unexpected entry in result {}", dslQuery, element);
}
}
} else if (gremlinQuery.hasSelectList()) {
ret.setAttributes(toAttributesResult(queryResult, gremlinQuery));
}
}
return ret;
}
Aggregations