Search in sources :

Example 61 with ByteIterator

use of site.ycsb.ByteIterator in project gora by apache.

the class GoraClientTest method testRead.

/**
 * Test read performs a read record test from the database.
 */
@Test
public void testRead() {
    insertData();
    HashMap<String, ByteIterator> results = new HashMap<>();
    // this could be null as well
    Set<String> fields = new HashSet<>();
    Status result = benchmarkClient.read(Constants.TEST_TABLE, Constants.TEST_KEY_1, fields, results);
    assertEquals(Status.OK, result);
    assertEquals(DATA_TO_INSERT.size(), results.size());
    assertEquals(DATA_TO_INSERT.get(Constants.TEST_FIELD_0).toString(), results.get(Constants.TEST_FIELD_0).toString());
    assertEquals(Constants.TEST_VALUE_0, results.get(Constants.TEST_FIELD_0).toString());
}
Also used : Status(site.ycsb.Status) ByteIterator(site.ycsb.ByteIterator) StringByteIterator(site.ycsb.StringByteIterator) Test(org.junit.Test)

Example 62 with ByteIterator

use of site.ycsb.ByteIterator in project gora by apache.

the class GoraBenchmarkClient 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 start key
 * @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 The result of the operation.
 */
@Override
public Status scan(String table, String startKey, int recordCount, Set<String> fields, Vector<HashMap<String, ByteIterator>> result) {
    try {
        Query<String, User> goraQuery = dataStore.newQuery();
        goraQuery.setStartKey(startKey);
        goraQuery.setLimit(recordCount);
        Result<String, User> resultSet = goraQuery.execute();
        while (resultSet.next()) {
            HashMap<String, ByteIterator> resultsMap = new HashMap<>();
            for (int fieldCount = 0; fieldCount < totalFieldCount; fieldCount++) {
                String field = FIELDS[fieldCount + 1];
                int fieldIndex = fieldCount + 1;
                String value = resultSet.get().get(fieldIndex).toString();
                resultsMap.put(field, new StringByteIterator(value));
            }
            result.add(resultsMap);
        }
    } catch (Exception e) {
        LOG.info("There is a problem in scanning data from the table \n {}", e.getMessage(), e);
        return Status.ERROR;
    }
    return Status.OK;
}
Also used : User(org.apache.gora.benchmark.generated.User) ByteIterator(site.ycsb.ByteIterator) StringByteIterator(site.ycsb.StringByteIterator) HashMap(java.util.HashMap) StringByteIterator(site.ycsb.StringByteIterator) DBException(site.ycsb.DBException) GoraException(org.apache.gora.util.GoraException)

Example 63 with ByteIterator

use of site.ycsb.ByteIterator in project YCSB by brianfrankcooper.

the class BackoffSelectStrategy method scanSpecificFields.

/**
 * Performs the {@link #scan(String, String, int, Set, Vector)} operation N1Ql only for a subset of the fields.
 *
 * @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 The result of the operation.
 */
private Status scanSpecificFields(final String table, final String startkey, final int recordcount, final Set<String> fields, final Vector<HashMap<String, ByteIterator>> result) {
    String scanSpecQuery = "SELECT " + joinFields(fields) + " FROM `" + bucketName + "` WHERE meta().id >= '$1' LIMIT $2";
    N1qlQueryResult queryResult = bucket.query(N1qlQuery.parameterized(scanSpecQuery, JsonArray.from(formatId(table, startkey), recordcount), N1qlParams.build().adhoc(adhoc).maxParallelism(maxParallelism)));
    if (!queryResult.parseSuccess() || !queryResult.finalSuccess()) {
        throw new RuntimeException("Error while parsing N1QL Result. Query: " + scanSpecQuery + ", Errors: " + queryResult.errors());
    }
    boolean allFields = fields == null || fields.isEmpty();
    result.ensureCapacity(recordcount);
    for (N1qlQueryRow row : queryResult) {
        JsonObject value = row.value();
        if (fields == null) {
            value = value.getObject(bucketName);
        }
        Set<String> f = allFields ? value.getNames() : fields;
        HashMap<String, ByteIterator> tuple = new HashMap<String, ByteIterator>(f.size());
        for (String field : f) {
            tuple.put(field, new StringByteIterator(value.getString(field)));
        }
        result.add(tuple);
    }
    return Status.OK;
}
Also used : ByteIterator(site.ycsb.ByteIterator) StringByteIterator(site.ycsb.StringByteIterator) StringByteIterator(site.ycsb.StringByteIterator) JsonObject(com.couchbase.client.java.document.json.JsonObject)

Example 64 with ByteIterator

use of site.ycsb.ByteIterator in project YCSB by brianfrankcooper.

the class GoogleDatastoreClient method doSingleItemMutation.

private Status doSingleItemMutation(String table, String key, @Nullable Map<String, ByteIterator> values, MutationType mutationType) {
    // First build the key.
    Key.Builder datastoreKey = buildPrimaryKey(table, key);
    // Build a commit request in non-transactional mode.
    // Single item mutation to google datastore
    // is always atomic and strongly consistent. Transaction is only necessary
    // for multi-item mutation, or Read-modify-write operation.
    CommitRequest.Builder commitRequest = CommitRequest.newBuilder();
    commitRequest.setMode(Mode.NON_TRANSACTIONAL);
    if (mutationType == MutationType.DELETE) {
        commitRequest.addMutationsBuilder().setDelete(datastoreKey);
    } else {
        // If this is not for delete, build the entity.
        Entity.Builder entityBuilder = Entity.newBuilder();
        entityBuilder.setKey(datastoreKey);
        for (Entry<String, ByteIterator> val : values.entrySet()) {
            entityBuilder.getMutableProperties().put(val.getKey(), Value.newBuilder().setStringValue(val.getValue().toString()).setExcludeFromIndexes(skipIndex).build());
        }
        Entity entity = entityBuilder.build();
        logger.debug("entity built as: " + entity.toString());
        if (mutationType == MutationType.UPSERT) {
            commitRequest.addMutationsBuilder().setUpsert(entity);
        } else if (mutationType == MutationType.UPDATE) {
            commitRequest.addMutationsBuilder().setUpdate(entity);
        } else {
            throw new RuntimeException("Impossible MutationType, code bug.");
        }
    }
    try {
        datastore.commit(commitRequest.build());
        logger.debug("successfully committed.");
    } catch (DatastoreException exception) {
        // Catch all Datastore rpc errors.
        // Log the exception, the name of the method called and the error code.
        logger.error(String.format("Datastore Exception when committing (%s): %s %s", exception.getMessage(), exception.getMethodName(), exception.getCode()));
        // will bubble up to the user as part of the YCSB Status "name".
        return new Status("ERROR-" + exception.getCode(), exception.getMessage());
    }
    return Status.OK;
}
Also used : Status(site.ycsb.Status) ByteIterator(site.ycsb.ByteIterator) StringByteIterator(site.ycsb.StringByteIterator) DatastoreException(com.google.datastore.v1.client.DatastoreException)

Example 65 with ByteIterator

use of site.ycsb.ByteIterator in project YCSB by brianfrankcooper.

the class GridDBClientTest method testUpdate.

@Test
public void testUpdate() {
    HashMap<String, ByteIterator> result = new HashMap<String, ByteIterator>();
    HashMap<String, ByteIterator> values = new HashMap<String, ByteIterator>();
    insertToDatabase();
    String keyForUpdate = "field2";
    Set<String> fields = Collections.singleton(keyForUpdate);
    String strValueToUpdate = "new_value_2";
    ByteIterator valForUpdate = new StringByteIterator(strValueToUpdate);
    values.put(keyForUpdate, valForUpdate);
    Status updateStatus = myClient.update(TEST_TABLE, DEFAULT_ROW_KEY, values);
    assertEquals(updateStatus, Status.OK);
    // After update, we read the update row for get new value
    myClient.read(TEST_TABLE, DEFAULT_ROW_KEY, fields, result);
    assertNotEquals(result.entrySet(), 0);
    boolean found = false;
    for (int i = 0; i < FIELD_COUNT; i++) {
        ByteIterator iter = result.get("field" + i);
        byte[] byteArray1 = iter.toArray();
        String value = new String(byteArray1);
        // check result has row value is new update value or not
        if (value.equals(strValueToUpdate)) {
            found = true;
        }
    }
    assertEquals(found, true);
}
Also used : Status(site.ycsb.Status) ByteIterator(site.ycsb.ByteIterator) StringByteIterator(site.ycsb.StringByteIterator) HashMap(java.util.HashMap) StringByteIterator(site.ycsb.StringByteIterator) Test(org.junit.Test)

Aggregations

ByteIterator (site.ycsb.ByteIterator)131 HashMap (java.util.HashMap)98 StringByteIterator (site.ycsb.StringByteIterator)92 Status (site.ycsb.Status)62 Test (org.junit.Test)53 ByteArrayByteIterator (site.ycsb.ByteArrayByteIterator)34 DBException (site.ycsb.DBException)30 Map (java.util.Map)20 IOException (java.io.IOException)10 Put (org.apache.hadoop.hbase.client.Put)8 ArrayList (java.util.ArrayList)7 Vector (java.util.Vector)7 ByteBuffer (java.nio.ByteBuffer)6 HashSet (java.util.HashSet)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 NumericByteIterator (site.ycsb.NumericByteIterator)5 EntityProperty (com.microsoft.azure.storage.table.EntityProperty)4 Properties (java.util.Properties)4 Assume.assumeNoException (org.junit.Assume.assumeNoException)4 DB (site.ycsb.DB)4