Search in sources :

Example 1 with User

use of org.apache.gora.benchmark.generated.User in project gora by apache.

the class GoraClientTest method testCorrectness.

@Test
public void testCorrectness() {
    Status result = benchmarkClient.insert(Constants.TEST_TABLE, Constants.TEST_KEY_4, INTEGER_DATA);
    assertEquals(result, Status.OK);
    try {
        User user = readRecord(Constants.TEST_KEY_4);
        assertEquals(190, sum(user));
    } catch (GoraException e) {
        LOG.info("There is a problem reading record from the datastore", e.getMessage(), e);
    }
}
Also used : Status(site.ycsb.Status) GoraException(org.apache.gora.util.GoraException) User(org.apache.gora.benchmark.generated.User) Test(org.junit.Test)

Example 2 with User

use of org.apache.gora.benchmark.generated.User in project gora by apache.

the class GoraBenchmarkClient method read.

/**
 * Read a record from the database. Each field/value pair from the result will
 * be stored in a HashMap.
 *
 * @param table The name of the table
 * @param key The record key of the record to read.
 * @param fields The list of fields to read, or null for all of them
 * @param result A HashMap oftestInsert field/value pairs for the result
 * @return The result of the operation.
 */
@Override
public Status read(String table, String key, Set<String> fields, Map<String, ByteIterator> result) {
    try {
        // Check for null is necessary.
        User user = (fields == null || fields.size() == 0) ? dataStore.get(key) : dataStore.get(key, fields.toArray(DUMMY_ARRAY));
        for (int fieldCount = 0; fieldCount < totalFieldCount; fieldCount++) {
            String field = FIELDS[fieldCount + 1];
            int fieldIndex = fieldCount + 1;
            String value = user.get(fieldIndex).toString();
            result.put(field, new StringByteIterator(value));
        }
    } catch (Exception e) {
        LOG.info("There is a problem in reading data from the table \n {}", e.getMessage(), e);
        return Status.ERROR;
    }
    return Status.OK;
}
Also used : User(org.apache.gora.benchmark.generated.User) StringByteIterator(site.ycsb.StringByteIterator) DBException(site.ycsb.DBException) GoraException(org.apache.gora.util.GoraException)

Example 3 with User

use of org.apache.gora.benchmark.generated.User 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 4 with User

use of org.apache.gora.benchmark.generated.User in project gora by apache.

the class GoraClientTest method testUpdate.

/**
 * Test update performs an update record test in the database
 *
 * @throws GoraException
 *           the gora exception
 */
@Test
public void testUpdate() throws GoraException {
    insertData();
    Status result = benchmarkClient.update(Constants.TEST_TABLE, Constants.TEST_KEY_1, DATA_TO_UPDATE);
    assertEquals(result, Status.OK);
    if (result == Status.OK) {
        benchmarkClient.getDataStore().flush();
        User u = readRecord(Constants.TEST_KEY_1);
        assertEquals(Constants.TEST_UPDATED_0, u.getField0().toString());
    }
}
Also used : Status(site.ycsb.Status) User(org.apache.gora.benchmark.generated.User) Test(org.junit.Test)

Example 5 with User

use of org.apache.gora.benchmark.generated.User in project gora by apache.

the class GoraBenchmarkClient 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 The result of the operation.
 */
@Override
public Status update(String table, String key, Map<String, ByteIterator> values) {
    try {
        // We first get the database object to update
        User user = dataStore.get(key);
        List<String> allFields = Arrays.asList(FIELDS);
        for (String field : values.keySet()) {
            if (GoraBenchmarkUtils.isFieldUpdatable(field, values)) {
                // Get the index of the field from the global fields array and call
                // the right method
                int indexOfFieldInArray = allFields.indexOf(field);
                user.put(indexOfFieldInArray, values.get(field).toString());
                user.setDirty(indexOfFieldInArray);
            }
        }
        dataStore.put(user.getUserId().toString(), user);
    } catch (Exception e) {
        LOG.info("There is a problem updating the records \n {}", e.getMessage(), e);
        return Status.ERROR;
    }
    return Status.OK;
}
Also used : User(org.apache.gora.benchmark.generated.User) DBException(site.ycsb.DBException) GoraException(org.apache.gora.util.GoraException)

Aggregations

User (org.apache.gora.benchmark.generated.User)5 GoraException (org.apache.gora.util.GoraException)4 DBException (site.ycsb.DBException)3 Test (org.junit.Test)2 Status (site.ycsb.Status)2 StringByteIterator (site.ycsb.StringByteIterator)2 HashMap (java.util.HashMap)1 ByteIterator (site.ycsb.ByteIterator)1