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;
}
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;
}
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);
}
}
}
Aggregations