Search in sources :

Example 1 with HugeElement

use of com.baidu.hugegraph.structure.HugeElement in project incubator-hugegraph by apache.

the class DeleteExpiredIndexJob method deleteExpiredIndex.

/*
     * Delete expired element(if exist) of the index,
     * otherwise just delete expired index only
     */
private void deleteExpiredIndex(HugeGraphParams graph, HugeIndex index) {
    GraphTransaction tx = graph.graphTransaction();
    HugeType type = index.indexLabel().queryType().isVertex() ? HugeType.VERTEX : HugeType.EDGE;
    IdQuery query = new IdQuery(type);
    query.query(index.elementId());
    query.showExpired(true);
    Iterator<?> elements = type.isVertex() ? tx.queryVertices(query) : tx.queryEdges(query);
    if (elements.hasNext()) {
        HugeElement element = (HugeElement) elements.next();
        if (element.expiredTime() == index.expiredTime()) {
            element.remove();
        } else {
            tx.removeIndex(index);
        }
    } else {
        tx.removeIndex(index);
    }
}
Also used : IdQuery(com.baidu.hugegraph.backend.query.IdQuery) GraphTransaction(com.baidu.hugegraph.backend.tx.GraphTransaction) HugeType(com.baidu.hugegraph.type.HugeType) HugeElement(com.baidu.hugegraph.structure.HugeElement)

Example 2 with HugeElement

use of com.baidu.hugegraph.structure.HugeElement in project incubator-hugegraph by apache.

the class HugeGraphAuthProxy method verifyElemPermission.

private <V extends Element> V verifyElemPermission(HugePermission actionPerm, boolean throwIfNoPerm, Supplier<V> elementFetcher) {
    return verifyResPermission(actionPerm, throwIfNoPerm, () -> {
        String graph = this.hugegraph.name();
        HugeElement elem = (HugeElement) elementFetcher.get();
        @SuppressWarnings("unchecked") ResourceObject<V> r = (ResourceObject<V>) ResourceObject.of(graph, elem);
        return r;
    });
}
Also used : HugeElement(com.baidu.hugegraph.structure.HugeElement)

Example 3 with HugeElement

use of com.baidu.hugegraph.structure.HugeElement in project incubator-hugegraph by apache.

the class GraphTransaction method traverseByLabel.

private <T> void traverseByLabel(SchemaLabel label, Function<Query, Iterator<T>> fetcher, Consumer<T> consumer, boolean deleting) {
    HugeType type = label.type() == HugeType.VERTEX_LABEL ? HugeType.VERTEX : HugeType.EDGE;
    Query query = label.enableLabelIndex() ? new ConditionQuery(type) : new Query(type);
    query.capacity(Query.NO_CAPACITY);
    query.limit(Query.NO_LIMIT);
    if (this.store().features().supportsQueryByPage()) {
        query.page(PageInfo.PAGE_NONE);
    }
    if (label.hidden()) {
        query.showHidden(true);
    }
    query.showDeleting(deleting);
    query.showExpired(deleting);
    if (label.enableLabelIndex()) {
        // Support label index, query by label index by paging
        assert query instanceof ConditionQuery;
        ((ConditionQuery) query).eq(HugeKeys.LABEL, label.id());
        Iterator<T> iter = fetcher.apply(query);
        try {
            // Fetch by paging automatically
            while (iter.hasNext()) {
                consumer.accept(iter.next());
                /*
                     * Commit per batch to avoid too much data in single commit,
                     * especially for Cassandra backend
                     */
                this.commitIfGtSize(GraphTransaction.COMMIT_BATCH);
            }
            // Commit changes if exists
            this.commit();
        } finally {
            CloseableIterator.closeIterator(iter);
        }
    } else {
        // Not support label index, query all and filter by label
        if (query.paging()) {
            query.limit(this.pageSize);
        }
        String page = null;
        do {
            Iterator<T> iter = fetcher.apply(query);
            try {
                while (iter.hasNext()) {
                    T e = iter.next();
                    SchemaLabel elemLabel = ((HugeElement) e).schemaLabel();
                    if (label.equals(elemLabel)) {
                        consumer.accept(e);
                        /*
                             * Commit per batch to avoid too much data in single
                             * commit, especially for Cassandra backend
                             */
                        this.commitIfGtSize(GraphTransaction.COMMIT_BATCH);
                    }
                }
                // Commit changes of every page before next page query
                this.commit();
                if (query.paging()) {
                    page = PageInfo.pageState(iter).toString();
                    query.page(page);
                }
            } finally {
                CloseableIterator.closeIterator(iter);
            }
        } while (page != null);
    }
}
Also used : Query(com.baidu.hugegraph.backend.query.Query) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) IdQuery(com.baidu.hugegraph.backend.query.IdQuery) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) SchemaLabel(com.baidu.hugegraph.schema.SchemaLabel) HugeType(com.baidu.hugegraph.type.HugeType) HugeElement(com.baidu.hugegraph.structure.HugeElement)

Example 4 with HugeElement

use of com.baidu.hugegraph.structure.HugeElement in project incubator-hugegraph by apache.

the class DeleteExpiredElementJob method execute.

@Override
public V execute() throws Exception {
    LOG.debug("Delete expired elements: {}", this.elements);
    HugeGraphParams graph = this.params();
    GraphTransaction tx = graph.graphTransaction();
    try {
        for (HugeElement element : this.elements) {
            element.remove();
        }
        tx.commit();
    } catch (Exception e) {
        tx.rollback();
        LOG.warn("Failed to delete expired elements: {}", this.elements);
        throw e;
    } finally {
        JOB_COUNTERS.jobCounter(graph.graph()).decrement();
    }
    return null;
}
Also used : HugeGraphParams(com.baidu.hugegraph.HugeGraphParams) GraphTransaction(com.baidu.hugegraph.backend.tx.GraphTransaction) HugeElement(com.baidu.hugegraph.structure.HugeElement)

Example 5 with HugeElement

use of com.baidu.hugegraph.structure.HugeElement in project incubator-hugegraph by apache.

the class IndexLabelRebuildJob method rebuildIndex.

private void rebuildIndex(SchemaLabel label, Collection<Id> indexLabelIds) {
    SchemaTransaction schemaTx = this.params().schemaTransaction();
    GraphTransaction graphTx = this.params().graphTransaction();
    Consumer<?> indexUpdater = (elem) -> {
        for (Id id : indexLabelIds) {
            graphTx.updateIndex(id, (HugeElement) elem, false);
        }
    };
    LockUtil.Locks locks = new LockUtil.Locks(schemaTx.graphName());
    try {
        locks.lockWrites(LockUtil.INDEX_LABEL_REBUILD, indexLabelIds);
        Set<IndexLabel> ils = indexLabelIds.stream().map(this.graph()::indexLabel).collect(Collectors.toSet());
        for (IndexLabel il : ils) {
            if (il.status() == SchemaStatus.CREATING) {
                continue;
            }
            schemaTx.updateSchemaStatus(il, SchemaStatus.REBUILDING);
        }
        this.removeIndex(indexLabelIds);
        /*
             * Note: Here must commit index transaction firstly.
             * Because remove index convert to (id like <?>:personByCity):
             * `delete from index table where label = ?`,
             * But append index will convert to (id like Beijing:personByCity):
             * `update index element_ids += xxx where field_value = ?
             * and index_label_name = ?`,
             * They have different id lead to it can't compare and optimize
             */
        graphTx.commit();
        try {
            if (label.type() == HugeType.VERTEX_LABEL) {
                @SuppressWarnings("unchecked") Consumer<Vertex> consumer = (Consumer<Vertex>) indexUpdater;
                graphTx.traverseVerticesByLabel((VertexLabel) label, consumer, false);
            } else {
                assert label.type() == HugeType.EDGE_LABEL;
                @SuppressWarnings("unchecked") Consumer<Edge> consumer = (Consumer<Edge>) indexUpdater;
                graphTx.traverseEdgesByLabel((EdgeLabel) label, consumer, false);
            }
            graphTx.commit();
        } catch (Throwable e) {
            for (IndexLabel il : ils) {
                schemaTx.updateSchemaStatus(il, SchemaStatus.INVALID);
            }
            throw e;
        }
        for (IndexLabel il : ils) {
            schemaTx.updateSchemaStatus(il, SchemaStatus.CREATED);
        }
    } finally {
        locks.unlock();
    }
}
Also used : VertexLabel(com.baidu.hugegraph.schema.VertexLabel) SchemaElement(com.baidu.hugegraph.schema.SchemaElement) ImmutableSet(com.google.common.collect.ImmutableSet) IndexLabel(com.baidu.hugegraph.schema.IndexLabel) Collection(java.util.Collection) Set(java.util.Set) HugeElement(com.baidu.hugegraph.structure.HugeElement) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) SchemaTransaction(com.baidu.hugegraph.backend.tx.SchemaTransaction) GraphTransaction(com.baidu.hugegraph.backend.tx.GraphTransaction) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) SchemaLabel(com.baidu.hugegraph.schema.SchemaLabel) SchemaStatus(com.baidu.hugegraph.type.define.SchemaStatus) LockUtil(com.baidu.hugegraph.util.LockUtil) Id(com.baidu.hugegraph.backend.id.Id) EdgeLabel(com.baidu.hugegraph.schema.EdgeLabel) HugeType(com.baidu.hugegraph.type.HugeType) Edge(org.apache.tinkerpop.gremlin.structure.Edge) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) LockUtil(com.baidu.hugegraph.util.LockUtil) IndexLabel(com.baidu.hugegraph.schema.IndexLabel) GraphTransaction(com.baidu.hugegraph.backend.tx.GraphTransaction) HugeElement(com.baidu.hugegraph.structure.HugeElement) Consumer(java.util.function.Consumer) Id(com.baidu.hugegraph.backend.id.Id) Edge(org.apache.tinkerpop.gremlin.structure.Edge) SchemaTransaction(com.baidu.hugegraph.backend.tx.SchemaTransaction)

Aggregations

HugeElement (com.baidu.hugegraph.structure.HugeElement)6 GraphTransaction (com.baidu.hugegraph.backend.tx.GraphTransaction)3 HugeType (com.baidu.hugegraph.type.HugeType)3 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)2 IdQuery (com.baidu.hugegraph.backend.query.IdQuery)2 SchemaLabel (com.baidu.hugegraph.schema.SchemaLabel)2 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)2 HugeGraph (com.baidu.hugegraph.HugeGraph)1 HugeGraphParams (com.baidu.hugegraph.HugeGraphParams)1 Id (com.baidu.hugegraph.backend.id.Id)1 Query (com.baidu.hugegraph.backend.query.Query)1 SchemaTransaction (com.baidu.hugegraph.backend.tx.SchemaTransaction)1 EdgeLabel (com.baidu.hugegraph.schema.EdgeLabel)1 IndexLabel (com.baidu.hugegraph.schema.IndexLabel)1 SchemaElement (com.baidu.hugegraph.schema.SchemaElement)1 VertexLabel (com.baidu.hugegraph.schema.VertexLabel)1 FakeVertex (com.baidu.hugegraph.testutil.FakeObjects.FakeVertex)1 SchemaStatus (com.baidu.hugegraph.type.define.SchemaStatus)1 LockUtil (com.baidu.hugegraph.util.LockUtil)1 ImmutableSet (com.google.common.collect.ImmutableSet)1