Skip to content

Commit 3ae4e43

Browse files
committed
Adds pipelines
1 parent 9a3bc6e commit 3ae4e43

File tree

4 files changed

+51
-21
lines changed

4 files changed

+51
-21
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ $ pip install rejson-py
1818
```python
1919
from rejson import ReJSONClient, Path
2020

21-
rj = ReJSONClient(host='localhost', port=6379)
21+
rj = Client(host='localhost', port=6379)
2222

2323
# Set the key `obj` to some object
2424
obj = {

rejson/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
`rejson-py is a client for ReJSON
33
"""
44

5-
from .client import ReJSONClient
5+
from .client import Client
66
from .path import Path

rejson/client.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def bulk_of_jsons(b):
3838
b[index] = json.loads(item)
3939
return b
4040

41-
class ReJSONClient(StrictRedis):
41+
class Client(StrictRedis):
4242
"""
4343
Implementation of ReJSON commands
4444
@@ -70,7 +70,7 @@ class ReJSONClient(StrictRedis):
7070
}
7171

7272
def __init__(self, *args, **kwargs):
73-
super(ReJSONClient, self).__init__(*args, **kwargs)
73+
super(Client, self).__init__(*args, **kwargs)
7474
self.__checkPrerequirements()
7575
# Set the module commands' callbacks
7676
for k, v in self.MODULE_CALLBACKS.iteritems():
@@ -237,3 +237,21 @@ def JSONObjLen(self, name, path=Path.rootPath()):
237237
``name``
238238
"""
239239
return self.execute_command('JSON.OBJLEN', name, str_path(path))
240+
241+
def pipeline(self, transaction=True, shard_hint=None):
242+
"""
243+
Return a new pipeline object that can queue multiple commands for
244+
later execution. ``transaction`` indicates whether all commands
245+
should be executed atomically. Apart from making a group of operations
246+
atomic, pipelines are useful for reducing the back-and-forth overhead
247+
between the client and server.
248+
"""
249+
return Pipeline(
250+
self.connection_pool,
251+
self.response_callbacks,
252+
transaction,
253+
shard_hint)
254+
255+
class Pipeline(BasePipeline, Client):
256+
"Pipeline for ReJSONClient"
257+
pass

test/test.py

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import redis
22
from unittest import TestCase
3-
from rejson import ReJSONClient, Path
3+
from rejson import Client, Path
44

55
class ReJSONTestCase(TestCase):
66
def testJSONSetGetDelShouldSucceed(self):
77
"Test basic JSONSet/Get/Del"
8-
rj = ReJSONClient()
8+
rj = Client()
99
rj.flushdb()
1010

1111
self.assertTrue(rj.JSONSet('foo', Path.rootPath(), 'bar'))
@@ -15,7 +15,7 @@ def testJSONSetGetDelShouldSucceed(self):
1515

1616
def testMGetShouldSucceed(self):
1717
"Test JSONMGet"
18-
rj = ReJSONClient()
18+
rj = Client()
1919
rj.flushdb()
2020

2121
rj.JSONSet('1', Path.rootPath(), 1)
@@ -26,15 +26,15 @@ def testMGetShouldSucceed(self):
2626

2727
def testTypeShouldSucceed(self):
2828
"Test JSONType"
29-
rj = ReJSONClient()
29+
rj = Client()
3030
rj.flushdb()
3131

3232
rj.JSONSet('1', Path.rootPath(), 1)
3333
self.assertEqual('integer', rj.JSONType('1'))
3434

3535
def testNumIncrByShouldSucceed(self):
3636
"Test JSONNumIncrBy"
37-
rj = ReJSONClient()
37+
rj = Client()
3838
rj.flushdb()
3939

4040
rj.JSONSet('num', Path.rootPath(), 1)
@@ -44,7 +44,7 @@ def testNumIncrByShouldSucceed(self):
4444

4545
def testNumMultByShouldSucceed(self):
4646
"Test JSONNumIncrBy"
47-
rj = ReJSONClient()
47+
rj = Client()
4848
rj.flushdb()
4949

5050
rj.JSONSet('num', Path.rootPath(), 1)
@@ -54,7 +54,7 @@ def testNumMultByShouldSucceed(self):
5454

5555
def testStrAppendShouldSucceed(self):
5656
"Test JSONStrAppend"
57-
rj = ReJSONClient()
57+
rj = Client()
5858
rj.flushdb()
5959

6060
rj.JSONSet('str', Path.rootPath(), 'foo')
@@ -63,7 +63,7 @@ def testStrAppendShouldSucceed(self):
6363

6464
def testStrLenShouldSucceed(self):
6565
"Test JSONStrLen"
66-
rj = ReJSONClient()
66+
rj = Client()
6767
rj.flushdb()
6868

6969
rj.JSONSet('str', Path.rootPath(), 'foo')
@@ -73,15 +73,15 @@ def testStrLenShouldSucceed(self):
7373

7474
def testArrAppendShouldSucceed(self):
7575
"Test JSONSArrAppend"
76-
rj = ReJSONClient()
76+
rj = Client()
7777
rj.flushdb()
7878

7979
rj.JSONSet('arr', Path.rootPath(), [1])
8080
self.assertEqual(2, rj.JSONArrAppend('arr', Path.rootPath(), 2))
8181

8282
def testArrIndexShouldSucceed(self):
8383
"Test JSONSArrIndex"
84-
rj = ReJSONClient()
84+
rj = Client()
8585
rj.flushdb()
8686

8787
rj.JSONSet('arr', Path.rootPath(), [0, 1, 2, 3, 4])
@@ -90,7 +90,7 @@ def testArrIndexShouldSucceed(self):
9090

9191
def testArrInsertShouldSucceed(self):
9292
"Test JSONSArrInsert"
93-
rj = ReJSONClient()
93+
rj = Client()
9494
rj.flushdb()
9595

9696
rj.JSONSet('arr', Path.rootPath(), [0, 4])
@@ -99,15 +99,15 @@ def testArrInsertShouldSucceed(self):
9999

100100
def testArrLenShouldSucceed(self):
101101
"Test JSONSArrLen"
102-
rj = ReJSONClient()
102+
rj = Client()
103103
rj.flushdb()
104104

105105
rj.JSONSet('arr', Path.rootPath(), [0, 1, 2, 3, 4])
106106
self.assertEqual(5, rj.JSONArrLen('arr', Path.rootPath()))
107107

108108
def testArrPopShouldSucceed(self):
109109
"Test JSONSArrPop"
110-
rj = ReJSONClient()
110+
rj = Client()
111111
rj.flushdb()
112112

113113
rj.JSONSet('arr', Path.rootPath(), [0, 1, 2, 3, 4])
@@ -119,7 +119,7 @@ def testArrPopShouldSucceed(self):
119119

120120
def testArrTrimShouldSucceed(self):
121121
"Test JSONSArrPop"
122-
rj = ReJSONClient()
122+
rj = Client()
123123
rj.flushdb()
124124

125125
rj.JSONSet('arr', Path.rootPath(), [0, 1, 2, 3, 4])
@@ -128,7 +128,7 @@ def testArrTrimShouldSucceed(self):
128128

129129
def testObjKeysShouldSucceed(self):
130130
"Test JSONSObjKeys"
131-
rj = ReJSONClient()
131+
rj = Client()
132132
rj.flushdb()
133133

134134
obj = { 'foo': 'bar', 'baz': 'qaz' }
@@ -141,18 +141,30 @@ def testObjKeysShouldSucceed(self):
141141

142142
def testObjLenShouldSucceed(self):
143143
"Test JSONSObjLen"
144-
rj = ReJSONClient()
144+
rj = Client()
145145
rj.flushdb()
146146

147147
obj = { 'foo': 'bar', 'baz': 'qaz' }
148148
rj.JSONSet('obj', Path.rootPath(), obj)
149149
self.assertEqual(len(obj), rj.JSONObjLen('obj', Path.rootPath()))
150150

151+
def testPipeline(self):
152+
"Test pipeline"
153+
rj = Client()
154+
rj.flushdb()
155+
156+
p = rj.pipeline()
157+
p.JSONSet('foo', Path.rootPath(), 'bar')
158+
p.JSONGet('foo')
159+
p.JSONDel('foo')
160+
p.exists('foo')
161+
self.assertListEqual([ True, 'bar', 1, False], p.execute())
162+
151163
def testUsageExampleShouldSucceed(self):
152164
"Test the usage example"
153165

154166
# Create a new rejson-py client
155-
rj = ReJSONClient(host='localhost', port=6379)
167+
rj = Client(host='localhost', port=6379)
156168

157169
# Set the key `obj` to some object
158170
obj = {

0 commit comments

Comments
 (0)