Search in sources :

Example 6 with SchemaTransaction

use of com.baidu.hugegraph.backend.tx.SchemaTransaction in project incubator-hugegraph by apache.

the class BackendStoreSystemInfoTest method testBackendStoreSystemInfoIllegalStateException.

@Test
public void testBackendStoreSystemInfoIllegalStateException() {
    HugeGraph graph = Mockito.mock(HugeGraph.class);
    SchemaTransaction stx = Mockito.mock(SchemaTransaction.class);
    Mockito.when(stx.getPropertyKey(PK_BACKEND_INFO)).thenThrow(new IllegalStateException("Should not exist schema " + "with same name '~backend_info'"));
    Mockito.when(stx.graph()).thenReturn(graph);
    Mockito.when(stx.storeInitialized()).thenReturn(true);
    BackendStoreSystemInfo info = new BackendStoreSystemInfo(stx);
    Assert.assertThrows(HugeException.class, () -> {
        Whitebox.invoke(BackendStoreSystemInfo.class, "info", info);
    }, e -> {
        Assert.assertContains("There exists multiple backend info", e.getMessage());
    });
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) BackendStoreSystemInfo(com.baidu.hugegraph.backend.store.BackendStoreSystemInfo) SchemaTransaction(com.baidu.hugegraph.backend.tx.SchemaTransaction) Test(org.junit.Test)

Example 7 with SchemaTransaction

use of com.baidu.hugegraph.backend.tx.SchemaTransaction in project incubator-hugegraph by apache.

the class IndexLabelRebuildJob method removeIndex.

private void removeIndex(Collection<Id> indexLabelIds) {
    SchemaTransaction schemaTx = this.params().schemaTransaction();
    GraphTransaction graphTx = this.params().graphTransaction();
    for (Id id : indexLabelIds) {
        IndexLabel il = schemaTx.getIndexLabel(id);
        if (il == null || il.status() == SchemaStatus.CREATING) {
            /*
                 * TODO: How to deal with non-existent index name:
                 * continue or throw exception?
                 */
            continue;
        }
        LockUtil.Locks locks = new LockUtil.Locks(schemaTx.graphName());
        try {
            locks.lockWrites(LockUtil.INDEX_LABEL_DELETE, indexLabelIds);
            graphTx.removeIndex(il);
        } catch (Throwable e) {
            schemaTx.updateSchemaStatus(il, SchemaStatus.INVALID);
            throw e;
        } finally {
            locks.unlock();
        }
    }
}
Also used : LockUtil(com.baidu.hugegraph.util.LockUtil) IndexLabel(com.baidu.hugegraph.schema.IndexLabel) GraphTransaction(com.baidu.hugegraph.backend.tx.GraphTransaction) Id(com.baidu.hugegraph.backend.id.Id) SchemaTransaction(com.baidu.hugegraph.backend.tx.SchemaTransaction)

Example 8 with SchemaTransaction

use of com.baidu.hugegraph.backend.tx.SchemaTransaction 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)

Example 9 with SchemaTransaction

use of com.baidu.hugegraph.backend.tx.SchemaTransaction in project incubator-hugegraph by apache.

the class OlapPropertyKeyRemoveJob method execute.

@Override
public Object execute() {
    Id olap = this.schemaId();
    // Remove olap data table
    this.params().graphTransaction().removeOlapPk(olap);
    // Remove corresponding index label and index data
    Id indexLabel = findOlapIndexLabel(this.params(), olap);
    if (indexLabel != null) {
        removeIndexLabel(this.params(), indexLabel);
    }
    // Remove olap property key
    SchemaTransaction schemaTx = this.params().schemaTransaction();
    PropertyKey propertyKey = schemaTx.getPropertyKey(olap);
    removeSchema(schemaTx, propertyKey);
    return null;
}
Also used : Id(com.baidu.hugegraph.backend.id.Id) SchemaTransaction(com.baidu.hugegraph.backend.tx.SchemaTransaction) PropertyKey(com.baidu.hugegraph.schema.PropertyKey)

Aggregations

SchemaTransaction (com.baidu.hugegraph.backend.tx.SchemaTransaction)9 Id (com.baidu.hugegraph.backend.id.Id)6 GraphTransaction (com.baidu.hugegraph.backend.tx.GraphTransaction)6 LockUtil (com.baidu.hugegraph.util.LockUtil)6 IndexLabel (com.baidu.hugegraph.schema.IndexLabel)4 EdgeLabel (com.baidu.hugegraph.schema.EdgeLabel)3 PropertyKey (com.baidu.hugegraph.schema.PropertyKey)2 VertexLabel (com.baidu.hugegraph.schema.VertexLabel)2 HugeException (com.baidu.hugegraph.HugeException)1 HugeGraph (com.baidu.hugegraph.HugeGraph)1 BackendStoreSystemInfo (com.baidu.hugegraph.backend.store.BackendStoreSystemInfo)1 SchemaElement (com.baidu.hugegraph.schema.SchemaElement)1 SchemaLabel (com.baidu.hugegraph.schema.SchemaLabel)1 HugeElement (com.baidu.hugegraph.structure.HugeElement)1 HugeType (com.baidu.hugegraph.type.HugeType)1 SchemaStatus (com.baidu.hugegraph.type.define.SchemaStatus)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Collection (java.util.Collection)1 Set (java.util.Set)1 Consumer (java.util.function.Consumer)1