Accessing database in AWS - Part 2 ( Refactoring your code )




I have refactored the code to as below.
As you can see, I have refactored getRegistration into two methods. The first method is generic so that tomorrow if you want to use GetLoans, we could use the generic method. And also the method "getRegistration" now uses the generic method "getValueFromDatabase".
I am using the scan method of AWS ( which is more intensive) than query because I have to search on a column which is NOT a partition key.
If it was a partition key, I could have used "query" instead of "scan".
I am using if (scanResult.getCount() != 0) break;   to return from the scan immediately as soon as I get one result.
Else it would scan the entire database and I would get a "Throughput exceeded violation/error" from AWS.

import com.amazonaws.regions.Regions;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.model.*;
import com.myob.beepy.RestAssuredFramework.Data.Env.EnvironmentConfiguration;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {EnvironmentConfiguration.class})
@TestPropertySource(locations = "classpath:enviromentConfiguration.yml")
public class DatabaseAccess {

    private final AmazonDynamoDB client;
    @Autowired
    EnvironmentConfiguration environmentConfiguration;

    public DatabaseAccess() {

        client = AmazonDynamoDBClientBuilder.standard().withRegion(Regions.AP_SOUTHEAST_2).build();
    }

    public List> getRegistration(String registrationId) {
        String registrationsTableName = environmentConfiguration.getTablePrefix() + "BF_Applications";
        String columnName = "loan_id";
        return getValueFromDatabase(registrationsTableName, columnName, registrationId);
    }

    private List> getValueFromDatabase(String tableName, String columnName, String valueOfColumnToScan) {
        HashMap scanFilter = new HashMap<>();
        ScanResult scanResult;
        Condition condition = new Condition()
                .withComparisonOperator(ComparisonOperator.EQ.toString())
                .withAttributeValueList(new AttributeValue(valueOfColumnToScan));
        scanFilter.put(columnName, condition);
        Map lastKeyEvaluated = null;
        do {
            ScanRequest scanRequest = new ScanRequest(tableName).withScanFilter(scanFilter)
                    .withExclusiveStartKey(lastKeyEvaluated)
                    .withConsistentRead(true);
            scanResult = client.scan(scanRequest);
            if (scanResult.getCount() != 0) break;
            lastKeyEvaluated = scanResult.getLastEvaluatedKey();
        } while (lastKeyEvaluated != null);
        return scanResult.getItems();
    }
}

Comments

Popular posts from this blog

The pesky scrollbars on Remote desktop - Finally fixed!!

Exploring RedCritter - website for Agile project mangement

API Testing with Rest Assured - Validating the json schema