1
+ from pprint import pprint
1
2
from typing import Union
2
3
import grpc
3
4
from FlyDB .client_grpc import db_pb2_grpc
@@ -25,7 +26,6 @@ def __init__(self):
25
26
# The stub provides methods to call the gRPC service methods defined in db.proto.
26
27
self .stub = db_pb2_grpc .GStringServiceStub (self .channel )
27
28
28
-
29
29
def _validate_input (self , value , value_type , param_name ):
30
30
if not isinstance (value , value_type ):
31
31
raise TypeError (f"{ param_name } must be of type { value_type } " )
@@ -35,7 +35,7 @@ def connect_option(self, dir_path: str, data_file_size: int, sync_write: bool):
35
35
Connects to the gRPC server with provided options.
36
36
37
37
Parameters:
38
- dir_path (str): The directory path for the FlyDB2 .
38
+ dir_path (str): The directory path for the FlyDB .
39
39
data_file_size (int): The size of data files.
40
40
sync_write (bool): Indicates whether to use synchronous writes.
41
41
@@ -60,13 +60,13 @@ def connect_option(self, dir_path: str, data_file_size: int, sync_write: bool):
60
60
61
61
def set (self , key : str , value : Union [str , int , float , bool , bytes ], expire : int ):
62
62
"""
63
- Sets the key-value pair in the FlyDB2 .
63
+ Sets the key-value pair in the FlyDB .
64
64
65
65
Parameters:
66
66
key (str): The key to be set.
67
67
value (Union[str, int, float, bool, bytes]): The value to be set.
68
- expire (int): The expiration time for the key-value pair in nanoseconds .
69
- When expire is 0, it never expires. Expire is in milliseconds.
68
+ expire (int): The expiration time for the key-value pair in milliseconds .
69
+ When expire is 0, it never expires.
70
70
71
71
Returns:
72
72
None
@@ -94,9 +94,23 @@ def set(self, key: str, value: Union[str, int, float, bool, bytes], expire: int)
94
94
if response .ok :
95
95
print ("Put data success!" )
96
96
97
+ def _return_type_response (self , response ):
98
+ if response .HasField ("StringValue" ):
99
+ return response .StringValue
100
+ elif response .HasField ("Int64Value" ):
101
+ return response .Int64Value
102
+ elif response .HasField ("Float64Value" ):
103
+ return response .Float64Value
104
+ elif response .HasField ("BoolValue" ):
105
+ return response .BoolValue
106
+ elif response .HasField ("BytesValue" ):
107
+ return response .BytesValue
108
+ else :
109
+ raise ValueError ("Unsupported value type" )
110
+
97
111
def get (self , key ):
98
112
"""
99
- Retrieves the value associated with the given key from the FlyDB2 .
113
+ Retrieves the value associated with the given key from the FlyDB .
100
114
101
115
Parameters:
102
116
key (str): The key for which the value needs to be retrieved.
@@ -105,37 +119,26 @@ def get(self, key):
105
119
Union[str, int, float, bool, bytes]: The value associated with the given key.
106
120
107
121
Raises:
108
- KeyError: If the key is not found in the FlyDB2 .
109
- TimeoutError: If the key has expired in the FlyDB2 .
122
+ KeyError: If the key is not found in the FlyDB .
123
+ TimeoutError: If the key has expired in the FlyDB .
110
124
"""
111
125
request = db_pb2 .GetRequest ()
112
126
request .key = key
113
127
try :
114
128
response = self .stub .Get (request )
115
129
# Determine the type of value and return accordingly
116
- if response .HasField ("StringValue" ):
117
- return response .StringValue
118
- elif response .HasField ("Int64Value" ):
119
- return response .Int64Value
120
- elif response .HasField ("Float64Value" ):
121
- return response .Float64Value
122
- elif response .HasField ("BoolValue" ):
123
- return response .BoolValue
124
- elif response .HasField ("BytesValue" ):
125
- return response .BytesValue
126
- else :
127
- raise ValueError ("Unsupported value type" )
130
+ return self ._return_type_response (response )
128
131
except grpc ._channel ._InactiveRpcError as e :
129
132
if "KeyNotFoundError" in str (e ):
130
- raise KeyError ("key is not found in the FlyDB2 " )
133
+ raise KeyError ("key is not found in the FlyDB " )
131
134
elif "Wrong value" in str (e ):
132
135
raise TimeoutError ("key expired" )
133
136
else :
134
137
raise
135
138
136
139
def delete (self , key ):
137
140
"""
138
- Deletes the key-value pair from the FlyDB2 .
141
+ Deletes the key-value pair from the FlyDB .
139
142
140
143
Parameters:
141
144
key (str): The key to be deleted.
@@ -144,7 +147,7 @@ def delete(self, key):
144
147
None
145
148
146
149
Raises:
147
- KeyError: If the key is not found in the FlyDB2 .
150
+ KeyError: If the key is not found in the FlyDB .
148
151
"""
149
152
request = db_pb2 .DelRequest ()
150
153
request .key = key
@@ -154,8 +157,166 @@ def delete(self, key):
154
157
print ("Delete data success!" )
155
158
except grpc ._channel ._InactiveRpcError as e :
156
159
if "KeyNotFoundError" in str (e ):
157
- raise KeyError ("key is not found in the FlyDB2" )
160
+ raise KeyError ("key is not found in the FlyDB" )
161
+ else :
162
+ raise
163
+
164
+ def type (self , key ):
165
+ """
166
+ Returns the type of the value associated with the given key.
167
+
168
+ Parameters:
169
+ key (str): The key for which the value type needs to be retrieved.
170
+
171
+ Returns:
172
+ str: The type of the value associated with the given key.
173
+
174
+ Raises:
175
+ KeyError: If the key is not found in the FlyDB.
176
+ """
177
+ request = db_pb2 .TypeRequest ()
178
+ request .key = key
179
+ try :
180
+ response = self .stub .Type (request )
181
+ return response .type
182
+ except grpc ._channel ._InactiveRpcError as e :
183
+ if "KeyNotFoundError" in str (e ):
184
+ raise KeyError ("key is not found in the FlyDB" )
158
185
else :
159
186
raise
160
187
188
+ def len (self , key ):
189
+ """
190
+ Returns the length of the string value associated with the given key.
161
191
192
+ Parameters:
193
+ key (str): The key for which the string length needs to be retrieved.
194
+
195
+ Returns:
196
+ int: The length of the string value associated with the given key.
197
+
198
+ Raises:
199
+ KeyError: If the key is not found in the FlyDB.
200
+ TypeError: If the value associated with the given key is not of type string.
201
+ """
202
+ request = db_pb2 .StrLenRequest ()
203
+ request .key = key
204
+ try :
205
+ response = self .stub .StrLen (request )
206
+ return response .length
207
+ except grpc ._channel ._InactiveRpcError as e :
208
+ if "KeyNotFoundError" in str (e ):
209
+ raise KeyError ("key is not found in the FlyDB" )
210
+ elif "Wrong value" in str (e ):
211
+ raise TypeError ("value is not of type string" )
212
+ else :
213
+ raise
214
+
215
+ def get_set (self , key , value , expire ):
216
+ """
217
+ Sets the value associated with the given key and returns the old value.
218
+
219
+ Parameters:
220
+ key (str): The key for which the value needs to be set.
221
+ value (Union[str, int, float, bool, bytes]): The value to be set.
222
+ expire (int): The expiration time for the key-value pair in milliseconds.
223
+ When expire is 0, it never expires.
224
+
225
+ Returns:
226
+ Union[str, int, float, bool, bytes]: The old value associated with the given key.
227
+
228
+ Raises:
229
+ TypeError: If the value associated with the given key is not of type string.
230
+ """
231
+ request = db_pb2 .GetSetRequest ()
232
+ request .key = key
233
+
234
+ if isinstance (value , str ):
235
+ request .StringValue = value
236
+ elif isinstance (value , int ):
237
+ request .Int64Value = value
238
+ elif isinstance (value , float ):
239
+ request .Float64Value = value
240
+ elif isinstance (value , bool ):
241
+ request .BoolValue = value
242
+ elif isinstance (value , bytes ):
243
+ request .BytesValue = value
244
+ else :
245
+ raise TypeError ("Unsupported type" )
246
+
247
+ request .expire = expire * 1000000
248
+ try :
249
+ response = self .stub .GetSet (request )
250
+ return self ._return_type_response (response )
251
+ except grpc ._channel ._InactiveRpcError as e :
252
+ if "Wrong value" in str (e ):
253
+ raise TypeError ("value is not of type string" )
254
+ else :
255
+ raise
256
+
257
+ def exist (self , key ):
258
+ """
259
+ Checks if the given key exists in the FlyDB.
260
+
261
+ Parameters:
262
+ key (str): The key to be checked.
263
+
264
+ Returns:
265
+ bool: True if the key exists in the FlyDB, False otherwise.
266
+ """
267
+ request = db_pb2 .ExistsRequest ()
268
+ request .key = key
269
+ response = self .stub .Exists (request )
270
+ return response .exists
271
+
272
+ def persist (self , key ):
273
+ """
274
+ Removes the expiration associated with the given key.
275
+
276
+ Parameters:
277
+ key (str): The key for which the expiration needs to be removed.
278
+
279
+ Returns:
280
+ None
281
+
282
+ Raises:
283
+ KeyError: If the key is not found in the FlyDB.
284
+ """
285
+ request = db_pb2 .PersistRequest ()
286
+ request .key = key
287
+ try :
288
+ response = self .stub .Persist (request )
289
+ if response .ok :
290
+ print ("Persist success!" )
291
+ except grpc ._channel ._InactiveRpcError as e :
292
+ if "KeyNotFoundError" in str (e ):
293
+ raise KeyError ("key is not found in the FlyDB" )
294
+ else :
295
+ raise
296
+
297
+ def mget (self , keys ):
298
+ """
299
+ Retrieves the values associated with the given keys from the FlyDB.
300
+
301
+ Parameters:
302
+ keys (list): The list of keys for which the values need to be retrieved.
303
+
304
+ Returns:
305
+ list: The list of values associated with the given keys.
306
+
307
+ Raises:
308
+ KeyError: If any of the keys is not found in the FlyDB.
309
+ """
310
+ request = db_pb2 .MGetRequest ()
311
+ request .keys .extend (keys )
312
+ try :
313
+ response = self .stub .MGet (request )
314
+ values = []
315
+ for value in response .values :
316
+ values .append (value )
317
+ return values
318
+ except grpc ._channel ._InactiveRpcError as e :
319
+ if "KeyNotFoundError" in str (e ):
320
+ raise KeyError ("key is not found in the FlyDB" )
321
+ else :
322
+ raise
0 commit comments