Skip to content

Commit 336cc5a

Browse files
authored
Support JSON.CLEAR command (#52)
* Support JSON.CLEAR command * Object clear test
1 parent da14683 commit 336cc5a

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/main/java/com/redislabs/modules/rejson/JReJSON.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ private enum Command implements ProtocolCommand {
6363
ARRINSERT("JSON.ARRINSERT"),
6464
ARRLEN("JSON.ARRLEN"),
6565
ARRPOP("JSON.ARRPOP"),
66-
ARRTRIM("JSON.ARRTRIM");
66+
ARRTRIM("JSON.ARRTRIM"),
67+
CLEAR("JSON.CLEAR");
6768

6869
private final byte[] raw;
6970

@@ -162,7 +163,6 @@ public Long del(String key) {
162163
return del(key, Path.ROOT_PATH);
163164
}
164165

165-
166166
/**
167167
* Deletes a path
168168
* @param key the key name
@@ -180,6 +180,13 @@ public Long del(String key, Path path) {
180180
}
181181
}
182182

183+
public long clear(String key, Path path) {
184+
try (Jedis jedis = getConnection()) {
185+
jedis.getClient().sendCommand(Command.CLEAR, key, path.toString());
186+
return jedis.getClient().getIntegerReply();
187+
}
188+
}
189+
183190
/**
184191
* Gets an object at the root path
185192
* @param key the key name

src/test/java/com/redislabs/modules/rejson/ClientTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,35 @@ public void testArrayLength() {
368368
assertEquals(Long.valueOf(3L), client.arrLen( "foobar", Path.of(".fooArr")));
369369
}
370370

371+
@Test
372+
public void clearArray() {
373+
client.set("foobar", new FooBarObject(), Path.ROOT_PATH);
374+
375+
Path arrPath = Path.of(".fooArr");
376+
assertEquals(Long.valueOf(3L), client.arrLen("foobar", arrPath));
377+
378+
assertEquals(1L, client.clear("foobar", arrPath));
379+
assertEquals(Long.valueOf(0L), client.arrLen("foobar", arrPath));
380+
381+
// ignore non-array
382+
Path strPath = Path.of("foo");
383+
assertEquals(0L, client.clear("foobar", strPath));
384+
assertEquals("bar", client.get("foobar", String.class, strPath));
385+
}
386+
387+
@Test
388+
public void clearObject() {
389+
Baz baz = new Baz("quuz", "grault", "waldo");
390+
Qux qux = new Qux("quux", "corge", "garply", baz);
391+
392+
client.set("qux", qux);
393+
Path objPath = Path.of("baz");
394+
assertEquals(baz, client.get("qux", Baz.class, objPath));
395+
396+
assertEquals(1L, client.clear("qux", objPath));
397+
assertEquals(new Baz(null, null, null), client.get("qux", Baz.class, objPath));
398+
}
399+
371400
@Test
372401
public void testArrayAppendSameType() {
373402
String json = "{ a: 'hello', b: [1, 2, 3], c: { d: ['ello'] }}";

0 commit comments

Comments
 (0)