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

Add rpush and rpop commands #39

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
8 changes: 4 additions & 4 deletions src/main/java/com/fiftyonred/mock_jedis/MockJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ 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();
}

@Override
Expand All @@ -830,7 +830,7 @@ 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
Expand Down Expand Up @@ -1515,7 +1515,7 @@ 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
Expand All @@ -1540,7 +1540,7 @@ 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
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 @@ -929,6 +929,36 @@ public Response<byte[]> lpop(final byte[] key) {
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> 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
19 changes: 19 additions & 0 deletions src/main/java/com/fiftyonred/mock_jedis/MockStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -560,13 +560,32 @@ public synchronized DataContainer lpop(final DataContainer key) {
return list == null || list.isEmpty() ? null : list.remove(list.size() - 1);
}

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);
}
for (DataContainer d: string) list.add(0, d);
return list.size();
}

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();
}

public synchronized List<DataContainer> lrange(final DataContainer key, long start, long end) {
final List<DataContainer> full = getListFromStorage(key, false);

if (full == null) {
return Collections.emptyList();
}

final List<DataContainer> result = new ArrayList<DataContainer>();

Expand Down
24 changes: 24 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,10 +106,34 @@ public void testList() {
assertEquals("a", j.lpop("test"));

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

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

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

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

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

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

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

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

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

@Test
public void testLRange() {
assertEquals(0, j.lrange("missingkey", 0, 100).size());

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