Search in sources :

Example 6 with DynamoDBKey

use of org.apache.gora.dynamodb.query.DynamoDBKey in project gora by apache.

the class DynamoDBNativeStore method getHashFromObj.

/**
 * Gets a hash key from an object of type T
 *
 * @param obj
 *          Object from which we will get a hash key
 * @return
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 */
private Object getHashFromObj(T obj) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    Object hashKey = null;
    // check if it is a DynamoDBKey
    if (obj instanceof DynamoDBKey) {
        hashKey = ((DynamoDBKey<?, ?>) obj).getHashKey();
    } else {
        // maybe the class has the method defined
        for (Method met : obj.getClass().getDeclaredMethods()) {
            if (met.getName().equals(GET_HASH_KEY_METHOD)) {
                Object[] params = null;
                hashKey = met.invoke(obj, params);
                break;
            }
        }
    }
    return hashKey;
}
Also used : DynamoDBKey(org.apache.gora.dynamodb.query.DynamoDBKey) Method(java.lang.reflect.Method)

Example 7 with DynamoDBKey

use of org.apache.gora.dynamodb.query.DynamoDBKey in project gora by apache.

the class DynamoDBNativeStore method getRangeKeyFromKey.

/**
 * Gets a range key from a key obj. This verifies if it is using a
 * {@link DynamoDBKey}
 *
 * @param obj
 *          Object from which a range key will be extracted
 * @return
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 */
private Object getRangeKeyFromKey(K obj) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    Object rangeKey = null;
    // check if it is a DynamoDBKey
    if (obj instanceof DynamoDBKey) {
        rangeKey = ((DynamoDBKey<?, ?>) obj).getRangeKey();
    } else {
        // maybe the class has the method defined
        for (Method met : obj.getClass().getDeclaredMethods()) {
            if (met.getName().equals(GET_RANGE_KEY_METHOD)) {
                Object[] params = null;
                rangeKey = met.invoke(obj, params);
                break;
            }
        }
    }
    return rangeKey;
}
Also used : DynamoDBKey(org.apache.gora.dynamodb.query.DynamoDBKey) Method(java.lang.reflect.Method)

Example 8 with DynamoDBKey

use of org.apache.gora.dynamodb.query.DynamoDBKey in project gora by apache.

the class TestDynamoDBNativeStore method assertTestDeleteByQueryDataStore.

// ==========================================================================
/**
 * Tests deleting items using a query
 */
@Override
public void assertTestDeleteByQueryDataStore() {
    try {
        log.info("test method: TestDeleteByQuery using DynamoDB store.");
        DynamoDBKey<Long, String> dKey = new DynamoDBKey<>();
        dKey.setHashKey(100L);
        dKey.setRangeKey("10/10/1880");
        Person p1 = buildPerson(dKey.getHashKey(), dKey.getRangeKey().toString(), "John", "Doe", "Peru", "Brazil", "Ecuador");
        dataStore.put(dKey, p1);
        dKey.setRangeKey("11/10/1707");
        Person p2 = buildPerson(dKey.getHashKey(), dKey.getRangeKey().toString(), "Juan", "Perez", "Germany", "USA", "Scotland");
        dataStore.put(dKey, p2);
        DynamoDBQuery.setScanCompOp(ComparisonOperator.LE);
        DynamoDBQuery.setType(DynamoDBQuery.SCAN_QUERY);
        Query<DynamoDBKey, Person> query = new DynamoDBQuery<DynamoDBKey, Person>();
        query.setKey(dKey);
        log.info("Number of records deleted: " + dataStore.deleteByQuery(query));
    } catch (Exception e) {
        log.error("Error while running test: TestDeleteByQuery", e.getMessage());
        throw new RuntimeException(e);
    }
}
Also used : DynamoDBQuery(org.apache.gora.dynamodb.query.DynamoDBQuery) DynamoDBKey(org.apache.gora.dynamodb.query.DynamoDBKey) Person(org.apache.gora.dynamodb.example.generated.Person) IOException(java.io.IOException)

Example 9 with DynamoDBKey

use of org.apache.gora.dynamodb.query.DynamoDBKey in project gora by apache.

the class TestDynamoDBNativeStore method assertTestDeleteDataStore.

/**
 * Method to delete items into the data store
 */
@Override
public void assertTestDeleteDataStore() {
    log.info("test method: testDelete by key");
    try {
        DynamoDBKey<Long, String> dKey = new DynamoDBKey<Long, String>();
        dKey.setHashKey(10L);
        dKey.setRangeKey("10/10/1985");
        Person p1 = new Person();
        p1.setHashKey(dKey.getHashKey());
        p1.setRangeKey(dKey.getRangeKey());
        p1.setFirstName("Joao");
        p1.setLastName("Velasco");
        dataStore.put(dKey, p1);
        assertTrue(dataStore.delete(dKey));
        dKey.setRangeKey("10/10/1000");
        assertFalse(dataStore.delete(dKey));
    } catch (Exception e) {
        log.error("error in test method: testDeleteDataStore.", e.getMessage());
        throw new RuntimeException(e);
    }
}
Also used : DynamoDBKey(org.apache.gora.dynamodb.query.DynamoDBKey) Person(org.apache.gora.dynamodb.example.generated.Person) IOException(java.io.IOException)

Example 10 with DynamoDBKey

use of org.apache.gora.dynamodb.query.DynamoDBKey in project gora by apache.

the class TestDynamoDBNativeStore method assertTestUpdateDataStore.

/**
 * Tests updating a specific item
 */
@Override
public void assertTestUpdateDataStore() {
    try {
        log.info("test method: TestUpdate using DynamoDB store.");
        DynamoDBKey<Long, String> dKey = new DynamoDBKey<>();
        dKey.setHashKey(13L);
        dKey.setRangeKey("10/10/1880");
        Person p1 = buildPerson(dKey.getHashKey(), dKey.getRangeKey().toString(), "Inca", "Atahualpa", "Peru", "Brazil", "Ecuador");
        dataStore.put(dKey, p1);
        p1.setFirstName("Ataucuri");
        dataStore.put(dKey, p1);
    } catch (Exception e) {
        log.error("error in test method: testUpdate.", e.getMessage());
        throw new RuntimeException(e);
    }
}
Also used : DynamoDBKey(org.apache.gora.dynamodb.query.DynamoDBKey) Person(org.apache.gora.dynamodb.example.generated.Person) IOException(java.io.IOException)

Aggregations

DynamoDBKey (org.apache.gora.dynamodb.query.DynamoDBKey)10 IOException (java.io.IOException)6 Person (org.apache.gora.dynamodb.example.generated.Person)5 Method (java.lang.reflect.Method)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 DynamoDBQuery (org.apache.gora.dynamodb.query.DynamoDBQuery)1 GoraException (org.apache.gora.util.GoraException)1