Search in sources :

Example 1 with VPackSlice

use of com.arangodb.velocypack.VPackSlice in project YCSB by brianfrankcooper.

the class ArangoDB3Client method fillMap.

/**
   * Fills the map with the properties from the BaseDocument.
   * 
   * @param resultMap
   *      The map to fill/
   * @param document
   *      The record to read from
   * @param fields
   *      The list of fields to read, or null for all of them
   * @return isSuccess
   */
private boolean fillMap(Map<String, ByteIterator> resultMap, VPackSlice document, Set<String> fields) {
    if (fields == null || fields.size() == 0) {
        for (Iterator<Entry<String, VPackSlice>> iterator = document.objectIterator(); iterator.hasNext(); ) {
            Entry<String, VPackSlice> next = iterator.next();
            VPackSlice value = next.getValue();
            if (value.isString()) {
                resultMap.put(next.getKey(), stringToByteIterator(value.getAsString()));
            } else if (!value.isCustom()) {
                logger.error("Error! Not the format expected! Actually is {}", value.getClass().getName());
                return false;
            }
        }
    } else {
        for (String field : fields) {
            VPackSlice value = document.get(field);
            if (value.isString()) {
                resultMap.put(field, stringToByteIterator(value.getAsString()));
            } else if (!value.isCustom()) {
                logger.error("Error! Not the format expected! Actually is {}", value.getClass().getName());
                return false;
            }
        }
    }
    return true;
}
Also used : Entry(java.util.Map.Entry) VPackSlice(com.arangodb.velocypack.VPackSlice)

Example 2 with VPackSlice

use of com.arangodb.velocypack.VPackSlice 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

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