26
26
import org .springframework .dao .DataAccessException ;
27
27
import org .springframework .data .keyvalue .core .AbstractKeyValueAdapter ;
28
28
import org .springframework .data .keyvalue .core .KeyValueAdapter ;
29
+ import org .springframework .data .keyvalue .core .mapping .KeyValuePersistentProperty ;
29
30
import org .springframework .data .redis .connection .RedisConnection ;
30
31
import org .springframework .data .redis .connection .RedisConnectionFactory ;
31
32
import org .springframework .data .redis .connection .jedis .JedisConnectionFactory ;
34
35
import org .springframework .data .redis .core .convert .RedisData ;
35
36
import org .springframework .data .redis .core .convert .ReferenceResolverImpl ;
36
37
import org .springframework .data .redis .core .index .IndexConfiguration ;
37
- import org .springframework .data .redis .util .ByteUtils ;
38
38
import org .springframework .data .util .CloseableIterator ;
39
39
40
40
/**
@@ -48,11 +48,11 @@ public class RedisKeyValueAdapter extends AbstractKeyValueAdapter {
48
48
49
49
private MappingRedisConverter converter ;
50
50
51
- public RedisKeyValueAdapter () {
51
+ RedisKeyValueAdapter () {
52
52
this ((IndexConfiguration ) null );
53
53
}
54
54
55
- public RedisKeyValueAdapter (IndexConfiguration indexConfiguration ) {
55
+ RedisKeyValueAdapter (IndexConfiguration indexConfiguration ) {
56
56
57
57
super (new RedisQueryEngine ());
58
58
@@ -86,35 +86,33 @@ public RedisKeyValueAdapter(RedisOperations<?, ?> redisOps, IndexConfiguration i
86
86
*/
87
87
public Object put (final Serializable id , final Object item , final Serializable keyspace ) {
88
88
89
- final RedisData rdo = new RedisData ();
90
- converter .write (item , rdo );
89
+ final RedisData rdo = item instanceof RedisData ? (RedisData ) item : new RedisData ();
90
+ if (!(item instanceof RedisData )) {
91
+ converter .write (item , rdo );
92
+ }
93
+
94
+ if (rdo .getId () == null ) {
91
95
92
- final byte [] indexPostFixPattern = converter .toBytes (":*" );
96
+ rdo .setId (id );
97
+ KeyValuePersistentProperty idProperty = converter .getMappingContext ().getPersistentEntity (item .getClass ())
98
+ .getIdProperty ();
99
+ converter .getMappingContext ().getPersistentEntity (item .getClass ()).getPropertyAccessor (item )
100
+ .setProperty (idProperty , id );
101
+
102
+ }
93
103
94
104
redisOps .execute (new RedisCallback <Object >() {
95
105
96
106
@ Override
97
107
public Object doInRedis (RedisConnection connection ) throws DataAccessException {
98
108
99
- connection .hMSet (rdo .getKey (), rdo .getData ());
100
- connection .sAdd (rdo .getKeyspace (), rdo .getId ());
101
-
102
- // remove id from potential indexes since those might be invalid with the new data
103
- for (byte [] potentialIndex : rdo .getIndexPaths ()) {
104
-
105
- Set <byte []> existingKeys = connection .keys (ByteUtils .concat (potentialIndex , indexPostFixPattern ));
106
-
107
- for (byte [] existingKey : existingKeys ) {
108
- connection .sRem (existingKey , rdo .getId ());
109
- }
110
- }
109
+ byte [] key = converter .toBytes (rdo .getId ());
111
110
112
- if (!rdo .getSimpleIndexKeys ().isEmpty ()) {
111
+ connection .del (createKey (rdo .getKeyspace (), rdo .getId ()));
112
+ connection .hMSet (createKey (rdo .getKeyspace (), rdo .getId ()), rdo .getBucket ().rawMap ());
113
+ connection .sAdd (converter .toBytes (rdo .getKeyspace ()), key );
113
114
114
- for (byte [] index : rdo .getSimpleIndexKeys ()) {
115
- connection .sAdd (index , rdo .getId ());
116
- }
117
- }
115
+ new IndexDataWriter (rdo .getKeyspace (), connection , converter ).updateIndexes (key , rdo .getIndexedData ());
118
116
return null ;
119
117
}
120
118
});
@@ -145,7 +143,7 @@ public Boolean doInRedis(RedisConnection connection) throws DataAccessException
145
143
*/
146
144
public Object get (Serializable id , Serializable keyspace ) {
147
145
148
- final byte [] binId = converter . convertToId (keyspace , id );
146
+ final byte [] binId = createKey (keyspace , id );
149
147
150
148
Map <byte [], byte []> raw = redisOps .execute (new RedisCallback <Map <byte [], byte []>>() {
151
149
@@ -176,18 +174,10 @@ public Object delete(final Serializable id, final Serializable keyspace) {
176
174
@ Override
177
175
public Void doInRedis (RedisConnection connection ) throws DataAccessException {
178
176
179
- connection .del (converter . convertToId ( binKeyspace , binId ));
177
+ connection .del (createKey ( keyspace , id ));
180
178
connection .sRem (binKeyspace , binId );
181
179
182
- Set <byte []> potentialIndex = connection .keys (converter .toBytes (keyspace + ".*" ));
183
-
184
- for (byte [] indexKey : potentialIndex ) {
185
- try {
186
- connection .sRem (indexKey , binId );
187
- } catch (Exception e ) {
188
- System .err .println (e );
189
- }
190
- }
180
+ new IndexDataWriter (keyspace , connection , converter ).removeKeyFromIndexes (binId );
191
181
return null ;
192
182
}
193
183
});
@@ -214,7 +204,7 @@ public List<Map<byte[], byte[]>> doInRedis(RedisConnection connection) throws Da
214
204
Set <byte []> members = connection .sMembers (binKeyspace );
215
205
216
206
for (byte [] id : members ) {
217
- rawData .add (connection .hGetAll (converter . convertToId (binKeyspace , id )));
207
+ rawData .add (connection .hGetAll (createKey (binKeyspace , id )));
218
208
}
219
209
220
210
return rawData ;
@@ -241,12 +231,7 @@ public void deleteAllOf(final Serializable keyspace) {
241
231
public Void doInRedis (RedisConnection connection ) throws DataAccessException {
242
232
243
233
connection .del (converter .toBytes (keyspace ));
244
-
245
- Set <byte []> potentialIndex = connection .keys (converter .toBytes (keyspace + ".*" ));
246
-
247
- for (byte [] indexKey : potentialIndex ) {
248
- connection .del (indexKey );
249
- }
234
+ new IndexDataWriter (keyspace , connection , converter ).removeAllIndexes ();
250
235
return null ;
251
236
}
252
237
});
@@ -301,6 +286,10 @@ public void clear() {
301
286
// nothing to do
302
287
}
303
288
289
+ public byte [] createKey (Serializable keyspace , Serializable id ) {
290
+ return this .converter .toBytes (keyspace + ":" + id );
291
+ }
292
+
304
293
/*
305
294
* (non-Javadoc)
306
295
* @see org.springframework.beans.factory.DisposableBean#destroy()
0 commit comments