Search in sources :

Example 6 with PredicatesHolder

use of org.unipop.query.predicates.PredicatesHolder in project unipop by unipop-graph.

the class UniGraph method query.

private <E extends Element, C extends Comparable> Iterator<E> query(Class<E> returnType, Object[] ids) {
    PredicatesHolder idPredicate = createIdPredicate(ids, returnType);
    SearchQuery<E> uniQuery = new SearchQuery<>(returnType, idPredicate, -1, null, null, null, null);
    return queryControllers.stream().<E>flatMap(controller -> ConversionUtils.asStream(controller.search(uniQuery))).iterator();
}
Also used : SearchQuery(org.unipop.query.search.SearchQuery) Configuration(org.apache.commons.configuration.Configuration) java.util(java.util) TraversalStrategies(org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategies) GraphComputer(org.apache.tinkerpop.gremlin.process.computer.GraphComputer) PropertyTypeFactory(org.unipop.util.PropertyTypeFactory) ElementHelper(org.apache.tinkerpop.gremlin.structure.util.ElementHelper) StandardStrategyProvider(org.unipop.process.strategyregistrar.StandardStrategyProvider) StringUtils(org.apache.commons.lang3.StringUtils) PredicatesHolderFactory(org.unipop.query.predicates.PredicatesHolderFactory) org.apache.tinkerpop.gremlin.structure(org.apache.tinkerpop.gremlin.structure) TraversalFilter(org.unipop.structure.traversalfilter.TraversalFilter) UnipopGraphProvider(org.unipop.test.UnipopGraphProvider) PredicatesHolder(org.unipop.query.predicates.PredicatesHolder) ConversionUtils(org.unipop.util.ConversionUtils) P(org.apache.tinkerpop.gremlin.process.traversal.P) PropertySchema(org.unipop.schema.property.PropertySchema) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) AddVertexQuery(org.unipop.query.mutation.AddVertexQuery) Collectors(java.util.stream.Collectors) ConfigurationControllerManager(org.unipop.query.controller.ConfigurationControllerManager) StrategyProvider(org.unipop.process.strategyregistrar.StrategyProvider) DefaultTraversalFilter(org.unipop.structure.traversalfilter.DefaultTraversalFilter) Stream(java.util.stream.Stream) ControllerManager(org.unipop.query.controller.ControllerManager) StringFactory(org.apache.tinkerpop.gremlin.structure.util.StringFactory) SearchQuery(org.unipop.query.search.SearchQuery) GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) org.unipop.schema.property.type(org.unipop.schema.property.type) PredicatesHolder(org.unipop.query.predicates.PredicatesHolder)

Example 7 with PredicatesHolder

use of org.unipop.query.predicates.PredicatesHolder in project unipop by unipop-graph.

the class VertexSchema method toPredicates.

/**
 * Receives a list of vertices and converts them to predicates
 * @param vertices A list of vertices
 * @return Predicate holder
 */
default PredicatesHolder toPredicates(List<? extends Vertex> vertices) {
    if (vertices == null || vertices.size() == 0)
        return PredicatesHolderFactory.abort();
    HashSet<PredicatesHolder> predicates = new HashSet<>();
    vertices.stream().collect(Collectors.groupingBy(Vertex::label)).forEach((label, labelVertices) -> {
        HasContainer labelPredicate = new HasContainer(T.label.getAccessor(), P.eq(label));
        HasContainer ids = new HasContainer(T.id.getAccessor(), P.within(labelVertices.stream().map(Vertex::id).collect(Collectors.toSet())));
        PredicatesHolder labelPredicates = PredicatesHolderFactory.and(ids, labelPredicate);
        PredicatesHolder toPredicates = toPredicates(labelPredicates);
        predicates.add(toPredicates);
    });
    return PredicatesHolderFactory.or(predicates);
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) UniVertex(org.unipop.structure.UniVertex) PredicatesHolder(org.unipop.query.predicates.PredicatesHolder) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)

Example 8 with PredicatesHolder

use of org.unipop.query.predicates.PredicatesHolder in project unipop by unipop-graph.

the class RestEdge method getSearch.

@Override
public BaseRequest getSearch(SearchVertexQuery query) {
    int limit = query.getOrders() == null || query.getOrders().size() > 0 ? -1 : query.getLimit();
    PredicatesHolder edgePredicates = this.toPredicates(query.getPredicates());
    PredicatesHolder vertexPredicates = this.getVertexPredicates(query.getVertices(), query.getDirection());
    PredicatesHolder predicatesHolder = PredicatesHolderFactory.and(edgePredicates, vertexPredicates);
    return createSearch(predicatesHolder, limit);
}
Also used : PredicatesHolder(org.unipop.query.predicates.PredicatesHolder)

Example 9 with PredicatesHolder

use of org.unipop.query.predicates.PredicatesHolder in project unipop by unipop-graph.

the class DateFieldPropertySchema method getValues.

@Override
public Set<Object> getValues(PredicatesHolder predicatesHolder) {
    Stream<PredicatesHolder> predicates = predicatesHolder.findKey(this.key).map(this::explodeConnective);
    Map<String, Date> datePredicates = new HashMap<>();
    predicates.flatMap(p -> p.getPredicates().stream()).forEach(has -> {
        String biPredicate = has.getBiPredicate().toString();
        Object value = has.getValue();
        switch(biPredicate) {
            case "eq":
                datePredicates.put("eq", fromDisplay(value.toString()));
                break;
            case "gt":
                datePredicates.put("gt", fromDisplay(value.toString()));
            case "gte":
                datePredicates.put("gte", fromDisplay(value.toString()));
                break;
            case "lt":
                datePredicates.put("lt", fromDisplay(value.toString()));
                break;
            case "lte":
                datePredicates.put("lte", fromDisplay(value.toString()));
                break;
            default:
                throw new IllegalArgumentException("cant get value");
        }
    });
    if (datePredicates.size() == 0)
        return Collections.emptySet();
    if (datePredicates.containsKey("eq"))
        return Collections.singleton(toSource(datePredicates.get("eq")));
    else if ((datePredicates.containsKey("gt") || datePredicates.containsKey("gte")) && (datePredicates.containsKey("lt") || datePredicates.containsKey("lte"))) {
        Date from = datePredicates.containsKey("gt") ? datePredicates.get("gt") : datePredicates.get("gte");
        Date to = datePredicates.containsKey("lt") ? datePredicates.get("lt") : datePredicates.get("lte");
        List<Date> dates = new ArrayList<>();
        long interval = this.interval;
        long endTime = to.getTime();
        long curTime = from.getTime();
        while (curTime <= endTime) {
            dates.add(new Date(curTime));
            curTime += interval;
        }
        return dates.stream().map(this::toSource).collect(Collectors.toSet());
    } else
        throw new IllegalArgumentException("cant get only gt or lt value");
}
Also used : AndP(org.apache.tinkerpop.gremlin.process.traversal.util.AndP) MultiDateFormat(org.unipop.util.MultiDateFormat) java.util(java.util) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) SimpleDateFormat(java.text.SimpleDateFormat) Collectors(java.util.stream.Collectors) PredicatesHolderFactory(org.unipop.query.predicates.PredicatesHolderFactory) BiPredicate(java.util.function.BiPredicate) JSONObject(org.json.JSONObject) Stream(java.util.stream.Stream) ConnectiveP(org.apache.tinkerpop.gremlin.process.traversal.util.ConnectiveP) PredicatesHolder(org.unipop.query.predicates.PredicatesHolder) ConversionUtils(org.unipop.util.ConversionUtils) P(org.apache.tinkerpop.gremlin.process.traversal.P) JSONArray(org.json.JSONArray) DateFormat(java.text.DateFormat) PredicatesHolder(org.unipop.query.predicates.PredicatesHolder) JSONObject(org.json.JSONObject)

Example 10 with PredicatesHolder

use of org.unipop.query.predicates.PredicatesHolder in project unipop by unipop-graph.

the class ConcatenateFieldPropertySchema method stringValueToPredicate.

private PredicatesHolder stringValueToPredicate(String value, HasContainer has, boolean collection) {
    String[] values = value.split(delimiter);
    if (values.length < schemas.size())
        return PredicatesHolderFactory.abort();
    Set<PredicatesHolder> predicates = new HashSet<>();
    for (int i = 0; i < schemas.size(); i++) {
        P predicate = has.getPredicate().clone();
        final Object currentValue = values[i];
        P p = new P(predicate.getBiPredicate(), collection ? Arrays.asList(currentValue) : currentValue);
        PredicatesHolder predicatesHolder = schemas.get(i).toPredicates(PredicatesHolderFactory.predicate(new HasContainer(has.getKey(), p)));
        predicates.add(predicatesHolder);
    }
    return PredicatesHolderFactory.and(predicates);
}
Also used : P(org.apache.tinkerpop.gremlin.process.traversal.P) PredicatesHolder(org.unipop.query.predicates.PredicatesHolder) HasContainer(org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer) JSONObject(org.json.JSONObject)

Aggregations

PredicatesHolder (org.unipop.query.predicates.PredicatesHolder)23 HasContainer (org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer)9 Collectors (java.util.stream.Collectors)7 java.util (java.util)6 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)6 Stream (java.util.stream.Stream)5 P (org.apache.tinkerpop.gremlin.process.traversal.P)5 JSONObject (org.json.JSONObject)5 PredicatesHolderFactory (org.unipop.query.predicates.PredicatesHolderFactory)4 SearchQuery (org.unipop.query.search.SearchQuery)4 AddVertexQuery (org.unipop.query.mutation.AddVertexQuery)3 ConversionUtils (org.unipop.util.ConversionUtils)3 Configuration (org.apache.commons.configuration.Configuration)2 StringUtils (org.apache.commons.lang3.StringUtils)2 GraphComputer (org.apache.tinkerpop.gremlin.process.computer.GraphComputer)2 TraversalStrategies (org.apache.tinkerpop.gremlin.process.traversal.TraversalStrategies)2 GraphTraversalSource (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource)2 org.apache.tinkerpop.gremlin.structure (org.apache.tinkerpop.gremlin.structure)2 Element (org.apache.tinkerpop.gremlin.structure.Element)2 JSONArray (org.json.JSONArray)2