Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
derjust committed Feb 2, 2018
1 parent 4748ee5 commit 2433068
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

Expand All @@ -37,20 +38,28 @@
public class HashRangeKeyIT {

@Autowired
PlaylistRepository playlistRepository;
private PlaylistRepository playlistRepository;

@Test
public void runCrudOperations() {
final String displayName = "displayName" + UUID.randomUUID().toString();
private Playlist generatePlaylist(String genre, String displayName) {
final String userName = "userName-" + UUID.randomUUID().toString();
final String playlistName = "playlistName-" + UUID.randomUUID().toString();
PlaylistId id = new PlaylistId(userName, playlistName);

Optional<Playlist> actual = playlistRepository.findById(id);
assertFalse(actual.isPresent());

Playlist playlist = new Playlist(id);
playlist.setDisplayName(displayName);
playlist.setGenre(genre);

return playlist;
}

@Test
public void runCrudOperations() {
String displayName = "displayName" + UUID.randomUUID().toString();
Playlist playlist = generatePlaylist("genre-" + UUID.randomUUID().toString(), displayName);
PlaylistId id = playlist.getId();

Optional<Playlist> actual = playlistRepository.findById(id);
assertFalse(actual.isPresent());

playlistRepository.save(playlist);

Expand All @@ -60,4 +69,16 @@ public void runCrudOperations() {
assertEquals(id.getPlaylistName(), actual.get().getPlaylistName());
assertEquals(id.getUserName(), actual.get().getUserName());
}

@Test
public void testGsiSharedRange() {
final String GENRE = "GENRE";
playlistRepository.save(generatePlaylist(GENRE, "1"));
playlistRepository.save(generatePlaylist(GENRE, "2"));
playlistRepository.save(generatePlaylist(GENRE, "3"));

List<Playlist> actual = playlistRepository.findByGenreAndPlaylistNameBetween(GENRE, "2", "3");

assertEquals(2, actual.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
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.DynamoDBIndexRangeKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;
import org.springframework.data.annotation.Id;
Expand All @@ -27,13 +30,19 @@ public class Playlist {
@Id
private PlaylistId playlistId;
private String displayName;
private String genre;

public Playlist() {
}

public Playlist(PlaylistId playlistId) {
this.playlistId = playlistId;
}

@DynamoDBIgnore
public PlaylistId getId() {
return this.playlistId;
}

@DynamoDBAttribute(attributeName="DisplayName")
public String getDisplayName() {
Expand All @@ -57,6 +66,7 @@ public void setUserName(String userName) {
}

@DynamoDBRangeKey(attributeName = "PlaylistName")
@DynamoDBIndexRangeKey(attributeName = "PlaylistName", globalSecondaryIndexName="Genre-PlaylistName-index")
public String getPlaylistName() {
return playlistId != null ? playlistId.getPlaylistName() : null;
}
Expand All @@ -67,4 +77,13 @@ public void setPlaylistName(String playlistName) {
}
playlistId.setPlaylistName(playlistName);
}

@DynamoDBIndexHashKey(attributeName = "Genre", globalSecondaryIndexName="Genre-PlaylistName-index")
public String getGenre() {
return this.genre;
}

public void setGenre(String genre) {
this.genre = genre;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
*/
package org.socialsignin.spring.data.dynamodb.domain.sample;

import java.util.List;

import org.springframework.data.repository.CrudRepository;

public interface PlaylistRepository extends CrudRepository<Playlist, PlaylistId> {

List<Playlist> findByGenreAndPlaylistNameBetween(String genre, String startPlaylistName, String endPlaylistName);

}
23 changes: 23 additions & 0 deletions src/test/resources/playlist_table.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,29 @@
"AttributeType": "S"
}
],
"GlobalSecondaryIndexes": [
{
"IndexName": "Genre-PlaylistName-index",
"KeySchema": [
{
"AttributeName": "Genre",
"KeyType": "HASH"
},
{
"AttributeName": "PlaylistName",
"KeyType": "RANGE"
}
],
"Projection": {
"NonKeyAttributes": [],
"ProjectionType": "ALL"
},
"ProvisionedThroughput": {
"ReadCapacityUnits": "10",
"WriteCapacityUnits": "10"
}
}
],
"KeySchema": [
{
"AttributeName": "UserName",
Expand Down

0 comments on commit 2433068

Please sign in to comment.