22
22
import org .springframework .dao .EmptyResultDataAccessException ;
23
23
import org .springframework .util .Assert ;
24
24
25
- import java .util .ArrayList ;
26
- import java .util .HashMap ;
25
+ import java .util .Collections ;
27
26
import java .util .List ;
28
27
import java .util .Map ;
29
28
import java .util .Optional ;
29
+ import java .util .concurrent .atomic .AtomicInteger ;
30
+ import java .util .stream .Collectors ;
31
+ import java .util .stream .StreamSupport ;
30
32
31
33
/**
32
34
* Default implementation of the
@@ -66,6 +68,9 @@ public SimpleDynamoDBCrudRepository(
66
68
67
69
@ Override
68
70
public Optional <T > findById (ID id ) {
71
+
72
+ Assert .notNull (id , "The given id must not be null!" );
73
+
69
74
T result ;
70
75
if (entityInformation .isRangeKeyAware ()) {
71
76
result = dynamoDBOperations .load (domainType ,
@@ -84,51 +89,29 @@ public Optional<T> findById(ID id) {
84
89
85
90
@ SuppressWarnings ("unchecked" )
86
91
public List <T > findAllById (Iterable <ID > ids ) {
87
- Map <Class <?>, List <KeyPair >> keyPairsMap = new HashMap <>();
88
- List <KeyPair > keyPairs = new ArrayList <>();
89
- for (ID id : ids ) {
90
- if (entityInformation .isRangeKeyAware ()) {
91
- keyPairs .add (new KeyPair ().withHashKey (
92
- entityInformation .getHashKey (id )).withRangeKey (
93
- entityInformation .getRangeKey (id )));
94
- } else {
95
- keyPairs .add (new KeyPair ().withHashKey (id ));
96
- }
97
- }
98
- keyPairsMap .put (domainType , keyPairs );
99
- return (List <T >) dynamoDBOperations .batchLoad (keyPairsMap ).get (dynamoDBOperations .getOverriddenTableName (domainType , entityInformation .getDynamoDBTableName ()));
100
- }
101
92
102
- protected T load (ID id ) {
103
- if (entityInformation .isRangeKeyAware ()) {
104
- return dynamoDBOperations .load (domainType ,
105
- entityInformation .getHashKey (id ),
106
- entityInformation .getRangeKey (id ));
107
- } else {
108
- return dynamoDBOperations .load (domainType ,
109
- entityInformation .getHashKey (id ));
110
- }
111
- }
93
+ Assert .notNull (ids , "The given ids must not be null!" );
94
+
95
+ // Works only with non-parallel streams!
96
+ AtomicInteger idx = new AtomicInteger ();
97
+ List <KeyPair > keyPairs = StreamSupport .stream (ids .spliterator (), false ).map (id -> {
98
+
99
+ Assert .notNull (id , "The given id at position " + idx .getAndIncrement () + " must not be null!" );
112
100
113
- @ SuppressWarnings ("unchecked" )
114
- protected List <T > loadBatch (Iterable <ID > ids ) {
115
- Map <Class <?>, List <KeyPair >> keyPairsMap = new HashMap <Class <?>, List <KeyPair >>();
116
- List <KeyPair > keyPairs = new ArrayList <KeyPair >();
117
- for (ID id : ids ) {
118
101
if (entityInformation .isRangeKeyAware ()) {
119
- keyPairs . add ( new KeyPair ().withHashKey (
102
+ return new KeyPair ().withHashKey (
120
103
entityInformation .getHashKey (id )).withRangeKey (
121
- entityInformation .getRangeKey (id ))) ;
104
+ entityInformation .getRangeKey (id ));
122
105
} else {
123
- keyPairs .add (new KeyPair ().withHashKey (id ));
124
-
106
+ return new KeyPair ().withHashKey (id );
125
107
}
126
- }
127
- keyPairsMap .put (domainType , keyPairs );
128
- return (List <T >) dynamoDBOperations .batchLoad (keyPairsMap ).get (domainType );
108
+ }).collect (Collectors .toList ());
109
+
110
+ Map <Class <?>, List <KeyPair >> keyPairsMap = Collections .singletonMap (domainType , keyPairs );
111
+ return (List <T >)dynamoDBOperations .batchLoad (keyPairsMap )
112
+ .get (dynamoDBOperations .getOverriddenTableName (domainType , entityInformation .getDynamoDBTableName ()));
129
113
}
130
114
131
-
132
115
@ Override
133
116
public <S extends T > S save (S entity ) {
134
117
0 commit comments