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;
}