Search in sources :

Example 6 with DynamoDBMapper

use of com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper in project gora by apache.

the class DynamoDBNativeStore method delete.

/**
   * Deletes the object using key
   *
   * @param key the key of the object
   * @return true for a successful process
   */
@Override
public boolean delete(K key) {
    try {
        T object = null;
        Object rangeKey = null, hashKey = null;
        DynamoDBMapper mapper = new DynamoDBMapper(dynamoDBStoreHandler.getDynamoDbClient());
        for (Method met : key.getClass().getDeclaredMethods()) {
            if (met.getName().equals(GET_RANGE_KEY_METHOD)) {
                Object[] params = null;
                rangeKey = met.invoke(key, params);
                break;
            }
        }
        for (Method met : key.getClass().getDeclaredMethods()) {
            if (met.getName().equals(GET_HASH_KEY_METHOD)) {
                Object[] params = null;
                hashKey = met.invoke(key, params);
                break;
            }
        }
        if (hashKey == null)
            object = (T) mapper.load(persistentClass, key);
        if (rangeKey == null)
            object = (T) mapper.load(persistentClass, hashKey);
        else
            object = (T) mapper.load(persistentClass, hashKey, rangeKey);
        if (object == null)
            return false;
        // setting key for dynamodbMapper
        mapper.delete(object);
        return true;
    } catch (Exception e) {
        LOG.error("Error while deleting value with key " + key.toString());
        LOG.error(e.getMessage());
        return false;
    }
}
Also used : DynamoDBMapper(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper) Method(java.lang.reflect.Method) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) NullArgumentException(org.apache.commons.lang.NullArgumentException) GoraException(org.apache.gora.util.GoraException)

Example 7 with DynamoDBMapper

use of com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper in project gora by apache.

the class DynamoDBNativeStore method execute.

/**
   * Executes a query after building a DynamoDB specific query based on the
   * received one
   */
@Override
public Result<K, T> execute(Query<K, T> query) {
    DynamoDBQuery<K, T> dynamoDBQuery = buildDynamoDBQuery(query);
    DynamoDBMapper mapper = new DynamoDBMapper(dynamoDBStoreHandler.getDynamoDbClient());
    List<T> objList = null;
    if (DynamoDBQuery.getType().equals(DynamoDBQuery.RANGE_QUERY))
        objList = mapper.scan(persistentClass, (DynamoDBScanExpression) dynamoDBQuery.getQueryExpression());
    if (DynamoDBQuery.getType().equals(DynamoDBQuery.SCAN_QUERY))
        objList = mapper.scan(persistentClass, (DynamoDBScanExpression) dynamoDBQuery.getQueryExpression());
    return new DynamoDBResult<K, T>(this, query, objList);
}
Also used : DynamoDBResult(org.apache.gora.dynamodb.query.DynamoDBResult) DynamoDBMapper(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper)

Aggregations

DynamoDBMapper (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper)6 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 GoraException (org.apache.gora.util.GoraException)3 AmazonClientException (com.amazonaws.AmazonClientException)2 IOException (java.io.IOException)2 NullArgumentException (org.apache.commons.lang.NullArgumentException)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 CreateTableRequest (com.amazonaws.services.dynamodbv2.model.CreateTableRequest)1 GlobalSecondaryIndex (com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndex)1 ProvisionedThroughput (com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput)1 ResourceNotFoundException (com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 DynamoDBResult (org.apache.gora.dynamodb.query.DynamoDBResult)1 Item (org.openhab.core.items.Item)1 HistoricItem (org.openhab.core.persistence.HistoricItem)1 State (org.openhab.core.types.State)1 UnDefType (org.openhab.core.types.UnDefType)1