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

com.github.lontime.extredisson.common.RedisKeys Maven / Gradle / Ivy

The newest version!
package com.github.lontime.extredisson.common;

import java.util.Objects;

import com.github.lontime.extredisson.configuration.OptionResolver;

/**
 * RedisKeys.
 * @author lontime
 * @since 1.0
 */
public class RedisKeys {

    public static final String SPLITTER = ":";

    public static final RedisKeys SYS = RedisKeys.create("sys");
    public static final RedisKeys USER = RedisKeys.create("usr");

    public static final RedisKeys SERVICE = RedisKeys.create("service");
    public static final RedisKeys REF = RedisKeys.create("ref_");
    public static final RedisKeys REF_SERVICE = RedisKeys.create(REF, "service");

    public static final RedisKeys LOCK = RedisKeys.create(SYS, "lock");

    public static final RedisKeys LOCK_WATCH = RedisKeys.create(LOCK, "watch");

    public static final RedisKeys USER_LOCK = RedisKeys.create(USER, "lock");

    public static final RedisKeys SYS_WATCH = RedisKeys.create(SYS, "watch");

    public static final RedisKeys SYS_REF = RedisKeys.create(SYS, "ref");

    public static final RedisKeys USER_REF = RedisKeys.create(USER, "ref");

    public static final RedisKeys USER_SERVICE = RedisKeys.create(USER, "service");

    public static final RedisKeys USER_REF_SERVICE = RedisKeys.create(USER_REF, "service");

    public static final RedisKeys USER_REPLY = RedisKeys.create(USER, "reply");

    public static final RedisKeys USER_REPLY_ALL = RedisKeys.create(USER_REPLY, "all");

    public static final RedisKeys SYS_REPLY = RedisKeys.create(SYS, "reply");

    public static final RedisKeys SYS_REPLY_ALL = RedisKeys.create(SYS_REPLY, "all");

    /** sys:topic */
    public static final RedisKeys SYS_TOPIC = RedisKeys.create(SYS, "topic");

    /** usr:topic */
    public static final RedisKeys USER_TOPIC = RedisKeys.create(USER, "topic");

    /** sys:ref:topic */
    public static final RedisKeys SYS_REF_TOPIC = RedisKeys.create(SYS_REF, "topic");

    /** usr:ref:topic */
    public static final RedisKeys USER_REF_TOPIC = RedisKeys.create(USER_REF, "topic");

    public static final RedisKeys META = RedisKeys.create(SYS, "mata");

    public static final RedisKeys NODE = RedisKeys.create(SYS, "node");

    public static final RedisKeys NODE_ALL = RedisKeys.create(NODE, "all");

    public static final RedisKeys SCHEDULER = RedisKeys.create(SYS, "sched");

    private final RedisKeys parent;

    private final String name;

    private final String fullName;

    private RedisKeys(RedisKeys parent, String name) {
        this.name = name;
        this.parent = parent;
        this.fullName = buildFullName();
    }

    private String buildFullName() {
        if (parent == null) {
            return name;
        }
        return String.join(SPLITTER, parent.getFullName(), name);
    }

    public RedisKeys getParent() {
        return parent;
    }

    public String getName() {
        return name;
    }

    public String getFullName() {
        return fullName;
    }

    public static boolean slaveOf(RedisKeys key, RedisKeys parentKey) {
        if (parentKey == null || key == null) {
            return false;
        }
        if (key.getFullName().equals(parentKey.getFullName())) {
            return true;
        }
        return slaveOf(key.getParent(), parentKey);
    }

    public static RedisKeys create(String name) {
        return create(null, name);
    }

    public static RedisKeys create(RedisKeys parent, String name) {
        if (name == null) {
            return parent;
        }
        return new RedisKeys(parent, name);
    }

    public static RedisKeys create(RedisKeys parent, RedisKeys name) {
        if (name == null) {
            return parent;
        }
        return new RedisKeys(parent, name.getFullName());
    }

    public static RedisKeys create(RedisKeys parent, String name0, String name1) {
        if (name1 == null) {
            return create(parent, name0);
        }
        return new RedisKeys(create(parent, name0), name1);
    }

    public static RedisKeys create(RedisKeys parent, String name0, String name1, String name2) {
        if (name2 == null) {
            return create(parent, name0, name1);
        }
        return create(create(parent, name0, name1), name2);
    }

    public static RedisKeys create(RedisKeys parent, String name0, String name1,
                                   String name2, String name3) {
        if (name3 == null) {
            return create(parent, name0, name1, name2);
        }
        return create(create(parent, name0, name1, name2), name3);
    }

    public static RedisKeys create(RedisKeys parent, String name0, String name1,
                                   String name2, String name3, String name4) {
        if (name4 == null) {
            return create(parent, name0, name1, name2, name3);
        }
        return create(create(parent, name0, name1, name2, name3), name4);
    }

    public static RedisKeys create(RedisKeys parent, String name0, String name1,
                                   String name2, String name3, String name4,
                                   String name5) {
        if (name5 == null) {
            return create(parent, name0, name1, name2, name3, name4);
        }
        return create(create(parent, name0, name1, name2, name3, name4), name5);
    }

    public static RedisKeys create(RedisKeys parent, String name0, String name1,
                                   String name2, String name3, String name4,
                                   String name5, String name6) {
        if (name6 == null) {
            return create(parent, name0, name1, name2, name3, name4, name5);
        }
        return create(create(parent, name0, name1, name2, name3, name4, name5), name6);
    }

    public static RedisKeys create(RedisKeys parent, String name0, String name1,
                                   String name2, String name3, String name4,
                                   String name5, String name6, String name7) {
        if (name7 == null) {
            return create(parent, name0, name1, name2, name3, name4, name5, name6);
        }
        return create(create(parent, name0, name1, name2, name3, name4, name5, name6),
                name7);
    }

    public static RedisKeys create(RedisKeys parent, String name0, String name1,
                                   String name2, String name3, String name4,
                                   String name5, String name6, String name7,
                                   String name8) {
        if (name8 == null) {
            return create(parent, name0, name1, name2, name3, name4, name5, name6, name7);
        }
        return create(create(parent, name0, name1, name2, name3, name4, name5, name6),
                name7, name8);
    }

    public static RedisKeys create(RedisKeys parent, String name0, String name1,
                                   String name2, String name3, String name4,
                                   String name5, String name6, String name7,
                                   String name8, String name9) {
        if (name9 == null) {
            return create(parent, name0, name1, name2, name3, name4, name5, name6, name7, name8);
        }
        return create(create(parent, name0, name1, name2, name3, name4, name5, name6, name7, name8),
                name7, name8, name9);
    }

    public static RedisKeys project() {
        return OptionResolver.getInstance().getProjectName();
    }

    public static RedisKeys projectAllTopics() {
        return create(project(), "topic", "all");
    }

    public static RedisKeys projectAllServices() {
        return create(project(), "service", "all");
    }

    public static RedisKeys projectAliveService(String serviceName) {
        return create(project(), "service", "alive", serviceName);
    }

    public static RedisKeys projectTopic(String topic, String ip) {
        return create(project(), "topic", topic, ip);
    }

    public static RedisKeys projectAliveTopic(String topic) {
        return create(project(), "topic", "alive", topic);
    }

    public static RedisKeys projectReply() {
        return create(project(), "reply");
    }

    public static RedisKeys projectReply(String to, String tag) {
        return create(project(), "reply", to, tag);
    }

    public static RedisKeys projectReply(String to) {
        return create(project(), "reply", to);
    }

    public static RedisKeys projectLock() {
        return create(project(), "lock");
    }

    public static RedisKeys projectService(boolean prefix) {
        return create(project(), wrapPrefix("service" , prefix));
    }

    public static RedisKeys projectService(String name, boolean prefix) {
        return create(project(), wrapPrefix("service" , prefix), name);
    }

    public static RedisKeys projectService(String name) {
        return projectService(name, false);
    }


    public static String data(String parentFullName) {
        return create(null, parentFullName, "data").getFullName();
    }

    public static String data(RedisKeys parent) {
        return data(parent.getFullName());
    }

    private static String wrapPrefix(String name, boolean prefix) {
        if (prefix) {
            return name.concat(SPLITTER);
        }
        return name;
    }

    @Override
    public boolean equals(Object other) {
        return other != null && Objects.equals(this.fullName, ((RedisKeys) other).fullName);
    }

    @Override
    public String toString() {
        return this.fullName;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy