All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.redisson.RedissonGeo Maven / Gradle / Ivy

There is a newer version: 0.40.13
Show newest version
/**
 * Copyright 2018 Nikita Koksharov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.redisson;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.redisson.api.GeoEntry;
import org.redisson.api.GeoOrder;
import org.redisson.api.GeoPosition;
import org.redisson.api.GeoUnit;
import org.redisson.api.RFuture;
import org.redisson.api.RGeo;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.Codec;
import org.redisson.client.codec.LongCodec;
import org.redisson.client.codec.StringCodec;
import org.redisson.client.protocol.RedisCommand;
import org.redisson.client.protocol.RedisCommands;
import org.redisson.client.protocol.decoder.CodecDecoder;
import org.redisson.client.protocol.decoder.GeoDistanceDecoder;
import org.redisson.client.protocol.decoder.GeoMapReplayDecoder;
import org.redisson.client.protocol.decoder.GeoPositionDecoder;
import org.redisson.client.protocol.decoder.GeoPositionMapDecoder;
import org.redisson.client.protocol.decoder.ListMultiDecoder;
import org.redisson.client.protocol.decoder.MultiDecoder;
import org.redisson.client.protocol.decoder.ObjectListReplayDecoder;
import org.redisson.command.CommandAsyncExecutor;
import org.redisson.connection.decoder.MapGetAllDecoder;

/**
 * Geospatial items holder
 * 
 * @author Nikita Koksharov
 *
 * @param  value
 */
public class RedissonGeo extends RedissonScoredSortedSet implements RGeo {

    private static final MultiDecoder> postitionDecoder = new ListMultiDecoder(new CodecDecoder(), new GeoPositionDecoder(), new ObjectListReplayDecoder(ListMultiDecoder.RESET), new GeoMapReplayDecoder());
    private static final MultiDecoder> distanceDecoder = new ListMultiDecoder(new GeoDistanceDecoder(), new GeoMapReplayDecoder());
    
    public RedissonGeo(CommandAsyncExecutor connectionManager, String name, RedissonClient redisson) {
        super(connectionManager, name, redisson);
    }
    
    public RedissonGeo(Codec codec, CommandAsyncExecutor connectionManager, String name, RedissonClient redisson) {
        super(codec, connectionManager, name, redisson);
    }

    @Override
    public RFuture addAsync(double longitude, double latitude, V member) {
        return commandExecutor.writeAsync(getName(), codec, RedisCommands.GEOADD, getName(), convert(longitude), convert(latitude), encode(member));
    }

    private String convert(double longitude) {
        return BigDecimal.valueOf(longitude).toPlainString();
    }

    @Override
    public long add(double longitude, double latitude, V member) {
        return get(addAsync(longitude, latitude, member));
    }

    @Override
    public long add(GeoEntry... entries) {
        return get(addAsync(entries));
    }

    @Override
    public RFuture addAsync(GeoEntry... entries) {
        List params = new ArrayList(entries.length + 1);
        params.add(getName());
        for (GeoEntry entry : entries) {
            params.add(entry.getLongitude());
            params.add(entry.getLatitude());
            params.add(encode(entry.getMember()));
        }
        return commandExecutor.writeAsync(getName(), StringCodec.INSTANCE, RedisCommands.GEOADD, params.toArray());
    }

    @Override
    public Double dist(V firstMember, V secondMember, GeoUnit geoUnit) {
        return get(distAsync(firstMember, secondMember, geoUnit));
    }
    
    @Override
    public RFuture distAsync(V firstMember, V secondMember, GeoUnit geoUnit) {
        return commandExecutor.readAsync(getName(), StringCodec.INSTANCE, RedisCommands.GEODIST, getName(), encode(firstMember), encode(secondMember), geoUnit);
    }
    
    @Override
    public Map hash(V... members) {
        return get(hashAsync(members));
    }
    
    @Override
    public RFuture> hashAsync(V... members) {
        List params = new ArrayList(members.length + 1);
        params.add(getName());
        for (Object member : members) {
            params.add(encode(member));
        }
        RedisCommand> command = new RedisCommand>("GEOHASH", new MapGetAllDecoder((List)Arrays.asList(members), 0));
        return commandExecutor.readAsync(getName(), StringCodec.INSTANCE, command, params.toArray());
    }
    
    @Override
    public Map pos(V... members) {
        return get(posAsync(members));
    }
    
    @Override
    public RFuture> posAsync(V... members) {
        List params = new ArrayList(members.length + 1);
        params.add(getName());
        for (Object member : members) {
            params.add(encode(member));
        }
        
        MultiDecoder> decoder = new ListMultiDecoder(new GeoPositionDecoder(), new ObjectListReplayDecoder(ListMultiDecoder.RESET), new GeoPositionMapDecoder((List)Arrays.asList(members)));
        RedisCommand> command = new RedisCommand>("GEOPOS", decoder);
        return commandExecutor.readAsync(getName(), StringCodec.INSTANCE, command, params.toArray());
    }
    
    @Override
    public List radius(double longitude, double latitude, double radius, GeoUnit geoUnit) {
        return get(radiusAsync(longitude, latitude, radius, geoUnit));
    }
    
    @Override
    public RFuture> radiusAsync(double longitude, double latitude, double radius, GeoUnit geoUnit) {
        return commandExecutor.readAsync(getName(), codec, RedisCommands.GEORADIUS_RO, getName(), convert(longitude), convert(latitude), radius, geoUnit);
    }

    @Override
    public List radius(double longitude, double latitude, double radius, GeoUnit geoUnit, int count) {
        return get(radiusAsync(longitude, latitude, radius, geoUnit, count));
    }
    
    @Override
    public RFuture> radiusAsync(double longitude, double latitude, double radius, GeoUnit geoUnit, int count) {
        return commandExecutor.readAsync(getName(), codec, RedisCommands.GEORADIUS_RO, getName(), convert(longitude), convert(latitude), 
                radius, geoUnit, "COUNT", count);
    }
    
    @Override
    public List radius(double longitude, double latitude, double radius, GeoUnit geoUnit, GeoOrder geoOrder) {
        return get(radiusAsync(longitude, latitude, radius, geoUnit, geoOrder));
    }
    
    @Override
    public RFuture> radiusAsync(double longitude, double latitude, double radius, GeoUnit geoUnit, GeoOrder geoOrder) {
        return commandExecutor.readAsync(getName(), codec, RedisCommands.GEORADIUS_RO, getName(), convert(longitude), convert(latitude), 
                radius, geoUnit, geoOrder);
    }

    @Override
    public List radius(double longitude, double latitude, double radius, GeoUnit geoUnit, GeoOrder geoOrder, int count) {
        return get(radiusAsync(longitude, latitude, radius, geoUnit, geoOrder, count));
    }
    
    @Override
    public RFuture> radiusAsync(double longitude, double latitude, double radius, GeoUnit geoUnit, GeoOrder geoOrder, int count) {
        return commandExecutor.readAsync(getName(), codec, RedisCommands.GEORADIUS_RO, getName(), convert(longitude), convert(latitude), 
                radius, geoUnit, "COUNT", count, geoOrder);
    }
    
    @Override
    public Map radiusWithDistance(double longitude, double latitude, double radius, GeoUnit geoUnit) {
        return get(radiusWithDistanceAsync(longitude, latitude, radius, geoUnit));
    }
    
    @Override
    public RFuture> radiusWithDistanceAsync(double longitude, double latitude, double radius, GeoUnit geoUnit) {
        RedisCommand> command = new RedisCommand>("GEORADIUS_RO", distanceDecoder);
        return commandExecutor.readAsync(getName(), codec, command, getName(), convert(longitude), convert(latitude), 
                radius, geoUnit, "WITHDIST");
    }
    
    @Override
    public Map radiusWithDistance(double longitude, double latitude, double radius, GeoUnit geoUnit, int count) {
        return get(radiusWithDistanceAsync(longitude, latitude, radius, geoUnit, count));
    }
    
    @Override
    public RFuture> radiusWithDistanceAsync(double longitude, double latitude, double radius, GeoUnit geoUnit, int count) {
        RedisCommand> command = new RedisCommand>("GEORADIUS_RO", distanceDecoder);
        return commandExecutor.readAsync(getName(), codec, command, getName(), convert(longitude), convert(latitude), 
                radius, geoUnit, "WITHDIST", "COUNT", count);
    }

    @Override
    public Map radiusWithDistance(double longitude, double latitude, double radius, GeoUnit geoUnit, GeoOrder geoOrder) {
        return get(radiusWithDistanceAsync(longitude, latitude, radius, geoUnit, geoOrder));
    }
    
    @Override
    public RFuture> radiusWithDistanceAsync(double longitude, double latitude, double radius, GeoUnit geoUnit, GeoOrder geoOrder) {
        RedisCommand> command = new RedisCommand>("GEORADIUS_RO", distanceDecoder);
        return commandExecutor.readAsync(getName(), codec, command, getName(), convert(longitude), convert(latitude), 
                radius, geoUnit, "WITHDIST", geoOrder);
    }
    
    @Override
    public Map radiusWithDistance(double longitude, double latitude, double radius, GeoUnit geoUnit, GeoOrder geoOrder, int count) {
        return get(radiusWithDistanceAsync(longitude, latitude, radius, geoUnit, geoOrder, count));
    }

    @Override
    public RFuture> radiusWithDistanceAsync(double longitude, double latitude, double radius, GeoUnit geoUnit, GeoOrder geoOrder, int count) {
        RedisCommand> command = new RedisCommand>("GEORADIUS_RO", distanceDecoder);
        return commandExecutor.readAsync(getName(), codec, command, getName(), convert(longitude), convert(latitude), 
                radius, geoUnit, "WITHDIST", "COUNT", count, geoOrder);
    }

    @Override
    public Map radiusWithPosition(double longitude, double latitude, double radius, GeoUnit geoUnit) {
        return get(radiusWithPositionAsync(longitude, latitude, radius, geoUnit));
    }
    
    @Override
    public RFuture> radiusWithPositionAsync(double longitude, double latitude, double radius, GeoUnit geoUnit) {
        RedisCommand> command = new RedisCommand>("GEORADIUS_RO", postitionDecoder);
        return commandExecutor.readAsync(getName(), codec, command, getName(), convert(longitude), convert(latitude), radius, geoUnit, "WITHCOORD");
    }
    
    @Override
    public Map radiusWithPosition(double longitude, double latitude, double radius, GeoUnit geoUnit, int count) {
        return get(radiusWithPositionAsync(longitude, latitude, radius, geoUnit, count));
    }
    
    @Override
    public RFuture> radiusWithPositionAsync(double longitude, double latitude, double radius, GeoUnit geoUnit, int count) {
        RedisCommand> command = new RedisCommand>("GEORADIUS_RO", postitionDecoder);
        return commandExecutor.readAsync(getName(), codec, command, getName(), convert(longitude), convert(latitude), 
                radius, geoUnit, "WITHCOORD", "COUNT", count);
    }
    
    @Override
    public Map radiusWithPosition(double longitude, double latitude, double radius, GeoUnit geoUnit, GeoOrder geoOrder) {
        return get(radiusWithPositionAsync(longitude, latitude, radius, geoUnit, geoOrder));
    }
    
    @Override
    public RFuture> radiusWithPositionAsync(double longitude, double latitude, double radius, GeoUnit geoUnit, GeoOrder geoOrder) {
        RedisCommand> command = new RedisCommand>("GEORADIUS_RO", postitionDecoder);
        return commandExecutor.readAsync(getName(), codec, command, getName(), convert(longitude), convert(latitude), 
                radius, geoUnit, "WITHCOORD", geoOrder);
    }

    @Override
    public Map radiusWithPosition(double longitude, double latitude, double radius, GeoUnit geoUnit, GeoOrder geoOrder, int count) {
        return get(radiusWithPositionAsync(longitude, latitude, radius, geoUnit, geoOrder, count));
    }
    
    @Override
    public RFuture> radiusWithPositionAsync(double longitude, double latitude, double radius, GeoUnit geoUnit, GeoOrder geoOrder, int count) {
        RedisCommand> command = new RedisCommand>("GEORADIUS_RO", postitionDecoder);
        return commandExecutor.readAsync(getName(), codec, command, getName(), convert(longitude), convert(latitude), 
                radius, geoUnit, "WITHCOORD", "COUNT", count, geoOrder);
    }

    @Override
    public List radius(V member, double radius, GeoUnit geoUnit) {
        return get(radiusAsync(member, radius, geoUnit));
    }
    
    @Override
    public RFuture> radiusAsync(V member, double radius, GeoUnit geoUnit) {
        return commandExecutor.readAsync(getName(), codec, RedisCommands.GEORADIUSBYMEMBER_RO, getName(), encode(member), radius, geoUnit);
    }
    
    @Override
    public List radius(V member, double radius, GeoUnit geoUnit, int count) {
        return get(radiusAsync(member, radius, geoUnit, count));
    }
    
    @Override
    public RFuture> radiusAsync(V member, double radius, GeoUnit geoUnit, int count) {
        return commandExecutor.readAsync(getName(), codec, RedisCommands.GEORADIUSBYMEMBER_RO, getName(), encode(member), radius, geoUnit, "COUNT", count);
    }

    @Override
    public List radius(V member, double radius, GeoUnit geoUnit, GeoOrder geoOrder) {
        return get(radiusAsync(member, radius, geoUnit, geoOrder));
    }
    
    @Override
    public RFuture> radiusAsync(V member, double radius, GeoUnit geoUnit, GeoOrder geoOrder) {
        return commandExecutor.readAsync(getName(), codec, RedisCommands.GEORADIUSBYMEMBER_RO, getName(), encode(member), radius, geoUnit, geoOrder);
    }

    @Override
    public List radius(V member, double radius, GeoUnit geoUnit, GeoOrder geoOrder, int count) {
        return get(radiusAsync(member, radius, geoUnit, geoOrder, count));
    }
    
    @Override
    public RFuture> radiusAsync(V member, double radius, GeoUnit geoUnit, GeoOrder geoOrder, int count) {
        return commandExecutor.readAsync(getName(), codec, RedisCommands.GEORADIUSBYMEMBER_RO, getName(), encode(member), radius, geoUnit, "COUNT", count, geoOrder);
    }
    
    @Override
    public Map radiusWithDistance(V member, double radius, GeoUnit geoUnit) {
        return get(radiusWithDistanceAsync(member, radius, geoUnit));
    }
    
    @Override
    public RFuture> radiusWithDistanceAsync(V member, double radius, GeoUnit geoUnit) {
        RedisCommand command = new RedisCommand("GEORADIUSBYMEMBER_RO", distanceDecoder);
        return commandExecutor.readAsync(getName(), codec, command, getName(), encode(member), radius, geoUnit, "WITHDIST");
    }

    @Override
    public Map radiusWithDistance(V member, double radius, GeoUnit geoUnit, int count) {
        return get(radiusWithDistanceAsync(member, radius, geoUnit, count));
    }
    
    @Override
    public RFuture> radiusWithDistanceAsync(V member, double radius, GeoUnit geoUnit, int count) {
        RedisCommand command = new RedisCommand("GEORADIUSBYMEMBER_RO", distanceDecoder);
        return commandExecutor.readAsync(getName(), codec, command, getName(), encode(member), radius, geoUnit, "WITHDIST", "COUNT", count);
    }

    @Override
    public Map radiusWithDistance(V member, double radius, GeoUnit geoUnit, GeoOrder geoOrder) {
        return get(radiusWithDistanceAsync(member, radius, geoUnit, geoOrder));
    }
    
    @Override
    public RFuture> radiusWithDistanceAsync(V member, double radius, GeoUnit geoUnit, GeoOrder geoOrder) {
        RedisCommand command = new RedisCommand("GEORADIUSBYMEMBER_RO", distanceDecoder);
        return commandExecutor.readAsync(getName(), codec, command, getName(), encode(member), radius, geoUnit, "WITHDIST", geoOrder);
    }
    
    @Override
    public Map radiusWithDistance(V member, double radius, GeoUnit geoUnit, GeoOrder geoOrder, int count) {
        return get(radiusWithDistanceAsync(member, radius, geoUnit, geoOrder, count));
    }
    
    @Override
    public RFuture> radiusWithDistanceAsync(V member, double radius, GeoUnit geoUnit, GeoOrder geoOrder, int count) {
        RedisCommand command = new RedisCommand("GEORADIUSBYMEMBER_RO", distanceDecoder);
        return commandExecutor.readAsync(getName(), codec, command, getName(), encode(member), radius, geoUnit, "WITHDIST", "COUNT", count, geoOrder);
    }
    
    @Override
    public Map radiusWithPosition(V member, double radius, GeoUnit geoUnit) {
        return get(radiusWithPositionAsync(member, radius, geoUnit));
    }
    
    @Override
    public RFuture> radiusWithPositionAsync(V member, double radius, GeoUnit geoUnit) {
        RedisCommand> command = new RedisCommand>("GEORADIUSBYMEMBER_RO", postitionDecoder);
        return commandExecutor.readAsync(getName(), codec, command, getName(), encode(member), radius, geoUnit, "WITHCOORD");
    }
    
    @Override
    public Map radiusWithPosition(V member, double radius, GeoUnit geoUnit, int count) {
        return get(radiusWithPositionAsync(member, radius, geoUnit, count));
    }
    
    @Override
    public RFuture> radiusWithPositionAsync(V member, double radius, GeoUnit geoUnit, int count) {
        RedisCommand> command = new RedisCommand>("GEORADIUSBYMEMBER_RO", postitionDecoder);
        return commandExecutor.readAsync(getName(), codec, command, getName(), encode(member), radius, geoUnit, "WITHCOORD", "COUNT", count);
    }

    @Override
    public Map radiusWithPosition(V member, double radius, GeoUnit geoUnit, GeoOrder geoOrder) {
        return get(radiusWithPositionAsync(member, radius, geoUnit, geoOrder));
    }
    
    @Override
    public RFuture> radiusWithPositionAsync(V member, double radius, GeoUnit geoUnit, GeoOrder geoOrder) {
        RedisCommand> command = new RedisCommand>("GEORADIUSBYMEMBER_RO", postitionDecoder);
        return commandExecutor.readAsync(getName(), codec, command, getName(), encode(member), radius, geoUnit, "WITHCOORD", geoOrder);
    }
    
    @Override
    public Map radiusWithPosition(V member, double radius, GeoUnit geoUnit, GeoOrder geoOrder, int count) {
        return get(radiusWithPositionAsync(member, radius, geoUnit, geoOrder, count));
    }
    
    @Override
    public RFuture> radiusWithPositionAsync(V member, double radius, GeoUnit geoUnit, GeoOrder geoOrder, int count) {
        RedisCommand> command = new RedisCommand>("GEORADIUSBYMEMBER_RO", postitionDecoder);
        return commandExecutor.readAsync(getName(), codec, command, getName(), encode(member), radius, geoUnit, "WITHCOORD", "COUNT", count, geoOrder);
    }

	@Override
	public long radiusStoreTo(String destName, double longitude, double latitude, double radius, GeoUnit geoUnit) {
		return get(radiusStoreToAsync(destName, longitude, latitude, radius, geoUnit));
	}

	@Override
	public RFuture radiusStoreToAsync(String destName, double longitude, double latitude, double radius, GeoUnit geoUnit) {
        return commandExecutor.writeAsync(getName(), LongCodec.INSTANCE, RedisCommands.GEORADIUS_STORE, getName(), convert(longitude), convert(latitude), radius, geoUnit, "STORE", destName);
	}

	@Override
	public long radiusStoreTo(String destName, double longitude, double latitude, double radius, GeoUnit geoUnit, int count) {
		return get(radiusStoreToAsync(destName, longitude, latitude, radius, geoUnit, count));
	}

	@Override
	public RFuture radiusStoreToAsync(String destName, double longitude, double latitude, double radius, GeoUnit geoUnit, int count) {
        return commandExecutor.writeAsync(getName(), LongCodec.INSTANCE, RedisCommands.GEORADIUS_STORE, getName(), convert(longitude), convert(latitude), radius, geoUnit, "COUNT", count, "STORE", destName);
	}

	@Override
	public long radiusStoreTo(String destName, double longitude, double latitude, double radius, GeoUnit geoUnit, GeoOrder geoOrder, int count) {
		return get(radiusStoreToAsync(destName, longitude, latitude, radius, geoUnit, geoOrder, count));
	}

	@Override
	public RFuture radiusStoreToAsync(String destName, double longitude, double latitude, double radius, GeoUnit geoUnit, GeoOrder geoOrder, int count) {
        return commandExecutor.writeAsync(getName(), LongCodec.INSTANCE, RedisCommands.GEORADIUS_STORE, getName(), convert(longitude), convert(latitude), radius, geoUnit, geoOrder, "COUNT", count, "STORE", destName);
	}

	@Override
	public long radiusStoreTo(String destName, V member, double radius, GeoUnit geoUnit) {
		return get(radiusStoreToAsync(destName, member, radius, geoUnit));
	}

	@Override
	public RFuture radiusStoreToAsync(String destName, V member, double radius, GeoUnit geoUnit) {
        return commandExecutor.writeAsync(getName(), codec, RedisCommands.GEORADIUSBYMEMBER_STORE, getName(), encode(member), radius, geoUnit, "STORE", destName);
	}

	@Override
	public long radiusStoreTo(String destName, V member, double radius, GeoUnit geoUnit, int count) {
		return get(radiusStoreToAsync(destName, member, radius, geoUnit, count));
	}

	@Override
	public RFuture radiusStoreToAsync(String destName, V member, double radius, GeoUnit geoUnit, int count) {
        return commandExecutor.writeAsync(getName(), codec, RedisCommands.GEORADIUSBYMEMBER_STORE, getName(), encode(member), radius, geoUnit, "COUNT", count, "STORE", destName);
	}

	@Override
	public long radiusStoreTo(String destName, V member, double radius, GeoUnit geoUnit, GeoOrder geoOrder, int count) {
        return get(radiusStoreToAsync(destName, member, radius, geoUnit, geoOrder, count));
	}

	@Override
	public RFuture radiusStoreToAsync(String destName, V member, double radius, GeoUnit geoUnit, GeoOrder geoOrder, int count) {
        return commandExecutor.writeAsync(getName(), codec, RedisCommands.GEORADIUSBYMEMBER_STORE, getName(), encode(member), radius, geoUnit, geoOrder, "COUNT", count, "STORE", destName);
	}

}