Search in sources :

Example 16 with BackendMutation

use of com.baidu.hugegraph.backend.store.BackendMutation in project incubator-hugegraph by apache.

the class AbstractTransaction method commitMutation2Backend.

protected void commitMutation2Backend(BackendMutation... mutations) {
    assert mutations.length > 0;
    this.committing2Backend = true;
    // If an exception occurred, catch in the upper layer and rollback
    this.store.beginTx();
    for (BackendMutation mutation : mutations) {
        this.store.mutate(mutation);
    }
    this.store.commitTx();
    this.committing2Backend = false;
}
Also used : BackendMutation(com.baidu.hugegraph.backend.store.BackendMutation)

Example 17 with BackendMutation

use of com.baidu.hugegraph.backend.store.BackendMutation in project incubator-hugegraph by apache.

the class StoreSerializer method writeMutations.

public static byte[] writeMutations(List<BackendMutation> mutations) {
    int estimateSize = mutations.size() * MUTATION_SIZE;
    // The first two bytes are reserved for StoreType and StoreAction
    BytesBuffer buffer = BytesBuffer.allocate(StoreCommand.HEADER_SIZE + 4 + estimateSize);
    StoreCommand.writeHeader(buffer);
    buffer.writeVInt(mutations.size());
    for (BackendMutation mutation : mutations) {
        buffer.writeBigBytes(writeMutation(mutation));
    }
    return buffer.bytes();
}
Also used : BackendMutation(com.baidu.hugegraph.backend.store.BackendMutation) BytesBuffer(com.baidu.hugegraph.backend.serializer.BytesBuffer)

Example 18 with BackendMutation

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

Example 19 with BackendMutation

use of com.baidu.hugegraph.backend.store.BackendMutation in project incubator-hugegraph by apache.

the class StoreSerializer method readMutations.

public static List<BackendMutation> readMutations(BytesBuffer buffer) {
    int size = buffer.readVInt();
    List<BackendMutation> mutations = new ArrayList<>(size);
    for (int i = 0; i < size; i++) {
        BytesBuffer buf = BytesBuffer.wrap(buffer.readBigBytes());
        mutations.add(readMutation(buf));
    }
    return mutations;
}
Also used : BackendMutation(com.baidu.hugegraph.backend.store.BackendMutation) ArrayList(java.util.ArrayList) BytesBuffer(com.baidu.hugegraph.backend.serializer.BytesBuffer)

Example 20 with BackendMutation

use of com.baidu.hugegraph.backend.store.BackendMutation in project incubator-hugegraph by apache.

the class AbstractTransaction method commitOrRollback.

@Watched(prefix = "tx")
public void commitOrRollback() {
    LOG.debug("Transaction commitOrRollback()");
    this.checkOwnerThread();
    /*
         * The mutation will be reset after commit, in order to log the
         * mutation after failure, let's save it to a local variable.
         */
    BackendMutation mutation = this.mutation();
    try {
        // Do commit
        this.commit();
    } catch (Throwable e1) {
        LOG.error("Failed to commit changes:", e1);
        // Do rollback
        try {
            this.rollback();
        } catch (Throwable e2) {
            LOG.error("Failed to rollback changes:\n {}", mutation, e2);
        }
        /*
             * Rethrow the commit exception
             * The e.getMessage maybe too long to see key information,
             */
        throw new BackendException("Failed to commit changes: %s(%s)", StringUtils.abbreviateMiddle(e1.getMessage(), ".", 256), HugeException.rootCause(e1));
    }
}
Also used : BackendMutation(com.baidu.hugegraph.backend.store.BackendMutation) BackendException(com.baidu.hugegraph.backend.BackendException) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Aggregations

BackendMutation (com.baidu.hugegraph.backend.store.BackendMutation)31 Test (org.junit.Test)22 BackendEntry (com.baidu.hugegraph.backend.store.BackendEntry)21 TextBackendEntry (com.baidu.hugegraph.backend.serializer.TextBackendEntry)20 BaseUnitTest (com.baidu.hugegraph.unit.BaseUnitTest)20 BinaryBackendEntry (com.baidu.hugegraph.backend.serializer.BinaryBackendEntry)3 BytesBuffer (com.baidu.hugegraph.backend.serializer.BytesBuffer)3 BackendAction (com.baidu.hugegraph.backend.store.BackendAction)2 BackendException (com.baidu.hugegraph.backend.BackendException)1 Id (com.baidu.hugegraph.backend.id.Id)1 BackendStore (com.baidu.hugegraph.backend.store.BackendStore)1 IncrCounter (com.baidu.hugegraph.backend.store.raft.RaftBackendStore.IncrCounter)1 StoreCommand (com.baidu.hugegraph.backend.store.raft.StoreCommand)1 Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)1 HugeType (com.baidu.hugegraph.type.HugeType)1 Action (com.baidu.hugegraph.type.define.Action)1 ArrayList (java.util.ArrayList)1