Search in sources :

Example 1 with RedisClusterNodeBuilder

use of org.springframework.data.redis.connection.RedisClusterNode.RedisClusterNodeBuilder in project redisson by redisson.

the class RedisClusterNodeDecoder method decode.

@Override
public List<RedisClusterNode> decode(ByteBuf buf, State state) throws IOException {
    String response = buf.toString(CharsetUtil.UTF_8);
    List<RedisClusterNode> nodes = new ArrayList<RedisClusterNode>();
    for (String nodeInfo : response.split("\n")) {
        String[] params = nodeInfo.split(" ");
        String nodeId = params[0];
        String flagsStr = params[2];
        Set<Flag> flags = EnumSet.noneOf(Flag.class);
        for (String flag : flagsStr.split(",")) {
            String flagValue = flag.toUpperCase().replaceAll("\\?", "");
            flags.add(Flag.valueOf(flagValue));
        }
        RedisURI address = null;
        if (!flags.contains(Flag.NOADDR)) {
            String addr = params[1].split("@")[0];
            address = new RedisURI("redis://" + addr);
        }
        String masterId = params[3];
        if ("-".equals(masterId)) {
            masterId = null;
        }
        Set<Integer> slotsCollection = new HashSet<Integer>();
        LinkState linkState = null;
        if (params.length >= 8 && params[7] != null) {
            linkState = LinkState.valueOf(params[7].toUpperCase());
        }
        if (params.length > 8) {
            for (int i = 0; i < params.length - 8; i++) {
                String slots = params[i + 8];
                if (slots.indexOf("-<-") != -1 || slots.indexOf("->-") != -1) {
                    continue;
                }
                String[] parts = slots.split("-");
                if (parts.length == 1) {
                    slotsCollection.add(Integer.valueOf(parts[0]));
                } else if (parts.length == 2) {
                    for (int j = Integer.valueOf(parts[0]); j < Integer.valueOf(parts[1]) + 1; j++) {
                        slotsCollection.add(j);
                    }
                }
            }
        }
        NodeType type = null;
        if (flags.contains(Flag.MASTER)) {
            type = NodeType.MASTER;
        } else if (flags.contains(Flag.SLAVE)) {
            type = NodeType.SLAVE;
        }
        RedisClusterNodeBuilder builder = RedisClusterNode.newRedisClusterNode().linkState(linkState).slaveOf(masterId).serving(new SlotRange(slotsCollection)).withId(nodeId).promotedAs(type).withFlags(flags);
        if (address != null) {
            builder.listeningAt(address.getHost(), address.getPort());
        }
        nodes.add(builder.build());
    }
    return nodes;
}
Also used : RedisURI(org.redisson.misc.RedisURI) ArrayList(java.util.ArrayList) Flag(org.springframework.data.redis.connection.RedisClusterNode.Flag) LinkState(org.springframework.data.redis.connection.RedisClusterNode.LinkState) RedisClusterNode(org.springframework.data.redis.connection.RedisClusterNode) NodeType(org.springframework.data.redis.connection.RedisNode.NodeType) SlotRange(org.springframework.data.redis.connection.RedisClusterNode.SlotRange) HashSet(java.util.HashSet) RedisClusterNodeBuilder(org.springframework.data.redis.connection.RedisClusterNode.RedisClusterNodeBuilder)

Aggregations

ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 RedisURI (org.redisson.misc.RedisURI)1 RedisClusterNode (org.springframework.data.redis.connection.RedisClusterNode)1 Flag (org.springframework.data.redis.connection.RedisClusterNode.Flag)1 LinkState (org.springframework.data.redis.connection.RedisClusterNode.LinkState)1 RedisClusterNodeBuilder (org.springframework.data.redis.connection.RedisClusterNode.RedisClusterNodeBuilder)1 SlotRange (org.springframework.data.redis.connection.RedisClusterNode.SlotRange)1 NodeType (org.springframework.data.redis.connection.RedisNode.NodeType)1