@SuppressWarnings("unchecked") Set<Tuple> rawTupleValues(Set<TypedTuple<V>> values) { if (values == null) { return null; } Set<Tuple> rawTuples = new LinkedHashSet<Tuple>(values.size()); for (TypedTuple<V> value : values) { byte[] rawValue; if (valueSerializer() == null && value.getValue() instanceof byte[]) { rawValue = (byte[]) value.getValue(); } else { rawValue = valueSerializer().serialize(value.getValue()); } rawTuples.add(new DefaultTuple(rawValue, value.getScore())); } return rawTuples; }
@Override public Cursor<TypedTuple<V>> scan(K key, final ScanOptions options) { final byte[] rawKey = rawKey(key); Cursor<Tuple> cursor = execute(new RedisCallback<Cursor<Tuple>>() { @Override public Cursor<Tuple> doInRedis(RedisConnection connection) throws DataAccessException { connection.select(dbIndex); return connection.zScan(rawKey, options); } }, true); return new ConvertingCursor<Tuple, TypedTuple<V>>(cursor, new Converter<Tuple, TypedTuple<V>>() { @Override public TypedTuple<V> convert(Tuple source) { return deserializeTuple(source); } }); }
public void addZSET(String serverName, int dbIndex, String key, double[] scores, String[] members) { RedisTemplate<String, Object> redisTemplate = redisTemplateFactory.getRedisTemplate(serverName); redisConnectionDbIndex.set(dbIndex); Set<TypedTuple<Object>> zset = new HashSet<TypedTuple<Object>>(); for(int i=0;i<members.length;i++) { final Object ob = members[i]; final double sc = scores[i]; zset.add(new TypedTuple () { private Object v; private double score; { v = ob; score = sc; } @Override public int compareTo(Object o) { if(o == null) return 1; if(o instanceof TypedTuple) { TypedTuple tto = (TypedTuple) o; return this.getScore()-tto.getScore() >= 0?1:-1; } return 1; } @Override public Object getValue() { return v; } @Override public Double getScore() { return score; } }); } redisTemplate.opsForZSet().add(key, zset); }
@Override public int compareTo(TypedTuple<V> o) { if (o == null) { return compareTo(Double.valueOf(0)); } return compareTo(o.getScore()); }
Set<TypedTuple<V>> deserializeTupleValues(Collection<Tuple> rawValues) { if (rawValues == null) { return null; } Set<TypedTuple<V>> set = new LinkedHashSet<TypedTuple<V>>(rawValues.size()); for (Tuple rawValue : rawValues) { set.add(deserializeTuple(rawValue)); } return set; }
@SuppressWarnings({ "unchecked", "rawtypes" }) TypedTuple<V> deserializeTuple(Tuple tuple) { Object value = tuple.getValue(); if (valueSerializer() != null) { value = valueSerializer().deserialize(tuple.getValue()); } return new DefaultTypedTuple(value, tuple.getScore()); }
public Long add(K key, Set<TypedTuple<V>> tuples) { final byte[] rawKey = rawKey(key); final Set<Tuple> rawValues = rawTupleValues(tuples); return execute(new RedisCallback<Long>() { public Long doInRedis(RedisConnection connection) { connection.select(dbIndex); return connection.zAdd(rawKey, rawValues); } }, true); }
public Set<TypedTuple<V>> rangeWithScores(K key, final long start, final long end) { final byte[] rawKey = rawKey(key); Set<Tuple> rawValues = execute(new RedisCallback<Set<Tuple>>() { public Set<Tuple> doInRedis(RedisConnection connection) { connection.select(dbIndex); return connection.zRangeWithScores(rawKey, start, end); } }, true); return deserializeTupleValues(rawValues); }
public Set<TypedTuple<V>> reverseRangeWithScores(K key, final long start, final long end) { final byte[] rawKey = rawKey(key); Set<Tuple> rawValues = execute(new RedisCallback<Set<Tuple>>() { public Set<Tuple> doInRedis(RedisConnection connection) { connection.select(dbIndex); return connection.zRevRangeWithScores(rawKey, start, end); } }, true); return deserializeTupleValues(rawValues); }
public Set<TypedTuple<V>> rangeByScoreWithScores(K key, final double min, final double max) { final byte[] rawKey = rawKey(key); Set<Tuple> rawValues = execute(new RedisCallback<Set<Tuple>>() { public Set<Tuple> doInRedis(RedisConnection connection) { connection.select(dbIndex); return connection.zRangeByScoreWithScores(rawKey, min, max); } }, true); return deserializeTupleValues(rawValues); }
public Set<TypedTuple<V>> rangeByScoreWithScores(K key, final double min, final double max, final long offset, final long count) { final byte[] rawKey = rawKey(key); Set<Tuple> rawValues = execute(new RedisCallback<Set<Tuple>>() { public Set<Tuple> doInRedis(RedisConnection connection) { connection.select(dbIndex); return connection.zRangeByScoreWithScores(rawKey, min, max, offset, count); } }, true); return deserializeTupleValues(rawValues); }
public Set<TypedTuple<V>> reverseRangeByScoreWithScores(K key, final double min, final double max) { final byte[] rawKey = rawKey(key); Set<Tuple> rawValues = execute(new RedisCallback<Set<Tuple>>() { public Set<Tuple> doInRedis(RedisConnection connection) { connection.select(dbIndex); return connection.zRevRangeByScoreWithScores(rawKey, min, max); } }, true); return deserializeTupleValues(rawValues); }
public Set<TypedTuple<V>> reverseRangeByScoreWithScores(K key, final double min, final double max, final long offset, final long count) { final byte[] rawKey = rawKey(key); Set<Tuple> rawValues = execute(new RedisCallback<Set<Tuple>>() { public Set<Tuple> doInRedis(RedisConnection connection) { connection.select(dbIndex); return connection.zRevRangeByScoreWithScores(rawKey, min, max, offset, count); } }, true); return deserializeTupleValues(rawValues); }
public static Object creatZSetValue(Set<TypedTuple<Object>> values) { List<RValue> rValues = new ArrayList<RValue>(); for(TypedTuple<Object> v:values) { rValues.add(new RValue(v.getScore(), v.getValue())); } return rValues; }
@Override public List<AutocompleteData> complete(String word, double min, double max, int offset) { Assert.hasLength(word, "Word cannot be empty or null"); String trimedWord = word.trim(); int trimedWordLength = trimedWord.length(); String key = keyRepository.getKey(trimedWord); List<AutocompleteData> autocompletes = new ArrayList<>(); for (int i = trimedWordLength; i < offset; i++) { if (autocompletes.size() == offset) break; Set<TypedTuple<String>> rangeResultsWithScore = stringRedisTemplate .opsForZSet() .reverseRangeByScoreWithScores(key + i, min, max, 0, offset); if (rangeResultsWithScore.isEmpty()) continue; for (TypedTuple<String> typedTuple : rangeResultsWithScore) { if (autocompletes.size() == offset) break; String value = typedTuple.getValue(); int minLength = Math.min(value.length(), trimedWordLength); if (!value.endsWith(DEFAULT_DELIMITER) || !value.startsWith(trimedWord.substring(0, minLength))) continue; autocompletes.add(new AutocompleteData(value.replace(DEFAULT_DELIMITER, ""), typedTuple.getScore().intValue())); } } Collections.sort(autocompletes); return autocompletes; }
public Object getZSET(String serverName, int dbIndex, String key) { RedisTemplate<String, Object> redisTemplate = redisTemplateFactory.getRedisTemplate(serverName); redisConnectionDbIndex.set(dbIndex); Set<TypedTuple<Object>> values = redisTemplate.opsForZSet().rangeWithScores(key, 0, 1000); return RValue.creatZSetValue(values); }
public Long add(Set<TypedTuple<V>> tuples) { return ops.add(getKey(), tuples); }
public Set<TypedTuple<V>> rangeByScoreWithScores(double min, double max) { return ops.rangeByScoreWithScores(getKey(), min, max); }
public Set<TypedTuple<V>> rangeWithScores(long start, long end) { return ops.rangeWithScores(getKey(), start, end); }
public Set<TypedTuple<V>> reverseRangeByScoreWithScores(double min, double max) { return ops.reverseRangeByScoreWithScores(getKey(), min, max); }
public Set<TypedTuple<V>> reverseRangeWithScores(long start, long end) { return ops.reverseRangeWithScores(getKey(), start, end); }
@Override public Cursor<TypedTuple<V>> scan(ScanOptions options) { return ops.scan(getKey(), options); }
public Long putZSetAll(String key,Set<TypedTuple<Object>> tuples) throws CacheAccessException{ try { return opsForZSet().add(key, tuples); } catch (Exception e) { logger.error(e.getMessage(),e); throw new CacheAccessException(ErrorCode.CONNECTION_CACHE_ERROR,e); } }
public Set<TypedTuple<Object>> getZSet(String key,long start,long end) throws CacheAccessException{ try { return opsForZSet().rangeWithScores(key, start, end); } catch (Exception e) { logger.error(e.getMessage(),e); throw new CacheAccessException(ErrorCode.CONNECTION_CACHE_ERROR,e); } }
public Set<TypedTuple<Object>> getZSetReverse(String key,long start,long end) throws CacheAccessException{ try { return opsForZSet().reverseRangeWithScores(key, start, end); } catch (Exception e) { logger.error(e.getMessage(),e); throw new CacheAccessException(ErrorCode.CONNECTION_CACHE_ERROR,e); } }
public Set<TypedTuple<Object>> getZSetByScore(String key,double min,double max) throws CacheAccessException{ try { return opsForZSet().rangeByScoreWithScores(key, min, max); } catch (Exception e) { logger.error(e.getMessage(),e); throw new CacheAccessException(ErrorCode.CONNECTION_CACHE_ERROR,e); } }
public Set<TypedTuple<Object>> getZSetByScoreReverse(String key,double min,double max) throws CacheAccessException{ try { return opsForZSet().reverseRangeByScoreWithScores(key, min, max); } catch (Exception e) { logger.error(e.getMessage(),e); throw new CacheAccessException(ErrorCode.CONNECTION_CACHE_ERROR,e); } }