Skip to content

Commit 3e587d1

Browse files
committed
Query find_with_scores fix: add read transaction
We need a read transaction to ensure the object data stays valid
1 parent 631840d commit 3e587d1

1 file changed

Lines changed: 21 additions & 19 deletions

File tree

objectbox/query.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def __init__(self, c_query, box: 'Box'):
2424

2525
def find(self) -> list:
2626
""" Finds a list of objects matching query. """
27-
with self._store.read_tx():
27+
with self._store.read_tx(): # We need a read transaction to ensure the object data stays valid
2828
# OBX_bytes_array*
2929
c_bytes_array_p = obx_query_find(self._c_query)
3030
try:
@@ -56,21 +56,22 @@ def find_ids(self) -> List[int]:
5656
def find_with_scores(self):
5757
""" Finds objects matching the query associated to their query score (e.g. distance in NN search).
5858
The result is sorted by score in ascending order. """
59-
c_bytes_score_array_p = obx_query_find_with_scores(self._c_query)
60-
try:
61-
# OBX_bytes_score_array
62-
c_bytes_score_array: OBX_bytes_score_array = c_bytes_score_array_p.contents
63-
result = []
64-
for i in range(c_bytes_score_array.count):
65-
c_bytes_score: OBX_bytes_score = c_bytes_score_array.bytes_scores[i]
66-
data = c_voidp_as_bytes(c_bytes_score.data, c_bytes_score.size)
67-
score = c_bytes_score.score
59+
with self._store.read_tx(): # We need a read transaction to ensure the object data stays valid
60+
c_bytes_score_array_p = obx_query_find_with_scores(self._c_query)
61+
try:
62+
# OBX_bytes_score_array
63+
c_bytes_score_array: OBX_bytes_score_array = c_bytes_score_array_p.contents
64+
result = []
65+
for i in range(c_bytes_score_array.count):
66+
c_bytes_score: OBX_bytes_score = c_bytes_score_array.bytes_scores[i]
67+
data = c_voidp_as_bytes(c_bytes_score.data, c_bytes_score.size)
68+
score = c_bytes_score.score
6869

69-
object_ = self._box._entity.unmarshal(data)
70-
result.append((object_, score))
71-
return result
72-
finally:
73-
obx_bytes_score_array_free(c_bytes_score_array_p)
70+
object_ = self._box._entity.unmarshal(data)
71+
result.append((object_, score))
72+
return result
73+
finally:
74+
obx_bytes_score_array_free(c_bytes_score_array_p)
7475

7576
def find_ids_with_scores(self) -> List[Tuple[int, float]]:
7677
""" Finds object IDs matching the query associated to their query score (e.g. distance in NN search).
@@ -125,18 +126,19 @@ def set_parameter_vector_f32(self,
125126
num_el = len(value)
126127
obx_query_param_vector_float32(self._c_query, self._entity.id, prop_id, c_value, num_el)
127128
return self
128-
129+
129130
def offset(self, offset: int):
130131
return obx_query_offset(self._c_query, offset)
131-
132+
132133
def limit(self, limit: int):
133134
return obx_query_limit(self._c_query, limit)
134135

135136
def set_parameter_alias_string(self, alias: str, value: str):
136-
return obx_query_param_alias_string(self._c_query,c_str(alias), c_str(value))
137+
return obx_query_param_alias_string(self._c_query, c_str(alias), c_str(value))
137138

138139
def set_parameter_alias_int(self, alias: str, value: int):
139140
return obx_query_param_alias_int(self._c_query, c_str(alias), value)
140141

141142
def set_parameter_alias_vector_f32(self, alias: str, value: Union[List[float], np.ndarray]):
142-
return obx_query_param_alias_vector_float32(self._c_query, c_str(alias), c_array(value, ctypes.c_float), len(value))
143+
return obx_query_param_alias_vector_float32(self._c_query, c_str(alias), c_array(value, ctypes.c_float),
144+
len(value))

0 commit comments

Comments
 (0)