Search in sources :

Example 41 with HugeClient

use of com.baidu.hugegraph.driver.HugeClient in project incubator-hugegraph-toolchain by apache.

the class ExecuteHistoryService method list.

public IPage<ExecuteHistory> list(int connId, long current, long pageSize) {
    HugeClient client = this.getClient(connId);
    QueryWrapper<ExecuteHistory> query = Wrappers.query();
    query.eq("conn_id", connId).orderByDesc("create_time");
    Page<ExecuteHistory> page = new Page<>(current, pageSize);
    IPage<ExecuteHistory> results = this.mapper.selectPage(page, query);
    int limit = this.config.get(HubbleOptions.EXECUTE_HISTORY_SHOW_LIMIT);
    if (results.getTotal() > limit) {
        log.debug("Execute history total records: {}", results.getTotal());
        results.setTotal(limit);
    }
    // Get the status of successful execution of asynchronous tasks
    Instant now = HubbleUtil.nowTime();
    results.getRecords().forEach((p) -> {
        if (p.getType().equals(ExecuteType.GREMLIN_ASYNC)) {
            try {
                Task task = client.task().get(p.getAsyncId());
                long endDate = task.updateTime() > 0 ? task.updateTime() : now.getLong(ChronoField.INSTANT_SECONDS);
                p.setDuration(endDate - task.createTime());
                p.setAsyncStatus(task.status().toUpperCase());
            } catch (Exception e) {
                p.setDuration(0L);
                p.setAsyncStatus(AsyncTaskStatus.UNKNOWN);
            }
        }
    });
    return results;
}
Also used : HugeClient(com.baidu.hugegraph.driver.HugeClient) Task(com.baidu.hugegraph.structure.Task) ExecuteHistory(com.baidu.hugegraph.entity.query.ExecuteHistory) Instant(java.time.Instant) Page(com.baomidou.mybatisplus.extension.plugins.pagination.Page) IPage(com.baomidou.mybatisplus.core.metadata.IPage) InternalException(com.baidu.hugegraph.exception.InternalException)

Example 42 with HugeClient

use of com.baidu.hugegraph.driver.HugeClient in project incubator-hugegraph-toolchain by apache.

the class GremlinQueryService method executeQuery.

public GremlinResult executeQuery(int connId, GremlinQuery query) {
    HugeClient client = this.getClient(connId);
    log.debug("The original gremlin ==> {}", query.getContent());
    String gremlin = this.optimize(query.getContent());
    log.debug("The optimized gremlin ==> {}", gremlin);
    // Execute gremlin query
    ResultSet resultSet = this.executeGremlin(gremlin, client);
    // Scan data, vote the result type
    TypedResult typedResult = this.parseResults(resultSet);
    // Build json view
    JsonView jsonView = new JsonView(typedResult.getData());
    // Build table view
    TableView tableView = this.buildTableView(typedResult);
    // Build graph view
    GraphView graphView = this.buildGraphView(typedResult, client);
    return GremlinResult.builder().type(typedResult.getType()).jsonView(jsonView).tableView(tableView).graphView(graphView).build();
}
Also used : HugeClient(com.baidu.hugegraph.driver.HugeClient) ResultSet(com.baidu.hugegraph.structure.gremlin.ResultSet) JsonView(com.baidu.hugegraph.entity.query.JsonView) TypedResult(com.baidu.hugegraph.entity.query.TypedResult) GraphView(com.baidu.hugegraph.entity.query.GraphView) TableView(com.baidu.hugegraph.entity.query.TableView)

Example 43 with HugeClient

use of com.baidu.hugegraph.driver.HugeClient in project incubator-hugegraph-toolchain by apache.

the class GremlinQueryService method executeAsyncTask.

public Long executeAsyncTask(int connId, GremlinQuery query) {
    HugeClient client = this.getClient(connId);
    log.debug("The async gremlin ==> {}", query.getContent());
    // Execute optimized gremlin query
    GremlinRequest request = new GremlinRequest(query.getContent());
    return client.gremlin().executeAsTask(request);
}
Also used : HugeClient(com.baidu.hugegraph.driver.HugeClient) GremlinRequest(com.baidu.hugegraph.api.gremlin.GremlinRequest)

Example 44 with HugeClient

use of com.baidu.hugegraph.driver.HugeClient in project incubator-hugegraph-toolchain by apache.

the class PropertyIndexService method list.

public IPage<PropertyIndex> list(int connId, HugeType type, int pageNo, int pageSize) {
    HugeClient client = this.client(connId);
    List<IndexLabel> indexLabels = client.schema().getIndexLabels();
    List<PropertyIndex> results = new ArrayList<>();
    for (IndexLabel indexLabel : indexLabels) {
        if (!indexLabel.baseType().equals(type)) {
            continue;
        }
        // Collect all indexlabels
        results.add(convert(indexLabel));
    }
    // Sort by owner name a-z, then index name a-z
    results.sort((o1, o2) -> {
        String owner1 = o1.getOwner();
        String owner2 = o2.getOwner();
        if (!owner1.equals(owner2)) {
            return owner1.compareTo(owner2);
        }
        return o1.getName().compareTo(o2.getName());
    });
    return PageUtil.page(results, pageNo, pageSize);
}
Also used : HugeClient(com.baidu.hugegraph.driver.HugeClient) IndexLabel(com.baidu.hugegraph.structure.schema.IndexLabel) ArrayList(java.util.ArrayList) PropertyIndex(com.baidu.hugegraph.entity.schema.PropertyIndex)

Example 45 with HugeClient

use of com.baidu.hugegraph.driver.HugeClient in project incubator-hugegraph-toolchain by apache.

the class PropertyIndexService method list.

/**
 * The sort result like that, content is 'name'
 * --------------+------------------------+---------------------------------
 * base_value    | index label name       | fields
 * --------------+------------------------+---------------------------------
 * xxxname       | xxxByName              | name
 * --------------+------------------------+---------------------------------
 *               | personByName           | name
 * person        +------------------------+---------------------------------
 *               | personByAgeAndName     | age name
 * --------------+------------------------+---------------------------------
 *               | softwareByName         | name
 * software      +------------------------+---------------------------------
 *               | softwareByPriveAndName | price name
 * --------------+------------------------+---------------------------------
 */
public IPage<PropertyIndex> list(int connId, HugeType type, String content, int pageNo, int pageSize) {
    HugeClient client = this.client(connId);
    List<IndexLabel> indexLabels = client.schema().getIndexLabels();
    Map<String, List<PropertyIndex>> matchedResults = new HashMap<>();
    Map<String, List<PropertyIndex>> unMatchResults = new HashMap<>();
    for (IndexLabel indexLabel : indexLabels) {
        if (!indexLabel.baseType().equals(type)) {
            continue;
        }
        String baseValue = indexLabel.baseValue();
        List<PropertyIndex> groupedIndexes;
        // Collect indexlabels that contains content
        boolean match = baseValue.contains(content);
        if (match) {
            groupedIndexes = matchedResults.computeIfAbsent(baseValue, k -> new ArrayList<>());
        } else {
            groupedIndexes = unMatchResults.computeIfAbsent(baseValue, k -> new ArrayList<>());
        }
        match = match || indexLabel.name().contains(content) || indexLabel.indexFields().stream().anyMatch(f -> f.contains(content));
        if (match) {
            groupedIndexes.add(convert(indexLabel));
        }
    }
    // Sort matched results by relevance
    if (!StringUtils.isEmpty(content)) {
        for (Map.Entry<String, List<PropertyIndex>> entry : matchedResults.entrySet()) {
            List<PropertyIndex> groupedIndexes = entry.getValue();
            groupedIndexes.sort(new Comparator<PropertyIndex>() {

                final int highScore = 2;

                final int lowScore = 1;

                @Override
                public int compare(PropertyIndex o1, PropertyIndex o2) {
                    int o1Score = 0;
                    if (o1.getName().contains(content)) {
                        o1Score += highScore;
                    }
                    if (o1.getFields().stream().anyMatch(field -> field.contains(content))) {
                        o1Score += lowScore;
                    }
                    int o2Score = 0;
                    if (o2.getName().contains(content)) {
                        o2Score += highScore;
                    }
                    if (o2.getFields().stream().anyMatch(field -> field.contains(content))) {
                        o2Score += lowScore;
                    }
                    return o2Score - o1Score;
                }
            });
        }
    }
    List<PropertyIndex> all = new ArrayList<>();
    matchedResults.values().forEach(all::addAll);
    unMatchResults.values().forEach(all::addAll);
    return PageUtil.page(all, pageNo, pageSize);
}
Also used : ServerException(com.baidu.hugegraph.exception.ServerException) BiFunction(java.util.function.BiFunction) SchemaEntity(com.baidu.hugegraph.entity.schema.SchemaEntity) HashMap(java.util.HashMap) PropertyIndex(com.baidu.hugegraph.entity.schema.PropertyIndex) ArrayList(java.util.ArrayList) Constant(com.baidu.hugegraph.common.Constant) Service(org.springframework.stereotype.Service) Map(java.util.Map) HugeClient(com.baidu.hugegraph.driver.HugeClient) Collection(java.util.Collection) ConflictDetail(com.baidu.hugegraph.entity.schema.ConflictDetail) IndexLabel(com.baidu.hugegraph.structure.schema.IndexLabel) ConflictStatus(com.baidu.hugegraph.entity.schema.ConflictStatus) Collectors(java.util.stream.Collectors) HugeType(com.baidu.hugegraph.structure.constant.HugeType) List(java.util.List) CollectionUtils(org.springframework.util.CollectionUtils) SchemaConflict(com.baidu.hugegraph.entity.schema.SchemaConflict) SchemaType(com.baidu.hugegraph.entity.schema.SchemaType) Log4j2(lombok.extern.log4j.Log4j2) PageUtil(com.baidu.hugegraph.util.PageUtil) Comparator(java.util.Comparator) Collections(java.util.Collections) IPage(com.baomidou.mybatisplus.core.metadata.IPage) StringUtils(org.springframework.util.StringUtils) HugeClient(com.baidu.hugegraph.driver.HugeClient) HashMap(java.util.HashMap) IndexLabel(com.baidu.hugegraph.structure.schema.IndexLabel) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) PropertyIndex(com.baidu.hugegraph.entity.schema.PropertyIndex)

Aggregations

HugeClient (com.baidu.hugegraph.driver.HugeClient)66 Vertex (com.baidu.hugegraph.structure.graph.Vertex)21 ArrayList (java.util.ArrayList)16 ExternalException (com.baidu.hugegraph.exception.ExternalException)15 IndexLabel (com.baidu.hugegraph.structure.schema.IndexLabel)14 ServerException (com.baidu.hugegraph.exception.ServerException)12 GraphManager (com.baidu.hugegraph.driver.GraphManager)11 Edge (com.baidu.hugegraph.structure.graph.Edge)10 SchemaManager (com.baidu.hugegraph.driver.SchemaManager)9 ResultSet (com.baidu.hugegraph.structure.gremlin.ResultSet)8 EdgeLabel (com.baidu.hugegraph.structure.schema.EdgeLabel)8 Test (org.junit.Test)8 VertexLabel (com.baidu.hugegraph.structure.schema.VertexLabel)7 PropertyKey (com.baidu.hugegraph.structure.schema.PropertyKey)6 ExecuteHistory (com.baidu.hugegraph.entity.query.ExecuteHistory)4 GraphView (com.baidu.hugegraph.entity.query.GraphView)4 LoadOptions (com.baidu.hugegraph.loader.executor.LoadOptions)4 ClientException (com.baidu.hugegraph.rest.ClientException)4 BeforeClass (org.junit.BeforeClass)4 GraphConnection (com.baidu.hugegraph.entity.GraphConnection)3