|
20 | 20 | import java.util.Collections;
|
21 | 21 | import java.util.Comparator;
|
22 | 22 | import java.util.LinkedHashMap;
|
| 23 | +import java.util.LinkedHashSet; |
23 | 24 | import java.util.List;
|
24 | 25 | import java.util.Map;
|
| 26 | +import java.util.Set; |
25 | 27 |
|
26 | 28 | import org.springframework.data.geo.Circle;
|
27 | 29 | import org.springframework.data.geo.GeoResult;
|
|
31 | 33 | import org.springframework.data.keyvalue.core.SortAccessor;
|
32 | 34 | import org.springframework.data.keyvalue.core.SpelSortAccessor;
|
33 | 35 | import org.springframework.data.keyvalue.core.query.KeyValueQuery;
|
| 36 | +import org.springframework.data.redis.connection.RedisConnection; |
34 | 37 | import org.springframework.data.redis.connection.RedisGeoCommands.GeoLocation;
|
35 | 38 | import org.springframework.data.redis.connection.RedisGeoCommands.GeoRadiusCommandArgs;
|
| 39 | +import org.springframework.data.redis.connection.util.ByteArrayWrapper; |
36 | 40 | import org.springframework.data.redis.core.convert.GeoIndexedPropertyValue;
|
37 | 41 | import org.springframework.data.redis.core.convert.RedisData;
|
38 | 42 | import org.springframework.data.redis.repository.query.RedisOperationChain;
|
39 | 43 | import org.springframework.data.redis.repository.query.RedisOperationChain.NearPath;
|
40 | 44 | import org.springframework.data.redis.repository.query.RedisOperationChain.PathAndValue;
|
41 | 45 | import org.springframework.data.redis.util.ByteUtils;
|
42 | 46 | import org.springframework.expression.spel.standard.SpelExpressionParser;
|
| 47 | +import org.springframework.lang.NonNullApi; |
43 | 48 | import org.springframework.lang.Nullable;
|
44 | 49 | import org.springframework.util.CollectionUtils;
|
45 | 50 |
|
@@ -95,44 +100,21 @@ private <T> List<T> doFind(RedisOperationChain criteria, long offset, int rows,
|
95 | 100 |
|
96 | 101 | RedisCallback<Map<byte[], Map<byte[], byte[]>>> callback = connection -> {
|
97 | 102 |
|
98 |
| - List<byte[]> allKeys = new ArrayList<>(); |
99 |
| - if (!criteria.getSismember().isEmpty()) { |
100 |
| - allKeys.addAll(connection.sInter(keys(keyspace + ":", criteria.getSismember()))); |
101 |
| - } |
102 |
| - |
103 |
| - if (!criteria.getOrSismember().isEmpty()) { |
104 |
| - allKeys.addAll(connection.sUnion(keys(keyspace + ":", criteria.getOrSismember()))); |
105 |
| - } |
106 |
| - |
107 |
| - if (criteria.getNear() != null) { |
108 |
| - |
109 |
| - GeoRadiusCommandArgs limit = GeoRadiusCommandArgs.newGeoRadiusArgs(); |
110 |
| - |
111 |
| - if (rows > 0) { |
112 |
| - limit = limit.limit(rows); |
113 |
| - } |
114 |
| - |
115 |
| - GeoResults<GeoLocation<byte[]>> x = connection.geoRadius(geoKey(keyspace + ":", criteria.getNear()), |
116 |
| - new Circle(criteria.getNear().getPoint(), criteria.getNear().getDistance()), limit); |
117 |
| - for (GeoResult<GeoLocation<byte[]>> y : x) { |
118 |
| - allKeys.add(y.getContent().getName()); |
119 |
| - } |
120 |
| - } |
121 |
| - |
| 103 | + List<byte[]> keys = findKeys(criteria, rows, keyspace, connection); |
122 | 104 | byte[] keyspaceBin = getRequiredAdapter().getConverter().getConversionService().convert(keyspace + ":",
|
123 | 105 | byte[].class);
|
124 | 106 |
|
125 | 107 | Map<byte[], Map<byte[], byte[]>> rawData = new LinkedHashMap<>();
|
126 | 108 |
|
127 |
| - if (allKeys.isEmpty() || allKeys.size() < offset) { |
| 109 | + if (keys.isEmpty() || keys.size() < offset) { |
128 | 110 | return Collections.emptyMap();
|
129 | 111 | }
|
130 | 112 |
|
131 | 113 | int offsetToUse = Math.max(0, (int) offset);
|
132 | 114 | if (rows > 0) {
|
133 |
| - allKeys = allKeys.subList(Math.max(0, offsetToUse), Math.min(offsetToUse + rows, allKeys.size())); |
| 115 | + keys = keys.subList(Math.max(0, offsetToUse), Math.min(offsetToUse + rows, keys.size())); |
134 | 116 | }
|
135 |
| - for (byte[] id : allKeys) { |
| 117 | + for (byte[] id : keys) { |
136 | 118 |
|
137 | 119 | byte[] singleKey = ByteUtils.concat(keyspaceBin, id);
|
138 | 120 | rawData.put(id, connection.hGetAll(singleKey));
|
@@ -161,6 +143,43 @@ private <T> List<T> doFind(RedisOperationChain criteria, long offset, int rows,
|
161 | 143 | return result;
|
162 | 144 | }
|
163 | 145 |
|
| 146 | + private List<byte[]> findKeys(RedisOperationChain criteria, int rows, String keyspace, RedisConnection connection) { |
| 147 | + |
| 148 | + List<byte[]> allKeys = new ArrayList<>(); |
| 149 | + |
| 150 | + if (!criteria.getSismember().isEmpty()) { |
| 151 | + allKeys.addAll(connection.sInter(keys(keyspace + ":", criteria.getSismember()))); |
| 152 | + } |
| 153 | + |
| 154 | + if (!criteria.getOrSismember().isEmpty()) { |
| 155 | + allKeys.addAll(connection.sUnion(keys(keyspace + ":", criteria.getOrSismember()))); |
| 156 | + } |
| 157 | + |
| 158 | + if (criteria.getNear() != null) { |
| 159 | + |
| 160 | + GeoRadiusCommandArgs limit = GeoRadiusCommandArgs.newGeoRadiusArgs(); |
| 161 | + |
| 162 | + if (rows > 0) { |
| 163 | + limit = limit.limit(rows); |
| 164 | + } |
| 165 | + |
| 166 | + GeoResults<GeoLocation<byte[]>> x = connection.geoRadius(geoKey(keyspace + ":", criteria.getNear()), |
| 167 | + new Circle(criteria.getNear().getPoint(), criteria.getNear().getDistance()), limit); |
| 168 | + for (GeoResult<GeoLocation<byte[]>> y : x) { |
| 169 | + allKeys.add(y.getContent().getName()); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + |
| 174 | + Set<ByteArrayWrapper> unique = new LinkedHashSet<>(allKeys.size()); |
| 175 | + allKeys.forEach(key -> unique.add(new ByteArrayWrapper(key))); |
| 176 | + |
| 177 | + List<byte[]> uniqueKeys = new ArrayList<>(unique.size()); |
| 178 | + unique.forEach(key -> uniqueKeys.add(key.getArray())); |
| 179 | + |
| 180 | + return uniqueKeys; |
| 181 | + } |
| 182 | + |
164 | 183 | @Override
|
165 | 184 | public Collection<?> execute(RedisOperationChain criteria, Comparator<?> sort, long offset, int rows,
|
166 | 185 | String keyspace) {
|
|
0 commit comments