A simple Java prettyPrint() method for JSON
Download JAR file : link ( It required to import JSONSimple library )
Download JAR file : link ( It required to import JSONSimple library )
"Redis is an open source, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets and sorted sets.
You can run atomic operations on these types, like appending to a string; incrementing the value in a hash; pushing to a list; computing set intersection, union and difference; or getting the member with highest ranking in a sorted set.In order to achieve its outstanding performance, Redis works with an in-memory dataset. Depending on your use case, you can persist it either by dumping the dataset to disk every once in a while, or by appending each command to a log." [link]
"Redis Sorted Sets are, similarly to Redis Sets, non repeating collections of Strings. The difference is that every member of a Sorted Set is associated with score, that is used in order to take the sorted set ordered, from the smallest to the greatest score. While members are unique, scores may be repeated."
public class PojoClass { private Long id; private int number; private String text; /* getters and setters omitted */ }
public class Utility { public static String getPojoName( Object pojo ) { return pojo.getClass().getName(); } public static String getId( Object pojo ) { try { return ""+pojo.getClass().getField("id").get(pojo); } catch (Exception ignored) { } return null; } public static String getPojoId( Object pojo ) { return getPojoName(pojo)+"_"+getId(pojo); } }
public static double calculateScore(Object value) { Class<?> valueClass = value.getClass(); Double score = 0.0; if ( valueClass.equals(Short.class) || valueClass.equals(Integer.class) || valueClass.equals(Long.class) || valueClass.equals(Float.class) || valueClass.equals(Double.class) ){ score = Double.parseDouble(""+value); } else if ( valueClass.equals(Character.class) ){ score = ((Integer)value).doubleValue() ; } else if ( valueClass.equals(Boolean.class) ){ score = ((Boolean)value)? 1.0 : 0.0; } else if ( valueClass.equals(Byte.class) ){ score = ((Byte)value).doubleValue(); } if ( valueClass.equals(String.class) ){ score = Double.parseDouble(""+((String)value).hashCode()); } return score; }
findEntries() is useful to retrive entries with a specific field value:public void persistEntry(String pojoName, String fieldName, Object fieldValue, Jedis jedis) { if (!value.getClass().equals(String.class)) { // if String.class : value must be persisted in index entry jedis.zadd(pojoName+":"+fieldName, this.calculateScore(fieldValue), value); } else { jedis.zadd(pojoName+":"+fieldName, this.calculateScore(fieldValue), ""); } }
public Set<String> findEntries(String pojoName, String fieldName, Object fieldValue, Jedis jedis) {
String key = pojoName+":"+fieldName; double minmaxScore = this.calculateScore(fieldValue); Set<String >keys = new HashSet<String>(); if (fieldValue instanceof String) { Set<String> keys_tmp = jedis.zrangeByScore(key, minmaxScore, minmaxScore); for (String k : keys_tmp) { String[] valueAndReferredEntity = k.split("\\$\\$\\$"); if (valueAndReferredEntity[0].equals(""+fieldValue)) { keys.add(valueAndReferredEntity[1]); } } } else { keys = jedis.zrangeByScore(key, minmaxScore, minmaxScore); } return keys; }
public void removeEntry(String remId, String remPojoName, String fieldName, Object fieldValue, Jedis jedis) { Set<String> keys = null; String key = remPojoName+":"+fieldName; double minmaxScore = Utility.calculateScore(fieldValue); keys = new HashSet<String>(); if (fieldValue instanceof String) { keys = jedis.zrangeByScore(key, minmaxScore, minmaxScore); for (String k : keys) { String[] valueAndReferredEntity = k.split("\\$\\$\\$"); if (valueAndReferredEntity[0].equals(""+fieldValue)) { jedis.zrem(key, k); } } } else { keys = jedis.zrangeByScore(key, minmaxScore, minmaxScore); for (String k : keys) { if (k.equals(""+remId)) { jedis.zrem(key, k); } } } }