Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
24 changes: 24 additions & 0 deletions rejson/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ def __init__(self, encoder=None, decoder=None, *args, **kwargs):
# Set the module commands' callbacks
MODULE_CALLBACKS = {
'JSON.DEL': long,
'JSON.CLEAR': long,
'JSON.GET': self._decode,
'JSON.MGET': bulk_of_jsons(self._decode),
'JSON.SET': lambda r: r and nativestr(r) == 'OK',
'JSON.NUMINCRBY': self._decode,
'JSON.NUMMULTBY': self._decode,
'JSON.TOGGLE': bool,
'JSON.STRAPPEND': long,
'JSON.STRLEN': long,
'JSON.ARRAPPEND': long,
Expand All @@ -65,6 +67,7 @@ def __init__(self, encoder=None, decoder=None, *args, **kwargs):
'JSON.ARRPOP': self._decode,
'JSON.ARRTRIM': long,
'JSON.OBJLEN': long,
'JSON.DEBUG': long,
}
for k, v in six.iteritems(MODULE_CALLBACKS):
self.set_response_callback(k, v)
Expand Down Expand Up @@ -99,6 +102,14 @@ def jsondel(self, name, path=Path.rootPath()):
"""
return self.execute_command('JSON.DEL', name, str_path(path))

def jsonclear(self, name, path=Path.rootPath()):
"""
Emptying arrays and objects (to have zero slots/keys without
deleting the array/object) returning the count of cleared paths
(ignoring non-array and non-objects paths)
"""
return self.execute_command('JSON.CLEAR', name, str_path(path))

def jsonget(self, name, *args, no_escape=False):
"""
Get the object stored as a JSON value at key ``name``
Expand Down Expand Up @@ -166,6 +177,13 @@ def jsonnummultby(self, name, path, number):
"""
return self.execute_command('JSON.NUMMULTBY', name, str_path(path), self._encode(number))

def jsontoggle(self, name, path=Path.rootPath()):
"""
Toggle boolean value under ``path`` at key ``name``,
Returning the new value.
"""
return self.execute_command('JSON.TOGGLE', name, str_path(path))

def jsonstrappend(self, name, string, path=Path.rootPath()):
"""
Appends to the string JSON value under ``path`` at key ``name`` the
Expand Down Expand Up @@ -243,6 +261,12 @@ def jsonobjlen(self, name, path=Path.rootPath()):
"""
return self.execute_command('JSON.OBJLEN', name, str_path(path))

def jsondebugmemory(self, name, path=Path.rootPath()):
"""
Returns the memory usage in bytes of a value under ``path`` from key ``name``.
"""
return self.execute_command("JSON.DEBUG", "MEMORY", name, str_path(path))

def pipeline(self, transaction=True, shard_hint=None):
"""
Return a new pipeline object that can queue multiple commands for
Expand Down
19 changes: 19 additions & 0 deletions tests/test_rejson.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ def testMGetShouldSucceed(self):
e = [1, 2]
self.assertListEqual(e, r)

def testClearShouldSucceed(self):
"Test JSONClear"

rj.jsonset('arr', Path.rootPath(), [0, 1, 2, 3, 4])
self.assertEqual(1, rj.jsonclear('arr', Path.rootPath()))
self.assertEqual([], rj.jsonget('arr'))

def testTypeShouldSucceed(self):
"Test JSONType"

Expand All @@ -82,6 +89,12 @@ def testNumMultByShouldSucceed(self):
self.assertEqual(5, rj.jsonnummultby('num', Path.rootPath(), 2.5))
self.assertEqual(2.5, rj.jsonnummultby('num', Path.rootPath(), 0.5))

def testToggleShouldSucceed(self):
"Test JSONToggle"

rj.jsonset('bool', Path.rootPath(), False)
self.assertTrue(rj.jsontoggle('bool', Path.rootPath()))

def testStrAppendShouldSucceed(self):
"Test JSONStrAppend"

Expand Down Expand Up @@ -161,6 +174,12 @@ def testObjLenShouldSucceed(self):
rj.jsonset('obj', Path.rootPath(), obj)
self.assertEqual(len(obj), rj.jsonobjlen('obj', Path.rootPath()))

def testDebugMemoryShouldSucceed(self):
"Test JSONDebug"

rj.jsonset('str', Path.rootPath(), 'foo')
self.assertEqual(24, rj.jsondebugmemory('str', Path.rootPath()))

def testPipelineShouldSucceed(self):
"Test pipeline"

Expand Down