@Override @SuppressWarnings("unchecked") public RedisValue<List<String>> sort(final CacheKey cacheKey, final String key, final SortingParams sortingParameters) { Collection<byte[]> params = sortingParameters.getParams(); String[] paramList = new String[params.size() + 1]; paramList[0] = key; int i = 1; for (byte[] p : params) { try { paramList[i++] = new String(p, "UTF-8"); } catch (Exception e) { throw new RuntimeException(e); } } final String localKey = concat("sort", paramList); RedisValue<?> res = cacheWrapper.get(cacheKey, localKey, new Callable<RedisValue<?>>() { @Override public RedisValue<?> call() throws Exception { return provider.sort(cacheKey, key, sortingParameters); } }); return (RedisValue<List<String>>) res; }
@Override public RedisValue<List<String>> sort(final CacheKey cacheKey, final String key, final SortingParams sortingParameters) { Collection<byte[]> params = sortingParameters.getParams(); String[] actualParams = new String[params.size() + 1]; actualParams[0] = key; int i = 1; for (byte[] p : params) { try { actualParams[i++] = new String(p, "UTF-8"); } catch (Exception e) { throw new RuntimeException(e); } } concat("sort", actualParams); RedisValue<List<String>> valueImpl = new RedisValue<List<String>>(); appendCommand(new PipelineCommand<List<String>>(valueImpl) { @Override public Response<List<String>> run() { return ((RedisValue<List<String>>) pipelineProvider.sort(cacheKey, key, sortingParameters)).getResponse(); } }); return valueImpl; }
@Test public void shouldReturnEmptyQueryOnEmptyResultOfSort() throws SyntaxError, IOException { when(localParamsMock.get("command")).thenReturn("sort"); when(localParamsMock.get("key")).thenReturn("simpleKey"); when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field"); when(jedisMock.sort(anyString(), any(SortingParams.class))).thenReturn(new ArrayList<String>()); when(requestMock.getSchema()).thenReturn(schema); when(schema.getQueryAnalyzer()).thenReturn(new StandardAnalyzer()); redisQParser = new RedisQParser("string_field", localParamsMock, paramsMock, requestMock, commandHandler); final Query query = redisQParser.parse(); final ArgumentCaptor<SortingParams> argument = ArgumentCaptor.forClass(SortingParams.class); verify(jedisMock).sort(eq("simpleKey"), argument.capture()); Assert.assertEquals(getSortingParamString(new SortingParams()), getSortingParamString(argument.getValue())); IndexSearcher searcher = new IndexSearcher(new MultiReader()); final Set<Term> terms = extractTerms(searcher, query); Assert.assertEquals(0, terms.size()); }
@Test public void shouldAddTermsFromSort() throws SyntaxError, IOException { when(localParamsMock.get("command")).thenReturn("sort"); when(localParamsMock.get("key")).thenReturn("simpleKey"); when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field"); when(jedisMock.sort(anyString(), any(SortingParams.class))).thenReturn(Arrays.asList("123", "321")); when(requestMock.getSchema()).thenReturn(schema); when(schema.getQueryAnalyzer()).thenReturn(new StandardAnalyzer()); redisQParser = new RedisQParser("string_field", localParamsMock, paramsMock, requestMock, commandHandler); final Query query = redisQParser.parse(); final ArgumentCaptor<SortingParams> argument = ArgumentCaptor.forClass(SortingParams.class); verify(jedisMock).sort(eq("simpleKey"), argument.capture()); Assert.assertEquals(getSortingParamString(new SortingParams()), getSortingParamString(argument.getValue())); IndexSearcher searcher = new IndexSearcher(new MultiReader()); final Set<Term> terms = extractTerms(searcher, query); Assert.assertEquals(2, terms.size()); }
@Test public void shouldAddTermsFromSortAlgorithmAlpha() throws SyntaxError, IOException { when(localParamsMock.get("command")).thenReturn("sort"); when(localParamsMock.get("key")).thenReturn("simpleKey"); when(localParamsMock.get("algorithm")).thenReturn("alpha"); when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field"); when(jedisMock.sort(anyString(), any(SortingParams.class))).thenReturn(Arrays.asList("123", "321")); when(requestMock.getSchema()).thenReturn(schema); when(schema.getQueryAnalyzer()).thenReturn(new StandardAnalyzer()); redisQParser = new RedisQParser("string_field", localParamsMock, paramsMock, requestMock, commandHandler); final Query query = redisQParser.parse(); final ArgumentCaptor<SortingParams> argument = ArgumentCaptor.forClass(SortingParams.class); verify(jedisMock).sort(eq("simpleKey"), argument.capture()); Assert.assertEquals(getSortingParamString(new SortingParams().alpha()), getSortingParamString(argument.getValue())); IndexSearcher searcher = new IndexSearcher(new MultiReader()); final Set<Term> terms = extractTerms(searcher, query); Assert.assertEquals(2, terms.size()); }
@Test public void shouldAddTermsFromSortOrderAsc() throws SyntaxError, IOException { when(localParamsMock.get("command")).thenReturn("sort"); when(localParamsMock.get("key")).thenReturn("simpleKey"); when(localParamsMock.get("order")).thenReturn("asc"); when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field"); when(jedisMock.sort(anyString(), any(SortingParams.class))).thenReturn(Arrays.asList("123", "321")); when(requestMock.getSchema()).thenReturn(schema); when(schema.getQueryAnalyzer()).thenReturn(new StandardAnalyzer()); redisQParser = new RedisQParser("string_field", localParamsMock, paramsMock, requestMock, commandHandler); final Query query = redisQParser.parse(); final ArgumentCaptor<SortingParams> argument = ArgumentCaptor.forClass(SortingParams.class); verify(jedisMock).sort(eq("simpleKey"), argument.capture()); Assert.assertEquals(getSortingParamString(new SortingParams().asc()), getSortingParamString(argument.getValue())); IndexSearcher searcher = new IndexSearcher(new MultiReader()); final Set<Term> terms = extractTerms(searcher, query); Assert.assertEquals(2, terms.size()); }
@Test public void shouldAddTermsFromSortOrderDesc() throws SyntaxError, IOException { when(localParamsMock.get("command")).thenReturn("sort"); when(localParamsMock.get("key")).thenReturn("simpleKey"); when(localParamsMock.get("order")).thenReturn("desc"); when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field"); when(jedisMock.sort(anyString(), any(SortingParams.class))).thenReturn(Arrays.asList("123", "321")); when(requestMock.getSchema()).thenReturn(schema); when(schema.getQueryAnalyzer()).thenReturn(new StandardAnalyzer()); redisQParser = new RedisQParser("string_field", localParamsMock, paramsMock, requestMock, commandHandler); final Query query = redisQParser.parse(); final ArgumentCaptor<SortingParams> argument = ArgumentCaptor.forClass(SortingParams.class); verify(jedisMock).sort(eq("simpleKey"), argument.capture()); Assert.assertEquals(getSortingParamString(new SortingParams().desc()), getSortingParamString(argument.getValue())); IndexSearcher searcher = new IndexSearcher(new MultiReader()); final Set<Term> terms = extractTerms(searcher, query); Assert.assertEquals(2, terms.size()); }
@Test public void shouldAddTermsFromSortLimit() throws SyntaxError, IOException { when(localParamsMock.get("command")).thenReturn("sort"); when(localParamsMock.get("key")).thenReturn("simpleKey"); when(localParamsMock.get("limit")).thenReturn("100"); when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field"); when(jedisMock.sort(anyString(), any(SortingParams.class))).thenReturn(Arrays.asList("123", "321")); when(requestMock.getSchema()).thenReturn(schema); when(schema.getQueryAnalyzer()).thenReturn(new StandardAnalyzer()); redisQParser = new RedisQParser("string_field", localParamsMock, paramsMock, requestMock, commandHandler); final Query query = redisQParser.parse(); final ArgumentCaptor<SortingParams> argument = ArgumentCaptor.forClass(SortingParams.class); verify(jedisMock).sort(eq("simpleKey"), argument.capture()); Assert.assertEquals(getSortingParamString(new SortingParams().limit(0, 100)), getSortingParamString(argument.getValue())); IndexSearcher searcher = new IndexSearcher(new MultiReader()); final Set<Term> terms = extractTerms(searcher, query); Assert.assertEquals(2, terms.size()); }
@Test public void shouldAddTermsFromSortOffset() throws SyntaxError, IOException { when(localParamsMock.get("command")).thenReturn("sort"); when(localParamsMock.get("key")).thenReturn("simpleKey"); when(localParamsMock.get("offset")).thenReturn("100"); when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field"); when(jedisMock.sort(anyString(), any(SortingParams.class))).thenReturn(Arrays.asList("123", "321")); when(requestMock.getSchema()).thenReturn(schema); when(schema.getQueryAnalyzer()).thenReturn(new StandardAnalyzer()); redisQParser = new RedisQParser("string_field", localParamsMock, paramsMock, requestMock, commandHandler); final Query query = redisQParser.parse(); final ArgumentCaptor<SortingParams> argument = ArgumentCaptor.forClass(SortingParams.class); verify(jedisMock).sort(eq("simpleKey"), argument.capture()); Assert.assertEquals(getSortingParamString(new SortingParams().limit(100, 0)), getSortingParamString(argument.getValue())); IndexSearcher searcher = new IndexSearcher(new MultiReader()); final Set<Term> terms = extractTerms(searcher, query); Assert.assertEquals(2, terms.size()); }
@Test public void shouldAddTermsFromSortLimitAndOffset() throws SyntaxError, IOException { when(localParamsMock.get("command")).thenReturn("sort"); when(localParamsMock.get("key")).thenReturn("simpleKey"); when(localParamsMock.get("offset")).thenReturn("100"); when(localParamsMock.get("limit")).thenReturn("1000"); when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field"); when(jedisMock.sort(anyString(), any(SortingParams.class))).thenReturn(Arrays.asList("123", "321")); when(requestMock.getSchema()).thenReturn(schema); when(schema.getQueryAnalyzer()).thenReturn(new StandardAnalyzer()); redisQParser = new RedisQParser("string_field", localParamsMock, paramsMock, requestMock, commandHandler); final Query query = redisQParser.parse(); final ArgumentCaptor<SortingParams> argument = ArgumentCaptor.forClass(SortingParams.class); verify(jedisMock).sort(eq("simpleKey"), argument.capture()); Assert.assertEquals(getSortingParamString(new SortingParams().limit(100, 1000)), getSortingParamString(argument.getValue())); IndexSearcher searcher = new IndexSearcher(new MultiReader()); final Set<Term> terms = extractTerms(searcher, query); Assert.assertEquals(2, terms.size()); }
@Test public void shouldAddTermsFromSortWithByClause() throws SyntaxError, IOException { when(localParamsMock.get("command")).thenReturn("sort"); when(localParamsMock.get("key")).thenReturn("simpleKey"); when(localParamsMock.get("by")).thenReturn("foo_*"); when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field"); when(jedisMock.sort(anyString(), any(SortingParams.class))).thenReturn(Arrays.asList("123", "321")); when(requestMock.getSchema()).thenReturn(schema); when(schema.getQueryAnalyzer()).thenReturn(new StandardAnalyzer()); redisQParser = new RedisQParser("string_field", localParamsMock, paramsMock, requestMock, commandHandler); final Query query = redisQParser.parse(); final ArgumentCaptor<SortingParams> argument = ArgumentCaptor.forClass(SortingParams.class); verify(jedisMock).sort(eq("simpleKey"), argument.capture()); Assert.assertEquals(getSortingParamString(new SortingParams().by("foo_*")), getSortingParamString(argument.getValue())); IndexSearcher searcher = new IndexSearcher(new MultiReader()); final Set<Term> terms = extractTerms(searcher, query); Assert.assertEquals(2, terms.size()); }
@Test public void shouldAddTermsFromSortWithSingleGetClause() throws SyntaxError, IOException { when(localParamsMock.get("command")).thenReturn("sort"); when(localParamsMock.get("key")).thenReturn("simpleKey"); when(localParamsMock.get("get")).thenReturn("get_*"); when(localParamsMock.getParameterNamesIterator()).thenReturn(Arrays.asList("command", "key", "get").iterator()); when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field"); when(jedisMock.sort(anyString(), any(SortingParams.class))).thenReturn(Arrays.asList("123", "321")); when(requestMock.getSchema()).thenReturn(schema); when(schema.getQueryAnalyzer()).thenReturn(new StandardAnalyzer()); redisQParser = new RedisQParser("string_field", localParamsMock, paramsMock, requestMock, commandHandler); final Query query = redisQParser.parse(); final ArgumentCaptor<SortingParams> argument = ArgumentCaptor.forClass(SortingParams.class); verify(jedisMock).sort(eq("simpleKey"), argument.capture()); Assert.assertEquals(getSortingParamString(new SortingParams().get("get_*")), getSortingParamString(argument.getValue())); IndexSearcher searcher = new IndexSearcher(new MultiReader()); final Set<Term> terms = extractTerms(searcher, query); Assert.assertEquals(2, terms.size()); }
@Test public void sortDesc() { jedis.lpush("foo", "3"); jedis.lpush("foo", "2"); jedis.lpush("foo", "1"); SortingParams sp = new SortingParams(); sp.desc(); List<String> result = jedis.sort("foo", sp); List<String> expected = new ArrayList<String>(); expected.add("3"); expected.add("2"); expected.add("1"); assertEquals(expected, result); // Binary jedis.lpush(bfoo, b3); jedis.lpush(bfoo, b2); jedis.lpush(bfoo, b1); SortingParams bsp = new SortingParams(); bsp.desc(); List<byte[]> bresult = jedis.sort(bfoo, bsp); List<byte[]> bexpected = new ArrayList<byte[]>(); bexpected.add(b3); bexpected.add(b2); bexpected.add(b1); assertByteArrayListEquals(bexpected, bresult); }
@Test public void sortLimit() { for (int n = 10; n > 0; n--) { jedis.lpush("foo", String.valueOf(n)); } SortingParams sp = new SortingParams(); sp.limit(0, 3); List<String> result = jedis.sort("foo", sp); List<String> expected = new ArrayList<String>(); expected.add("1"); expected.add("2"); expected.add("3"); assertEquals(expected, result); // Binary jedis.rpush(bfoo, new byte[] { (byte) '4' }); jedis.rpush(bfoo, new byte[] { (byte) '3' }); jedis.rpush(bfoo, new byte[] { (byte) '2' }); jedis.rpush(bfoo, new byte[] { (byte) '1' }); SortingParams bsp = new SortingParams(); bsp.limit(0, 3); List<byte[]> bresult = jedis.sort(bfoo, bsp); List<byte[]> bexpected = new ArrayList<byte[]>(); bexpected.add(b1); bexpected.add(b2); bexpected.add(b3); assertByteArrayListEquals(bexpected, bresult); }
@Test public void sortAlpha() { jedis.lpush("foo", "1"); jedis.lpush("foo", "2"); jedis.lpush("foo", "10"); SortingParams sp = new SortingParams(); sp.alpha(); List<String> result = jedis.sort("foo", sp); List<String> expected = new ArrayList<String>(); expected.add("1"); expected.add("10"); expected.add("2"); assertEquals(expected, result); // Binary jedis.lpush(bfoo, b1); jedis.lpush(bfoo, b2); jedis.lpush(bfoo, b10); SortingParams bsp = new SortingParams(); bsp.alpha(); List<byte[]> bresult = jedis.sort(bfoo, bsp); List<byte[]> bexpected = new ArrayList<byte[]>(); bexpected.add(b1); bexpected.add(b10); bexpected.add(b2); assertByteArrayListEquals(bexpected, bresult); }
/** * 返回或保存给定列表、集合、有序集合 key 中经过排序的元素。 * 排序默认以数字作为对象,值被解释为双精度浮点数,然后进行比较。 */ public <E> List<E> sort(Object key, SortingParams sortingParameters) { if (null == key) { return null; } if (cluster) { return convertBytesCollectionToList_deserializeVal(jedisCluster.sort(serializeKey(key), sortingParameters)); } else { return convertBytesCollectionToList_deserializeVal(jedisOperator.sort(serializeKey(key), sortingParameters)); } }
public List<String> sort(final String key, final SortingParams sortingParameters) { return execute(new JedisExecutor<List<String>>() { @Override List<String> doInJedis(Jedis jedis) { return jedis.sort(key, sortingParameters); } }); }
public List<byte[]> sort(final byte[] key, final SortingParams sortingParameters) { return execute(new JedisExecutor<List<byte[]>>() { @Override List<byte[]> doInJedis(Jedis jedis) { return jedis.sort(key, sortingParameters); } }); }
public final List<String> sort(final String key, final SortingParams sortingParameters) { return jedisTemplate.run(key, new Executor<List<String>>() { public List<String> execute(ShardedJedis jedis) { return jedis.sort(key, sortingParameters); } }); }
public final List<byte[]> sort(final byte[] key, final SortingParams sortingParameters) { return jedisTemplate.run(key, new Executor<List<byte[]>>() { public List<byte[]> execute(ShardedJedis jedis) { return jedis.sort(key, sortingParameters); } }); }
public static final List<String> sort(final String key, final SortingParams sortingParameters) { return JedisTemplate.run(key, new Executor<List<String>>() { public List<String> execute(ShardedJedis jedis) { return jedis.sort(key, sortingParameters); } }); }
public static final List<byte[]> sort(final byte[] key, final SortingParams sortingParameters) { return JedisTemplate.run(key, new Executor<List<byte[]>>() { public List<byte[]> execute(ShardedJedis jedis) { return jedis.sort(key, sortingParameters); } }); }
@Override public List<String> sort(String key, SortingParams sortingParameters) { Span span = helper.buildSpan("sort", key); span.setTag("sortingParameters", TracingHelper.toString(sortingParameters.getParams())); try { return super.sort(key, sortingParameters); } catch (Exception e) { onError(e, span); throw e; } finally { span.finish(); } }
@Override public Long sort(String key, SortingParams sortingParameters, String dstkey) { Span span = helper.buildSpan("sort", key); span.setTag("sortingParameters", TracingHelper.toString(sortingParameters.getParams())); span.setTag("dstkey", dstkey); try { return super.sort(key, sortingParameters, dstkey); } catch (Exception e) { onError(e, span); throw e; } finally { span.finish(); } }
@Override public List<byte[]> sort(byte[] key, SortingParams sortingParameters) { Span span = helper.buildSpan("sort", key); span.setTag("sortingParameters", TracingHelper.toString(sortingParameters.getParams())); try { return super.sort(key, sortingParameters); } catch (Exception e) { onError(e, span); throw e; } finally { span.finish(); } }
@Override public Long sort(byte[] key, SortingParams sortingParameters, byte[] dstkey) { Span span = helper.buildSpan("sort", key); span.setTag("sortingParameters", TracingHelper.toString(sortingParameters.getParams())); span.setTag("dstkey", Arrays.toString(dstkey)); try { return super.sort(key, sortingParameters, dstkey); } catch (Exception e) { onError(e, span); throw e; } finally { span.finish(); } }
public List<String> sort(final String key, final SortParams<SortingParams> params) { return (List<String>) execute(new RedisCallback<Jedis>() { public Object doInRedis(Jedis redis) { return redis.sort(key, params.getParamList().get(0)); } }); }
public void sortstore(final String key, final String destKey, final SortParams<SortingParams> params) { execute(new RedisCallback<Jedis>() { public Object doInRedis(Jedis redis) { redis.sort(key, params.getParamList().get(0), destKey); return null; } }); }
@SuppressWarnings("unchecked") @Override public List<String> sort(final String key, final SortingParams sortingParameters) { return (List<String>) this.execute(new Invoker() { @Override public Object execute(Jedis jedis) { return jedis.sort(key, sortingParameters); } }); }
@Test public void sortDesc() { jedis.lpush("foo", "3"); jedis.lpush("foo", "2"); jedis.lpush("foo", "1"); SortingParams sp = new SortingParams(); sp.desc(); List<String> result = jedis.sort("foo", sp); List<String> expected = new ArrayList<String>(); expected.add("3"); expected.add("2"); expected.add("1"); assertEquals(expected, result); // Binary jedis.lpush(bfoo, b3); jedis.lpush(bfoo, b2); jedis.lpush(bfoo, b1); SortingParams bsp = new SortingParams(); bsp.desc(); List<byte[]> bresult = jedis.sort(bfoo, bsp); List<byte[]> bexpected = new ArrayList<byte[]>(); bexpected.add(b3); bexpected.add(b2); bexpected.add(b1); assertEquals(bexpected, bresult); }
@Test public void sortLimit() { for (int n = 10; n > 0; n--) { jedis.lpush("foo", String.valueOf(n)); } SortingParams sp = new SortingParams(); sp.limit(0, 3); List<String> result = jedis.sort("foo", sp); List<String> expected = new ArrayList<String>(); expected.add("1"); expected.add("2"); expected.add("3"); assertEquals(expected, result); // Binary jedis.rpush(bfoo, new byte[] { (byte) '4' }); jedis.rpush(bfoo, new byte[] { (byte) '3' }); jedis.rpush(bfoo, new byte[] { (byte) '2' }); jedis.rpush(bfoo, new byte[] { (byte) '1' }); SortingParams bsp = new SortingParams(); bsp.limit(0, 3); List<byte[]> bresult = jedis.sort(bfoo, bsp); List<byte[]> bexpected = new ArrayList<byte[]>(); bexpected.add(b1); bexpected.add(b2); bexpected.add(b3); assertEquals(bexpected, bresult); }
@Test public void sortAlpha() { jedis.lpush("foo", "1"); jedis.lpush("foo", "2"); jedis.lpush("foo", "10"); SortingParams sp = new SortingParams(); sp.alpha(); List<String> result = jedis.sort("foo", sp); List<String> expected = new ArrayList<String>(); expected.add("1"); expected.add("10"); expected.add("2"); assertEquals(expected, result); // Binary jedis.lpush(bfoo, b1); jedis.lpush(bfoo, b2); jedis.lpush(bfoo, b10); SortingParams bsp = new SortingParams(); bsp.alpha(); List<byte[]> bresult = jedis.sort(bfoo, bsp); List<byte[]> bexpected = new ArrayList<byte[]>(); bexpected.add(b1); bexpected.add(b10); bexpected.add(b2); assertEquals(bexpected, bresult); }
@Override public RedisValue<List<String>> sort(CacheKey cacheKey, String key, SortingParams sortingParameters) { if (DEBUG_REDIS_CACHE) { logCall("sort", key, sortingParameters.toString()); } Jedis jedis = pool.getResource(); try { return (new RedisValue<List<String>>()).setValue(jedis.sort(key, sortingParameters)); } finally { if (jedis != null) { jedis.close(); }; } }