Skip to content

Commit 7326e0a

Browse files
committed
added Store, deprecated ObjectBox #44
1 parent 8aa7465 commit 7326e0a

9 files changed

Lines changed: 71 additions & 46 deletions

File tree

objectbox/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from objectbox.box import Box
1717
from objectbox.builder import Builder
1818
from objectbox.model import Model
19+
from objectbox.store import Store
1920
from objectbox.objectbox import ObjectBox
2021
from objectbox.c import NotFoundException, version_core
2122
from objectbox.version import Version
@@ -24,6 +25,7 @@
2425
'Box',
2526
'Builder',
2627
'Model',
28+
'Store',
2729
'ObjectBox',
2830
'NotFoundException',
2931
'version',

objectbox/box.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@
1414

1515

1616
from objectbox.model.entity import _Entity
17-
from objectbox.objectbox import ObjectBox
17+
from objectbox.store import Store
1818
from objectbox.query_builder import QueryBuilder
1919
from objectbox.condition import QueryCondition
2020
from objectbox.c import *
2121

2222

2323
class Box:
24-
def __init__(self, ob: ObjectBox, entity: _Entity):
24+
def __init__(self, store: Store, entity: _Entity):
2525
if not isinstance(entity, _Entity):
2626
raise Exception("Given type is not an Entity")
2727

28-
self._ob = ob
28+
self._store = store
2929
self._entity = entity
30-
self._c_box = obx_box(ob._c_store, entity.id)
30+
self._c_box = obx_box(store._c_store, entity.id)
3131

3232
def is_empty(self) -> bool:
3333
is_empty = ctypes.c_bool()
@@ -109,7 +109,7 @@ def _put_many(self, objects) -> None:
109109
self._entity.set_object_id(objects[k], ids[k])
110110

111111
def get(self, id: int):
112-
with self._ob.read_tx():
112+
with self._store.read_tx():
113113
c_data = ctypes.c_void_p()
114114
c_size = ctypes.c_size_t()
115115
code : obx_err = obx_box_get(self._c_box, id, ctypes.byref(
@@ -122,7 +122,7 @@ def get(self, id: int):
122122
return self._entity.unmarshal(data)
123123

124124
def get_all(self) -> list:
125-
with self._ob.read_tx():
125+
with self._store.read_tx():
126126
# OBX_bytes_array*
127127
c_bytes_array_p = obx_box_get_all(self._c_box)
128128

@@ -166,7 +166,7 @@ def query(self, condition: Optional[QueryCondition] = None) -> QueryBuilder:
166166
Useful for a user-friendly API design; for example:
167167
``box.query(name_property.equals("Johnny")).build()``
168168
"""
169-
qb = QueryBuilder(self._ob, self)
169+
qb = QueryBuilder(self._store, self)
170170
if condition is not None:
171171
condition.apply(qb)
172172
return qb

objectbox/builder.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
from objectbox.c import *
1717
from objectbox.model import Model
18-
from objectbox.objectbox import ObjectBox
18+
from objectbox.store import Store
1919
from objectbox.store_options import StoreOptions
2020

2121

@@ -38,7 +38,7 @@ def model(self, model: Model) -> 'Builder':
3838
self._model._finish()
3939
return self
4040

41-
def build(self) -> 'ObjectBox':
41+
def build(self) -> 'Store':
4242
options = StoreOptions()
4343
try:
4444
if self._directory:
@@ -50,4 +50,4 @@ def build(self) -> 'ObjectBox':
5050
options._free()
5151
raise
5252
c_store = obx_store_open(options._c_handle)
53-
return ObjectBox(c_store)
53+
return Store(c_store)

objectbox/objectbox.py

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,11 @@
1313
# limitations under the License.
1414

1515

16-
from objectbox.c import *
17-
import objectbox.transaction
18-
19-
20-
class ObjectBox:
21-
def __init__(self, c_store: OBX_store_p):
22-
self._c_store = c_store
23-
24-
def __del__(self):
25-
self.close()
26-
27-
def read_tx(self):
28-
return objectbox.transaction.read(self)
29-
30-
def write_tx(self):
31-
return objectbox.transaction.write(self)
32-
33-
def close(self):
34-
c_store_to_close = self._c_store
35-
if c_store_to_close:
36-
self._c_store = None
37-
obx_store_close(c_store_to_close)
16+
import objectbox.store
17+
from warnings import warn
18+
19+
class ObjectBox(objectbox.store.Store):
20+
def __init__(self, *args, **kwargs):
21+
"""This throws a deprecation warning on initialization."""
22+
warn(f'{self.__class__.__name__} will be deprecated, use Store from objectbox.store.', DeprecationWarning, stacklevel=2)
23+
super().__init__(*args, **kwargs)

objectbox/query.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ def __init__(self, c_query, box: 'Box'):
2020
self._c_query = c_query
2121
self._box = box
2222
self._entity = self._box._entity
23-
self._ob = box._ob
23+
self._store = box._store
2424

2525
def find(self) -> list:
2626
""" Finds a list of objects matching query. """
27-
with self._ob.read_tx():
27+
with self._store.read_tx():
2828
# OBX_bytes_array*
2929
c_bytes_array_p = obx_query_find(self._c_query)
3030
try:

objectbox/query_builder.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44

55
from objectbox.c import *
66
from objectbox.model.properties import Property
7-
from objectbox.objectbox import ObjectBox
7+
from objectbox.store import Store
88
from objectbox.query import Query
99
from objectbox.utils import check_float_vector
1010

1111

1212
class QueryBuilder:
13-
def __init__(self, ob: ObjectBox, box: 'Box'):
13+
def __init__(self, store: Store, box: 'Box'):
1414
self._box = box
1515
self._entity = box._entity
16-
self._c_builder = obx_query_builder(ob._c_store, box._entity.id)
16+
self._c_builder = obx_query_builder(store._c_store, box._entity.id)
1717

1818
def close(self) -> int:
1919
return obx_qb_close(self._c_builder)

objectbox/store.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright 2019-2024 ObjectBox Ltd. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
from objectbox.c import *
17+
import objectbox.transaction
18+
19+
20+
class Store:
21+
def __init__(self, c_store: OBX_store_p):
22+
self._c_store = c_store
23+
24+
def __del__(self):
25+
self.close()
26+
27+
def read_tx(self):
28+
return objectbox.transaction.read(self)
29+
30+
def write_tx(self):
31+
return objectbox.transaction.write(self)
32+
33+
def close(self):
34+
c_store_to_close = self._c_store
35+
if c_store_to_close:
36+
self._c_store = None
37+
obx_store_close(c_store_to_close)

objectbox/transaction.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@
1717

1818

1919
@contextmanager
20-
def read(ob: 'ObjectBox'):
21-
tx = obx_txn_read(ob._c_store)
20+
def read(store: 'Store'):
21+
tx = obx_txn_read(store._c_store)
2222
try:
2323
yield
2424
finally:
2525
obx_txn_close(tx)
2626

2727

2828
@contextmanager
29-
def write(ob: 'ObjectBox'):
30-
tx = obx_txn_write(ob._c_store)
29+
def write(store: 'Store'):
30+
tx = obx_txn_write(store._c_store)
3131
try:
3232
yield
3333
obx_txn_success(tx)

tests/common.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
test_dir = 'testdata'
1111

1212

13-
def load_empty_test_objectbox(db_name: str = test_dir) -> objectbox.ObjectBox:
13+
def load_empty_test_objectbox(db_name: str = test_dir) -> objectbox.Store:
1414
model = objectbox.Model()
1515
model.entity(TestEntity, last_property_id=IdUid(27, 1027))
1616
model.last_entity_id = IdUid(2, 2)
@@ -19,7 +19,7 @@ def load_empty_test_objectbox(db_name: str = test_dir) -> objectbox.ObjectBox:
1919
return objectbox.Builder().model(model).directory(db_name).build()
2020

2121

22-
def load_empty_test_datetime(name: str = "") -> objectbox.ObjectBox:
22+
def load_empty_test_datetime(name: str = "") -> objectbox.Store:
2323
model = objectbox.Model()
2424
model.entity(TestEntityDatetime, last_property_id=IdUid(4, 2004))
2525
model.last_entity_id = IdUid(2, 2)
@@ -29,7 +29,7 @@ def load_empty_test_datetime(name: str = "") -> objectbox.ObjectBox:
2929
return objectbox.Builder().model(model).directory(db_name).build()
3030

3131

32-
def load_empty_test_flex(name: str = "") -> objectbox.ObjectBox:
32+
def load_empty_test_flex(name: str = "") -> objectbox.Store:
3333
model = objectbox.Model()
3434
model.entity(TestEntityFlex, last_property_id=IdUid(2, 3002))
3535
model.last_entity_id = IdUid(3, 3)
@@ -39,8 +39,8 @@ def load_empty_test_flex(name: str = "") -> objectbox.ObjectBox:
3939
return objectbox.Builder().model(model).directory(db_name).build()
4040

4141

42-
def create_test_objectbox(db_name: Optional[str] = None, clear_db: bool = True) -> objectbox.ObjectBox:
43-
""" Creates an ObjectBox instance with all entities. """
42+
def create_test_objectbox(db_name: Optional[str] = None, clear_db: bool = True) -> objectbox.Store:
43+
""" Creates a Store instance with all entities. """
4444

4545
db_path = test_dir if db_name is None else path.join(test_dir, db_name)
4646
print(f"DB path: \"{db_path}\"")

0 commit comments

Comments
 (0)