This repository was archived by the owner on Aug 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathdatabase.py
More file actions
148 lines (122 loc) · 5.1 KB
/
database.py
File metadata and controls
148 lines (122 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import json
from .resource import Resource
from .document import Document
from .design import Design
from .index import Index
class Database(Resource):
"""
Connection to a specific database.
Learn more about the raw API from the [Cloudant docs](http://docs.cloudant.com/api/database.html).
"""
def document(self, name, **kwargs):
"""
Create a `Document` object from `name`.
"""
opts = dict(self.opts, **kwargs)
return Document(self._make_url(name), session=self._session, **opts)
def design(self, name, **kwargs):
"""
Create a `Design` object from `name`, like so:
db.design('test')
# refers to DB/_design/test
"""
opts = dict(self.opts, **kwargs)
return Design(self._make_url('/'.join(['_design', name])), session=self._session, **opts)
def __getitem__(self, name):
"""Shortcut to `Database.document`."""
return self.document(name, **self.opts)
def __setitem__(self, name, doc):
"""Creates `doc` with an ID of `name`."""
response = self.put(name, params=doc)
# block until result if the object is using async
if hasattr(response, 'result'):
response = response.result()
response.raise_for_status()
def __delitem__(self, name):
"""
Shortcut to synchronously deleting the document from the database.
For example:
del db['docKey']
"""
doc = self.document(name)
response = doc.get()
# block until result if the object is using async/is a future
if hasattr(response, 'result'):
response = response.result()
response.raise_for_status()
rev = response.json()['_rev']
deletion = doc.delete(rev)
# block until result if the object is using async/is a future
if hasattr(deletion, 'result'):
deletion = deletion.result()
deletion.raise_for_status()
def all_docs(self, **kwargs):
"""
Return an `Index` object referencing all documents in the database.
You can treat it like an iterator:
for doc in db.all_docs():
print doc
"""
opts = dict(self.opts, **kwargs)
return Index(self._make_url('_all_docs'), session=self._session, **opts)
def __iter__(self):
"""Formats `Database.all_docs` for use as an iterator."""
return self.all_docs().__iter__()
def bulk_docs(self, *docs, **kwargs):
"""
Save many docs, all at once. Each `doc` argument must be a dict, like this:
db.bulk_docs({...}, {...}, {...})
# saves all documents in one HTTP request
For more detail on bulk operations, see
[Creating or updating multiple documents](http://docs.cloudant.com/api/database.html#creating-or-updating-multiple-documents)
"""
params = {
'docs': docs
}
return self.post('_bulk_docs', params=params, **kwargs)
def changes(self, **kwargs):
"""
Gets a list of the changes made to the database.
This can be used to monitor for update and modifications to the database
for post processing or synchronization.
Automatically adjusts the request to handle the different response behavior
of polling, longpolling, and continuous modes.
For more information about the `_changes` feed, see
[the docs](http://docs.cloudant.com/api/database.html#obtaining-a-list-of-changes).
"""
size = 512 # requests default chunk size value
if 'params' in kwargs:
if 'feed' in kwargs['params']:
if kwargs['params']['feed'] == 'continuous':
kwargs['stream'] = True
size = 1 # 1 byte because we don't want to hold the last
# record in memory buffer in awaiting for new data
emit_heartbeats = kwargs.pop('emit_heartbeats', False)
response = self.get('_changes', **kwargs)
response.raise_for_status()
for line in response.iter_lines(chunk_size=size):
if not line:
if emit_heartbeats:
yield None
continue
line = line.decode('utf-8')
if line[-1] == ',':
line = line[:-1]
yield json.loads(line)
def missing_revs(self, revs, **kwargs):
"""
Refers to [this method](http://docs.cloudant.com/api/database.html#retrieving-missing-revisions).
"""
return self.post('_missing_revs', params=revs, **kwargs)
def revs_diff(self, revs, **kwargs):
"""
Refers to [this method](http://docs.cloudant.com/api/database.html#retrieving-differences-between-revisions)
"""
return self.post('_revs_diff', params=revs, **kwargs)
def view_cleanup(self, **kwargs):
"""
Cleans up the cached view output on disk for a given view. For example:
print db.view_cleanup().result().json()
# {'ok': True}
"""
return self.post('_view_cleanup', **kwargs)