Search in sources :

Example 1 with TransactionEntity

use of com.arangodb.entity.TransactionEntity in project YCSB by brianfrankcooper.

the class ArangoDBClient 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 (String field : values.keySet()) {
                updateDoc.addAttribute(field, byteIteratorToString(values.get(field)));
            }
            arangoDriver.updateDocument(table, 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());
            TransactionEntity transaction = arangoDriver.createTransaction(transactionAction);
            transaction.addWriteCollection(table);
            transaction.setParams(createDocumentHandle(table, key));
            arangoDriver.executeTransaction(transaction);
            return Status.OK;
        }
    } catch (ArangoException e) {
        if (e.getErrorNumber() != ErrorNums.ERROR_ARANGO_DOCUMENT_NOT_FOUND) {
            logger.error("Fail to update: {} {} with ex {}", table, key, e.toString());
        } else {
            logger.debug("Trying to update document not exist: {} {}", table, key);
            return Status.NOT_FOUND;
        }
    } catch (RuntimeException e) {
        logger.error("Exception while trying update {} {} with ex {}", table, key, e.toString());
    }
    return Status.ERROR;
}
Also used : BaseDocument(com.arangodb.entity.BaseDocument) TransactionEntity(com.arangodb.entity.TransactionEntity) ArangoException(com.arangodb.ArangoException)

Aggregations

ArangoException (com.arangodb.ArangoException)1 BaseDocument (com.arangodb.entity.BaseDocument)1 TransactionEntity (com.arangodb.entity.TransactionEntity)1