Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

basic support for rpush and rpop #33

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/main/java/com/fiftyonred/mock_jedis/MockJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -805,8 +805,8 @@ public String substr(String key, int start, int end) {

@Override
public Long rpush(String key, String... strings) {
throw new UnsupportedOperationException(NOT_IMPLEMENTED);
}
return pipeline.rpush(key, strings).get();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please reformat leading spaces to tabs

}

@Override
public String ltrim(String key, long start, long end) {
Expand All @@ -830,8 +830,8 @@ public Long lrem(String key, long count, String value) {

@Override
public String rpop(String key) {
throw new UnsupportedOperationException(NOT_IMPLEMENTED);
}
return pipeline.rpop(key).get();
}

@Override
public String rpoplpush(String srckey, String dstkey) {
Expand Down Expand Up @@ -1515,8 +1515,8 @@ public Double hincrByFloat(byte[] key, byte[] field, double value) {

@Override
public Long rpush(byte[] key, byte[]... strings) {
throw new UnsupportedOperationException(NOT_IMPLEMENTED);
}
return pipeline.rpush(key, strings).get();
}

@Override
public String ltrim(byte[] key, long start, long end) {
Expand All @@ -1540,8 +1540,8 @@ public Long lrem(byte[] key, long count, byte[] value) {

@Override
public byte[] rpop(byte[] key) {
throw new UnsupportedOperationException(NOT_IMPLEMENTED);
}
return pipeline.rpop(key).get();
}

@Override
public byte[] rpoplpush(byte[] srckey, byte[] dstkey) {
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/fiftyonred/mock_jedis/MockPipeline.java
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,20 @@ public Response<Long> lpush(final byte[] key, final byte[]... string) {
return response;
}

@Override
public Response<Long> rpush(final String key, final String... string) {
final Response<Long> response = new Response<Long>(BuilderFactory.LONG);
response.set((long) mockStorage.rpush(DataContainer.from(key), DataContainer.from(string)));
return response;
}

@Override
public Response<Long> rpush(final byte[] key, final byte[]... string) {
final Response<Long> response = new Response<Long>(BuilderFactory.LONG);
response.set((long) mockStorage.rpush(DataContainer.from(key), DataContainer.from(string)));
return response;
}

@Override
public Response<String> lpop(final String key) {
final Response<String> response = new Response<String>(BuilderFactory.STRING);
Expand All @@ -929,6 +943,22 @@ public Response<byte[]> lpop(final byte[] key) {
return response;
}

@Override
public Response<String> rpop(final String key) {
final Response<String> response = new Response<String>(BuilderFactory.STRING);
final DataContainer result = mockStorage.rpop(DataContainer.from(key));
response.set(result == null ? null : result.getBytes());
return response;
}

@Override
public Response<byte[]> rpop(final byte[] key) {
final Response<byte[]> response = new Response<byte[]>(BuilderFactory.BYTE_ARRAY);
final DataContainer result = mockStorage.rpop(DataContainer.from(key));
response.set(result == null ? null : result.getBytes());
return response;
}

@Override
public Response<Long> llen(final String key) {
final Response<Long> response = new Response<Long>(BuilderFactory.LONG);
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/fiftyonred/mock_jedis/MockStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -555,11 +555,32 @@ public synchronized int lpush(final DataContainer key, final DataContainer... st
return list.size();
}

public synchronized int rpush(final DataContainer key, final DataContainer... string) {
List<DataContainer> list = getListFromStorage(key, true);
if (list == null) {
list = new ArrayList<DataContainer>();
listStorage.put(key, list);
}

List<DataContainer> elems = Arrays.asList(string);
for (int i = elems.size() -1; i > -1; i--) {
DataContainer elem = elems.get(i);
list.add(0, elem);
}

return list.size();
}

public synchronized DataContainer lpop(final DataContainer key) {
final List<DataContainer> list = getListFromStorage(key, true);
return list == null || list.isEmpty() ? null : list.remove(list.size() - 1);
}

public synchronized DataContainer rpop(final DataContainer key) {
final List<DataContainer> list = getListFromStorage(key, true);
return list == null || list.isEmpty() ? null : list.remove(0);
}

public synchronized int llen(final DataContainer key) {
final List<DataContainer> list = getListFromStorage(key, false);
return list == null ? 0 : list.size();
Expand Down
95 changes: 95 additions & 0 deletions src/test/java/com/fiftyonred/mock_jedis/MockJedisTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,103 @@ public void testList() {
assertEquals("a", j.lpop("test"));

assertEquals(Long.valueOf(0), j.llen("test"));

j.lpush("test", "a");
j.lpush("test", "b");
j.lpush("test", "c");

assertEquals(Long.valueOf(3), j.llen("test"));

assertEquals("a", j.rpop("test"));
assertEquals("b", j.rpop("test"));
assertEquals("c", j.rpop("test"));

assertEquals(Long.valueOf(0), j.llen("test"));

j.rpush("test", "x");
j.rpush("test", "y");
j.rpush("test", "z");

assertEquals(Long.valueOf(3), j.llen("test"));

assertEquals("x", j.lpop("test"));
assertEquals("y", j.lpop("test"));
assertEquals("z", j.lpop("test"));

assertEquals(Long.valueOf(0), j.llen("test"));

j.rpush("test", "x");
j.rpush("test", "y");
j.rpush("test", "z");

assertEquals(Long.valueOf(3), j.llen("test"));

assertEquals("z", j.rpop("test"));
assertEquals("y", j.rpop("test"));
assertEquals("x", j.rpop("test"));

assertEquals(Long.valueOf(0), j.llen("test"));
}

@Test
public void testMixedListPush() {
assertEquals(Long.valueOf(0), j.llen("test"));

j.lpush("test", "a");
j.rpush("test", "x");
j.lpush("test", "b");
j.rpush("test", "y");
j.lpush("test", "c");
j.rpush("test", "z");

assertEquals(Long.valueOf(6), j.llen("test"));

assertEquals("c", j.lpop("test"));
assertEquals("b", j.lpop("test"));
assertEquals("a", j.lpop("test"));
assertEquals("x", j.lpop("test"));
assertEquals("y", j.lpop("test"));
assertEquals("z", j.lpop("test"));

assertEquals(Long.valueOf(0), j.llen("test"));

j.lpush("test", "a");
j.rpush("test", "x");
j.lpush("test", "b");
j.rpush("test", "y");
j.lpush("test", "c");
j.rpush("test", "z");

assertEquals(Long.valueOf(6), j.llen("test"));

assertEquals("z", j.rpop("test"));
assertEquals("y", j.rpop("test"));
assertEquals("x", j.rpop("test"));
assertEquals("a", j.rpop("test"));
assertEquals("b", j.rpop("test"));
assertEquals("c", j.rpop("test"));

assertEquals(Long.valueOf(0), j.llen("test"));

j.lpush("test", "a");
j.rpush("test", "x");
j.lpush("test", "b");
j.rpush("test", "y");
j.lpush("test", "c");
j.rpush("test", "z");

assertEquals(Long.valueOf(6), j.llen("test"));

assertEquals("z", j.rpop("test"));
assertEquals("c", j.lpop("test"));
assertEquals("y", j.rpop("test"));
assertEquals("b", j.lpop("test"));
assertEquals("x", j.rpop("test"));
assertEquals("a", j.lpop("test"));

assertEquals(Long.valueOf(0), j.llen("test"));
}

@Test
public void testLRange() {
j.lpush("test", "a");
Expand Down