Skip to content

Commit 7b86cfc

Browse files
author
Shubham
committed
Add methods for filter variables
1 parent 50d3cb0 commit 7b86cfc

File tree

3 files changed

+58
-12
lines changed

3 files changed

+58
-12
lines changed

objectbox/c.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616
import ctypes.util
1717
import os
1818
import platform
19-
from objectbox.version import Version
19+
from ctypes import c_char_p
2020
from typing import *
21+
2122
import numpy as np
22-
from enum import IntEnum
23+
24+
from objectbox.version import Version
2325

2426
# This file contains C-API bindings based on lib/objectbox.h, linking to the 'objectbox' shared library.
2527
# The bindings are implementing using ctypes, see https://docs.python.org/dev/library/ctypes.html for introduction.
@@ -1218,4 +1220,11 @@ def c_array_pointer(py_list: Union[List[Any], np.ndarray], c_type):
12181220
obx_sync_listener_login_failure = c_fn('obx_sync_listener_login_failure', None, [OBX_sync_p, OBX_sync_listener_login_failure, ctypes.c_void_p])
12191221
obx_sync_listener_error = c_fn('obx_sync_listener_error', None, [OBX_sync_p, OBX_sync_listener_error, ctypes.c_void_p])
12201222

1221-
obx_sync_wait_for_logged_in_state = c_fn_rc('obx_sync_wait_for_logged_in_state', [OBX_sync_p, ctypes.c_uint64])
1223+
obx_sync_wait_for_logged_in_state = c_fn_rc('obx_sync_wait_for_logged_in_state', [OBX_sync_p, ctypes.c_uint64])
1224+
1225+
obx_sync_filter_variables_put = c_fn_rc('obx_sync_filter_variables_put',
1226+
[OBX_sync_p, c_char_p, c_char_p])
1227+
obx_sync_filter_variables_remove = c_fn_rc('obx_sync_filter_variables_remove',
1228+
[OBX_sync_p, c_char_p])
1229+
obx_sync_filter_variables_remove_all = c_fn_rc('obx_sync_filter_variables_remove_all',
1230+
[OBX_sync_p])

objectbox/sync.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
import ctypes
2-
from collections.abc import Callable
3-
from ctypes import c_void_p
2+
from enum import Enum, auto, IntEnum
43

54
import objectbox.c as c
65
from objectbox import Store
7-
from enum import Enum, auto, IntEnum
8-
9-
from objectbox.c import OBX_sync_listener_login
106

117

128
class SyncCredentials:
@@ -92,6 +88,7 @@ class SyncLoginEvent:
9288
CREDENTIALS_REJECTED = 'credentials_rejected'
9389
UNKNOWN_ERROR = 'unknown_error'
9490

91+
9592
class SyncCode(IntEnum):
9693
OK = 20
9794
REQ_REJECTED = 40
@@ -102,13 +99,15 @@ class SyncCode(IntEnum):
10299
CLIENT_ID_TAKEN = 61
103100
TX_VIOLATED_UNIQUE = 71
104101

102+
105103
class SyncChange:
106104
def __init__(self, entity_id: int, entity: type, puts: list[int], removals: list[int]):
107105
self.entity_id = entity_id
108106
self.entity = entity
109107
self.puts = puts
110108
self.removals = removals
111109

110+
112111
class SyncLoginListener:
113112

114113
def on_logged_in(self):
@@ -117,6 +116,7 @@ def on_logged_in(self):
117116
def on_login_failed(self, sync_login_code: SyncCode):
118117
pass
119118

119+
120120
class SyncConnectionListener:
121121

122122
def on_connected(self):
@@ -125,11 +125,13 @@ def on_connected(self):
125125
def on_disconnected(self):
126126
pass
127127

128+
128129
class SyncErrorListener:
129130

130131
def on_error(self, sync_error_code: int):
131132
pass
132133

134+
133135
class SyncClient:
134136

135137
def __init__(self, store: Store, server_urls: list[str],
@@ -151,7 +153,8 @@ def __init__(self, store: Store, server_urls: list[str],
151153
self.__store = store
152154
self.__server_urls = [url.encode('utf-8') for url in server_urls]
153155

154-
self.__c_sync_client_ptr = c.obx_sync_urls(store.c_store(), c.c_array_pointer(self.__server_urls, ctypes.c_char_p),
156+
self.__c_sync_client_ptr = c.obx_sync_urls(store.c_store(),
157+
c.c_array_pointer(self.__server_urls, ctypes.c_char_p),
155158
len(self.__server_urls))
156159

157160
def set_credentials(self, credentials: SyncCredentials):
@@ -223,7 +226,8 @@ def is_closed(self) -> bool:
223226

224227
def set_login_listener(self, login_listener: SyncLoginListener):
225228
self.__c_login_listener = c.OBX_sync_listener_login(lambda arg: login_listener.on_logged_in())
226-
self.__c_login_failure_listener = c.OBX_sync_listener_login_failure(lambda arg, sync_login_code: login_listener.on_login_failed(sync_login_code))
229+
self.__c_login_failure_listener = c.OBX_sync_listener_login_failure(
230+
lambda arg, sync_login_code: login_listener.on_login_failed(sync_login_code))
227231
c.obx_sync_listener_login(
228232
self.__c_sync_client_ptr,
229233
self.__c_login_listener,
@@ -250,12 +254,22 @@ def set_connection_listener(self, connection_listener: SyncConnectionListener):
250254
)
251255

252256
def set_error_listener(self, error_listener: SyncErrorListener):
253-
self.__c_error_listener = c.OBX_sync_listener_error(lambda arg, sync_error_code: error_listener.on_error(sync_error_code))
257+
self.__c_error_listener = c.OBX_sync_listener_error(
258+
lambda arg, sync_error_code: error_listener.on_error(sync_error_code))
254259
c.obx_sync_listener_error(
255260
self.__c_sync_client_ptr,
256261
self.__c_error_listener,
257262
None
258263
)
259264

260265
def wait_for_logged_in_state(self, timeout_millis: int):
261-
c.obx_sync_wait_for_logged_in_state(self.__c_sync_client_ptr, timeout_millis)
266+
c.obx_sync_wait_for_logged_in_state(self.__c_sync_client_ptr, timeout_millis)
267+
268+
def add_filter_variable(self, name: str, value: str):
269+
c.obx_sync_filter_variables_put(self.__c_sync_client_ptr, name.encode('utf-8'), value.encode('utf-8'))
270+
271+
def remove_filter_variable(self, name: str):
272+
c.obx_sync_filter_variables_remove(self.__c_sync_client_ptr, name.encode('utf-8'))
273+
274+
def remove_all_filter_variables(self):
275+
c.obx_sync_filter_variables_remove_all(self.__c_sync_client_ptr)

tests/test_sync.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
from time import sleep
2+
3+
import pytest
4+
5+
from objectbox.exceptions import IllegalArgumentError
26
from objectbox.sync import *
37

8+
49
def test_sync_protocol_version():
510
version = SyncClient.protocol_version()
611
assert version >= 1
@@ -32,3 +37,21 @@ def test_sync_listener(test_store, login_listener, connection_listener):
3237
assert login_listener.login_failure_code == SyncCode.CREDENTIALS_REJECTED
3338
assert connection_listener.connected_called
3439
assert connection_listener.disconnected_called
40+
41+
42+
def test_filter_variables(test_store):
43+
server_urls = ["ws://localhost:9999"]
44+
45+
filter_vars = {
46+
"name1": "val1",
47+
"name2": "val2"
48+
}
49+
client = SyncClient(test_store, server_urls, filter_vars)
50+
51+
client.add_filter_variable("name3", "val3")
52+
client.remove_filter_variable("name1")
53+
client.add_filter_variable("name4", "val4")
54+
client.remove_all_filter_variables()
55+
56+
with pytest.raises(IllegalArgumentError, match="Filter variables must have a name"):
57+
client.add_filter_variable("", "val5")

0 commit comments

Comments
 (0)