/** * extract the distance string from a {@link org.springframework.data.geo.Distance} object. * * @param distance distance object to extract string from * @param sb StringBuilder to build the distance string */ private void extractDistanceString(Distance distance, StringBuilder sb) { // handle Distance object sb.append((int) distance.getValue()); Metrics metric = (Metrics) distance.getMetric(); switch (metric) { case KILOMETERS: sb.append("km"); break; case MILES: sb.append("mi"); break; } }
@Override public GeoResults<GeoLocation<byte[]>> geoRadiusByMember(byte[] key, byte[] member, Distance radius) { return read(key, ByteArrayCodec.INSTANCE, GEORADIUSBYMEMBER, key, member, radius.getValue(), radius.getMetric().getAbbreviation()); }
@Override public GeoResults<GeoLocation<byte[]>> geoRadiusByMember(byte[] key, byte[] member, Distance radius) { return read(key, ByteArrayCodec.INSTANCE, GEORADIUSBYMEMBER, key, member, radius.getValue(), radius.getMetric().getAbbreviation()); }
@Override public GeoResults<GeoLocation<byte[]>> geoRadiusByMember(byte[] key, byte[] member, Distance radius) { Assert.notNull(key, "Key must not be null!"); Assert.notNull(member, "Member must not be null!"); Assert.notNull(radius, "Radius must not be null!"); GeoUnit geoUnit = JedisConverters.toGeoUnit(radius.getMetric()); try { return JedisConverters.geoRadiusResponseToGeoResultsConverter(radius.getMetric()) .convert(connection.getCluster().georadiusByMember(key, member, radius.getValue(), geoUnit)); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override public GeoResults<GeoLocation<byte[]>> geoRadiusByMember(byte[] key, byte[] member, Distance radius, GeoRadiusCommandArgs args) { List<Object> params = new ArrayList<Object>(); params.add(key); params.add(member); params.add(radius.getValue()); params.add(radius.getMetric().getAbbreviation()); RedisCommand<GeoResults<GeoLocation<byte[]>>> command; if (args.getFlags().contains(GeoRadiusCommandArgs.Flag.WITHCOORD)) { command = new RedisCommand<GeoResults<GeoLocation<byte[]>>>("GEORADIUSBYMEMBER", postitionDecoder); params.add("WITHCOORD"); } else { MultiDecoder<GeoResults<GeoLocation<byte[]>>> distanceDecoder = new ListMultiDecoder(new GeoDistanceDecoder(), new GeoResultsDecoder(radius.getMetric())); command = new RedisCommand<GeoResults<GeoLocation<byte[]>>>("GEORADIUSBYMEMBER", distanceDecoder); params.add("WITHDIST"); } if (args.getLimit() != null) { params.add("COUNT"); params.add(args.getLimit()); } if (args.getSortDirection() != null) { params.add(args.getSortDirection().name()); } return read(key, ByteArrayCodec.INSTANCE, command, params.toArray()); }
@Override public GeoResults<GeoLocation<byte[]>> geoRadiusByMember(byte[] key, byte[] member, Distance radius, GeoRadiusCommandArgs args) { List<Object> params = new ArrayList<Object>(); params.add(key); params.add(member); params.add(radius.getValue()); params.add(radius.getMetric().getAbbreviation()); RedisCommand<GeoResults<GeoLocation<byte[]>>> command; if (args.getFlags().contains(GeoRadiusCommandArgs.Flag.WITHCOORD)) { command = new RedisCommand<GeoResults<GeoLocation<byte[]>>>("GEORADIUSBYMEMBER", postitionDecoder); params.add("WITHCOORD"); } else { MultiDecoder<GeoResults<GeoLocation<byte[]>>> distanceDecoder = new ListMultiDecoder(new GeoDistanceDecoder(), new GeoResultsDecoder(radius.getMetric())); command = new RedisCommand<GeoResults<GeoLocation<byte[]>>>("GEORADIUSBYMEMBER", distanceDecoder); params.add("WITHDIST"); } if (args.getLimit() != null) { params.add("COUNT"); params.add(args.getLimit()); } if (args.getSortDirection() != null) { params.add(args.getSortDirection().name()); } return read(key, ByteArrayCodec.INSTANCE, command, params.toArray()); }
@Override public GeoResults<GeoLocation<byte[]>> geoRadiusByMember(byte[] key, byte[] member, Distance radius, GeoRadiusCommandArgs args) { Assert.notNull(key, "Key must not be null!"); Assert.notNull(member, "Member must not be null!"); Assert.notNull(radius, "Radius must not be null!"); Assert.notNull(args, "Args must not be null!"); GeoUnit geoUnit = JedisConverters.toGeoUnit(radius.getMetric()); redis.clients.jedis.params.GeoRadiusParam geoRadiusParam = JedisConverters.toGeoRadiusParam(args); try { return JedisConverters.geoRadiusResponseToGeoResultsConverter(radius.getMetric()) .convert(connection.getCluster().georadiusByMember(key, member, radius.getValue(), geoUnit, geoRadiusParam)); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override public GeoResults<GeoLocation<byte[]>> geoRadius(byte[] key, Circle within, GeoRadiusCommandArgs args) { List<Object> params = new ArrayList<Object>(); params.add(key); params.add(convert(within.getCenter().getX())); params.add(convert(within.getCenter().getY())); params.add(within.getRadius().getValue()); params.add(within.getRadius().getMetric().getAbbreviation()); RedisCommand<GeoResults<GeoLocation<byte[]>>> command; if (args.getFlags().contains(GeoRadiusCommandArgs.Flag.WITHCOORD)) { command = new RedisCommand<GeoResults<GeoLocation<byte[]>>>("GEORADIUS", postitionDecoder); params.add("WITHCOORD"); } else { MultiDecoder<GeoResults<GeoLocation<byte[]>>> distanceDecoder = new ListMultiDecoder(new GeoDistanceDecoder(), new GeoResultsDecoder(within.getRadius().getMetric())); command = new RedisCommand<GeoResults<GeoLocation<byte[]>>>("GEORADIUS", distanceDecoder); params.add("WITHDIST"); } if (args.getLimit() != null) { params.add("COUNT"); params.add(args.getLimit()); } if (args.getSortDirection() != null) { params.add(args.getSortDirection().name()); } return read(key, ByteArrayCodec.INSTANCE, command, params.toArray()); }
@Override public GeoResults<GeoLocation<byte[]>> geoRadius(byte[] key, Circle within, GeoRadiusCommandArgs args) { List<Object> params = new ArrayList<Object>(); params.add(key); params.add(convert(within.getCenter().getX())); params.add(convert(within.getCenter().getY())); params.add(within.getRadius().getValue()); params.add(within.getRadius().getMetric().getAbbreviation()); RedisCommand<GeoResults<GeoLocation<byte[]>>> command; if (args.getFlags().contains(GeoRadiusCommandArgs.Flag.WITHCOORD)) { command = new RedisCommand<GeoResults<GeoLocation<byte[]>>>("GEORADIUS", postitionDecoder); params.add("WITHCOORD"); } else { MultiDecoder<GeoResults<GeoLocation<byte[]>>> distanceDecoder = new ListMultiDecoder(new GeoDistanceDecoder(), new GeoResultsDecoder(within.getRadius().getMetric())); command = new RedisCommand<GeoResults<GeoLocation<byte[]>>>("GEORADIUS", distanceDecoder); params.add("WITHDIST"); } if (args.getLimit() != null) { params.add("COUNT"); params.add(args.getLimit()); } if (args.getSortDirection() != null) { params.add(args.getSortDirection().name()); } return read(key, ByteArrayCodec.INSTANCE, command, params.toArray()); }
@Override public Document convert(Circle source) { if (source == null) { return null; } Document result = new Document(); result.put("center", PointToDocumentConverter.INSTANCE.convert(source.getCenter())); result.put("radius", source.getRadius().getNormalizedValue()); result.put("metric", source.getRadius().getMetric().toString()); return result; } }
@Override public Document convert(Sphere source) { if (source == null) { return null; } Document result = new Document(); result.put("center", PointToDocumentConverter.INSTANCE.convert(source.getCenter())); result.put("radius", source.getRadius().getNormalizedValue()); result.put("metric", source.getRadius().getMetric().toString()); return result; } }
@Override public Flux<CommandResponse<GeoRadiusByMemberCommand, Flux<GeoResult<GeoLocation<ByteBuffer>>>>> geoRadiusByMember( Publisher<GeoRadiusByMemberCommand> commands) { return connection.execute(cmd -> Flux.from(commands).concatMap(command -> { Assert.notNull(command.getKey(), "Key must not be null!"); Assert.notNull(command.getMember(), "Member must not be null!"); Assert.notNull(command.getDistance(), "Distance must not be null!"); GeoArgs geoArgs = command.getArgs().isPresent() ? LettuceConverters.toGeoArgs(command.getArgs().get()) : new GeoArgs(); Flux<GeoResult<GeoLocation<ByteBuffer>>> result = cmd .georadiusbymember(command.getKey(), command.getMember(), command.getDistance().getValue(), LettuceConverters.toGeoArgsUnit(command.getDistance().getMetric()), geoArgs) .map(converter(command.getDistance().getMetric())::convert); return Mono.just(new CommandResponse<>(command, result)); })); }
@SuppressWarnings({ "unchecked", "rawtypes" }) protected Flux<GeoResult<Object>> doExecuteQuery(@Nullable Query query, Class<?> type, String collection) { Point nearLocation = accessor.getGeoNearLocation(); NearQuery nearQuery = NearQuery.near(nearLocation); if (query != null) { nearQuery.query(query); } Range<Distance> distances = accessor.getDistanceRange(); distances.getUpperBound().getValue().ifPresent(it -> nearQuery.maxDistance(it).in(it.getMetric())); distances.getLowerBound().getValue().ifPresent(it -> nearQuery.minDistance(it).in(it.getMetric())); Pageable pageable = accessor.getPageable(); nearQuery.with(pageable); return (Flux) operations.geoNear(nearQuery, type, collection); }
@Override public GeoResults<GeoLocation<byte[]>> geoRadius(byte[] key, Circle within) { Assert.notNull(key, "Key must not be null!"); Assert.notNull(within, "Within must not be null!"); try { return JedisConverters.geoRadiusResponseToGeoResultsConverter(within.getRadius().getMetric()) .convert(connection.getCluster().georadius(key, within.getCenter().getX(), within.getCenter().getY(), within.getRadius().getValue(), JedisConverters.toGeoUnit(within.getRadius().getMetric()))); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override public GeoResults<GeoLocation<byte[]>> geoRadius(byte[] key, Circle within, GeoRadiusCommandArgs args) { Assert.notNull(key, "Key must not be null!"); Assert.notNull(within, "Within must not be null!"); Assert.notNull(args, "Args must not be null!"); GeoRadiusParam geoRadiusParam = JedisConverters.toGeoRadiusParam(args); try { return JedisConverters.geoRadiusResponseToGeoResultsConverter(within.getRadius().getMetric()) .convert(connection.getCluster().georadius(key, within.getCenter().getX(), within.getCenter().getY(), within.getRadius().getValue(), JedisConverters.toGeoUnit(within.getRadius().getMetric()), geoRadiusParam)); } catch (Exception ex) { throw convertJedisAccessException(ex); } }
@Override public GeoResults<GeoLocation<V>> convert(GeoResults<GeoLocation<byte[]>> source) { List<GeoResult<GeoLocation<V>>> values = new ArrayList<>(source.getContent().size()); for (GeoResult<GeoLocation<byte[]>> value : source.getContent()) { values.add(new GeoResult<>( new GeoLocation<>(serializer.deserialize(value.getContent().getName()), value.getContent().getPoint()), value.getDistance())); } return new GeoResults<>(values, source.getAverageDistance().getMetric()); } }
@Override public Flux<CommandResponse<GeoRadiusCommand, Flux<GeoResult<GeoLocation<ByteBuffer>>>>> geoRadius( Publisher<GeoRadiusCommand> commands) { return connection.execute(cmd -> Flux.from(commands).concatMap(command -> { Assert.notNull(command.getKey(), "Key must not be null!"); Assert.notNull(command.getPoint(), "Point must not be null!"); Assert.notNull(command.getDistance(), "Distance must not be null!"); GeoArgs geoArgs = command.getArgs().isPresent() ? LettuceConverters.toGeoArgs(command.getArgs().get()) : new GeoArgs(); Flux<GeoResult<GeoLocation<ByteBuffer>>> result = cmd .georadius(command.getKey(), command.getPoint().getX(), command.getPoint().getY(), command.getDistance().getValue(), LettuceConverters.toGeoArgsUnit(command.getDistance().getMetric()), geoArgs) // .map(converter(command.getDistance().getMetric())::convert); return Mono.just(new CommandResponse<>(command, result)); })); }
@SuppressWarnings("unchecked") GeoResults<Object> doExecuteQuery(Query query) { Point nearLocation = accessor.getGeoNearLocation(); NearQuery nearQuery = NearQuery.near(nearLocation); if (query != null) { nearQuery.query(query); } Range<Distance> distances = accessor.getDistanceRange(); distances.getLowerBound().getValue().ifPresent(it -> nearQuery.minDistance(it).in(it.getMetric())); distances.getUpperBound().getValue().ifPresent(it -> nearQuery.maxDistance(it).in(it.getMetric())); Pageable pageable = accessor.getPageable(); nearQuery.with(pageable); return (GeoResults<Object>) operation.near(nearQuery).all(); }
@Override public GeoResults<GeoLocation<byte[]>> geoRadius(byte[] key, Circle within) { RedisCommand<GeoResults<GeoLocation<byte[]>>> command = new RedisCommand<GeoResults<GeoLocation<byte[]>>>("GEORADIUS", new GeoResultsDecoder()); return read(key, ByteArrayCodec.INSTANCE, command, key, convert(within.getCenter().getX()), convert(within.getCenter().getY()), within.getRadius().getValue(), within.getRadius().getMetric().getAbbreviation()); }
@Override public GeoResults<GeoLocation<byte[]>> geoRadius(byte[] key, Circle within) { RedisCommand<GeoResults<GeoLocation<byte[]>>> command = new RedisCommand<GeoResults<GeoLocation<byte[]>>>("GEORADIUS", new GeoResultsDecoder()); return read(key, ByteArrayCodec.INSTANCE, command, key, convert(within.getCenter().getX()), convert(within.getCenter().getY()), within.getRadius().getValue(), within.getRadius().getMetric().getAbbreviation()); }