djkevincr commented on a change in pull request #186: [GORA-527] Implement a 
data store for redis
URL: https://github.com/apache/gora/pull/186#discussion_r315993566
 
 

 ##########
 File path: gora-redis/src/main/java/org/apache/gora/redis/store/RedisStore.java
 ##########
 @@ -0,0 +1,531 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.gora.redis.store;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.invoke.MethodHandles;
+import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.stream.Collectors;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import org.apache.avro.Schema;
+import org.apache.commons.io.IOUtils;
+import org.apache.gora.persistency.impl.PersistentBase;
+import org.apache.gora.query.PartitionQuery;
+import org.apache.gora.query.Query;
+import org.apache.gora.query.Result;
+import org.apache.gora.query.impl.PartitionQueryImpl;
+import org.apache.gora.redis.query.RedisQuery;
+import org.apache.gora.redis.query.RedisResult;
+import org.apache.gora.redis.util.DatumHandler;
+import org.apache.gora.redis.util.ServerMode;
+import org.apache.gora.redis.util.StorageMode;
+import org.apache.gora.store.impl.DataStoreBase;
+import org.apache.gora.util.GoraException;
+import org.redisson.Redisson;
+import org.redisson.api.RBatch;
+import org.redisson.api.RBucket;
+import org.redisson.api.RBucketAsync;
+import org.redisson.api.RFuture;
+import org.redisson.api.RLexSortedSet;
+import org.redisson.api.RLexSortedSetAsync;
+import org.redisson.api.RList;
+import org.redisson.api.RListAsync;
+import org.redisson.api.RMap;
+import org.redisson.api.RMapAsync;
+import org.redisson.api.RedissonClient;
+import org.redisson.config.Config;
+import org.redisson.config.ReadMode;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+
+/**
+ * Implementation of a Redis data store to be used by gora.
+ *
+ * @param <K> class to be used for the key
+ * @param <T> class to be persisted within the store
+ */
+public class RedisStore<K, T extends PersistentBase> extends DataStoreBase<K, 
T> {
+
+  //Redis constants
+  private static final String FIELD_SEPARATOR = ".";
+  private static final String WILDCARD = "*";
+  private static final String INDEX = "index";
+  private static final String START_TAG = "{";
+  private static final String END_TAG = "}";
+  private static final String PREFIX = "redis://";
+
+  protected static final String PARSE_MAPPING_FILE_KEY = 
"gora.redis.mapping.file";
+  protected static final String DEFAULT_MAPPING_FILE = 
"gora-redis-mapping.xml";
+  protected static final String XML_MAPPING_DEFINITION = "gora.mapping";
+  private RedissonClient redisInstance;
+  private RedisMapping mapping;
+  public static final Logger LOG = 
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
+  private static final DatumHandler handler = new DatumHandler();
+  private StorageMode mode;
+
+  /**
+   * 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
+   * @param persistentClass
+   * @param properties
+   * @throws org.apache.gora.util.GoraException
+   */
+  @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));
+      }
+      mapping = readMapping(mappingStream);
+      Config config = new Config();
+      String storage = getConf().get("gora.datastore.redis.storage", 
properties.getProperty("gora.datastore.redis.storage"));
+      mode = StorageMode.valueOf(storage);
+      String modeString = getConf().get("gora.datastore.redis.mode", 
properties.getProperty("gora.datastore.redis.mode"));
+      ServerMode connectionMode = ServerMode.valueOf(modeString);
+      String name = getConf().get("gora.datastore.redis.masterName", 
properties.getProperty("gora.datastore.redis.masterName"));
+      String readm = getConf().get("gora.datastore.redis.readMode", 
properties.getProperty("gora.datastore.redis.readMode"));
+      //Override address in tests
+      String[] hosts = getConf().get("gora.datastore.redis.address", 
properties.getProperty("gora.datastore.redis.address")).split(",");
+      for (int i = 0; i < hosts.length; i++) {
+        hosts[i] = PREFIX + hosts[i];
+      }
+      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);
+    }
+  }
+
+  protected RedisMapping readMapping(InputStream inputStream) throws 
IOException {
 
 Review comment:
   Please refactor this move this logic to separate mapping builder file. 
Please check other datastores which has mapping builder patter implemented.  
Eg:- RedisMappingBuilder

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to