Search in sources :

Example 1 with Action

use of com.baidu.hugegraph.type.define.Action 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 2 with Action

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

the class StoreSerializer method readMutation.

public static BackendMutation readMutation(BytesBuffer buffer) {
    int size = buffer.readVInt();
    BackendMutation mutation = new BackendMutation(size);
    for (int i = 0; i < size; i++) {
        // read action
        Action action = Action.fromCode(buffer.read());
        // read HugeType
        HugeType type = SerialEnum.fromCode(HugeType.class, buffer.read());
        // read id
        byte[] idBytes = buffer.readBytes();
        // read subId
        Id subId = buffer.readId();
        if (subId.equals(IdGenerator.ZERO)) {
            subId = null;
        }
        // read ttl
        long ttl = buffer.readVLong();
        BinaryBackendEntry entry = new BinaryBackendEntry(type, idBytes);
        entry.subId(subId);
        entry.ttl(ttl);
        // read columns
        int columnsSize = buffer.readVInt();
        for (int c = 0; c < columnsSize; c++) {
            byte[] name = buffer.readBytes();
            byte[] value = buffer.readBytes();
            entry.column(BackendColumn.of(name, value));
        }
        mutation.put(entry, action);
    }
    return mutation;
}
Also used : BackendMutation(com.baidu.hugegraph.backend.store.BackendMutation) Action(com.baidu.hugegraph.type.define.Action) BackendAction(com.baidu.hugegraph.backend.store.BackendAction) BinaryBackendEntry(com.baidu.hugegraph.backend.serializer.BinaryBackendEntry) Id(com.baidu.hugegraph.backend.id.Id) HugeType(com.baidu.hugegraph.type.HugeType)

Aggregations

Id (com.baidu.hugegraph.backend.id.Id)2 Action (com.baidu.hugegraph.type.define.Action)2 BinaryBackendEntry (com.baidu.hugegraph.backend.serializer.BinaryBackendEntry)1 BackendAction (com.baidu.hugegraph.backend.store.BackendAction)1 BackendMutation (com.baidu.hugegraph.backend.store.BackendMutation)1 Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)1 HugeType (com.baidu.hugegraph.type.HugeType)1