-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathbox.py
More file actions
188 lines (153 loc) · 6.8 KB
/
Copy pathbox.py
File metadata and controls
188 lines (153 loc) · 6.8 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# Copyright 2019-2024 ObjectBox Ltd. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from objectbox.model.entity import _Entity
from objectbox.store import Store
from objectbox.query_builder import QueryBuilder
from objectbox.condition import QueryCondition
from objectbox.c import *
from objectbox.exceptions import DbError
class Box:
"""Interface to Entities"""
def __init__(self, store: Store, entity: _Entity):
if not isinstance(entity, _Entity):
raise Exception("Given type is not an Entity")
self._store = store
self._entity = entity
self._c_box = obx_box(store._c_store, entity._id)
def is_empty(self) -> bool:
"""Returns true if box is empty (i.e. no objects of entity type are available)."""
is_empty = ctypes.c_bool()
obx_box_is_empty(self._c_box, ctypes.byref(is_empty))
return bool(is_empty.value)
def count(self, limit: int = 0) -> int:
"""Returns the count of existing objects."""
count = ctypes.c_uint64()
obx_box_count(self._c_box, limit, ctypes.byref(count))
return int(count.value)
def put(self, *objects):
"""Puts an object (or a list of objects) and returns its ID (or nothing for a list objects)"""
if len(objects) != 1:
self._put_many(objects)
elif isinstance(objects[0], list):
self._put_many(objects[0])
else:
return self._put_one(objects[0])
def _put_one(self, obj) -> int:
id = object_id = self._entity._get_object_id(obj)
if not id:
id = obx_box_id_for_put(self._c_box, 0)
data = self._entity._marshal(obj, id)
obx_box_put(self._c_box, id, bytes(data), len(data))
if id != object_id:
self._entity._set_object_id(obj, id)
return id
def _put_many(self, objects) -> None:
# retrieve IDs from the objects (to distinguish new objects and updates)
new = {}
ids = {}
for k in range(len(objects)):
id = self._entity._get_object_id(objects[k])
if not id:
new[k] = 0
ids[k] = id
# acquire IDs for the new objects and set them
if len(new) > 0:
c_next_id = obx_id()
obx_box_ids_for_put(self._c_box, len(new), ctypes.byref(c_next_id))
next_id = c_next_id.value
for k in new.keys():
ids[k] = next_id
next_id += 1
# allocate a C bytes array structure where we will push the object data
# OBX_bytes_array with .count = len(objects)
c_bytes_array_p = obx_bytes_array(len(objects))
try:
# we need to keep the data around until put_many is executed because obx_bytes_array_set doesn't do a copy
data = {}
for k in range(len(objects)):
data[k] = bytes(self._entity._marshal(objects[k], ids[k]))
key = ctypes.c_size_t(k)
# OBX_bytes_array.data[k] = data
obx_bytes_array_set(c_bytes_array_p, key,
data[k], len(data[k]))
c_ids = (obx_id * len(ids))(*ids.values())
obx_box_put_many(self._c_box, c_bytes_array_p,
c_ids, OBXPutMode_PUT)
finally:
obx_bytes_array_free(c_bytes_array_p)
# assign new IDs on the object
for k in new.keys():
self._entity._set_object_id(objects[k], ids[k])
def get(self, id: int):
"""Get object by given Id or None if not found."""
with self._store.read_tx():
c_data = ctypes.c_void_p()
c_size = ctypes.c_size_t()
code : obx_err = obx_box_get(self._c_box, id, ctypes.byref(
c_data), ctypes.byref(c_size))
if code == 404:
return None
elif code != 0:
raise DbError.from_code(code)
data = c_voidp_as_bytes(c_data, c_size.value)
return self._entity._unmarshal(data)
def get_all(self) -> list:
"""Get all objects."""
with self._store.read_tx():
# OBX_bytes_array*
c_bytes_array_p = obx_box_get_all(self._c_box)
try:
# OBX_bytes_array
c_bytes_array = c_bytes_array_p.contents
result = list()
for i in range(c_bytes_array.count):
# OBX_bytes
c_bytes = c_bytes_array.data[i]
data = c_voidp_as_bytes(c_bytes.data, c_bytes.size)
result.append(self._entity._unmarshal(data))
return result
finally:
obx_bytes_array_free(c_bytes_array_p)
def remove(self, id_or_object) -> bool:
"""Remove object by id or object."""
if isinstance(id_or_object, self._entity._user_type):
id = self._entity._get_object_id(id_or_object)
else:
id = id_or_object
code : obx_err = obx_box_remove(self._c_box, id)
if code == 404:
return False
elif code != 0:
raise DbError.from_code(code)
return True
def remove_all(self) -> int:
"""Removes all objects and returns number of removed."""
count = ctypes.c_uint64()
obx_box_remove_all(self._c_box, ctypes.byref(count))
return int(count.value)
def query(self, condition: Optional[QueryCondition] = None) -> QueryBuilder:
""" Initiates Query creation for the Entity associated by this Box.
Technically, it creates a QueryBuilder object, and you have to call build() on it to get the Query object.
:param condition:
Applies the given condition(s) to the new QueryBuilder object.
For example, assuming you defined an @Entity called "MyEntity" with a string property "name":
``query = box.query(MyEntity.name.equals("Johnny")).build()``
It's also possible to pass multiple conditions:
``query = box.query(MyEntity.name.equals("Johnny") & MyEntity.age.greater(21)).build()``
Note: ``&`` is the logical AND operator, and ``|`` is the logical OR operator.
"""
qb = QueryBuilder(self._store, self)
if condition is not None:
condition.apply(qb)
return qb