Search in sources :

Example 16 with Watched

use of com.baidu.hugegraph.perf.PerfUtil.Watched in project incubator-hugegraph by apache.

the class TestGraph method initGratefulSchema.

@Watched
public void initGratefulSchema(IdStrategy idStrategy) {
    SchemaManager schema = this.graph.schema();
    schema.propertyKey("id").asInt().ifNotExist().create();
    schema.propertyKey("weight").asInt().ifNotExist().create();
    schema.propertyKey("name").ifNotExist().create();
    schema.propertyKey("songType").asText().ifNotExist().create();
    schema.propertyKey("performances").asInt().ifNotExist().create();
    switch(idStrategy) {
        case AUTOMATIC:
            schema.vertexLabel("song").properties("id", "name", "songType", "performances").nullableKeys("id", "name", "songType", "performances").ifNotExist().create();
            schema.vertexLabel("artist").properties("id", "name").nullableKeys("id", "name").ifNotExist().create();
            break;
        case CUSTOMIZE_STRING:
            schema.vertexLabel("song").properties("id", "name", "songType", "performances").nullableKeys("id", "name", "songType", "performances").useCustomizeStringId().ifNotExist().create();
            schema.vertexLabel("artist").properties("id", "name").nullableKeys("id", "name").useCustomizeStringId().ifNotExist().create();
            break;
        default:
            throw new AssertionError(String.format("Id strategy must be customize or automatic"));
    }
    schema.edgeLabel("followedBy").link("song", "song").properties("weight").nullableKeys("weight").ifNotExist().create();
    schema.edgeLabel("sungBy").link("song", "artist").ifNotExist().create();
    schema.edgeLabel("writtenBy").link("song", "artist").ifNotExist().create();
    schema.indexLabel("songByName").onV("song").by("name").ifNotExist().create();
    schema.indexLabel("songBySongType").onV("song").by("songType").ifNotExist().create();
    schema.indexLabel("songByPerf").onV("song").by("performances").ifNotExist().create();
    schema.indexLabel("artistByName").onV("artist").by("name").ifNotExist().create();
    schema.indexLabel("followedByByWeight").onE("followedBy").by("weight").range().ifNotExist().create();
}
Also used : SchemaManager(com.baidu.hugegraph.schema.SchemaManager) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Example 17 with Watched

use of com.baidu.hugegraph.perf.PerfUtil.Watched in project incubator-hugegraph by apache.

the class ConditionQuery method condition.

@Watched
public <T> T condition(Object key) {
    List<Object> valuesEQ = InsertionOrderUtil.newList();
    List<Object> valuesIN = InsertionOrderUtil.newList();
    for (Condition c : this.conditions) {
        if (c.isRelation()) {
            Condition.Relation r = (Condition.Relation) c;
            if (r.key().equals(key)) {
                if (r.relation() == RelationType.EQ) {
                    valuesEQ.add(r.value());
                } else if (r.relation() == RelationType.IN) {
                    Object value = r.value();
                    assert value instanceof List;
                    valuesIN.add(value);
                }
            }
        }
    }
    if (valuesEQ.isEmpty() && valuesIN.isEmpty()) {
        return null;
    }
    if (valuesEQ.size() == 1 && valuesIN.size() == 0) {
        @SuppressWarnings("unchecked") T value = (T) valuesEQ.get(0);
        return value;
    }
    if (valuesEQ.size() == 0 && valuesIN.size() == 1) {
        @SuppressWarnings("unchecked") T value = (T) valuesIN.get(0);
        return value;
    }
    Set<Object> intersectValues = InsertionOrderUtil.newSet();
    for (Object value : valuesEQ) {
        List<Object> valueAsList = ImmutableList.of(value);
        if (intersectValues.isEmpty()) {
            intersectValues.addAll(valueAsList);
        } else {
            CollectionUtil.intersectWithModify(intersectValues, valueAsList);
        }
    }
    for (Object value : valuesIN) {
        @SuppressWarnings("unchecked") List<Object> valueAsList = (List<Object>) value;
        if (intersectValues.isEmpty()) {
            intersectValues.addAll(valueAsList);
        } else {
            CollectionUtil.intersectWithModify(intersectValues, valueAsList);
        }
    }
    if (intersectValues.size() == 0) {
        return null;
    }
    E.checkState(intersectValues.size() == 1, "Illegal key '%s' with more than one value: %s", key, intersectValues);
    @SuppressWarnings("unchecked") T value = (T) intersectValues.iterator().next();
    return value;
}
Also used : Relation(com.baidu.hugegraph.backend.query.Condition.Relation) Relation(com.baidu.hugegraph.backend.query.Condition.Relation) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Example 18 with Watched

use of com.baidu.hugegraph.perf.PerfUtil.Watched in project incubator-hugegraph by apache.

the class BackendMutation method optimizeUpdates.

/**
 * The optimized scenes include but are not limited to:
 * 1.If you want to delete an entry, the other mutations previously
 *   can be ignored.
 * 2.As similar to the No.1 item, If you want to insert an entry,
 *   the other mutations previously also can be ignored.
 * 3.If you append an entry and then eliminate it, the new action
 *   can override the old one.
 */
@Watched(prefix = "mutation")
private void optimizeUpdates(BackendEntry entry, Action action) {
    final Id id = entry.id();
    assert id != null;
    final List<BackendAction> items = this.updates.get(entry.type(), id);
    assert items != null;
    boolean ignoreCurrent = false;
    for (Iterator<BackendAction> iter = items.iterator(); iter.hasNext(); ) {
        BackendAction originItem = iter.next();
        Action originAction = originItem.action();
        switch(action) {
            case INSERT:
                iter.remove();
                break;
            case DELETE:
                if (originAction == Action.INSERT) {
                    throw incompatibleActionException(action, originAction);
                } else if (originAction == Action.DELETE) {
                    ignoreCurrent = true;
                } else {
                    iter.remove();
                }
                break;
            case APPEND:
                if (entry.type().isUniqueIndex() && originAction == Action.APPEND) {
                    throw new IllegalArgumentException(String.format("Unique constraint conflict is found in" + " transaction between %s and %s", entry, originItem.entry()));
                }
                if (originAction == Action.INSERT || originAction == Action.DELETE) {
                    throw incompatibleActionException(action, originAction);
                } else {
                    Id subId = entry.subId();
                    Id originSubId = originItem.entry().subId();
                    assert subId != null;
                    if (subId == originSubId || subId.equals(originSubId)) {
                        iter.remove();
                    }
                }
                break;
            case ELIMINATE:
                if (originAction == Action.INSERT || originAction == Action.DELETE) {
                    throw incompatibleActionException(action, originAction);
                } else {
                    Id subId = entry.subId();
                    Id originSubId = originItem.entry().subId();
                    assert subId != null;
                    if (subId == originSubId || subId.equals(originSubId)) {
                        iter.remove();
                    }
                }
                break;
            default:
                throw new AssertionError(String.format("Unknown mutate action: %s", action));
        }
    }
    if (!ignoreCurrent) {
        items.add(BackendAction.of(action, entry));
    }
}
Also used : Action(com.baidu.hugegraph.type.define.Action) Id(com.baidu.hugegraph.backend.id.Id) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Example 19 with Watched

use of com.baidu.hugegraph.perf.PerfUtil.Watched in project incubator-hugegraph by apache.

the class HugeTraverser method edgesOfVertex.

@Watched
protected Iterator<Edge> edgesOfVertex(Id source, Directions dir, Map<Id, String> labels, long limit) {
    if (labels == null || labels.isEmpty()) {
        return this.edgesOfVertex(source, dir, (Id) null, limit);
    }
    ExtendableIterator<Edge> results = new ExtendableIterator<>();
    for (Id label : labels.keySet()) {
        E.checkNotNull(label, "edge label");
        results.extend(this.edgesOfVertex(source, dir, label, limit));
    }
    if (limit == NO_LIMIT) {
        return results;
    }
    long[] count = new long[1];
    return new LimitIterator<>(results, e -> {
        return count[0]++ >= limit;
    });
}
Also used : LimitIterator(com.baidu.hugegraph.iterator.LimitIterator) ExtendableIterator(com.baidu.hugegraph.iterator.ExtendableIterator) Id(com.baidu.hugegraph.backend.id.Id) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Example 20 with Watched

use of com.baidu.hugegraph.perf.PerfUtil.Watched in project incubator-hugegraph by apache.

the class DoubleWayMultiPathsRecords method linkPath.

@Watched
protected final PathSet linkPath(int source, int target, boolean ring) {
    PathSet paths = new PathSet();
    PathSet sources = this.linkSourcePath(source);
    PathSet targets = this.linkTargetPath(target);
    for (Path tpath : targets) {
        tpath.reverse();
        for (Path spath : sources) {
            if (!ring) {
                // Avoid loop in path
                if (CollectionUtils.containsAny(spath.vertices(), tpath.vertices())) {
                    continue;
                }
            }
            List<Id> ids = new ArrayList<>(spath.vertices());
            ids.addAll(tpath.vertices());
            Id crosspoint = this.id(this.movingForward ? target : source);
            paths.add(new Path(crosspoint, ids));
        }
    }
    return paths;
}
Also used : Path(com.baidu.hugegraph.traversal.algorithm.HugeTraverser.Path) PathSet(com.baidu.hugegraph.traversal.algorithm.HugeTraverser.PathSet) ArrayList(java.util.ArrayList) Id(com.baidu.hugegraph.backend.id.Id) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Aggregations

Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)65 Id (com.baidu.hugegraph.backend.id.Id)30 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)9 EdgeId (com.baidu.hugegraph.backend.id.EdgeId)8 IndexLabel (com.baidu.hugegraph.schema.IndexLabel)8 SchemaManager (com.baidu.hugegraph.schema.SchemaManager)7 ArrayList (java.util.ArrayList)7 PropertyKey (com.baidu.hugegraph.schema.PropertyKey)6 HugeVertex (com.baidu.hugegraph.structure.HugeVertex)6 BatchIdHolder (com.baidu.hugegraph.backend.page.IdHolder.BatchIdHolder)5 HugeIndex (com.baidu.hugegraph.structure.HugeIndex)5 IdHolder (com.baidu.hugegraph.backend.page.IdHolder)4 FixedIdHolder (com.baidu.hugegraph.backend.page.IdHolder.FixedIdHolder)4 PagingIdHolder (com.baidu.hugegraph.backend.page.IdHolder.PagingIdHolder)4 IdQuery (com.baidu.hugegraph.backend.query.IdQuery)4 Query (com.baidu.hugegraph.backend.query.Query)4 SchemaJob (com.baidu.hugegraph.job.schema.SchemaJob)4 SchemaLabel (com.baidu.hugegraph.schema.SchemaLabel)4 VertexLabel (com.baidu.hugegraph.schema.VertexLabel)4 HugeEdge (com.baidu.hugegraph.structure.HugeEdge)4