Search in sources :

Example 1 with DynamoDBKey

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

the class DynamoDBNativeStore method deleteByQuery.

/**
 * Deletes items using a specific query
 *
 * @param query matching records to this query will be deleted
 * @return
 */
@Override
@SuppressWarnings("unchecked")
public long deleteByQuery(Query<K, T> query) throws GoraException {
    // TODO verify whether or not we are deleting a whole row
    // String[] fields = getFieldsToQuery(query.getFields());
    // find whether all fields are queried, which means that complete
    // rows will be deleted
    // boolean isAllFields = Arrays.equals(fields
    // , getBeanFactory().getCachedPersistent().getFields());
    ArrayList<T> deletes = null;
    try {
        Result<K, T> result = execute(query);
        deletes = new ArrayList<T>();
        while (result.next()) {
            T resultObj = result.get();
            deletes.add(resultObj);
            @SuppressWarnings("rawtypes") DynamoDBKey dKey = new DynamoDBKey();
            dKey.setHashKey(getHashFromObj(resultObj));
            dKey.setRangeKey(getRangeKeyFromObj(resultObj));
            delete((K) dKey);
        }
    } catch (GoraException e) {
        // If it is a GoraException we assume it is already logged
        throw e;
    } catch (Exception e) {
        throw new GoraException(e);
    }
    return deletes.size();
}
Also used : GoraException(org.apache.gora.util.GoraException) DynamoDBKey(org.apache.gora.dynamodb.query.DynamoDBKey) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) GoraException(org.apache.gora.util.GoraException)

Example 2 with DynamoDBKey

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

the class DynamoDBNativeStore method getRangeKeyFromObj.

/**
 * Gets a range key from an object T
 *
 * @param obj
 *          Object from which a range key will be extracted
 * @return
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 */
private Object getRangeKeyFromObj(T 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 3 with DynamoDBKey

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

the class TestDynamoDBNativeStore method assertTestGetDataStore.

/**
 * Method to get an specific object using a key
 */
@Override
public void assertTestGetDataStore() {
    log.info("test method: testGet using specific data store.");
    try {
        DynamoDBKey<Long, String> dKey = new DynamoDBKey<>();
        dKey.setHashKey(11L);
        dKey.setRangeKey("10/10/1999");
        // insert item
        Person p1 = buildPerson(dKey.getHashKey(), dKey.getRangeKey().toString(), "Inca", "Atahualpa", "Peru", "Brazil", "Ecuador");
        dataStore.put(dKey, p1);
        // get item
        Person p2 = dataStore.get(dKey);
        printPersonInfo(p2);
    } catch (Exception e) {
        log.error("error in test method: testGetDataStore.", 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 4 with DynamoDBKey

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

the class TestDynamoDBNativeStore method assertPut.

/**
 * Method to put items into the data store
 */
@Override
public void assertPut() {
    try {
        log.info("test method: TestPut using DynamoDB store.");
        DynamoDBKey<Long, String> dKey = new DynamoDBKey<>();
        dKey.setHashKey(12L);
        dKey.setRangeKey("10/10/1880");
        Person p1 = buildPerson(dKey.getHashKey(), dKey.getRangeKey().toString(), "Inca", "Atahualpa", "Peru", "Brazil", "Ecuador");
        dataStore.put(dKey, p1);
        dKey.setRangeKey("11/10/1707");
        Person p2 = buildPerson(dKey.getHashKey(), dKey.getRangeKey().toString(), "William", "Wallace", "Germany", "USA", "Scotland");
        dataStore.put(dKey, p2);
    } catch (Exception e) {
        log.error("error in test method: testPut.", 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 5 with DynamoDBKey

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

the class DynamoDBNativeStore method getHashFromKey.

/**
 * Gets a hash key from a key of type K
 *
 * @param obj
 *          Object from which we will get a hash key
 * @return
 * @throws IllegalArgumentException
 * @throws IllegalAccessException
 * @throws InvocationTargetException
 */
private Object getHashFromKey(K 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)

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