Skip to content

Commit 17bfe4c

Browse files
committed
idsync: calc Property fb attributes only on sync #25
1 parent 2b85863 commit 17bfe4c

4 files changed

Lines changed: 22 additions & 7 deletions

File tree

objectbox/model/entity.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ def uid(self) -> int:
5353
def has_uid(self) -> bool:
5454
return self.iduid.uid != 0
5555

56+
def on_sync(self):
57+
""" Method called once ID/UID are synced with the model file. """
58+
for prop in self.properties:
59+
prop.on_sync()
60+
5661
def __call__(self, **properties):
5762
""" The constructor of the user Entity class. """
5863
object_ = self.user_type()
@@ -198,9 +203,7 @@ def marshal(self, object, id: int) -> bytearray:
198203
val = date_value_to_int(val, 1000000000) # convert to nanoseconds
199204
builder.Prepend(prop._fb_type, val)
200205

201-
fb_slot = prop_id - 1
202-
# fb_v_offset = 4 + 2 * fb_slot
203-
builder.Slot(fb_slot)
206+
builder.Slot(prop._fb_slot)
204207

205208
builder.Finish(builder.EndObject())
206209
return builder.Output()
@@ -216,10 +219,7 @@ def unmarshal(self, data: bytes):
216219

217220
# fill it with the data read from FlatBuffers
218221
for prop in self.properties:
219-
fb_slot = prop.id - 1
220-
fb_v_offset = 4 + 2 * fb_slot
221-
# 4 + 2 * fb_slot
222-
o = table.Offset(fb_v_offset)
222+
o = table.Offset(prop._fb_v_offset)
223223
val = None
224224
ob_type = prop._ob_type
225225
if not o:

objectbox/model/idsync.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,8 @@ def sync(self) -> bool:
285285
logger.info(f"Model changed, writing model.json: {self.model_filepath}")
286286
self._save_model_json()
287287

288+
self.model.on_sync() # Notify model synced
289+
288290
return write_json
289291

290292

objectbox/model/model.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ def __init__(self):
3030

3131
self._c_model = None
3232

33+
def on_sync(self):
34+
""" Method called once ID/UID are synced with the model file. """
35+
for entity in self.entities:
36+
entity.on_sync()
37+
3338
def entity(self, entity: _Entity):
3439
if not isinstance(entity, _Entity):
3540
raise Exception(f"The given type is not an Entity: {type(entity)}. "

objectbox/model/properties.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ def __init__(self, pytype: Type, uid: int = 0, **kwargs):
182182
self._flags = 0
183183
self._set_flags()
184184

185+
self._fb_slot = None
186+
self._fb_v_offset = None
187+
185188
@property
186189
def id(self):
187190
return self.iduid.id
@@ -196,6 +199,11 @@ def has_uid(self):
196199
def is_id(self) -> bool:
197200
return isinstance(self, Id)
198201

202+
def on_sync(self):
203+
""" Method called once ID/UID are synced with the model file. """
204+
self._fb_slot = self.id - 1
205+
self._fb_v_offset = 4 + 2 * self._fb_slot
206+
199207
def _determine_ob_type(self) -> OBXPropertyType:
200208
""" Tries to infer the OBX property type from the Python type. """
201209
ts = self._py_type

0 commit comments

Comments
 (0)