Skip to content

Commit 978a9a9

Browse files
author
Shubham
committed
Add Sync class with static factory methods to construct SyncClient
1 parent 5a3bf3e commit 978a9a9

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

objectbox/c.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,3 +1232,30 @@ def c_array_pointer(py_list: Union[List[Any], np.ndarray], c_type):
12321232
# OBX_C_API obx_err obx_sync_outgoing_message_count(OBX_sync* sync, uint64_t limit, uint64_t* out_count);
12331233
obx_sync_outgoing_message_count = c_fn_rc('obx_sync_outgoing_message_count',
12341234
[OBX_sync_p, ctypes.c_uint64, ctypes.POINTER(ctypes.c_uint64)])
1235+
1236+
OBXFeature = ctypes.c_int
1237+
1238+
1239+
class Feature(IntEnum):
1240+
ResultArray = 1
1241+
TimeSeries = 2
1242+
Sync = 3
1243+
DebugLog = 4
1244+
Admin = 5
1245+
Tree = 6
1246+
SyncServer = 7
1247+
WebSockets = 8
1248+
Cluster = 9
1249+
HttpServer = 10
1250+
GraphQL = 11
1251+
Backup = 12
1252+
Lmdb = 13
1253+
VectorSearch = 14
1254+
Wal = 15
1255+
SyncMongoDb = 16
1256+
Auth = 17
1257+
Trial = 18
1258+
SyncFilters = 19
1259+
1260+
1261+
obx_has_feature = c_fn('obx_has_feature', ctypes.c_bool, [OBXFeature])

objectbox/sync.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,3 +311,58 @@ def get_outgoing_message_count(self, limit: int = 0) -> int:
311311
outgoing_message_count = ctypes.c_uint64(0)
312312
c.obx_sync_outgoing_message_count(self.__c_sync_client_ptr, limit, ctypes.byref(outgoing_message_count))
313313
return outgoing_message_count.value
314+
315+
316+
class Sync:
317+
__sync_clients: dict[Store, SyncClient] = {}
318+
319+
@staticmethod
320+
def is_available() -> bool:
321+
return c.obx_has_feature(c.Feature.Sync)
322+
323+
@staticmethod
324+
def client(
325+
store: Store,
326+
server_url: str,
327+
credential: SyncCredentials,
328+
filter_variables: dict[str, str] | None = None
329+
) -> SyncClient:
330+
client = SyncClient(store, [server_url], filter_variables)
331+
client.set_credentials(credential)
332+
return client
333+
334+
@staticmethod
335+
def client_multi_creds(
336+
store: Store,
337+
server_url: str,
338+
credentials_list: list[SyncCredentials],
339+
filter_variables: dict[str, str] | None = None
340+
) -> SyncClient:
341+
client = SyncClient(store, [server_url], filter_variables)
342+
client.set_multiple_credentials(credentials_list)
343+
return client
344+
345+
@staticmethod
346+
def client_multi_urls(
347+
store: Store,
348+
server_urls: list[str],
349+
credential: SyncCredentials,
350+
filter_variables: dict[str, str] | None = None
351+
) -> SyncClient:
352+
client = SyncClient(store, server_urls, filter_variables)
353+
client.set_credentials(credential)
354+
return client
355+
356+
@staticmethod
357+
def client_multi_creds_multi_urls(
358+
store: Store,
359+
server_urls: list[str],
360+
credentials_list: list[SyncCredentials],
361+
filter_variables: dict[str, str] | None = None
362+
) -> SyncClient:
363+
if store in Sync.__sync_clients:
364+
raise ValueError('Only one sync client can be active for a store')
365+
client = SyncClient(store, server_urls, filter_variables)
366+
client.set_multiple_credentials(credentials_list)
367+
Sync.__sync_clients[store] = client
368+
return client

0 commit comments

Comments
 (0)