Search in sources :

Example 1 with ServerMode

use of org.apache.gora.redis.util.ServerMode in project gora by apache.

the class RedisStore method initialize.

/**
 * Initialize the data store by reading the credentials, setting the client's
 * properties up and reading the mapping file. Initialize is called when then
 * the call to {@link org.apache.gora.store.DataStoreFactory#createDataStore}
 * is made.
 *
 * @param keyClass Gora's key class
 * @param persistentClass Persistent class
 * @param properties Configurations for the data store
 * @throws org.apache.gora.util.GoraException Unexpected exception during initialization
 */
@Override
public void initialize(Class<K> keyClass, Class<T> persistentClass, Properties properties) throws GoraException {
    try {
        super.initialize(keyClass, persistentClass, properties);
        InputStream mappingStream;
        if (properties.containsKey(XML_MAPPING_DEFINITION)) {
            if (LOG.isTraceEnabled()) {
                LOG.trace("{} = {}", XML_MAPPING_DEFINITION, properties.getProperty(XML_MAPPING_DEFINITION));
            }
            mappingStream = IOUtils.toInputStream(properties.getProperty(XML_MAPPING_DEFINITION), (Charset) null);
        } else {
            mappingStream = getClass().getClassLoader().getResourceAsStream(getConf().get(PARSE_MAPPING_FILE_KEY, DEFAULT_MAPPING_FILE));
        }
        RedisMappingBuilder mappingBuilder = new RedisMappingBuilder(this);
        mapping = mappingBuilder.readMapping(mappingStream);
        Config config = new Config();
        String storage = getConf().get(GORA_REDIS_STORAGE, properties.getProperty(GORA_REDIS_STORAGE));
        mode = StorageMode.valueOf(storage);
        String modeString = getConf().get(GORA_REDIS_MODE, properties.getProperty(GORA_REDIS_MODE));
        ServerMode connectionMode = ServerMode.valueOf(modeString);
        String name = getConf().get(GORA_REDIS_MASTERNAME, properties.getProperty(GORA_REDIS_MASTERNAME));
        String readm = getConf().get(GORA_REDIS_READMODE, properties.getProperty(GORA_REDIS_READMODE));
        // Override address in tests
        String[] hosts = getConf().get(GORA_REDIS_ADDRESS, properties.getProperty(GORA_REDIS_ADDRESS)).split(",");
        for (int indexHosts = 0; indexHosts < hosts.length; indexHosts++) {
            hosts[indexHosts] = PREFIX + hosts[indexHosts];
        }
        switch(connectionMode) {
            case SINGLE:
                config.useSingleServer().setAddress(hosts[0]).setDatabase(mapping.getDatabase());
                break;
            case CLUSTER:
                config.useClusterServers().addNodeAddress(hosts);
                break;
            case REPLICATED:
                config.useReplicatedServers().addNodeAddress(hosts).setDatabase(mapping.getDatabase());
                break;
            case SENTINEL:
                config.useSentinelServers().setMasterName(name).setReadMode(ReadMode.valueOf(readm)).addSentinelAddress(hosts);
                break;
            default:
                throw new AssertionError(connectionMode.name());
        }
        redisInstance = Redisson.create(config);
        if (autoCreateSchema && !schemaExists()) {
            createSchema();
        }
    } catch (IOException ex) {
        throw new GoraException(ex);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) InputStream(java.io.InputStream) Config(org.redisson.config.Config) Charset(java.nio.charset.Charset) IOException(java.io.IOException) ServerMode(org.apache.gora.redis.util.ServerMode)

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Charset (java.nio.charset.Charset)1 ServerMode (org.apache.gora.redis.util.ServerMode)1 GoraException (org.apache.gora.util.GoraException)1 Config (org.redisson.config.Config)1