Search in sources :

Example 1 with BaseDocument

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

the class ArangoDBClient method insert.

/**
   * Insert 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.
   * 
   * @param table
   *      The name of the table
   * @param key
   *      The record key of the record to insert.
   * @param values
   *      A HashMap of field/value pairs to insert in the record
   * @return Zero on success, a non-zero error code on error. See the
   *     {@link DB} class's description for a discussion of error codes.
   */
@Override
public Status insert(String table, String key, HashMap<String, ByteIterator> values) {
    try {
        BaseDocument toInsert = new BaseDocument(key);
        for (Map.Entry<String, ByteIterator> entry : values.entrySet()) {
            toInsert.addAttribute(entry.getKey(), byteIteratorToString(entry.getValue()));
        }
        arangoDriver.createDocument(table, toInsert, true, /*create collection if not exist*/
        waitForSync);
        return Status.OK;
    } catch (ArangoException e) {
        if (e.getErrorNumber() != ErrorNums.ERROR_ARANGO_UNIQUE_CONSTRAINT_VIOLATED) {
            logger.error("Fail to insert: {} {} with ex {}", table, key, e.toString());
        } else {
            logger.debug("Trying to create document with duplicate key: {} {}", table, key);
            return Status.BAD_REQUEST;
        }
    } catch (RuntimeException e) {
        logger.error("Exception while trying insert {} {} 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) ArangoException(com.arangodb.ArangoException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with BaseDocument

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

the class ArangoDBClient method read.

/**
   * Read a record from the database. Each field/value pair from the result
   * will be stored in a HashMap.
   * 
   * @param table
   *      The name of the table
   * @param key
   *      The record key of the record to read.
   * @param fields
   *      The list of fields to read, or null for all of them
   * @param result
   *      A HashMap of field/value pairs for the result
   * @return Zero on success, a non-zero error code on error or "not found".
   */
@SuppressWarnings("unchecked")
@Override
public Status read(String table, String key, Set<String> fields, HashMap<String, ByteIterator> result) {
    try {
        DocumentEntity<BaseDocument> targetDoc = arangoDriver.getDocument(table, key, BaseDocument.class);
        BaseDocument aDocument = targetDoc.getEntity();
        if (!this.fillMap(result, aDocument.getProperties(), fields)) {
            return Status.ERROR;
        }
        return Status.OK;
    } catch (ArangoException e) {
        if (e.getErrorNumber() != ErrorNums.ERROR_ARANGO_DOCUMENT_NOT_FOUND) {
            logger.error("Fail to read: {} {} with ex {}", table, key, e.toString());
        } else {
            logger.debug("Trying to read document not exist: {} {}", table, key);
            return Status.NOT_FOUND;
        }
    } catch (RuntimeException e) {
        logger.error("Exception while trying read {} {} with ex {}", table, key, e.toString());
    }
    return Status.ERROR;
}
Also used : BaseDocument(com.arangodb.entity.BaseDocument) ArangoException(com.arangodb.ArangoException)

Example 3 with BaseDocument

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

the class ArangoDBClient method scan.

/**
   * Perform a range scan for a set of records in the database. Each
   * field/value pair from the result will be stored in a HashMap.
   * 
   * @param table
   *      The name of the table
   * @param startkey
   *      The record key of the first record to read.
   * @param recordcount
   *      The number of records to read
   * @param fields
   *      The list of fields to read, or null for all of them
   * @param result
   *      A Vector of HashMaps, where each HashMap is a set field/value
   *      pairs for one record
   * @return Zero on success, a non-zero error code on error. See the
   *     {@link DB} class's description for a discussion of error codes.
   */
@Override
public Status scan(String table, String startkey, int recordcount, Set<String> fields, Vector<HashMap<String, ByteIterator>> result) {
    DocumentCursor<BaseDocument> cursor = null;
    try {
        String aqlQuery = String.format("FOR target IN %s FILTER target._key >= @key SORT target._key ASC LIMIT %d RETURN %s ", table, recordcount, constructReturnForAQL(fields, "target"));
        Map<String, Object> bindVars = new MapBuilder().put("key", startkey).get();
        cursor = arangoDriver.executeDocumentQuery(aqlQuery, bindVars, null, BaseDocument.class);
        Iterator<BaseDocument> iterator = cursor.entityIterator();
        while (iterator.hasNext()) {
            BaseDocument aDocument = iterator.next();
            HashMap<String, ByteIterator> aMap = new HashMap<String, ByteIterator>(aDocument.getProperties().size());
            if (!this.fillMap(aMap, aDocument.getProperties())) {
                return Status.ERROR;
            }
            result.add(aMap);
        }
        return Status.OK;
    } catch (Exception e) {
        logger.error("Exception while trying scan {} {} {} with ex {}", table, startkey, recordcount, e.toString());
    } finally {
        if (cursor != null) {
            try {
                cursor.close();
            } catch (ArangoException e) {
                logger.error("Fail to close cursor", e);
            }
        }
    }
    return Status.ERROR;
}
Also used : StringByteIterator(com.yahoo.ycsb.StringByteIterator) ByteIterator(com.yahoo.ycsb.ByteIterator) BaseDocument(com.arangodb.entity.BaseDocument) HashMap(java.util.HashMap) MapBuilder(com.arangodb.util.MapBuilder) ArangoException(com.arangodb.ArangoException) DBException(com.yahoo.ycsb.DBException) ArangoException(com.arangodb.ArangoException)

Example 4 with BaseDocument

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

Example 5 with BaseDocument

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

the class ArangoDB3Client method insert.

/**
   * Insert 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.
   * 
   * @param table
   *      The name of the table
   * @param key
   *      The record key of the record to insert.
   * @param values
   *      A HashMap of field/value pairs to insert in the record
   * @return Zero on success, a non-zero error code on error. See the
   *     {@link DB} class's description for a discussion of error codes.
   */
@Override
public Status insert(String table, String key, HashMap<String, ByteIterator> values) {
    try {
        BaseDocument toInsert = new BaseDocument(key);
        for (Map.Entry<String, ByteIterator> entry : values.entrySet()) {
            toInsert.addAttribute(entry.getKey(), byteIteratorToString(entry.getValue()));
        }
        DocumentCreateOptions options = new DocumentCreateOptions().waitForSync(waitForSync);
        arangoDB.db(databaseName).collection(table).insertDocument(toInsert, options);
        return Status.OK;
    } catch (ArangoDBException e) {
        logger.error("Exception while trying insert {} {} 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) DocumentCreateOptions(com.arangodb.model.DocumentCreateOptions) HashMap(java.util.HashMap) Map(java.util.Map) ArangoDBException(com.arangodb.ArangoDBException)

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