Skip to content

Commit 026992f

Browse files
tammoippendjc
authored andcommitted
Allow mango filters in _changes API
If `filter==‘selector’` then there should be another kwarg `_selector` with a `dict` containing the selector / mango filter.
1 parent 013c2b1 commit 026992f

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

couchdb/client.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,7 +1186,12 @@ def update_doc(self, name, docid=None, **options):
11861186
return headers, body
11871187

11881188
def _changes(self, **opts):
1189-
_, _, data = self.resource.get('_changes', **opts)
1189+
# use streaming `get` and `post` methods
1190+
if opts.get('filter') == '_selector':
1191+
selector = opts.pop('_selector', None)
1192+
_, _, data = self.resource.post('_changes', selector, **opts)
1193+
else:
1194+
_, _, data = self.resource.get('_changes', **opts)
11901195
lines = data.iterchunks()
11911196
for ln in lines:
11921197
if not ln: # skip heartbeats
@@ -1205,7 +1210,12 @@ def changes(self, **opts):
12051210
"""
12061211
if opts.get('feed') == 'continuous':
12071212
return self._changes(**opts)
1208-
_, _, data = self.resource.get_json('_changes', **opts)
1213+
1214+
if opts.get('filter') == '_selector':
1215+
selector = opts.pop('_selector', None)
1216+
_, _, data = self.resource.post_json('_changes', selector, **opts)
1217+
else:
1218+
_, _, data = self.resource.get_json('_changes', **opts)
12091219
return data
12101220

12111221

couchdb/tests/client.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,28 @@ def test_changes_conn_usable(self):
570570
# in a good state from the previous request.
571571
self.assertTrue(self.db.info()['doc_count'] == 0)
572572

573+
def test_changes_conn_usable_selector(self):
574+
if self.server.version_info()[0] < 2:
575+
return
576+
# Consume a changes feed to get a used connection in the pool.
577+
list(self.db.changes(feed='continuous',
578+
filter='_selector',
579+
timeout=0,
580+
_selector={'selector': {}}))
581+
# Try using the connection again to make sure the connection was left
582+
# in a good state from the previous request.
583+
self.assertTrue(self.db.info()['doc_count'] == 0)
584+
585+
def test_changes_usable_selector(self):
586+
if self.server.version_info()[0] < 2:
587+
return
588+
# Consume a changes feed to get a used connection in the pool.
589+
list(self.db.changes(filter='_selector',
590+
_selector={'selector': {}}))
591+
# Try using the connection again to make sure the connection was left
592+
# in a good state from the previous request.
593+
self.assertTrue(self.db.info()['doc_count'] == 0)
594+
573595
def test_changes_heartbeat(self):
574596
def wakeup():
575597
time.sleep(.3)

0 commit comments

Comments
 (0)