This repository has been archived by the owner on May 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sample.py
executable file
·132 lines (104 loc) · 3.14 KB
/
sample.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
#!/usr/bin/env python
# coding:utf-8
from pgclient import *
from utils.tools import ScanOptions
from twisted.internet import reactor
from twisted.internet.defer import inlineCallbacks, Deferred
@inlineCallbacks
def basic_test():
# init
c = Pegasus(['127.0.0.1:34601', '127.0.0.1:34602', '127.0.0.1:34603'], 'temp')
suc = yield c.init()
if not suc:
reactor.stop()
print('ERROR: connect pegasus server failed')
return
# set
try:
ret = yield c.set('hkey1', 'skey1', 'value', 0, 500)
print('set ret: ', ret)
except Exception as e:
print(e)
# exist
ret = yield c.exist('hkey1', 'skey1')
print('exist ret: ', ret)
ret = yield c.exist('hkey1', 'skey2')
print('exist ret: ', ret)
# get
ret = yield c.get('hkey1', 'skey1')
print('get ret: ', ret)
ret = yield c.get('hkey1', 'skey2')
print('get ret: ', ret)
# ttl
yield c.set('hkey2', 'skey1', 'value', 123)
d = Deferred()
reactor.callLater(2, d.callback, 'ok') # 2 seconds later
yield d
ret = yield c.ttl('hkey2', 'skey1')
print('ttl ret: ', ret)
# remove
ret = yield c.remove('hkey2', 'skey1')
print('remove ret: ', ret)
# multi_set
kvs = {'skey1': 'value1', 'skey2': 'value2', 'skey3': 'value3'}
ret = yield c.multi_set('hkey3', kvs, 999)
print('multi_set ret: ', ret)
# multi_get
ks = set(kvs.keys())
ret = yield c.multi_get('hkey3', ks)
print('multi_get ret: ', ret)
ret = yield c.multi_get('hkey3', ks, 1)
print('multi_get ret: ', ret)
while ret[0] == 7: # has more data
ks.remove(ret[1].keys()[0])
ret = yield c.multi_get('hkey3', ks, 1)
print('multi_get ret: ', ret)
ret = yield c.multi_get('hkey3', ks, 100, 10000, True)
print('multi_get ret: ', ret)
# sort_key_count
ret = yield c.sort_key_count('hkey3')
print('sort_key_count ret: ', ret)
# get_sort_keys
ret = yield c.get_sort_keys('hkey3', 100, 10000)
print('get_sort_keys ret: ', ret)
# multi_del
ret = yield c.multi_del('hkey3', ks)
print('multi_del ret: ', ret)
# scan
o = ScanOptions()
o.batch_size = 1
s = c.get_scanner('hkey3', '1', '7', o)
while True:
try:
ret = yield s.get_next()
print('get_next ret: ', ret)
except Exception as e:
print(e)
break
if not ret:
break
s.close()
# scan all
yield c.multi_set('0', kvs, 999)
yield c.multi_set('1', kvs, 999)
yield c.multi_set('2', kvs, 999)
yield c.multi_set('3', kvs, 999)
yield c.multi_set('4', kvs, 999)
yield c.multi_set('5', kvs, 999)
yield c.multi_set('6', kvs, 999)
ss = c.get_unordered_scanners(3, o)
for s in ss:
while True:
try:
ret = yield s.get_next()
print('get_next ret: ', ret)
except Exception as e:
print(e)
break
if not ret:
break
s.close()
reactor.stop()
if __name__ == "__main__":
reactor.callWhenRunning(basic_test)
reactor.run()