|
| 1 | +package picoded.dstack.redisson; |
| 2 | + |
| 3 | +import java.util.Map; |
| 4 | +import java.util.logging.Level; |
| 5 | +import java.util.logging.Logger; |
| 6 | + |
| 7 | +import picoded.core.struct.GenericConvertMap; |
| 8 | +import picoded.dstack.core.*; |
| 9 | + |
| 10 | +import org.redisson.Redisson; |
| 11 | +import org.redisson.config.Config; |
| 12 | +import org.redisson.api.RedissonClient; |
| 13 | + |
| 14 | +/** |
| 15 | + * [Internal use only] |
| 16 | + * |
| 17 | + * Redisson based stack provider |
| 18 | + **/ |
| 19 | +public class RedissonStack extends CoreStack { |
| 20 | + |
| 21 | + /** |
| 22 | + * The internal Redis connection |
| 23 | + */ |
| 24 | + |
| 25 | + protected RedissonClient conn = null; |
| 26 | + |
| 27 | + //------------------------------------------------------------------------- |
| 28 | + // Database connection constructor |
| 29 | + //------------------------------------------------------------------------- |
| 30 | + |
| 31 | + /** |
| 32 | + * Given the redis config object, get the full_url |
| 33 | + * https://github.com/lettuce-io/lettuce-core/wiki/Redis-URI-and-connection-details |
| 34 | + */ |
| 35 | + public static String getFullConnectionURL(GenericConvertMap<String, Object> config) { |
| 36 | + |
| 37 | + // Get the full connection url, and use it if present |
| 38 | + String full_url = config.getString("full_url", null); |
| 39 | + if (full_url != null) { |
| 40 | + return full_url; |
| 41 | + } |
| 42 | + |
| 43 | + // Get the DB name (required) |
| 44 | + // Redis db are labelled from 0 to 15 |
| 45 | + String dbname = config.getString("name", null); |
| 46 | + if (dbname == null || dbname.isEmpty()) { |
| 47 | + throw new IllegalArgumentException("Missing database 'name' for redis config"); |
| 48 | + } |
| 49 | + |
| 50 | + // Lets get the config respectively |
| 51 | + String protocol = config.getString("protocol", "redis"); |
| 52 | + String user = config.getString("user", null); |
| 53 | + String pass = config.getString("pass", null); |
| 54 | + String host = config.getString("host", "172.17.0.1"); //default docker ip |
| 55 | + int port = config.getInt("port", 6379); //default redis port |
| 56 | + |
| 57 | + // Lets build the auth str |
| 58 | + String authStr = ""; |
| 59 | + if (user != null && pass != null) { |
| 60 | + authStr = user + ":" + pass + "@"; |
| 61 | + } |
| 62 | + |
| 63 | + return protocol + "://" + authStr + host + ":" + port + "/" + dbname; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Given the redis config object, get the Redisson client connection |
| 68 | + */ |
| 69 | + public static RedissonClient setupFromConfig(GenericConvertMap<String, Object> inConfig) { |
| 70 | + // Get the full_url |
| 71 | + String full_url = getFullConnectionURL(inConfig); |
| 72 | + |
| 73 | + // Apply config |
| 74 | + Config config = new Config(); |
| 75 | + config.useSingleServer().setAddress(full_url); |
| 76 | + |
| 77 | + // Create the client, and return it |
| 78 | + return Redisson.create(config); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Constructor with configuration map |
| 83 | + */ |
| 84 | + public RedissonStack(GenericConvertMap<String, Object> inConfig) { |
| 85 | + //https://github.com/redisson/redisson/wiki/10.-additional-features#107-low-level-redis-client |
| 86 | + super(inConfig); |
| 87 | + |
| 88 | + // Extract the connection config object |
| 89 | + GenericConvertMap<String, Object> dbConfig = inConfig.fetchGenericConvertStringMap("redis"); |
| 90 | + |
| 91 | + // If DB config is missing, throw an error |
| 92 | + if (dbConfig == null) { |
| 93 | + throw new IllegalArgumentException( |
| 94 | + "Missing 'RedissonStack' config object for Redis stack provider"); |
| 95 | + } |
| 96 | + |
| 97 | + // Get the connection & database |
| 98 | + conn = setupFromConfig(dbConfig); |
| 99 | + } |
| 100 | + |
| 101 | + //-------------------------------------------------------------------------- |
| 102 | + // |
| 103 | + // Internal package methods |
| 104 | + // |
| 105 | + //-------------------------------------------------------------------------- |
| 106 | + |
| 107 | + // /** |
| 108 | + // * @return pong |
| 109 | + // */ |
| 110 | + // protected String ping() { |
| 111 | + // return "pong"; |
| 112 | + // } |
| 113 | + |
| 114 | + /** |
| 115 | + * @return the internal hazelcastInstance connection |
| 116 | + */ |
| 117 | + protected RedissonClient getConnection() { |
| 118 | + return conn; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Initialize and return the requested data structure with the given name or type if its supported |
| 123 | + * |
| 124 | + * @param name name of the datastructure to initialize |
| 125 | + * @param type implmentation type (KeyValueMap / KeyLongMap / DataObjectMap / FileWorkspaceMap) |
| 126 | + * |
| 127 | + * @return initialized data structure if type is supported |
| 128 | + */ |
| 129 | + protected Core_DataStructure initDataStructure(String name, String type) { |
| 130 | + // Initialize for the respective type |
| 131 | + Core_DataStructure ret = null; |
| 132 | + if (type.equalsIgnoreCase("DataObjectMap")) { |
| 133 | + ret = new Redisson_DataObjectMap(this, name); |
| 134 | + } |
| 135 | + if (type.equalsIgnoreCase("KeyValueMap")) { |
| 136 | + ret = new Redisson_KeyValueMap(this, name); |
| 137 | + } |
| 138 | + // If datastrucutre initialized, setup name |
| 139 | + if (ret != null) { |
| 140 | + ret.configMap().put("name", name); |
| 141 | + } |
| 142 | + // Return the initialized object (or null) |
| 143 | + return ret; |
| 144 | + } |
| 145 | +} |
0 commit comments