Search in sources :

Example 1 with HugeType

use of com.baidu.hugegraph.type.HugeType in project incubator-hugegraph by apache.

the class CachedSchemaTransaction method listenChanges.

private void listenChanges() {
    // Listen store event: "store.init", "store.clear", ...
    Set<String> storeEvents = ImmutableSet.of(Events.STORE_INIT, Events.STORE_CLEAR, Events.STORE_TRUNCATE);
    this.storeEventListener = event -> {
        if (storeEvents.contains(event.name())) {
            LOG.debug("Graph {} clear schema cache on event '{}'", this.graph(), event.name());
            this.clearCache(true);
            return true;
        }
        return false;
    };
    this.store().provider().listen(this.storeEventListener);
    // Listen cache event: "cache"(invalid cache item)
    this.cacheEventListener = event -> {
        LOG.debug("Graph {} received schema cache event: {}", this.graph(), event);
        Object[] args = event.args();
        E.checkArgument(args.length > 0 && args[0] instanceof String, "Expect event action argument");
        if (Cache.ACTION_INVALID.equals(args[0])) {
            event.checkArgs(String.class, HugeType.class, Id.class);
            HugeType type = (HugeType) args[1];
            Id id = (Id) args[2];
            this.invalidateCache(type, id);
            this.resetCachedAll(type);
            return true;
        } else if (Cache.ACTION_CLEAR.equals(args[0])) {
            event.checkArgs(String.class, HugeType.class);
            this.clearCache(false);
            return true;
        }
        return false;
    };
    EventHub schemaEventHub = this.params().schemaEventHub();
    if (!schemaEventHub.containsListener(Events.CACHE)) {
        schemaEventHub.listen(Events.CACHE, this.cacheEventListener);
    }
}
Also used : EventHub(com.baidu.hugegraph.event.EventHub) Id(com.baidu.hugegraph.backend.id.Id) HugeType(com.baidu.hugegraph.type.HugeType)

Example 2 with HugeType

use of com.baidu.hugegraph.type.HugeType in project incubator-hugegraph by apache.

the class IndexLabelBuilder method createWithTask.

/**
 * Create index label with async mode
 */
@Override
public SchemaElement.TaskWithSchema createWithTask() {
    HugeType type = HugeType.INDEX_LABEL;
    this.checkSchemaName(this.name);
    return this.lockCheckAndCreateSchema(type, this.name, name -> {
        IndexLabel indexLabel = this.indexLabelOrNull(name);
        if (indexLabel != null) {
            if (this.checkExist || !hasSameProperties(indexLabel)) {
                throw new ExistedException(type, name);
            }
            return new SchemaElement.TaskWithSchema(indexLabel, IdGenerator.ZERO);
        }
        this.checkSchemaIdIfRestoringMode(type, this.id);
        this.checkBaseType();
        this.checkIndexType();
        if (VertexLabel.OLAP_VL.name().equals(this.baseValue)) {
            return new SchemaElement.TaskWithSchema(this.build(), IdGenerator.ZERO);
        }
        SchemaLabel schemaLabel = this.loadBaseLabel();
        /*
             * If new index label is prefix of existed index label, or has
             * the same fields, fail to create new index label.
             */
        this.checkFields(schemaLabel.properties());
        this.checkRepeatIndex(schemaLabel);
        Userdata.check(this.userdata, Action.INSERT);
        // Async delete index label which is prefix of the new index label
        // TODO: use event to replace direct call
        Set<Id> removeTasks = this.removeSubIndex(schemaLabel);
        indexLabel = this.build();
        assert indexLabel.name().equals(name);
        /*
             * If not rebuild, just create index label and return.
             * The actual indexes may be rebuilt later as needed
             */
        if (!this.rebuild) {
            indexLabel.status(SchemaStatus.CREATED);
            this.graph().addIndexLabel(schemaLabel, indexLabel);
            return new SchemaElement.TaskWithSchema(indexLabel, IdGenerator.ZERO);
        }
        // Create index label (just schema)
        indexLabel.status(SchemaStatus.CREATING);
        this.graph().addIndexLabel(schemaLabel, indexLabel);
        try {
            // Async rebuild index
            Id rebuildTask = this.rebuildIndex(indexLabel, removeTasks);
            E.checkNotNull(rebuildTask, "rebuild-index task");
            return new SchemaElement.TaskWithSchema(indexLabel, rebuildTask);
        } catch (Throwable e) {
            this.updateSchemaStatus(indexLabel, SchemaStatus.INVALID);
            throw e;
        }
    });
}
Also used : ExistedException(com.baidu.hugegraph.exception.ExistedException) IndexLabel(com.baidu.hugegraph.schema.IndexLabel) SchemaLabel(com.baidu.hugegraph.schema.SchemaLabel) Id(com.baidu.hugegraph.backend.id.Id) HugeType(com.baidu.hugegraph.type.HugeType)

Example 3 with HugeType

use of com.baidu.hugegraph.type.HugeType in project incubator-hugegraph by apache.

the class IndexLabelRebuildJob method schemaElement.

private SchemaElement schemaElement() {
    HugeType type = this.schemaType();
    Id id = this.schemaId();
    switch(type) {
        case VERTEX_LABEL:
            return this.graph().vertexLabel(id);
        case EDGE_LABEL:
            return this.graph().edgeLabel(id);
        case INDEX_LABEL:
            return this.graph().indexLabel(id);
        default:
            throw new AssertionError(String.format("Invalid HugeType '%s' for rebuild", type));
    }
}
Also used : Id(com.baidu.hugegraph.backend.id.Id) HugeType(com.baidu.hugegraph.type.HugeType)

Example 4 with HugeType

use of com.baidu.hugegraph.type.HugeType 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 5 with HugeType

use of com.baidu.hugegraph.type.HugeType in project incubator-hugegraph by apache.

the class RocksDBStore method tableDBMapping.

protected Map<String, RocksDBSessions> tableDBMapping() {
    Map<String, RocksDBSessions> tableDBMap = InsertionOrderUtil.newMap();
    for (Entry<HugeType, String> e : this.tableDiskMapping.entrySet()) {
        HugeType type = e.getKey();
        RocksDBSessions db = this.db(e.getValue());
        String key = type != HugeType.OLAP ? this.table(type).table() : type.string();
        tableDBMap.put(key, db);
    }
    return tableDBMap;
}
Also used : HugeType(com.baidu.hugegraph.type.HugeType)

Aggregations

HugeType (com.baidu.hugegraph.type.HugeType)34 Id (com.baidu.hugegraph.backend.id.Id)16 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)5 Query (com.baidu.hugegraph.backend.query.Query)4 ExistedException (com.baidu.hugegraph.exception.ExistedException)4 SchemaLabel (com.baidu.hugegraph.schema.SchemaLabel)4 ArrayList (java.util.ArrayList)4 EdgeId (com.baidu.hugegraph.backend.id.EdgeId)3 Condition (com.baidu.hugegraph.backend.query.Condition)3 IdQuery (com.baidu.hugegraph.backend.query.IdQuery)3 BinaryBackendEntry (com.baidu.hugegraph.backend.serializer.BinaryBackendEntry)3 BinaryId (com.baidu.hugegraph.backend.serializer.BinaryBackendEntry.BinaryId)3 BackendAction (com.baidu.hugegraph.backend.store.BackendAction)3 IndexLabel (com.baidu.hugegraph.schema.IndexLabel)3 Lock (java.util.concurrent.locks.Lock)3 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)3 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)3 HugeException (com.baidu.hugegraph.HugeException)2 BackendException (com.baidu.hugegraph.backend.BackendException)2 BinaryEntryIterator (com.baidu.hugegraph.backend.serializer.BinaryEntryIterator)2