-
Notifications
You must be signed in to change notification settings - Fork 1
/
examples.py
368 lines (271 loc) · 10.5 KB
/
examples.py
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import os
import sys
from typing import Dict
from dotenv import dotenv_values
from hyper_connect import connect
from hyper_connect.types import (
Hyper,
HyperDocsResult,
HyperGetResult,
IdResult,
ListOptions,
OkIdResult,
QueryOptions,
Result,
SearchQueryOptions,
)
config = dotenv_values("./.env")
connection_string: str = str(config["HYPER"])
hyper: Hyper = connect(connection_string)
############################
#
# DATA SERVICE EXAMPLES
#
############################
def data_add():
movie: Dict = {
"_id": "movie-5000",
"type": "movie",
"title": "Back to the Future 2",
"year": "1987",
}
result: IdResult = hyper.data.add(movie)
print("hyper.data.add result --> ", result)
# OKIdResult: hyper.data.add result --> {'_id': 'movie-4000', 'ok': True, 'status': 201}
# NotOkResult: hyper.data.add result --> {'ok': False, 'status': 409, 'msg': 'document conflict'}
def data_delete():
id: str = "movie-5001"
result: IdResult = hyper.data.remove(id)
print("hyper.data.remove result --> ", result)
# hyper.data.remove result --> {'_id': 'movie-5001', 'ok': True, 'status': 200}
def data_get():
id: str = "movie-5000"
result: HyperGetResult = hyper.data.get(id)
print("hyper.data.get result --> ", result)
# hyper.data.get result --> {'_id': 'movie-5000', 'type': 'movie', 'title': 'Back to the Future 2', 'year': '1987', 'status': 200}
def data_update():
book: Dict = {
"_id": "book-000100",
"type": "book",
"name": "The Lorax 100",
"author": "Dr. Suess",
"published": "1969",
}
result: IdResult = hyper.data.update("book-000100", book)
print("hyper.data.update result --> ", result)
# hyper.data.update result --> {'ok': True, '_id': 'book-000100', 'status': 200}
def list_range():
options: ListOptions = {
"startkey": "book-000105",
"endkey": "book-000106",
}
result: IdResult = hyper.data.list(options)
print("hyper.data.list result --> ", result)
# hyper.data.list result --> {'docs': [{'_id': 'book-000105', 'type': 'book', 'name': 'The Lorax 105', 'author': 'Dr. Suess', 'published': '1969'}, {'_id': 'book-000106', 'type': 'book', 'name': 'The Lorax 106', 'author': 'Dr. Suess', 'published': '1969'}], 'ok': True, 'status': 200}
def list_keys():
options: ListOptions = {
"startkey": None,
"limit": None,
"endkey": None,
"keys": ["book-000105", "book-000106"],
"descending": None,
}
result = hyper.data.list(options)
print("hyper.data.list result --> ", result)
def query():
selector = {"type": "book"}
options: QueryOptions = {
"fields": ["_id", "name", "published"],
"sort": None,
"limit": 3,
"useIndex": None,
}
result: HyperDocsResult = hyper.data.query(selector, options)
print("hyper.data.query result --> ", result)
# hyper.data.query result --> {'docs': [{'_id': 'book-000010', 'name': 'The Lorax', 'published': '1959'}, {'_id': 'book-000020', 'name': 'The Lumberjack named Lorax the tree slayer', 'published': '1969'}, {'_id': 'book-000100', 'name': 'The Lorax 100', 'published': '1969'}], 'ok': True, 'status': 200}
def query_index():
name: str = "_idxauthorpublished"
fields: List[str] = ["author", "published"]
indexresult: Result = hyper.data.index(name, fields)
print("index result --> ", indexresult)
# index result --> {'ok': True, 'status': 201}
selector = {"type": "book", "author": "James A. Michener"}
options: QueryOptions = {
"fields": ["author", "published"],
"sort": [{"author": "DESC"}, {"published": "DESC"}],
"useIndex": "_idxauthorpublished",
}
result: HyperDocsResult = hyper.data.query(selector, options)
print("hyper.data.query result --> ", result)
# hyper.data.query result --> {'docs': [{'author': 'James A. Michener', 'published': '1985'}, {'author': 'James A. Michener', 'published': '1959'}, {'author': 'James A. Michener', 'published': '1947'}], 'ok': True, 'status': 200}
def bulk():
docs: list[Dict] = [
{
"_id": "movie-6000",
"type": "movie",
"title": "Jeremiah Johnson",
"year": "1972",
},
{
"_id": "movie-6001",
"type": "movie",
"title": "Butch Cass_idy and the Sundance K_id",
"year": "1969",
},
{
"_id": "movie-6002",
"type": "movie",
"title": "The Great Gatsby",
"year": "1974",
},
{
"_id": "movie-6003",
"type": "movie",
"title": "The Natural",
"year": "1984",
},
{
"_id": "movie-6004",
"type": "movie",
"title": "The Sting",
"year": "1973",
},
]
result: HyperDocsResult = hyper.data.bulk(docs)
print("hyper.data.bulk result --> ", result)
# hyper.data.bulk result --> {'results': [{'ok': True, '_id': 'movie-6000'}, {'ok': True, '_id': 'movie-6001'}, {'ok': True, '_id': 'movie-6002'}, {'ok': True, '_id': 'movie-6003'}, {'ok': True, '_id': 'movie-6004'}], 'ok': True, 'status': 201}
############################
#
# CACHE SERVICE EXAMPLES
#
############################
def add_cache():
movie: Dict = {
"_id": "movie-5000",
"type": "movie",
"title": "Back to the Future 2",
"year": "1987",
}
result = hyper.cache.add(key="movie-5000", value=movie, ttl="1w")
print("hyper.cache.add result --> ", result)
# OkResult - hyper.cache.add result --> {'ok': True, 'status': 201}
# NotOkResult - hyper.cache.add result --> {'ok': False, 'status': 409, 'msg': 'Document Conflict'}
def get_cache():
key = "movie-5000"
result: HyperGetResult = hyper.cache.get_async(key)
print("hyper.cache.get_async result --> ", result)
# hyper.cache.get_async result --> {'_id': 'movie-5000', 'type': 'movie', 'title': 'Back to the Future 2', 'year': '1987', 'status': 200}
def remove_cache():
key = "movie-5000"
result = hyper.cache.remove(key)
print("hyper.cache.remove result --> ", result)
# hyper.cache.remove result --> {'ok': True, 'status': 200}
def update_cache():
movie: Dict = {
"_id": "movie-5000",
"type": "movie",
"title": "Back to the Future 2",
"year": "1988",
}
result = hyper.cache.set(key="movie-5000", value=movie, ttl="1w")
print("hyper.cache.set result --> ", result)
# hyper.cache.set result --> {'ok': True, 'status': 200}
def query_cache():
result = hyper.cache.query(pattern="movie-500*")
print("hyper.cache.query result --> ", result)
# hyper.cache.query result --> {'docs': [{'key': 'movie-5001', 'value': {'_id': 'movie-5001', 'type': 'movie', 'title': 'Back to the Future 3', 'year': '1989'}}, {'key': 'movie-5000', 'value': {'_id': 'movie-5000', 'type': 'movie', 'title': 'Back to the Future 2', 'year': '1988'}}], 'ok': True, 'status': 200}
hyper.search.update(key, doc)
############################
#
# SEARCH SERVICE EXAMPLES
#
############################
def add_search():
movie: Dict = {
"_id": "movie-5000",
"type": "movie",
"title": "Back to the Future 2",
"year": "1987",
}
result = hyper.search.add(key="movie-5000", doc=movie)
print("hyper.search.add result --> ", result)
# hyper.search.add result --> {'ok': True, 'status': 201}
def get_search():
result: HyperGetResult = hyper.search.get(key="movie-5000")
print("hyper.search.get result --> ", result)
# hyper.search.get result --> {'key': 'movie-5000', 'doc': {'type': 'movie', 'title': 'Back to the Future 2', 'year': '1988', '_id': 'movie-5000'}, 'ok': True, 'status': 200}
def remove_search():
key = "movie-5000"
result = hyper.search.remove(key)
print("hyper.search.remove result --> ", result)
# hyper.search.remove result --> {'ok': True, 'status': 200}
def update_search():
movie: Dict = {
"_id": "movie-5000",
"type": "movie",
"title": "Back to the Future 2",
"year": "1988",
}
result = hyper.search.update(key="movie-5000", doc=movie)
print("hyper.search.update result --> ", result)
# hyper.search.update result --> {'ok': True, 'status': 200}
def query_search():
query: str = "Future"
options: SearchQueryOptions = {
"fields": ["_id", "title", "year"],
"filter": None,
}
result = hyper.search.query(query, options)
print("hyper.search.query result --> ", result)
# hyper.search.query result --> {'matches': [{'type': 'movie', 'title': 'Back to the Future', 'year': '1985', '_id': 'movie-102'}], 'ok': True, 'status': 200}
def load_search():
bulkmoviedocs: List[Dict] = [
{
"_id": "movie-104",
"type": "movie",
"title": "Full Metal Jacket",
"year": "1987",
},
{
"_id": "movie-105",
"type": "movie",
"title": "Predator",
"year": "1987",
},
]
result: HyperSearchLoadResult = hyper.search.load(docs=bulkmoviedocs)
print(" hyper.search.load result -> ", result)
###############################
#
# STORAGE SERVICE EXAMPLES
#
###############################
def storage_upload():
# begin upload remix.png image file
br_remix_png: io.BufferedReader = open(
os.path.join(sys.path[0], "remix.png"), "rb"
)
br_remix_png_upload_result: Result = hyper.storage.upload(
name="remix", data=br_remix_png
)
br_remix_png.close()
# end upload
print("br_remix_png_upload_result ->", br_remix_png_upload_result)
# br_remix_png_upload_result -> {'ok': True, 'status': 201}
def storage_download():
br_remix_png_download_result: requests.Response = hyper.storage.download(
name="remix"
)
# begin download of remix.png image file as remix_downloaded.png
path = os.path.join(sys.path[0], "remix_downloaded.png")
with open(path, "wb") as fd:
for chunk in br_remix_png_download_result.iter_content(chunk_size=128):
fd.write(chunk)
# end download
print("br_remix_png_download_result ->", br_remix_png_download_result)
# br_remix_png_download_result -> <Response [200]>
print(br_remix_png_download_result.status_code)
# 200
def storage_delete():
remove_result: Result = hyper.storage.remove(name="remix-notfound")
print("remove_result ->", remove_result)