PageRenderTime 63ms CodeModel.GetById 14ms RepoModel.GetById 4ms app.codeStats 0ms

/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceGeoCommands.java

http://github.com/SpringSource/spring-data-redis
Java | 316 lines | 178 code | 58 blank | 80 comment | 2 complexity | 65cd2421a90fee13af68df170ba4b73c MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Copyright 2017-2021 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * https://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.data.redis.connection.lettuce;
  17. import io.lettuce.core.GeoArgs;
  18. import io.lettuce.core.GeoSearch;
  19. import io.lettuce.core.GeoWithin;
  20. import io.lettuce.core.api.async.RedisGeoAsyncCommands;
  21. import java.util.ArrayList;
  22. import java.util.Collection;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.Map.Entry;
  26. import java.util.Set;
  27. import org.springframework.core.convert.converter.Converter;
  28. import org.springframework.data.geo.Circle;
  29. import org.springframework.data.geo.Distance;
  30. import org.springframework.data.geo.GeoResults;
  31. import org.springframework.data.geo.Metric;
  32. import org.springframework.data.geo.Point;
  33. import org.springframework.data.redis.connection.RedisGeoCommands;
  34. import org.springframework.data.redis.domain.geo.GeoReference;
  35. import org.springframework.data.redis.domain.geo.GeoShape;
  36. import org.springframework.lang.Nullable;
  37. import org.springframework.util.Assert;
  38. /**
  39. * @author Christoph Strobl
  40. * @author Mark Paluch
  41. * @since 2.0
  42. */
  43. class LettuceGeoCommands implements RedisGeoCommands {
  44. private final LettuceConnection connection;
  45. LettuceGeoCommands(LettuceConnection connection) {
  46. this.connection = connection;
  47. }
  48. /*
  49. * (non-Javadoc)
  50. * @see org.springframework.data.redis.connection.RedisGeoCommands#geoAdd(byte[], org.springframework.data.geo.Point, byte[])
  51. */
  52. @Override
  53. public Long geoAdd(byte[] key, Point point, byte[] member) {
  54. Assert.notNull(key, "Key must not be null!");
  55. Assert.notNull(point, "Point must not be null!");
  56. Assert.notNull(member, "Member must not be null!");
  57. return connection.invoke().just(RedisGeoAsyncCommands::geoadd, key, point.getX(), point.getY(), member);
  58. }
  59. /*
  60. * (non-Javadoc)
  61. * @see org.springframework.data.redis.connection.RedisGeoCommands#geoAdd(byte[], java.util.Map)
  62. */
  63. @Override
  64. public Long geoAdd(byte[] key, Map<byte[], Point> memberCoordinateMap) {
  65. Assert.notNull(key, "Key must not be null!");
  66. Assert.notNull(memberCoordinateMap, "MemberCoordinateMap must not be null!");
  67. List<Object> values = new ArrayList<>();
  68. for (Entry<byte[], Point> entry : memberCoordinateMap.entrySet()) {
  69. values.add(entry.getValue().getX());
  70. values.add(entry.getValue().getY());
  71. values.add(entry.getKey());
  72. }
  73. return geoAdd(key, values);
  74. }
  75. /*
  76. * (non-Javadoc)
  77. * @see org.springframework.data.redis.connection.RedisGeoCommands#geoAdd(byte[], java.lang.Iterable)
  78. */
  79. @Override
  80. public Long geoAdd(byte[] key, Iterable<GeoLocation<byte[]>> locations) {
  81. Assert.notNull(key, "Key must not be null!");
  82. Assert.notNull(locations, "Locations must not be null!");
  83. List<Object> values = new ArrayList<>();
  84. for (GeoLocation<byte[]> location : locations) {
  85. values.add(location.getPoint().getX());
  86. values.add(location.getPoint().getY());
  87. values.add(location.getName());
  88. }
  89. return geoAdd(key, values);
  90. }
  91. @Nullable
  92. private Long geoAdd(byte[] key, Collection<Object> values) {
  93. return connection.invoke().just(it -> it.geoadd(key, values.toArray()));
  94. }
  95. /*
  96. * (non-Javadoc)
  97. * @see org.springframework.data.redis.connection.RedisGeoCommands#geoDist(byte[], byte[], byte[])
  98. */
  99. @Override
  100. public Distance geoDist(byte[] key, byte[] member1, byte[] member2) {
  101. return geoDist(key, member1, member2, DistanceUnit.METERS);
  102. }
  103. /*
  104. * (non-Javadoc)
  105. * @see org.springframework.data.redis.connection.RedisGeoCommands#geoDist(byte[], byte[], byte[], org.springframework.data.geo.Metric)
  106. */
  107. @Override
  108. public Distance geoDist(byte[] key, byte[] member1, byte[] member2, Metric metric) {
  109. Assert.notNull(key, "Key must not be null!");
  110. Assert.notNull(member1, "Member1 must not be null!");
  111. Assert.notNull(member2, "Member2 must not be null!");
  112. Assert.notNull(metric, "Metric must not be null!");
  113. GeoArgs.Unit geoUnit = LettuceConverters.toGeoArgsUnit(metric);
  114. Converter<Double, Distance> distanceConverter = LettuceConverters.distanceConverterForMetric(metric);
  115. return connection.invoke().from(RedisGeoAsyncCommands::geodist, key, member1, member2, geoUnit)
  116. .get(distanceConverter);
  117. }
  118. /*
  119. * (non-Javadoc)
  120. * @see org.springframework.data.redis.connection.RedisGeoCommands#geoHash(byte[], byte[][])
  121. */
  122. @Override
  123. public List<String> geoHash(byte[] key, byte[]... members) {
  124. Assert.notNull(key, "Key must not be null!");
  125. Assert.notNull(members, "Members must not be null!");
  126. Assert.noNullElements(members, "Members must not contain null!");
  127. return connection.invoke().fromMany(RedisGeoAsyncCommands::geohash, key, members)
  128. .toList(it -> it.getValueOrElse(null));
  129. }
  130. /*
  131. * (non-Javadoc)
  132. * @see org.springframework.data.redis.connection.RedisGeoCommands#geoPos(byte[], byte[][])
  133. */
  134. @Override
  135. public List<Point> geoPos(byte[] key, byte[]... members) {
  136. Assert.notNull(key, "Key must not be null!");
  137. Assert.notNull(members, "Members must not be null!");
  138. Assert.noNullElements(members, "Members must not contain null!");
  139. return connection.invoke().fromMany(RedisGeoAsyncCommands::geopos, key, members)
  140. .toList(LettuceConverters::geoCoordinatesToPoint);
  141. }
  142. /*
  143. * (non-Javadoc)
  144. * @see org.springframework.data.redis.connection.RedisGeoCommands#geoRadius(byte[], org.springframework.data.geo.Circle)
  145. */
  146. @Override
  147. public GeoResults<GeoLocation<byte[]>> geoRadius(byte[] key, Circle within) {
  148. Assert.notNull(key, "Key must not be null!");
  149. Assert.notNull(within, "Within must not be null!");
  150. Converter<Set<byte[]>, GeoResults<GeoLocation<byte[]>>> geoResultsConverter = LettuceConverters
  151. .bytesSetToGeoResultsConverter();
  152. return connection.invoke()
  153. .from(it -> it.georadius(key, within.getCenter().getX(), within.getCenter().getY(),
  154. within.getRadius().getValue(), LettuceConverters.toGeoArgsUnit(within.getRadius().getMetric())))
  155. .get(geoResultsConverter);
  156. }
  157. /*
  158. * (non-Javadoc)
  159. * @see org.springframework.data.redis.connection.RedisGeoCommands#geoRadius(byte[], org.springframework.data.geo.Circle, org.springframework.data.redis.core.GeoRadiusCommandArgs)
  160. */
  161. @Override
  162. public GeoResults<GeoLocation<byte[]>> geoRadius(byte[] key, Circle within, GeoRadiusCommandArgs args) {
  163. Assert.notNull(key, "Key must not be null!");
  164. Assert.notNull(within, "Within must not be null!");
  165. Assert.notNull(args, "Args must not be null!");
  166. GeoArgs geoArgs = LettuceConverters.toGeoArgs(args);
  167. Converter<List<GeoWithin<byte[]>>, GeoResults<GeoLocation<byte[]>>> geoResultsConverter = LettuceConverters
  168. .geoRadiusResponseToGeoResultsConverter(within.getRadius().getMetric());
  169. return connection.invoke()
  170. .from(it -> it.georadius(key, within.getCenter().getX(), within.getCenter().getY(),
  171. within.getRadius().getValue(), LettuceConverters.toGeoArgsUnit(within.getRadius().getMetric()), geoArgs))
  172. .get(geoResultsConverter);
  173. }
  174. /*
  175. * (non-Javadoc)
  176. * @see org.springframework.data.redis.connection.RedisGeoCommands#geoRadiusByMember(byte[], byte[], double)
  177. */
  178. @Override
  179. public GeoResults<GeoLocation<byte[]>> geoRadiusByMember(byte[] key, byte[] member, double radius) {
  180. return geoRadiusByMember(key, member, new Distance(radius, DistanceUnit.METERS));
  181. }
  182. /*
  183. * (non-Javadoc)
  184. * @see org.springframework.data.redis.connection.RedisGeoCommands#geoRadiusByMember(byte[], byte[], double, org.springframework.data.geo.Metric)
  185. */
  186. @Override
  187. public GeoResults<GeoLocation<byte[]>> geoRadiusByMember(byte[] key, byte[] member, Distance radius) {
  188. Assert.notNull(key, "Key must not be null!");
  189. Assert.notNull(member, "Member must not be null!");
  190. Assert.notNull(radius, "Radius must not be null!");
  191. GeoArgs.Unit geoUnit = LettuceConverters.toGeoArgsUnit(radius.getMetric());
  192. Converter<Set<byte[]>, GeoResults<GeoLocation<byte[]>>> converter = LettuceConverters
  193. .bytesSetToGeoResultsConverter();
  194. return connection.invoke().from(RedisGeoAsyncCommands::georadiusbymember, key, member, radius.getValue(), geoUnit)
  195. .get(converter);
  196. }
  197. /*
  198. * (non-Javadoc)
  199. * @see org.springframework.data.redis.connection.RedisGeoCommands#geoRadiusByMember(byte[], byte[], org.springframework.data.geo.Distance, org.springframework.data.redis.core.GeoRadiusCommandArgs)
  200. */
  201. @Override
  202. public GeoResults<GeoLocation<byte[]>> geoRadiusByMember(byte[] key, byte[] member, Distance radius,
  203. GeoRadiusCommandArgs args) {
  204. Assert.notNull(key, "Key must not be null!");
  205. Assert.notNull(member, "Member must not be null!");
  206. Assert.notNull(radius, "Radius must not be null!");
  207. Assert.notNull(args, "Args must not be null!");
  208. GeoArgs.Unit geoUnit = LettuceConverters.toGeoArgsUnit(radius.getMetric());
  209. GeoArgs geoArgs = LettuceConverters.toGeoArgs(args);
  210. Converter<List<GeoWithin<byte[]>>, GeoResults<GeoLocation<byte[]>>> geoResultsConverter = LettuceConverters
  211. .geoRadiusResponseToGeoResultsConverter(radius.getMetric());
  212. return connection.invoke()
  213. .from(RedisGeoAsyncCommands::georadiusbymember, key, member, radius.getValue(), geoUnit, geoArgs)
  214. .get(geoResultsConverter);
  215. }
  216. /*
  217. * (non-Javadoc)
  218. * @see org.springframework.data.redis.connection.RedisGeoCommands#geoRemove(byte[], byte[][])
  219. */
  220. @Override
  221. public Long geoRemove(byte[] key, byte[]... values) {
  222. return connection.zSetCommands().zRem(key, values);
  223. }
  224. /*
  225. * (non-Javadoc)
  226. * @see org.springframework.data.redis.connection.RedisGeoCommands#geoSearch(byte[], org.springframework.data.redis.connection.RedisGeoCommands.GeoReference, org.springframework.data.redis.connection.RedisGeoCommands.GeoShape, org.springframework.data.redis.connection.RedisGeoCommands.GeoSearchCommandArgs)
  227. */
  228. @Override
  229. public GeoResults<GeoLocation<byte[]>> geoSearch(byte[] key, GeoReference<byte[]> reference, GeoShape predicate,
  230. GeoSearchCommandArgs args) {
  231. Assert.notNull(key, "Key must not be null!");
  232. Assert.notNull(reference, "Reference must not be null!");
  233. Assert.notNull(predicate, "GeoPredicate must not be null!");
  234. Assert.notNull(args, "GeoSearchCommandArgs must not be null!");
  235. GeoSearch.GeoRef<byte[]> ref = LettuceConverters.toGeoRef(reference);
  236. GeoSearch.GeoPredicate lettucePredicate = LettuceConverters.toGeoPredicate(predicate);
  237. GeoArgs geoArgs = LettuceConverters.toGeoArgs(args);
  238. return connection.invoke().from(RedisGeoAsyncCommands::geosearch, key, ref, lettucePredicate, geoArgs)
  239. .get(LettuceConverters.geoRadiusResponseToGeoResultsConverter(predicate.getMetric()));
  240. }
  241. /*
  242. * (non-Javadoc)
  243. * @see org.springframework.data.redis.connection.RedisGeoCommands#geoSearchStore(byte[], byte[], org.springframework.data.redis.connection.RedisGeoCommands.GeoReference, org.springframework.data.redis.connection.RedisGeoCommands.GeoShape, org.springframework.data.redis.connection.RedisGeoCommands.GeoSearchStoreCommandArgs)
  244. */
  245. @Override
  246. public Long geoSearchStore(byte[] destKey, byte[] key, GeoReference<byte[]> reference, GeoShape predicate,
  247. GeoSearchStoreCommandArgs args) {
  248. Assert.notNull(key, "Key must not be null!");
  249. Assert.notNull(reference, "Reference must not be null!");
  250. Assert.notNull(predicate, "GeoPredicate must not be null!");
  251. Assert.notNull(args, "GeoSearchCommandArgs must not be null!");
  252. GeoSearch.GeoRef<byte[]> ref = LettuceConverters.toGeoRef(reference);
  253. GeoSearch.GeoPredicate lettucePredicate = LettuceConverters.toGeoPredicate(predicate);
  254. GeoArgs geoArgs = LettuceConverters.toGeoArgs(args);
  255. return connection.invoke().just(
  256. connection -> connection.geosearchstore(destKey, key, ref, lettucePredicate, geoArgs, args.isStoreDistance()));
  257. }
  258. }