Skip to main content
edited tags; edited title
Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Simplifying and optimizing chat Chat logger using Redis

added 4 characters in body; edited tags; edited title
Source Link
Alex L
  • 5.8k
  • 2
  • 26
  • 69

Simplifying and optimizing the structure of this codechat logger

I'm developing a Java EE project which uses Redis through the Jedis library. I'm retrieving the chat history of a particular channel from the servlet context and if absent, from Redis. 

The PMD plugin of Netbeans shows me a message that the Cyclomatic Complexity of the code is 15 which is very large. I wish to reduce that and I need to know if there is a better mechanism which can be used.

Simplifying and optimizing the structure of this code

I'm developing a Java EE project which uses Redis through the Jedis library. I'm retrieving the chat history of a particular channel from the servlet context and if absent, from Redis. The PMD plugin of Netbeans shows me a message that the Cyclomatic Complexity of the code is 15 which is very large. I wish to reduce that and I need to know if there is a better mechanism which can be used.

Simplifying and optimizing chat logger

I'm developing a Java EE project which uses Redis through the Jedis library. I'm retrieving the chat history of a particular channel from the servlet context and if absent, from Redis. 

The PMD plugin of Netbeans shows me a message that the Cyclomatic Complexity of the code is 15 which is very large. I wish to reduce that and I need to know if there is a better mechanism which can be used.

deleted 1056 characters in body
Source Link
Malachi
  • 29.1k
  • 11
  • 87
  • 188
private transient final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(HistoryRetriever.class);
            private transient final JedisHelper jedisHelper = new JedisHelper();
        
 public String retrieveHistory(final JSONObject data) {
    final ServletContext publiccontext String= retrieveHistoryContextManager.getContext(final);
    JSONObject dataobject = new JSONObject();
 {   final Jedis jedis = jedisHelper.getJedisObjectFromPool();
        
                final ServletContext context = ContextManager.getContext();
                JSONObject object = newtry JSONObject();{
               String finalkey1, Jediskey2, jedischannel, =timestamp, jedisHelper.getJedisObjectFromPool();count;
        
        key1 = data.getString("key1");
      try {
 key2 = data.getString("key2");
        channel = key2 + "_" + data.getString("channel-id");
   String key1, key2, channel,  timestamp, count;= "";
        count = "";
        int historyType = 0;
        
                    key1 = data.getString("key1");
                    key2 = dataLOGGER.getStringinfo("key2");
             "History requested for " + channel + channel" =belonging key2to +subkey: "_"" + data.getString("channel-id"key2);
                    timestamp = "";
                    count = "";
                    int historyType = 0;
        
        if (jedisHelper.checkValidity(key1, key2)) {
         LOGGER   if (data.infohas("History"timestamp")) requested{
 for " + channel + " belonging to subkey: " + key2    historyType = 1;
                timestamp = data.getString("timestamp");
            }
            if (data.has("count")) {
                historyType = 2;
                count = data.getString("count");
            }
        
                  ConcurrentHashMap history if= (jedisHelperConcurrentHashMap) context.checkValiditygetAttribute(key1, key2)"history") {;
                        if (data.has("timestamp")null == history) {
                            historyTypehistory = 1;
                            timestampnew =ConcurrentHashMap<Object, data.getStringObject>("timestamp");
                        }
                        if (datacontext.hassetAttribute("count")) {
                            historyType = 2;
                            count ="history", data.getString("count"history);
                        }
        
                        ConcurrentHashMap historyobject = getHistoryFromRedis(ConcurrentHashMap)timestamp, context.getAttribute("history"count, channel, historyType);
                        if (null ==} history)else {
                          ConcurrentSkipListMap<Long, String> historymyHistory = new ConcurrentHashMap<Object(ConcurrentSkipListMap<Long, Object>(String>);
                            contexthistory.setAttributeget("history", historychannel);
        
                if (null == myHistory) {
        object = getHistoryFromRedis          LOGGER.info(timestamp,"History countfor this channel not found in Context");
                    myHistory = new ConcurrentSkipListMap<Long, String>();
                    history.put(channel, historyTypemyHistory);
        
                    object = getHistoryFromRedis(timestamp, count, channel, historyType);
                } else {
                    /* Check for history in context */
  ConcurrentSkipListMap<Long                  if (historyType == 1) {
                        final Map<Long, String> myHistorychannelHistory = myHistory.tailMap(ConcurrentSkipListMap<LongLong.parseLong(timestamp));
                        if (!channelHistory.isEmpty()) {
                            for (Map.Entry<Long, String> entry : channelHistory.entrySet()) history{
                                object.getput(channelString.valueOf(entry.getKey()), entry.getValue());
                            }
                        }
                    }
        
                            if (nullhistoryType == myHistory2) {
                                LOGGER.info("History for this channel not found in Context");
                                myHistory = newfinal ConcurrentSkipListMap<LongMap<Long, String>();
                              channelHistory = historymyHistory.putdescendingMap(channel, myHistory);
        
                        if (!channelHistory.isEmpty()) {
        object                    int counter = getHistoryFromRedis(timestamp,0;
 count, channel, historyType                         final int i = Integer.parseInt(count);
        
                            } else {
                                /* Check for history in context */
                                if (historyType == 1) {
                                    final Map<Long, String> channelHistory = myHistory.tailMap(Long.parseLong(timestamp));
                                    if (!channelHistory.isEmpty()) {
                                        for (Map.Entry<Long, String> entry : channelHistory.entrySet()) {
                                            object.put(String.valueOf(entry.getKey()),if entry.getValue());
                                     counter >= i) }{
                                    }break;
                                }
        
                                if (historyType == 2) {
        
                                    final Map<Long, String> channelHistory = myHistory.descendingMap();
        
                                    if (!channelHistory.isEmpty()) {
                                        int counter = 0;
                                        final int i = Integer.parseInt(count);
        
                                        for (Map.Entry<Long, String> entry : channelHistory.entrySet()) {
                                            if (counter >= i) {
                                                break;
                                            }
                                            object.put(String.valueOf(entry.getKey()), entry.getValue());
                                            counter++;
                                        }
                                    }
                            }
                        }
                    }
                } else {
                    LOGGER.info("Invalid Keys or keys deactivated");
                }
            } catch (JSONException ex) {
                LOGGER.error("JSON Exception: " + ex);
            } catch (NumberFormatException ex) {
                LOGGER.error("NumberFormatException: " + ex);
            } finally {
                jedisHelper.returnJedisObjectToPool(jedis);
                LOGGER.info("History is: " + object.toString());
            }
        } else {
  return object         LOGGER.toStringinfo("Invalid Keys or keys deactivated");
        }
    } catch (JSONException ex) {
        LOGGER.error("JSON Exception: " + ex);
    } catch (NumberFormatException ex) {
        LOGGER.error("NumberFormatException: " + ex);
    } finally {
        jedisHelper.returnJedisObjectToPool(jedis);
        LOGGER.info("History is: " + object.toString());
    }
    return object.toString();
}
    
        private JSONObject getHistoryFromRedis(final String timestamp, final String count, final String channelId, final int type) {
    
            LOGGER.info("get history from redis");
            Set<Tuple> set = null;
            JSONObject history = new JSONObject();
    
            try {
                final Jedis jedis = jedisHelper.getJedisObjectFromPool();
    
                if (type == 1 && timestamp != null && !timestamp.isEmpty() && !"0".equals(timestamp)) {
                    set = jedis.zrangeByScoreWithScores(channelId, timestamp, String.valueOf(System.currentTimeMillis()));
                }
    
                if (type == 2 && !"0".equals(count)) {
                    final int limit = Integer.parseInt(count);
                    set = jedis.zrevrangeByScoreWithScores(channelId, "+inf", "-inf", 0, limit);
                }
    
                if (set != null) {
                    history = new JSONObject(set);
                }
    
                jedisHelper.returnJedisObjectToPool(jedis);
    
            } catch (NumberFormatException ex) {
                LOGGER.error("Exception in returning history from Redis: " + ex);
            }
            return history;
        }
private transient final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(HistoryRetriever.class);
            private transient final JedisHelper jedisHelper = new JedisHelper();
        
            public String retrieveHistory(final JSONObject data) {
        
                final ServletContext context = ContextManager.getContext();
                JSONObject object = new JSONObject();
                final Jedis jedis = jedisHelper.getJedisObjectFromPool();
        
                try {
                    String key1, key2, channel, timestamp, count;
        
                    key1 = data.getString("key1");
                    key2 = data.getString("key2");
                    channel = key2 + "_" + data.getString("channel-id");
                    timestamp = "";
                    count = "";
                    int historyType = 0;
        
                    LOGGER.info("History requested for " + channel + " belonging to subkey: " + key2);
        
                    if (jedisHelper.checkValidity(key1, key2)) {
                        if (data.has("timestamp")) {
                            historyType = 1;
                            timestamp = data.getString("timestamp");
                        }
                        if (data.has("count")) {
                            historyType = 2;
                            count = data.getString("count");
                        }
        
                        ConcurrentHashMap history = (ConcurrentHashMap) context.getAttribute("history");
                        if (null == history) {
                            history = new ConcurrentHashMap<Object, Object>();
                            context.setAttribute("history", history);
        
                            object = getHistoryFromRedis(timestamp, count, channel, historyType);
        
                        } else {
                            ConcurrentSkipListMap<Long, String> myHistory = (ConcurrentSkipListMap<Long, String>) history.get(channel);
        
                            if (null == myHistory) {
                                LOGGER.info("History for this channel not found in Context");
                                myHistory = new ConcurrentSkipListMap<Long, String>();
                                history.put(channel, myHistory);
        
                                object = getHistoryFromRedis(timestamp, count, channel, historyType);
        
                            } else {
                                /* Check for history in context */
                                if (historyType == 1) {
                                    final Map<Long, String> channelHistory = myHistory.tailMap(Long.parseLong(timestamp));
                                    if (!channelHistory.isEmpty()) {
                                        for (Map.Entry<Long, String> entry : channelHistory.entrySet()) {
                                            object.put(String.valueOf(entry.getKey()), entry.getValue());
                                        }
                                    }
                                }
        
                                if (historyType == 2) {
        
                                    final Map<Long, String> channelHistory = myHistory.descendingMap();
        
                                    if (!channelHistory.isEmpty()) {
                                        int counter = 0;
                                        final int i = Integer.parseInt(count);
        
                                        for (Map.Entry<Long, String> entry : channelHistory.entrySet()) {
                                            if (counter >= i) {
                                                break;
                                            }
                                            object.put(String.valueOf(entry.getKey()), entry.getValue());
                                            counter++;
                                        }
                                    }
                            }
                        }
                    }
                } else {
                    LOGGER.info("Invalid Keys or keys deactivated");
                }
            } catch (JSONException ex) {
                LOGGER.error("JSON Exception: " + ex);
            } catch (NumberFormatException ex) {
                LOGGER.error("NumberFormatException: " + ex);
            } finally {
                jedisHelper.returnJedisObjectToPool(jedis);
                LOGGER.info("History is: " + object.toString());
            }
            return object.toString();
        }
    
        private JSONObject getHistoryFromRedis(final String timestamp, final String count, final String channelId, final int type) {
    
            LOGGER.info("get history from redis");
            Set<Tuple> set = null;
            JSONObject history = new JSONObject();
    
            try {
                final Jedis jedis = jedisHelper.getJedisObjectFromPool();
    
                if (type == 1 && timestamp != null && !timestamp.isEmpty() && !"0".equals(timestamp)) {
                    set = jedis.zrangeByScoreWithScores(channelId, timestamp, String.valueOf(System.currentTimeMillis()));
                }
    
                if (type == 2 && !"0".equals(count)) {
                    final int limit = Integer.parseInt(count);
                    set = jedis.zrevrangeByScoreWithScores(channelId, "+inf", "-inf", 0, limit);
                }
    
                if (set != null) {
                    history = new JSONObject(set);
                }
    
                jedisHelper.returnJedisObjectToPool(jedis);
    
            } catch (NumberFormatException ex) {
                LOGGER.error("Exception in returning history from Redis: " + ex);
            }
            return history;
        }
private transient final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(HistoryRetriever.class);
private transient final JedisHelper jedisHelper = new JedisHelper();
        
public String retrieveHistory(final JSONObject data) {
    final ServletContext context = ContextManager.getContext();
    JSONObject object = new JSONObject();
    final Jedis jedis = jedisHelper.getJedisObjectFromPool();
        
    try {
        String key1, key2, channel, timestamp, count;
        
        key1 = data.getString("key1");
        key2 = data.getString("key2");
        channel = key2 + "_" + data.getString("channel-id");
        timestamp = "";
        count = "";
        int historyType = 0;
        
        LOGGER.info("History requested for " + channel + " belonging to subkey: " + key2);
        
        if (jedisHelper.checkValidity(key1, key2)) {
            if (data.has("timestamp")) {
                historyType = 1;
                timestamp = data.getString("timestamp");
            }
            if (data.has("count")) {
                historyType = 2;
                count = data.getString("count");
            }
        
            ConcurrentHashMap history = (ConcurrentHashMap) context.getAttribute("history");
            if (null == history) {
                history = new ConcurrentHashMap<Object, Object>();
                context.setAttribute("history", history);
        
                object = getHistoryFromRedis(timestamp, count, channel, historyType);
            } else {
                ConcurrentSkipListMap<Long, String> myHistory = (ConcurrentSkipListMap<Long, String>) history.get(channel);
        
                if (null == myHistory) {
                    LOGGER.info("History for this channel not found in Context");
                    myHistory = new ConcurrentSkipListMap<Long, String>();
                    history.put(channel, myHistory);
        
                    object = getHistoryFromRedis(timestamp, count, channel, historyType);
                } else {
                    /* Check for history in context */
                    if (historyType == 1) {
                        final Map<Long, String> channelHistory = myHistory.tailMap(Long.parseLong(timestamp));
                        if (!channelHistory.isEmpty()) {
                            for (Map.Entry<Long, String> entry : channelHistory.entrySet()) {
                                object.put(String.valueOf(entry.getKey()), entry.getValue());
                            }
                        }
                    }
        
                    if (historyType == 2) {
                        final Map<Long, String> channelHistory = myHistory.descendingMap();
        
                        if (!channelHistory.isEmpty()) {
                            int counter = 0;
                            final int i = Integer.parseInt(count);
        
                            for (Map.Entry<Long, String> entry : channelHistory.entrySet()) {
                                if (counter >= i) {
                                    break;
                                }
                                object.put(String.valueOf(entry.getKey()), entry.getValue());
                                counter++;
                            }
                        }
                    }
                }
            }
        } else {
            LOGGER.info("Invalid Keys or keys deactivated");
        }
    } catch (JSONException ex) {
        LOGGER.error("JSON Exception: " + ex);
    } catch (NumberFormatException ex) {
        LOGGER.error("NumberFormatException: " + ex);
    } finally {
        jedisHelper.returnJedisObjectToPool(jedis);
        LOGGER.info("History is: " + object.toString());
    }
    return object.toString();
}
    
private JSONObject getHistoryFromRedis(final String timestamp, final String count, final String channelId, final int type) {
    
    LOGGER.info("get history from redis");
    Set<Tuple> set = null;
    JSONObject history = new JSONObject();
    
    try {
        final Jedis jedis = jedisHelper.getJedisObjectFromPool();
    
        if (type == 1 && timestamp != null && !timestamp.isEmpty() && !"0".equals(timestamp)) {
            set = jedis.zrangeByScoreWithScores(channelId, timestamp, String.valueOf(System.currentTimeMillis()));
        }
    
        if (type == 2 && !"0".equals(count)) {
            final int limit = Integer.parseInt(count);
            set = jedis.zrevrangeByScoreWithScores(channelId, "+inf", "-inf", 0, limit);
        }
    
        if (set != null) {
            history = new JSONObject(set);
        }
    
        jedisHelper.returnJedisObjectToPool(jedis);
    
    } catch (NumberFormatException ex) {
        LOGGER.error("Exception in returning history from Redis: " + ex);
    }
    return history;
}
Source Link
Loading