Skip to content

Commit 353220a

Browse files
authored
Merge pull request #64 from alex-arana/master
Fix for Issue #52 - Investigate Order By DynamoDBIndexRangeKey Support.
2 parents 505fb63 + 99092ed commit 353220a

File tree

6 files changed

+205
-0
lines changed

6 files changed

+205
-0
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@
316316
<table>src/test/resources/playlist_table.json</table>
317317
<table>src/test/resources/feeduser_table.json</table>
318318
<table>src/test/resources/customerhistory_table.json</table>
319+
<table>src/test/resources/installation_table.json</table>
319320
</tables>
320321
<port>${dynamodblocal.port}</port>
321322
<dist>${project.build.directory}/dynamodb-dist</dist>

src/main/java/org/socialsignin/spring/data/dynamodb/repository/query/AbstractDynamoDBQueryCriteria.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ protected QueryRequest buildQueryRequest(String tableName, String theIndexName,
109109
}
110110
}
111111

112+
if (sort != null) {
113+
for (Order order : sort) {
114+
final String sortProperty = order.getProperty();
115+
if (entityInformation.isGlobalIndexRangeKeyProperty(sortProperty)) {
116+
allowedSortProperties.add(sortProperty);
117+
}
118+
}
119+
}
120+
112121
queryRequest.setKeyConditions(keyConditions);
113122
queryRequest.setSelect(Select.ALL_PROJECTED_ATTRIBUTES);
114123
applySortIfSpecified(queryRequest, new ArrayList<String>(new HashSet<String>(allowedSortProperties)));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.socialsignin.spring.data.dynamodb.domain.sample;
2+
3+
import static org.junit.Assert.fail;
4+
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertFalse;
6+
import static org.junit.Assert.assertNotNull;
7+
8+
import java.util.Calendar;
9+
import java.util.Date;
10+
import java.util.List;
11+
12+
import org.junit.Test;
13+
import org.junit.runner.RunWith;
14+
15+
import org.socialsignin.spring.data.dynamodb.core.ConfigurationTI;
16+
import org.socialsignin.spring.data.dynamodb.repository.config.EnableDynamoDBRepositories;
17+
import org.springframework.beans.factory.annotation.Autowired;
18+
import org.springframework.context.annotation.Configuration;
19+
import org.springframework.test.context.ContextConfiguration;
20+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
21+
22+
23+
/**
24+
* Shows the usage of Hash+Range key combinations with global secondary indexes.
25+
*/
26+
@RunWith(SpringJUnit4ClassRunner.class)
27+
@ContextConfiguration(classes = {ConfigurationTI.class, GlobalSecondaryIndexWithRangeKeyIT.TestAppConfig.class})
28+
public class GlobalSecondaryIndexWithRangeKeyIT {
29+
30+
@Configuration
31+
@EnableDynamoDBRepositories(basePackages = "org.socialsignin.spring.data.dynamodb.domain.sample")
32+
public static class TestAppConfig {
33+
}
34+
35+
@Autowired
36+
private InstallationRepository installationRepository;
37+
38+
@Test
39+
public void testFindBySystemIdOrderByUpdatedAtDesc() {
40+
installationRepository.save(new Installation("systemId", createDate(10, 5, 1995)));
41+
installationRepository.save(new Installation("systemId", createDate(20, 10, 2001)));
42+
installationRepository.save(new Installation("systemId", createDate(28, 10, 2016)));
43+
44+
final List<Installation> actual = installationRepository.findBySystemIdOrderByUpdatedAtDesc("systemId");
45+
assertNotNull(actual);
46+
assertFalse(actual.isEmpty());
47+
48+
Date previousDate = null;
49+
for (final Installation installation : actual) {
50+
assertEquals(installation.getSystemId(), "systemId");
51+
if (previousDate != null && installation.getUpdatedAt().compareTo(previousDate) != -1) {
52+
fail("Results were not returned in descending order of updated date!");
53+
} else {
54+
previousDate = installation.getUpdatedAt();
55+
}
56+
}
57+
}
58+
59+
private Date createDate(final int dayOfMonth, final int month, final int year) {
60+
final Calendar calendar = Calendar.getInstance();
61+
calendar.set(year, month, dayOfMonth, 0, 0, 0);
62+
calendar.set(Calendar.MILLISECOND, 0);
63+
return calendar.getTime();
64+
}
65+
}
66+
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.socialsignin.spring.data.dynamodb.domain.sample;
2+
3+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAutoGeneratedKey;
4+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
5+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBIndexHashKey;
6+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBIndexRangeKey;
7+
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
8+
import java.io.Serializable;
9+
import java.util.Date;
10+
11+
12+
/**
13+
* Test for <a href="https://github.com/derjust/spring-data-dynamodb/issues/52">Issue 52</a>.
14+
*/
15+
@DynamoDBTable(tableName = "installations")
16+
public class Installation implements Serializable {
17+
private static final long serialVersionUID = 1L;
18+
19+
@DynamoDBHashKey
20+
@DynamoDBAutoGeneratedKey
21+
public String id;
22+
23+
@DynamoDBIndexHashKey(globalSecondaryIndexName = "idx-global-systemid")
24+
private String systemId;
25+
26+
@DynamoDBIndexRangeKey(globalSecondaryIndexName = "idx-global-systemid")
27+
private Date updatedAt;
28+
29+
30+
public Installation() {
31+
32+
}
33+
34+
public Installation(final String systemId, final Date updatedAt) {
35+
this.systemId = systemId;
36+
this.updatedAt = updatedAt;
37+
}
38+
39+
public String getId() {
40+
return id;
41+
}
42+
43+
public void setId(final String id) {
44+
this.id = id;
45+
}
46+
47+
public String getSystemId() {
48+
return systemId;
49+
}
50+
51+
public void setSystemId(final String systemId) {
52+
this.systemId = systemId;
53+
}
54+
55+
public Date getUpdatedAt() {
56+
return updatedAt;
57+
}
58+
59+
public void setUpdatedAt(final Date updatedAt) {
60+
this.updatedAt = updatedAt;
61+
}
62+
63+
@Override
64+
public String toString() {
65+
return "Installation [id='" + id + "', systemId='" + systemId + "', updatedAt='" + updatedAt + "']";
66+
}
67+
}
68+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.socialsignin.spring.data.dynamodb.domain.sample;
2+
3+
import java.util.List;
4+
import org.springframework.data.repository.CrudRepository;
5+
6+
7+
public interface InstallationRepository extends CrudRepository<Installation, String> {
8+
9+
public List<Installation> findBySystemIdOrderByUpdatedAtDesc(String systemId);
10+
11+
}
12+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"AttributeDefinitions": [
3+
{
4+
"AttributeName": "id",
5+
"AttributeType": "S"
6+
},
7+
{
8+
"AttributeName": "systemId",
9+
"AttributeType": "S"
10+
},
11+
{
12+
"AttributeName": "updatedAt",
13+
"AttributeType": "S"
14+
}
15+
],
16+
"KeySchema": [
17+
{
18+
"AttributeName": "id",
19+
"KeyType": "HASH"
20+
}
21+
],
22+
"ProvisionedThroughput": {
23+
"ReadCapacityUnits": "1",
24+
"WriteCapacityUnits": "1"
25+
},
26+
"GlobalSecondaryIndexes": [
27+
{
28+
"IndexName": "idx-global-systemid",
29+
"KeySchema": [
30+
{
31+
"AttributeName": "systemId",
32+
"KeyType": "HASH"
33+
},
34+
{
35+
"AttributeName": "updatedAt",
36+
"KeyType": "RANGE"
37+
}
38+
],
39+
"Projection": {
40+
"ProjectionType": "ALL"
41+
},
42+
"ProvisionedThroughput": {
43+
"ReadCapacityUnits": 1,
44+
"WriteCapacityUnits": 1
45+
}
46+
}
47+
],
48+
"TableName": "installations"
49+
}

0 commit comments

Comments
 (0)