Search in sources :

Example 1 with MapBuilder

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

use of com.arangodb.util.MapBuilder in project YCSB by brianfrankcooper.

the class ArangoDB3Client 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) {
    ArangoCursor<VPackSlice> 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 = arangoDB.db(databaseName).query(aqlQuery, bindVars, null, VPackSlice.class);
        while (cursor.hasNext()) {
            VPackSlice aDocument = cursor.next();
            HashMap<String, ByteIterator> aMap = new HashMap<String, ByteIterator>(aDocument.size());
            if (!this.fillMap(aMap, aDocument)) {
                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 (IOException e) {
                logger.error("Fail to close cursor", e);
            }
        }
    }
    return Status.ERROR;
}
Also used : StringByteIterator(com.yahoo.ycsb.StringByteIterator) ByteIterator(com.yahoo.ycsb.ByteIterator) HashMap(java.util.HashMap) VPackSlice(com.arangodb.velocypack.VPackSlice) MapBuilder(com.arangodb.util.MapBuilder) IOException(java.io.IOException) DBException(com.yahoo.ycsb.DBException) ArangoDBException(com.arangodb.ArangoDBException) IOException(java.io.IOException)

Aggregations

MapBuilder (com.arangodb.util.MapBuilder)2 ByteIterator (com.yahoo.ycsb.ByteIterator)2 DBException (com.yahoo.ycsb.DBException)2 StringByteIterator (com.yahoo.ycsb.StringByteIterator)2 HashMap (java.util.HashMap)2 ArangoDBException (com.arangodb.ArangoDBException)1 ArangoException (com.arangodb.ArangoException)1 BaseDocument (com.arangodb.entity.BaseDocument)1 VPackSlice (com.arangodb.velocypack.VPackSlice)1 IOException (java.io.IOException)1