Search in sources :

Example 6 with BaseDocument

use of com.arangodb.entity.BaseDocument 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

BaseDocument (com.arangodb.entity.BaseDocument)6 ArangoException (com.arangodb.ArangoException)4 ByteIterator (com.yahoo.ycsb.ByteIterator)4 StringByteIterator (com.yahoo.ycsb.StringByteIterator)4 HashMap (java.util.HashMap)3 ArangoDBException (com.arangodb.ArangoDBException)2 Map (java.util.Map)2 TransactionEntity (com.arangodb.entity.TransactionEntity)1 DocumentCreateOptions (com.arangodb.model.DocumentCreateOptions)1 TransactionOptions (com.arangodb.model.TransactionOptions)1 MapBuilder (com.arangodb.util.MapBuilder)1 DBException (com.yahoo.ycsb.DBException)1