Skip to content

Commit

Permalink
Merge pull request #2 from agorapulse/feature/dynamodb-custom-indicies
Browse files Browse the repository at this point in the history
DynamoDB custom indicies
  • Loading branch information
musketyr committed Feb 23, 2018
2 parents 360108a + 7a5f32f commit bff8d3e
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ dependencies {

}

String currentVersion = '0.2.0'
String currentVersion = '0.2.1'

version = currentVersion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,10 @@ class DruDynamoDBMapper extends DynamoDBMapper {
List<T> ret = new ArrayList<>(dataSet.findAllByType(type))

if (expression.hashKeyValues) {
PropertyMetadata property = DynamoDB.INSTANCE.getDynamoDBClassMetadata(type).hash
PropertyMetadata property = expression.indexName ?
DynamoDB.INSTANCE.getDynamoDBClassMetadata(type).getHashIndexProperty(expression.indexName) :
DynamoDB.INSTANCE.getDynamoDBClassMetadata(type).hash

ret = ret.findAll {
it."$property.name" == expression.hashKeyValues."$property.name"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.agorapulse.dru.persistence.meta.PropertyMetadata
import com.agorapulse.dru.pojo.meta.PojoClassMetadata
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBIgnore
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBIndexHashKey
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey

/**
Expand Down Expand Up @@ -87,4 +88,14 @@ class DynamoDBClassMetadata extends PojoClassMetadata {
}
}
}

PropertyMetadata getHashIndexProperty(String index) {
for (PropertyMetadata property in persistentProperties) {
DynamoDBIndexHashKey annotation = getAnnotation(type, property.name, DynamoDBIndexHashKey)
if (annotation && (annotation.globalSecondaryIndexName() == index || index in annotation.globalSecondaryIndexNames())) {
return property
}
}
throw new IllegalArgumentException("Index $index in not associated to any property of $type")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,18 @@ class DynamoDBSpec extends Specification {
':bar' | [theRange: 'bar']
null | [:]
}

void 'find hash index'() {
when:
ClassMetadata classMetadata = new DynamoDBClassMetadata(DynamoDBTester)
then:
classMetadata.getHashIndexProperty('foo')
classMetadata.getHashIndexProperty('bar')
classMetadata.getHashIndexProperty('baz')

when:
classMetadata.getHashIndexProperty('boo')
then:
thrown(IllegalArgumentException)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.agorapulse.dru.dynamodb.persistence

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBIgnore
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBIndexHashKey
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable

Expand All @@ -19,4 +20,7 @@ class DynamoDBTester {

@DynamoDBIgnore
String theIgnored

@DynamoDBIndexHashKey(globalSecondaryIndexNames = ['foo', 'bar'], globalSecondaryIndexName = 'baz')
String theIndexed
}
5 changes: 5 additions & 0 deletions examples/avl/src/main/groovy/avl/MissionLogEntry.groovy
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package avl

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBIndexHashKey
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMarshalling
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable
Expand Down Expand Up @@ -40,7 +42,10 @@ class MissionLogEntry {
Map<String, Object> ext
// end::ext[]

@DynamoDBIndexHashKey(globalSecondaryIndexName='agentIdMissionLogEntryIndex')
@DynamoDBAttribute
Long agentId

Long villainId
String itemName

Expand Down
16 changes: 16 additions & 0 deletions examples/avl/src/test/groovy/avl/DynamoDBSampleSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.agorapulse.dru.dynamodb.persistence.DynamoDB
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression
import com.amazonaws.services.dynamodbv2.datamodeling.PaginatedQueryList
import org.joda.time.DateTime
import org.junit.Rule
import spock.lang.Specification
Expand Down Expand Up @@ -89,4 +90,19 @@ class DynamoDBSampleSpec extends Specification {
service.query(7).count == 1
}
// end::grailsService[]

void 'use secondary global index'() {
when:
DynamoDBMapper mapper = DynamoDB.createMapper(dru)

DynamoDBQueryExpression queryExpression = new DynamoDBQueryExpression<MissionLogEntry>()
.withHashKeyValues(new MissionLogEntry(agentId: 101))
.withIndexName('agentIdMissionLogEntryIndex')
.withConsistentRead(false)

PaginatedQueryList<MissionLogEntry> result = mapper.query(MissionLogEntry, queryExpression)

then:
result.size() == 1
}
}

0 comments on commit bff8d3e

Please sign in to comment.