Search in sources :

Example 1 with TransactionOptions

use of com.arangodb.model.TransactionOptions in project YCSB by brianfrankcooper.

the class ArangoDB3Client method update.

/**
   * Update a record in the database. Any field/value pairs in the specified
   * values HashMap will be written into the record with the specified record
   * key, overwriting any existing values with the same field name.
   * 
   * @param table
   *      The name of the table
   * @param key
   *      The record key of the record to write.
   * @param values
   *      A HashMap of field/value pairs to update in the record
   * @return Zero on success, a non-zero error code on error. See this class's
   *     description for a discussion of error codes.
   */
@Override
public Status update(String table, String key, HashMap<String, ByteIterator> values) {
    try {
        if (!transactionUpdate) {
            BaseDocument updateDoc = new BaseDocument();
            for (Entry<String, ByteIterator> field : values.entrySet()) {
                updateDoc.addAttribute(field.getKey(), byteIteratorToString(field.getValue()));
            }
            arangoDB.db(databaseName).collection(table).updateDocument(key, updateDoc);
            return Status.OK;
        } else {
            // id for documentHandle
            String transactionAction = "function (id) {" + // use internal database functions
            "var db = require('internal').db;" + // collection.update(document, data, overwrite, keepNull, waitForSync)
            String.format("db._update(id, %s, true, false, %s);}", mapToJson(values), Boolean.toString(waitForSync).toLowerCase());
            TransactionOptions options = new TransactionOptions();
            options.writeCollections(table);
            options.params(createDocumentHandle(table, key));
            arangoDB.db(databaseName).transaction(transactionAction, Void.class, options);
            return Status.OK;
        }
    } catch (ArangoDBException e) {
        logger.error("Exception while trying update {} {} with ex {}", table, key, e.toString());
    }
    return Status.ERROR;
}
Also used : StringByteIterator(com.yahoo.ycsb.StringByteIterator) ByteIterator(com.yahoo.ycsb.ByteIterator) BaseDocument(com.arangodb.entity.BaseDocument) TransactionOptions(com.arangodb.model.TransactionOptions) ArangoDBException(com.arangodb.ArangoDBException)

Aggregations

ArangoDBException (com.arangodb.ArangoDBException)1 BaseDocument (com.arangodb.entity.BaseDocument)1 TransactionOptions (com.arangodb.model.TransactionOptions)1 ByteIterator (com.yahoo.ycsb.ByteIterator)1 StringByteIterator (com.yahoo.ycsb.StringByteIterator)1