You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by GitBox <gi...@apache.org> on 2020/01/01 03:17:17 UTC

[GitHub] [calcite] hsyuan commented on a change in pull request #1591: [CALCITE-3510] Implement Redis adapter

hsyuan commented on a change in pull request #1591: [CALCITE-3510] Implement  Redis adapter
URL: https://github.com/apache/calcite/pull/1591#discussion_r362300355
 
 

 ##########
 File path: redis/src/main/java/org/apache/calcite/adapter/redis/RedisJedisManager.java
 ##########
 @@ -0,0 +1,99 @@
+/*
+ * 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.calcite.adapter.redis;
+
+import org.apache.calcite.util.trace.CalciteTrace;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
+
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
+
+import org.slf4j.Logger;
+
+import java.util.Map;
+
+import redis.clients.jedis.Jedis;
+import redis.clients.jedis.JedisPool;
+import redis.clients.jedis.JedisPoolConfig;
+import redis.clients.jedis.Protocol;
+
+import static java.util.Objects.requireNonNull;
+
+/**
+ * Manages connections to the Redis nodes.
+ */
+public class RedisJedisManager implements AutoCloseable {
+  private static final Logger LOGGER = CalciteTrace.getPlannerTracer();
+  private final LoadingCache<String, JedisPool> jedisPoolCache;
+  private final JedisPoolConfig jedisPoolConfig;
+
+  private final int maxTotal = GenericObjectPoolConfig.DEFAULT_MAX_TOTAL;
+  private final int maxIdle = GenericObjectPoolConfig.DEFAULT_MAX_IDLE;
+  private final int minIdle = GenericObjectPoolConfig.DEFAULT_MIN_IDLE;
+  private final int timeout = Protocol.DEFAULT_TIMEOUT;
+
+  private final String host;
+  private final String password;
+  private final int port;
+  private final int database;
+
+  public RedisJedisManager(String host, int port, int database, String password) {
+    JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
+    jedisPoolConfig.setMaxTotal(maxTotal);
+    jedisPoolConfig.setMaxIdle(maxIdle);
+    jedisPoolConfig.setMinIdle(minIdle);
+    this.host = host;
+    this.port = port;
+    this.database = database;
+    this.password = password;
+    this.jedisPoolConfig = jedisPoolConfig;
+    this.jedisPoolCache =
+        CacheBuilder.newBuilder().build(CacheLoader.from(this::createConsumer));
+  }
+
+  public JedisPool getJedisPool() {
+    requireNonNull(host, "host is null");
+    return jedisPoolCache.getUnchecked(host);
+  }
+
+  public Jedis getResource() {
+    return getJedisPool().getResource();
+  }
+
+  private JedisPool createConsumer() {
+    String pwd = password;
+    if (StringUtils.isEmpty(pwd)) {
+      pwd = null;
+    }
+    return new JedisPool(jedisPoolConfig, host, port, timeout, pwd, database);
+  }
+
+  @Override public void close() {
 
 Review comment:
   maybe you just need `Cache.invalidateAll()` if you implement the removalListener

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services