Binary Sorting fixed, and under U tests.

This commit is contained in:
Yaourt
2010-11-07 18:10:07 +01:00
parent 3281bafd2e
commit 4a71dff006
2 changed files with 199 additions and 1 deletions

View File

@@ -26,8 +26,25 @@ public class SortingParams {
* @return the SortingParams Object
*/
public SortingParams by(final String pattern) {
return by(pattern.getBytes(Protocol.UTF8));
}
/**
* Sort by weight in keys.
* <p>
* Takes a pattern that is used in order to generate the key names of the
* weights used for sorting. Weight key names are obtained substituting the
* first occurrence of * with the actual value of the elements on the list.
* <p>
* The pattern for a normal key/value pair is "keyname*" and for a value in
* a hash "keyname*->fieldname".
*
* @param pattern
* @return the SortingParams Object
*/
public SortingParams by(final byte[] pattern) {
params.add(BY.raw);
params.add(pattern.getBytes(Protocol.UTF8));
params.add(pattern);
return this;
}
@@ -117,4 +134,27 @@ public class SortingParams {
}
return this;
}
/**
* Retrieving external keys from the result of the search.
* <p>
* Takes a pattern that is used in order to generate the key names of the
* result of sorting. The key names are obtained substituting the first
* occurrence of * with the actual value of the elements on the list.
* <p>
* The pattern for a normal key/value pair is "keyname*" and for a value in
* a hash "keyname*->fieldname".
* <p>
* To get the list itself use the char # as pattern.
*
* @param patterns
* @return the SortingParams Object
*/
public SortingParams get(byte[]... patterns) {
for (final byte[] pattern : patterns) {
params.add(GET.raw);
params.add(pattern);
}
return this;
}
}

View File

@@ -6,8 +6,29 @@ import java.util.List;
import org.junit.Test;
import redis.clients.jedis.SortingParams;
import redis.clients.jedis.tests.JedisTest;
public class SortingCommandsTest extends JedisCommandTestBase {
final byte[] bfoo = {0x01, 0x02, 0x03, 0x04};
// final byte[] bfoo = {'b', 'f', 'o', 'o'};
final byte[] bbar1 = {0x05, 0x06, 0x07, 0x08, '1'};
final byte[] bbar2 = {0x05, 0x06, 0x07, 0x08, '2'};
final byte[] bbar3 = {0x05, 0x06, 0x07, 0x08, '3'};
final byte[] bbar10 = {0x05, 0x06, 0x07, 0x08, '1', '0'};
final byte[] bbarstar = {0x05, 0x06, 0x07, 0x08, '*'};
// final byte[] bbar1 = {'b', 'b', 'a', 'r', '1'};
// final byte[] bbar2 = {'b', 'b', 'a', 'r', '2'};
// final byte[] bbar3 = {'b', 'b', 'a', 'r', '3'};
// final byte[] bbarstar = {'b', 'b', 'a', 'r', '*'};
// final byte[] bbarstar = {0x05, 0x06, 0x07, 0x08, '*'};
final byte[] bcar1 = {0x0A, 0x0B, 0x0C, 0x0D, '1'};
final byte[] bcar2 = {0x0A, 0x0B, 0x0C, 0x0D, '2'};
final byte[] bcar10 = {0x0A, 0x0B, 0x0C, 0x0D, '1', '0'};
final byte[] bcarstar = {0x0A, 0x0B, 0x0C, 0x0D, '*'};
final byte[] b1 = {'1'};
final byte[] b2 = {'2'};
final byte[] b3 = {'3'};
final byte[] b10 = {'1', '0'};
@Test
public void sort() {
jedis.lpush("foo", "3");
@@ -22,6 +43,20 @@ public class SortingCommandsTest extends JedisCommandTestBase {
expected.add("3");
assertEquals(expected, result);
//Binary
jedis.lpush(bfoo, b3);
jedis.lpush(bfoo, b2);
jedis.lpush(bfoo, b1);
List<byte[]> bresult = jedis.sort(bfoo);
List<byte[]> bexpected = new ArrayList<byte[]>();
bexpected.add(b1);
bexpected.add(b2);
bexpected.add(b3);
assertTrue(JedisTest.isListAreEquals(bexpected, bresult));
}
@Test
@@ -45,6 +80,28 @@ public class SortingCommandsTest extends JedisCommandTestBase {
expected.add("1");
assertEquals(expected, result);
//Binary
jedis.lpush(bfoo, b2);
jedis.lpush(bfoo, b3);
jedis.lpush(bfoo, b1);
jedis.set(bbar1, b3);
jedis.set(bbar2, b2);
jedis.set(bbar3, b1);
SortingParams bsp = new SortingParams();
bsp.by(bbarstar);
List<byte[]> bresult = jedis.sort(bfoo, bsp);
List<byte[]> bexpected = new ArrayList<byte[]>();
bexpected.add(b3);
bexpected.add(b2);
bexpected.add(b1);
assertTrue(JedisTest.isListAreEquals(bexpected, bresult));
}
@Test
@@ -64,6 +121,24 @@ public class SortingCommandsTest extends JedisCommandTestBase {
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);
JedisTest.isListAreEquals(bexpected, bresult);
}
@Test
@@ -83,7 +158,26 @@ public class SortingCommandsTest extends JedisCommandTestBase {
expected.add("3");
assertEquals(expected, result);
//Binary
for (int n = 10; n > 0; n--) {
jedis.lpush(bfoo, new byte[]{(byte)n});
}
SortingParams bsp = new SortingParams();
sp.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);
JedisTest.isListAreEquals(bexpected, bresult);
}
@Test
public void sortAlpha() {
@@ -102,6 +196,23 @@ public class SortingCommandsTest extends JedisCommandTestBase {
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);
JedisTest.isListAreEquals(bexpected, bresult);
}
@Test
@@ -132,7 +243,37 @@ public class SortingCommandsTest extends JedisCommandTestBase {
expected.add("bar10");
assertEquals(expected, result);
//Binary
jedis.lpush(bfoo, b1);
jedis.lpush(bfoo, b2);
jedis.lpush(bfoo, b10);
jedis.set(bbar1, bbar1);
jedis.set(bbar2, bbar2);
jedis.set(bbar10, bbar10);
jedis.set(bcar1, bcar1);
jedis.set(bcar2, bcar2);
jedis.set(bcar10, bcar10);
SortingParams bsp = new SortingParams();
sp.get(bcarstar, bbarstar);
List<byte[]> bresult = jedis.sort(bfoo, bsp);
List<byte[]> bexpected = new ArrayList<byte[]>();
bexpected.add(bcar1);
bexpected.add(bbar1);
bexpected.add(bcar2);
bexpected.add(bbar2);
bexpected.add(bcar10);
bexpected.add(bbar10);
JedisTest.isListAreEquals(bexpected, bresult);
}
@Test
public void sortStore() {
@@ -149,6 +290,23 @@ public class SortingCommandsTest extends JedisCommandTestBase {
assertEquals(3, result);
assertEquals(expected, jedis.lrange("result", 0, 1000));
//Binary
jedis.lpush(bfoo, b1);
jedis.lpush(bfoo, b2);
jedis.lpush(bfoo, b10);
byte[] bkresult = new byte[]{0X09, 0x0A, 0x0B, 0x0C};
int bresult = jedis.sort(bfoo, bkresult);
List<byte[]> bexpected = new ArrayList<byte[]>();
bexpected.add(b1);
bexpected.add(b2);
bexpected.add(b10);
assertEquals(3, bresult);
JedisTest.isListAreEquals(bexpected, jedis.lrange(bkresult, 0, 1000));
}
}