Search in sources :

Example 1 with ArangoDBException

use of com.arangodb.ArangoDBException 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 2 with ArangoDBException

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

Example 3 with ArangoDBException

use of com.arangodb.ArangoDBException in project YCSB by brianfrankcooper.

the class ArangoDB3Client method init.

/**
   * Initialize any state for this DB. Called once per DB instance; there is
   * one DB instance per client thread.
   * 
   * Actually, one client process will share one DB instance here.(Coincide to
   * mongoDB driver)
   */
@Override
public void init() throws DBException {
    synchronized (ArangoDB3Client.class) {
        Properties props = getProperties();
        collectionName = props.getProperty("table", "usertable");
        // Set the DB address
        String ip = props.getProperty("arangodb.ip", "localhost");
        String portStr = props.getProperty("arangodb.port", "8529");
        int port = Integer.parseInt(portStr);
        // If clear db before run
        String dropDBBeforeRunStr = props.getProperty("arangodb.dropDBBeforeRun", "false");
        dropDBBeforeRun = Boolean.parseBoolean(dropDBBeforeRunStr);
        // Set the sync mode
        String waitForSyncStr = props.getProperty("arangodb.waitForSync", "false");
        waitForSync = Boolean.parseBoolean(waitForSyncStr);
        // Set if transaction for update
        String transactionUpdateStr = props.getProperty("arangodb.transactionUpdate", "false");
        transactionUpdate = Boolean.parseBoolean(transactionUpdateStr);
        // Init ArangoDB connection
        try {
            arangoDB = new ArangoDB.Builder().host(ip).port(port).build();
        } catch (Exception e) {
            logger.error("Failed to initialize ArangoDB", e);
            System.exit(-1);
        }
        if (INIT_COUNT.getAndIncrement() == 0) {
            // Init the database
            if (dropDBBeforeRun) {
                // Try delete first
                try {
                    arangoDB.db(databaseName).drop();
                } catch (ArangoDBException e) {
                    logger.info("Fail to delete DB: {}", databaseName);
                }
            }
            try {
                arangoDB.createDatabase(databaseName);
                logger.info("Database created: " + databaseName);
            } catch (ArangoDBException e) {
                logger.error("Failed to create database: {} with ex: {}", databaseName, e.toString());
            }
            try {
                arangoDB.db(databaseName).createCollection(collectionName);
                logger.info("Collection created: " + collectionName);
            } catch (ArangoDBException e) {
                logger.error("Failed to create collection: {} with ex: {}", collectionName, e.toString());
            }
            logger.info("ArangoDB client connection created to {}:{}", ip, port);
            // Log the configuration
            logger.info("Arango Configuration: dropDBBeforeRun: {}; address: {}:{}; databaseName: {};" + " waitForSync: {}; transactionUpdate: {};", dropDBBeforeRun, ip, port, databaseName, waitForSync, transactionUpdate);
        }
    }
}
Also used : MapBuilder(com.arangodb.util.MapBuilder) VPackBuilder(com.arangodb.velocypack.VPackBuilder) Properties(java.util.Properties) DBException(com.yahoo.ycsb.DBException) ArangoDBException(com.arangodb.ArangoDBException) IOException(java.io.IOException) ArangoDBException(com.arangodb.ArangoDBException)

Aggregations

ArangoDBException (com.arangodb.ArangoDBException)3 BaseDocument (com.arangodb.entity.BaseDocument)2 ByteIterator (com.yahoo.ycsb.ByteIterator)2 StringByteIterator (com.yahoo.ycsb.StringByteIterator)2 DocumentCreateOptions (com.arangodb.model.DocumentCreateOptions)1 TransactionOptions (com.arangodb.model.TransactionOptions)1 MapBuilder (com.arangodb.util.MapBuilder)1 VPackBuilder (com.arangodb.velocypack.VPackBuilder)1 DBException (com.yahoo.ycsb.DBException)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Properties (java.util.Properties)1