Search in sources :

Example 1 with E

use of com.baidu.hugegraph.util.E in project incubator-hugegraph by apache.

the class HugeTraverser method edgesOfVertex.

private Iterator<Edge> edgesOfVertex(Id source, EdgeStep edgeStep, boolean mustAllSK) {
    Id[] edgeLabels = edgeStep.edgeLabels();
    Query query = GraphTransaction.constructEdgesQuery(source, edgeStep.direction(), edgeLabels);
    ConditionQuery filter = null;
    if (mustAllSK) {
        this.fillFilterBySortKeys(query, edgeLabels, edgeStep.properties());
    } else {
        filter = (ConditionQuery) query.copy();
        this.fillFilterByProperties(filter, edgeStep.properties());
    }
    query.capacity(Query.NO_CAPACITY);
    if (edgeStep.limit() != NO_LIMIT) {
        query.limit(edgeStep.limit());
    }
    Iterator<Edge> edges = this.graph().edges(query);
    if (filter != null) {
        ConditionQuery finalFilter = filter;
        edges = new FilterIterator<>(edges, (e) -> {
            return finalFilter.test((HugeEdge) e);
        });
    }
    return edgeStep.skipSuperNodeIfNeeded(edges);
}
Also used : QueryResults(com.baidu.hugegraph.backend.query.QueryResults) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) NotFoundException(com.baidu.hugegraph.exception.NotFoundException) CollectionUtil(com.baidu.hugegraph.util.CollectionUtil) MultivaluedMap(jakarta.ws.rs.core.MultivaluedMap) FilterIterator(com.baidu.hugegraph.iterator.FilterIterator) ImmutableList(com.google.common.collect.ImmutableList) CollectionUtils(org.apache.commons.collections.CollectionUtils) HugeGraph(com.baidu.hugegraph.HugeGraph) Map(java.util.Map) Query(com.baidu.hugegraph.backend.query.Query) CollectionFactory(com.baidu.hugegraph.util.collection.CollectionFactory) E(com.baidu.hugegraph.util.E) CoreOptions(com.baidu.hugegraph.config.CoreOptions) Edge(org.apache.tinkerpop.gremlin.structure.Edge) MapperIterator(com.baidu.hugegraph.iterator.MapperIterator) CollectionType(com.baidu.hugegraph.type.define.CollectionType) Logger(org.slf4j.Logger) ImmutableSet(com.google.common.collect.ImmutableSet) Iterator(java.util.Iterator) ExtendableIterator(com.baidu.hugegraph.iterator.ExtendableIterator) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) EdgeStep(com.baidu.hugegraph.traversal.algorithm.steps.EdgeStep) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) InsertionOrderUtil(com.baidu.hugegraph.util.InsertionOrderUtil) HugeKeys(com.baidu.hugegraph.type.define.HugeKeys) GraphTransaction(com.baidu.hugegraph.backend.tx.GraphTransaction) LimitIterator(com.baidu.hugegraph.iterator.LimitIterator) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched) Objects(java.util.Objects) MultivaluedHashMap(jakarta.ws.rs.core.MultivaluedHashMap) SchemaLabel(com.baidu.hugegraph.schema.SchemaLabel) HugeException(com.baidu.hugegraph.HugeException) List(java.util.List) Log(com.baidu.hugegraph.util.Log) Directions(com.baidu.hugegraph.type.define.Directions) Aggregate(com.baidu.hugegraph.backend.query.Aggregate) Id(com.baidu.hugegraph.backend.id.Id) TraversalUtil(com.baidu.hugegraph.traversal.optimize.TraversalUtil) Collections(java.util.Collections) HugeType(com.baidu.hugegraph.type.HugeType) Query(com.baidu.hugegraph.backend.query.Query) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) Id(com.baidu.hugegraph.backend.id.Id) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge)

Example 2 with E

use of com.baidu.hugegraph.util.E in project incubator-hugegraph by apache.

the class HbaseStore method truncate.

@Override
public void truncate() {
    this.checkOpened();
    // Total time may cost 3 * TRUNCATE_TIMEOUT, due to there are 3 stores
    long timeout = this.sessions.config().get(HbaseOptions.TRUNCATE_TIMEOUT);
    long start = System.currentTimeMillis();
    BiFunction<String, Future<Void>, Void> wait = (table, future) -> {
        long elapsed = System.currentTimeMillis() - start;
        long remainingTime = timeout - elapsed / 1000L;
        try {
            return future.get(remainingTime, TimeUnit.SECONDS);
        } catch (Exception e) {
            throw new BackendException("Error when truncating table '%s' of '%s' store: %s", table, this.store, e.toString());
        }
    };
    // Truncate tables
    List<String> tables = this.tableNames();
    Map<String, Future<Void>> futures = new HashMap<>(tables.size());
    try {
        // Disable tables async
        for (String table : tables) {
            futures.put(table, this.sessions.disableTableAsync(table));
        }
        for (Map.Entry<String, Future<Void>> entry : futures.entrySet()) {
            wait.apply(entry.getKey(), entry.getValue());
        }
    } catch (Exception e) {
        this.enableTables();
        throw new BackendException("Failed to disable table for '%s' store", e, this.store);
    }
    try {
        // Truncate tables async
        for (String table : tables) {
            futures.put(table, this.sessions.truncateTableAsync(table));
        }
        for (Map.Entry<String, Future<Void>> entry : futures.entrySet()) {
            wait.apply(entry.getKey(), entry.getValue());
        }
    } catch (Exception e) {
        this.enableTables();
        throw new BackendException("Failed to truncate table for '%s' store", e, this.store);
    }
    LOG.debug("Store truncated: {}", this.store);
}
Also used : TableNotFoundException(org.apache.hadoop.hbase.TableNotFoundException) BackendEntry(com.baidu.hugegraph.backend.store.BackendEntry) BiFunction(java.util.function.BiFunction) BackendException(com.baidu.hugegraph.backend.BackendException) HashMap(java.util.HashMap) BackendStoreProvider(com.baidu.hugegraph.backend.store.BackendStoreProvider) ConnectionException(com.baidu.hugegraph.exception.ConnectionException) AbstractBackendStore(com.baidu.hugegraph.backend.store.AbstractBackendStore) Future(java.util.concurrent.Future) BackendAction(com.baidu.hugegraph.backend.store.BackendAction) Map(java.util.Map) Query(com.baidu.hugegraph.backend.query.Query) Session(com.baidu.hugegraph.backend.store.hbase.HbaseSessions.Session) E(com.baidu.hugegraph.util.E) NamespaceExistException(org.apache.hadoop.hbase.NamespaceExistException) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) TableExistsException(org.apache.hadoop.hbase.TableExistsException) BackendFeatures(com.baidu.hugegraph.backend.store.BackendFeatures) IOException(java.io.IOException) BackendMutation(com.baidu.hugegraph.backend.store.BackendMutation) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Log(com.baidu.hugegraph.util.Log) Id(com.baidu.hugegraph.backend.id.Id) HugeConfig(com.baidu.hugegraph.config.HugeConfig) HugeType(com.baidu.hugegraph.type.HugeType) HashMap(java.util.HashMap) TableNotFoundException(org.apache.hadoop.hbase.TableNotFoundException) BackendException(com.baidu.hugegraph.backend.BackendException) ConnectionException(com.baidu.hugegraph.exception.ConnectionException) NamespaceExistException(org.apache.hadoop.hbase.NamespaceExistException) TableExistsException(org.apache.hadoop.hbase.TableExistsException) IOException(java.io.IOException) BackendException(com.baidu.hugegraph.backend.BackendException) Future(java.util.concurrent.Future) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with E

use of com.baidu.hugegraph.util.E in project incubator-hugegraph by apache.

the class HugeGremlinServer method start.

public static GremlinServer start(String conf, String graphsDir, EventHub hub) throws Exception {
    // Start GremlinServer with inject traversal source
    LOG.info(GremlinServer.getHeader());
    final Settings settings;
    try {
        settings = Settings.read(conf);
    } catch (Exception e) {
        LOG.error("Can't found the configuration file at '{}' or " + "being parsed properly. [{}]", conf, e.getMessage());
        throw e;
    }
    // Scan graph confs and inject into gremlin server context
    E.checkState(settings.graphs != null, "The GremlinServer's settings.graphs is null");
    settings.graphs.putAll(ConfigUtil.scanGraphsDir(graphsDir));
    LOG.info("Configuring Gremlin Server from {}", conf);
    ContextGremlinServer server = new ContextGremlinServer(settings, hub);
    // Inject customized traversal source
    server.injectTraversalSource();
    server.start().exceptionally(t -> {
        LOG.error("Gremlin Server was unable to start and will " + "shutdown now: {}", t.getMessage());
        server.stop().join();
        throw new HugeException("Failed to start Gremlin Server");
    }).join();
    return server;
}
Also used : HugeException(com.baidu.hugegraph.HugeException) ContextGremlinServer(com.baidu.hugegraph.auth.ContextGremlinServer) Log(com.baidu.hugegraph.util.Log) Logger(org.slf4j.Logger) ConfigUtil(com.baidu.hugegraph.util.ConfigUtil) GremlinServer(org.apache.tinkerpop.gremlin.server.GremlinServer) Settings(org.apache.tinkerpop.gremlin.server.Settings) EventHub(com.baidu.hugegraph.event.EventHub) E(com.baidu.hugegraph.util.E) ContextGremlinServer(com.baidu.hugegraph.auth.ContextGremlinServer) HugeException(com.baidu.hugegraph.HugeException) Settings(org.apache.tinkerpop.gremlin.server.Settings) HugeException(com.baidu.hugegraph.HugeException)

Example 4 with E

use of com.baidu.hugegraph.util.E in project incubator-hugegraph by apache.

the class GraphTransaction method joinTxEdges.

private Iterator<?> joinTxEdges(Query query, Iterator<HugeEdge> edges, Map<Id, HugeVertex> removingVertices) {
    assert query.resultType().isEdge();
    BiFunction<Query, HugeEdge, HugeEdge> matchTxFunc = (q, e) -> {
        assert q.resultType() == HugeType.EDGE;
        if (e.expired() && !q.showExpired()) {
            // Filter expired edges with TTL
            return null;
        }
        // Filter edges matched conditions
        return q.test(e) ? e : q.test(e = e.switchOwner()) ? e : null;
    };
    edges = this.joinTxRecords(query, edges, matchTxFunc, this.addedEdges, this.removedEdges, this.updatedEdges);
    if (removingVertices.isEmpty()) {
        return edges;
    }
    // Filter edges that belong to deleted vertex
    return new FilterIterator<HugeEdge>(edges, edge -> {
        for (HugeVertex v : removingVertices.values()) {
            if (edge.belongToVertex(v)) {
                return false;
            }
        }
        return true;
    });
}
Also used : Arrays(java.util.Arrays) QueryResults(com.baidu.hugegraph.backend.query.QueryResults) ListIterator(com.baidu.hugegraph.iterator.ListIterator) BiFunction(java.util.function.BiFunction) Graph(org.apache.tinkerpop.gremlin.structure.Graph) BackendException(com.baidu.hugegraph.backend.BackendException) HugeProperty(com.baidu.hugegraph.structure.HugeProperty) HugeElement(com.baidu.hugegraph.structure.HugeElement) ElementHelper(org.apache.tinkerpop.gremlin.structure.util.ElementHelper) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) SplicingIdGenerator(com.baidu.hugegraph.backend.id.SplicingIdGenerator) LimitExceedException(com.baidu.hugegraph.exception.LimitExceedException) DeleteExpiredJob(com.baidu.hugegraph.job.system.DeleteExpiredJob) LockUtil(com.baidu.hugegraph.util.LockUtil) Map(java.util.Map) Query(com.baidu.hugegraph.backend.query.Query) EdgeLabel(com.baidu.hugegraph.schema.EdgeLabel) IteratorUtils(org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils) MapperIterator(com.baidu.hugegraph.iterator.MapperIterator) BackendStore(com.baidu.hugegraph.backend.store.BackendStore) ExtendableIterator(com.baidu.hugegraph.iterator.ExtendableIterator) FlatMapperIterator(com.baidu.hugegraph.iterator.FlatMapperIterator) IndexLabel(com.baidu.hugegraph.schema.IndexLabel) Collection(java.util.Collection) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) Set(java.util.Set) QueryList(com.baidu.hugegraph.backend.page.QueryList) InsertionOrderUtil(com.baidu.hugegraph.util.InsertionOrderUtil) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) LimitIterator(com.baidu.hugegraph.iterator.LimitIterator) ForbiddenException(jakarta.ws.rs.ForbiddenException) List(java.util.List) Element(org.apache.tinkerpop.gremlin.structure.Element) CloseableIterator(org.apache.tinkerpop.gremlin.structure.util.CloseableIterator) BatchMapperIterator(com.baidu.hugegraph.iterator.BatchMapperIterator) IdStrategy(com.baidu.hugegraph.type.define.IdStrategy) Id(com.baidu.hugegraph.backend.id.Id) AggregateFunc(com.baidu.hugegraph.backend.query.Aggregate.AggregateFunc) HugeVertexFeatures(com.baidu.hugegraph.structure.HugeFeatures.HugeVertexFeatures) HugeGraphParams(com.baidu.hugegraph.HugeGraphParams) BackendEntry(com.baidu.hugegraph.backend.store.BackendEntry) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) NotFoundException(com.baidu.hugegraph.exception.NotFoundException) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ConditionQueryFlatten(com.baidu.hugegraph.backend.query.ConditionQueryFlatten) FilterIterator(com.baidu.hugegraph.iterator.FilterIterator) PropertyKey(com.baidu.hugegraph.schema.PropertyKey) ImmutableList(com.google.common.collect.ImmutableList) CollectionUtils(org.apache.commons.collections.CollectionUtils) HugeGraph(com.baidu.hugegraph.HugeGraph) OptimizedType(com.baidu.hugegraph.backend.query.ConditionQuery.OptimizedType) E(com.baidu.hugegraph.util.E) CoreOptions(com.baidu.hugegraph.config.CoreOptions) Edge(org.apache.tinkerpop.gremlin.structure.Edge) VertexLabel(com.baidu.hugegraph.schema.VertexLabel) PageInfo(com.baidu.hugegraph.backend.page.PageInfo) Iterator(java.util.Iterator) HugeEdgeProperty(com.baidu.hugegraph.structure.HugeEdgeProperty) Condition(com.baidu.hugegraph.backend.query.Condition) EdgeId(com.baidu.hugegraph.backend.id.EdgeId) BackendMutation(com.baidu.hugegraph.backend.store.BackendMutation) HugeKeys(com.baidu.hugegraph.type.define.HugeKeys) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched) Consumer(java.util.function.Consumer) SchemaLabel(com.baidu.hugegraph.schema.SchemaLabel) Action(com.baidu.hugegraph.type.define.Action) HugeException(com.baidu.hugegraph.HugeException) IdQuery(com.baidu.hugegraph.backend.query.IdQuery) Directions(com.baidu.hugegraph.type.define.Directions) Aggregate(com.baidu.hugegraph.backend.query.Aggregate) HugeVertexProperty(com.baidu.hugegraph.structure.HugeVertexProperty) HugeIndex(com.baidu.hugegraph.structure.HugeIndex) HugeConfig(com.baidu.hugegraph.config.HugeConfig) IdHolderList(com.baidu.hugegraph.backend.page.IdHolderList) HugeType(com.baidu.hugegraph.type.HugeType) Query(com.baidu.hugegraph.backend.query.Query) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) IdQuery(com.baidu.hugegraph.backend.query.IdQuery) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) FilterIterator(com.baidu.hugegraph.iterator.FilterIterator) HugeVertex(com.baidu.hugegraph.structure.HugeVertex)

Example 5 with E

use of com.baidu.hugegraph.util.E in project incubator-hugegraph by apache.

the class API method commit.

public static <R> R commit(HugeGraph g, Callable<R> callable) {
    Consumer<Throwable> rollback = (error) -> {
        if (error != null) {
            LOG.error("Failed to commit", error);
        }
        try {
            g.tx().rollback();
        } catch (Throwable e) {
            LOG.error("Failed to rollback", e);
        }
    };
    try {
        R result = callable.call();
        g.tx().commit();
        SUCCEED_METER.mark();
        return result;
    } catch (IllegalArgumentException | NotFoundException | ForbiddenException e) {
        ILLEGAL_ARG_ERROR_METER.mark();
        rollback.accept(null);
        throw e;
    } catch (RuntimeException e) {
        EXPECTED_ERROR_METER.mark();
        rollback.accept(e);
        throw e;
    } catch (Throwable e) {
        UNKNOWN_ERROR_METER.mark();
        rollback.accept(e);
        // TODO: throw the origin exception 'e'
        throw new HugeException("Failed to commit", e);
    }
}
Also used : NotFoundException(jakarta.ws.rs.NotFoundException) NotSupportedException(jakarta.ws.rs.NotSupportedException) Logger(org.slf4j.Logger) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) Callable(java.util.concurrent.Callable) ForbiddenException(jakarta.ws.rs.ForbiddenException) Consumer(java.util.function.Consumer) HugeException(com.baidu.hugegraph.HugeException) RestServer(com.baidu.hugegraph.server.RestServer) Meter(com.codahale.metrics.Meter) Log(com.baidu.hugegraph.util.Log) Checkable(com.baidu.hugegraph.define.Checkable) MetricsUtil(com.baidu.hugegraph.metrics.MetricsUtil) JsonUtil(com.baidu.hugegraph.util.JsonUtil) MediaType(jakarta.ws.rs.core.MediaType) HugeGraph(com.baidu.hugegraph.HugeGraph) Map(java.util.Map) E(com.baidu.hugegraph.util.E) GraphManager(com.baidu.hugegraph.core.GraphManager) ForbiddenException(jakarta.ws.rs.ForbiddenException) NotFoundException(jakarta.ws.rs.NotFoundException) HugeException(com.baidu.hugegraph.HugeException)

Aggregations

E (com.baidu.hugegraph.util.E)5 HugeException (com.baidu.hugegraph.HugeException)4 Log (com.baidu.hugegraph.util.Log)4 Map (java.util.Map)4 Logger (org.slf4j.Logger)4 HugeGraph (com.baidu.hugegraph.HugeGraph)3 Id (com.baidu.hugegraph.backend.id.Id)3 Query (com.baidu.hugegraph.backend.query.Query)3 HugeType (com.baidu.hugegraph.type.HugeType)3 Collection (java.util.Collection)3 Iterator (java.util.Iterator)3 List (java.util.List)3 BackendException (com.baidu.hugegraph.backend.BackendException)2 Aggregate (com.baidu.hugegraph.backend.query.Aggregate)2 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)2 QueryResults (com.baidu.hugegraph.backend.query.QueryResults)2 BackendEntry (com.baidu.hugegraph.backend.store.BackendEntry)2 BackendMutation (com.baidu.hugegraph.backend.store.BackendMutation)2 CoreOptions (com.baidu.hugegraph.config.CoreOptions)2 HugeConfig (com.baidu.hugegraph.config.HugeConfig)2