23
23
import java .util .List ;
24
24
import java .util .Map ;
25
25
26
- import org .springframework .dao .DataAccessException ;
27
26
import org .springframework .data .geo .Circle ;
28
27
import org .springframework .data .geo .GeoResult ;
29
28
import org .springframework .data .geo .GeoResults ;
30
29
import org .springframework .data .keyvalue .core .CriteriaAccessor ;
31
30
import org .springframework .data .keyvalue .core .QueryEngine ;
32
31
import org .springframework .data .keyvalue .core .SortAccessor ;
33
32
import org .springframework .data .keyvalue .core .query .KeyValueQuery ;
34
- import org .springframework .data .redis .connection .RedisConnection ;
35
33
import org .springframework .data .redis .connection .RedisGeoCommands .GeoLocation ;
36
34
import org .springframework .data .redis .core .convert .GeoIndexedPropertyValue ;
37
35
import org .springframework .data .redis .core .convert .RedisData ;
@@ -53,7 +51,7 @@ class RedisQueryEngine extends QueryEngine<RedisKeyValueAdapter, RedisOperationC
53
51
/**
54
52
* Creates new {@link RedisQueryEngine} with defaults.
55
53
*/
56
- public RedisQueryEngine () {
54
+ RedisQueryEngine () {
57
55
this (new RedisCriteriaAccessor (), null );
58
56
}
59
57
@@ -64,7 +62,7 @@ public RedisQueryEngine() {
64
62
* @param sortAccessor
65
63
* @see QueryEngine#QueryEngine(CriteriaAccessor, SortAccessor)
66
64
*/
67
- public RedisQueryEngine (CriteriaAccessor <RedisOperationChain > criteriaAccessor ,
65
+ private RedisQueryEngine (CriteriaAccessor <RedisOperationChain > criteriaAccessor ,
68
66
SortAccessor <Comparator <?>> sortAccessor ) {
69
67
super (criteriaAccessor , sortAccessor );
70
68
}
@@ -75,69 +73,64 @@ public RedisQueryEngine(CriteriaAccessor<RedisOperationChain> criteriaAccessor,
75
73
*/
76
74
@ Override
77
75
@ SuppressWarnings ("unchecked" )
78
- public <T > Collection <T > execute (final RedisOperationChain criteria , final Comparator <?> sort , final long offset ,
79
- final int rows , final String keyspace , Class <T > type ) {
76
+ public <T > Collection <T > execute (RedisOperationChain criteria , Comparator <?> sort , long offset , int rows ,
77
+ String keyspace , Class <T > type ) {
80
78
81
79
if (criteria == null
82
80
|| (CollectionUtils .isEmpty (criteria .getOrSismember ()) && CollectionUtils .isEmpty (criteria .getSismember ()))
83
81
&& criteria .getNear () == null ) {
84
82
return (Collection <T >) getAdapter ().getAllOf (keyspace , offset , rows );
85
83
}
86
84
87
- RedisCallback <Map <byte [], Map <byte [], byte []>>> callback = new RedisCallback < Map < byte [], Map < byte [], byte []>>>() {
85
+ RedisCallback <Map <byte [], Map <byte [], byte []>>> callback = connection -> {
88
86
89
- @ Override
90
- public Map <byte [], Map <byte [], byte []>> doInRedis (RedisConnection connection ) throws DataAccessException {
91
-
92
- List <byte []> allKeys = new ArrayList <byte []>();
93
- if (!criteria .getSismember ().isEmpty ()) {
94
- allKeys .addAll (connection .sInter (keys (keyspace + ":" , criteria .getSismember ())));
95
- }
87
+ List <byte []> allKeys = new ArrayList <>();
88
+ if (!criteria .getSismember ().isEmpty ()) {
89
+ allKeys .addAll (connection .sInter (keys (keyspace + ":" , criteria .getSismember ())));
90
+ }
96
91
97
- if (!criteria .getOrSismember ().isEmpty ()) {
98
- allKeys .addAll (connection .sUnion (keys (keyspace + ":" , criteria .getOrSismember ())));
99
- }
92
+ if (!criteria .getOrSismember ().isEmpty ()) {
93
+ allKeys .addAll (connection .sUnion (keys (keyspace + ":" , criteria .getOrSismember ())));
94
+ }
100
95
101
- if (criteria .getNear () != null ) {
96
+ if (criteria .getNear () != null ) {
102
97
103
- GeoResults <GeoLocation <byte []>> x = connection .geoRadius (geoKey (keyspace + ":" , criteria .getNear ()),
104
- new Circle (criteria .getNear ().getPoint (), criteria .getNear ().getDistance ()));
105
- for (GeoResult <GeoLocation <byte []>> y : x ) {
106
- allKeys .add (y .getContent ().getName ());
107
- }
98
+ GeoResults <GeoLocation <byte []>> x = connection .geoRadius (geoKey (keyspace + ":" , criteria .getNear ()),
99
+ new Circle (criteria .getNear ().getPoint (), criteria .getNear ().getDistance ()));
100
+ for (GeoResult <GeoLocation <byte []>> y : x ) {
101
+ allKeys .add (y .getContent ().getName ());
108
102
}
103
+ }
109
104
110
- byte [] keyspaceBin = getAdapter ().getConverter ().getConversionService ().convert (keyspace + ":" , byte [].class );
111
-
112
- final Map <byte [], Map <byte [], byte []>> rawData = new LinkedHashMap <byte [], Map <byte [], byte []>>();
113
-
114
- if (allKeys .isEmpty () || allKeys .size () < offset ) {
115
- return Collections .emptyMap ();
116
- }
105
+ byte [] keyspaceBin = getAdapter ().getConverter ().getConversionService ().convert (keyspace + ":" , byte [].class );
117
106
118
- int offsetToUse = Math .max (0 , (int ) offset );
119
- if (rows > 0 ) {
120
- allKeys = allKeys .subList (Math .max (0 , offsetToUse ), Math .min (offsetToUse + rows , allKeys .size ()));
121
- }
122
- for (byte [] id : allKeys ) {
107
+ Map <byte [], Map <byte [], byte []>> rawData = new LinkedHashMap <>();
123
108
124
- byte [] singleKey = ByteUtils . concat ( keyspaceBin , id );
125
- rawData . put ( id , connection . hGetAll ( singleKey ) );
126
- }
109
+ if ( allKeys . isEmpty () || allKeys . size () < offset ) {
110
+ return Collections . emptyMap ( );
111
+ }
127
112
128
- return rawData ;
113
+ int offsetToUse = Math .max (0 , (int ) offset );
114
+ if (rows > 0 ) {
115
+ allKeys = allKeys .subList (Math .max (0 , offsetToUse ), Math .min (offsetToUse + rows , allKeys .size ()));
116
+ }
117
+ for (byte [] id : allKeys ) {
129
118
119
+ byte [] singleKey = ByteUtils .concat (keyspaceBin , id );
120
+ rawData .put (id , connection .hGetAll (singleKey ));
130
121
}
122
+
123
+ return rawData ;
131
124
};
132
125
133
126
Map <byte [], Map <byte [], byte []>> raw = this .getAdapter ().execute (callback );
134
127
135
- List <T > result = new ArrayList <T >(raw .size ());
128
+ List <T > result = new ArrayList <>(raw .size ());
136
129
for (Map .Entry <byte [], Map <byte [], byte []>> entry : raw .entrySet ()) {
137
130
138
131
RedisData data = new RedisData (entry .getValue ());
139
132
data .setId (getAdapter ().getConverter ().getConversionService ().convert (entry .getKey (), String .class ));
140
- data .setKeyspace (keyspace . toString () );
133
+ data .setKeyspace (keyspace );
141
134
142
135
T converted = this .getAdapter ().getConverter ().read (type , data );
143
136
@@ -153,8 +146,8 @@ public Map<byte[], Map<byte[], byte[]>> doInRedis(RedisConnection connection) th
153
146
* @see org.springframework.data.keyvalue.core.QueryEngine#execute(java.lang.Object, java.lang.Object, int, int, java.lang.String)
154
147
*/
155
148
@ Override
156
- public Collection <?> execute (final RedisOperationChain criteria , Comparator <?> sort , long offset , int rows ,
157
- final String keyspace ) {
149
+ public Collection <?> execute (RedisOperationChain criteria , Comparator <?> sort , long offset , int rows ,
150
+ String keyspace ) {
158
151
return execute (criteria , sort , offset , rows , keyspace , Object .class );
159
152
}
160
153
@@ -163,26 +156,22 @@ public Collection<?> execute(final RedisOperationChain criteria, Comparator<?> s
163
156
* @see org.springframework.data.keyvalue.core.QueryEngine#count(java.lang.Object, java.lang.String)
164
157
*/
165
158
@ Override
166
- public long count (final RedisOperationChain criteria , final String keyspace ) {
159
+ public long count (RedisOperationChain criteria , String keyspace ) {
167
160
168
161
if (criteria == null ) {
169
162
return this .getAdapter ().count (keyspace );
170
163
}
171
164
172
- return this .getAdapter ().execute (new RedisCallback < Long >() {
165
+ return this .getAdapter ().execute (connection -> {
173
166
174
- @ Override
175
- public Long doInRedis (RedisConnection connection ) throws DataAccessException {
176
-
177
- String key = keyspace + ":" ;
178
- byte [][] keys = new byte [criteria .getSismember ().size ()][];
179
- int i = 0 ;
180
- for (Object o : criteria .getSismember ()) {
181
- keys [i ] = getAdapter ().getConverter ().getConversionService ().convert (key + o , byte [].class );
182
- }
183
-
184
- return (long ) connection .sInter (keys ).size ();
167
+ String key = keyspace + ":" ;
168
+ byte [][] keys = new byte [criteria .getSismember ().size ()][];
169
+ int i = 0 ;
170
+ for (Object o : criteria .getSismember ()) {
171
+ keys [i ] = getAdapter ().getConverter ().getConversionService ().convert (key + o , byte [].class );
185
172
}
173
+
174
+ return (long ) connection .sInter (keys ).size ();
186
175
});
187
176
}
188
177
0 commit comments