Skip to content

Commit decab4c

Browse files
loryrutagreenrobot
authored andcommitted
Add ObjectBoxException, rename CoreException to StorageException #64
1 parent 9da8d62 commit decab4c

File tree

7 files changed

+189
-172
lines changed

7 files changed

+189
-172
lines changed

objectbox/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
from objectbox.model.entity import Entity
2020
from objectbox.model.properties import Id, String, Index, Bool, Int8, Int16, Int32, Int64, Float32, Float64, Bytes, BoolVector, Int8Vector, Int16Vector, Int32Vector, Int64Vector, Float32Vector, Float64Vector, CharVector, BoolList, Int8List, Int16List, Int32List, Int64List, Float32List, Float64List, CharList, Date, DateNano, Flex, HnswIndex, VectorDistanceType, HnswFlags
2121
from objectbox.model.model import Model
22-
from objectbox.c import DbException, CoreException, version_core, DebugFlags
22+
from objectbox.c import version_core, DebugFlags
23+
from objectbox.exceptions import StorageException
2324
from objectbox.version import Version
2425
from objectbox.condition import PropertyQueryCondition
2526
from objectbox.query import Query
@@ -66,7 +67,7 @@
6667
'VectorDistanceType',
6768
'Store',
6869
'ObjectBox',
69-
'CoreException',
70+
'StorageException',
7071
'version',
7172
'version_info',
7273
'DebugFlags',

objectbox/box.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from objectbox.query_builder import QueryBuilder
1919
from objectbox.condition import QueryCondition
2020
from objectbox.c import *
21+
from objectbox.exceptions import StorageException
2122

2223

2324
class Box:
@@ -121,7 +122,7 @@ def get(self, id: int):
121122
if code == 404:
122123
return None
123124
elif code != 0:
124-
raise CoreException(code)
125+
raise StorageException.from_code(code)
125126
data = c_voidp_as_bytes(c_data, c_size.value)
126127
return self._entity._unmarshal(data)
127128

@@ -156,7 +157,7 @@ def remove(self, id_or_object) -> bool:
156157
if code == 404:
157158
return False
158159
elif code != 0:
159-
raise CoreException(code)
160+
raise StorageException.from_code(code)
160161
return True
161162

162163
def remove_all(self) -> int:

objectbox/c.py

Lines changed: 50 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -94,32 +94,33 @@ class LogLevel(IntEnum):
9494
Warn = 40
9595
Error = 50
9696

97+
9798
class DebugFlags(IntEnum):
9899
"""Debug flags"""
99-
100+
100101
NONE = 0,
101-
102+
102103
LOG_TRANSACTIONS_READ = 1,
103104
""" Log read transactions """
104-
105+
105106
LOG_TRANSACTIONS_WRITE = 2,
106107
""" Log write transactions """
107-
108+
108109
LOG_QUERIES = 3,
109110
""" Log queries """
110-
111+
111112
LOG_QUERY_PARAMETERS = 8,
112113
""" Log query parameters """
113-
114+
114115
LOG_ASYNC_QUEUE = 16,
115116
""" Log async queue """
116-
117+
117118
LOG_CACHE_HITS = 32,
118119
""" Log cache hits """
119-
120+
120121
LOG_CACHE_ALL = 64,
121122
""" Log cache hits """
122-
123+
123124
LOG_TREE = 128
124125
""" Log tree operations """
125126

@@ -255,12 +256,7 @@ class OBX_query(ctypes.Structure):
255256
C.obx_last_error_code.restype = obx_err
256257

257258

258-
class DbException(Exception):
259-
""" Base class for database exceptions. """
260-
pass
261-
262-
263-
class CoreExceptionCode(IntEnum):
259+
class StorageErrorCode(IntEnum):
264260
OBX_SUCCESS = 0
265261
OBX_NOT_FOUND = 404
266262
OBX_NO_SUCCESS = 1001
@@ -307,42 +303,27 @@ class CoreExceptionCode(IntEnum):
307303
OBX_ERROR_TREE_OTHER = 10699
308304

309305

310-
# TODO rename?
311-
class CoreException(DbException):
312-
"""A database exception having a ``code`` attribute for error details."""
313-
code = CoreExceptionCode.OBX_NO_SUCCESS # Re-defined by the derived classes
314-
315-
def __init__(self):
316-
self.message = py_str(C.obx_last_error_message())
317-
super(CoreException, self).__init__("%d (%s) - %s" % (self.code.value, self.code.name, self.message))
318-
319-
@staticmethod
320-
def last():
321-
"""Creates a CoreException of the last error that was generated in core."""
322-
return CoreException(C.obx_last_error())
323-
324-
325306
def check_obx_err(code: obx_err, func, args) -> obx_err:
326307
""" Raises an exception if obx_err is not successful. """
327-
if code != CoreExceptionCode.OBX_SUCCESS:
328-
from objectbox.exceptions import create_core_exception
329-
raise create_core_exception(code)
308+
if code != StorageErrorCode.OBX_SUCCESS:
309+
from objectbox.exceptions import create_storage_exception
310+
raise create_storage_exception(code)
330311
return code
331312

332313

333314
def check_obx_qb_cond(qb_cond: obx_qb_cond, func, args) -> obx_qb_cond:
334315
""" Raises an exception if obx_qb_cond is not successful. """
335316
if qb_cond == 0:
336-
from objectbox.exceptions import create_core_exception
337-
raise create_core_exception(C.obx_last_error_code())
317+
from objectbox.exceptions import create_storage_exception
318+
raise create_storage_exception(C.obx_last_error_code())
338319
return qb_cond
339320

340321

341322
# assert that the returned pointer/int is non-empty
342323
def check_result(result, func, args):
343324
if not result:
344-
from objectbox.exceptions import create_core_exception
345-
raise create_core_exception(C.obx_last_error_code())
325+
from objectbox.exceptions import create_storage_exception
326+
raise create_storage_exception(C.obx_last_error_code())
346327
return result
347328

348329

@@ -423,10 +404,13 @@ def c_array_pointer(py_list: Union[List[Any], np.ndarray], c_type):
423404

424405

425406
# OBX_C_API float obx_vector_distance_float32(OBXVectorDistanceType type, const float* vector1, const float* vector2, size_t dimension);
426-
obx_vector_distance_float32 = c_fn("obx_vector_distance_float32", ctypes.c_float, [OBXVectorDistanceType, ctypes.POINTER(ctypes.c_float), ctypes.POINTER(ctypes.c_float), ctypes.c_size_t])
407+
obx_vector_distance_float32 = c_fn("obx_vector_distance_float32", ctypes.c_float,
408+
[OBXVectorDistanceType, ctypes.POINTER(ctypes.c_float),
409+
ctypes.POINTER(ctypes.c_float), ctypes.c_size_t])
427410

428411
# OBX_C_API float obx_vector_distance_to_relevance(OBXVectorDistanceType type, float distance);
429-
obx_vector_distance_to_relevance = c_fn("obx_vector_distance_to_relevance", ctypes.c_float, [OBXVectorDistanceType, ctypes.c_float])
412+
obx_vector_distance_to_relevance = c_fn("obx_vector_distance_to_relevance", ctypes.c_float,
413+
[OBXVectorDistanceType, ctypes.c_float])
430414

431415
# OBX_model* (void);
432416
obx_model = c_fn('obx_model', OBX_model_p, [])
@@ -462,7 +446,8 @@ def c_array_pointer(py_list: Union[List[Any], np.ndarray], c_type):
462446
c_fn_rc('obx_model_property_index_hnsw_flags', [OBX_model_p, OBXHnswFlags])
463447

464448
# obx_err obx_model_property_index_hnsw_distance_type(OBX_model* model, OBXVectorDistanceType value)
465-
obx_model_property_index_hnsw_distance_type = c_fn_rc('obx_model_property_index_hnsw_distance_type', [OBX_model_p, OBXVectorDistanceType])
449+
obx_model_property_index_hnsw_distance_type = c_fn_rc('obx_model_property_index_hnsw_distance_type',
450+
[OBX_model_p, OBXVectorDistanceType])
466451

467452
# obx_err obx_model_property_index_hnsw_reparation_backlink_probability(OBX_model* model, float value)
468453
obx_model_property_index_hnsw_reparation_backlink_probability = \
@@ -516,10 +501,12 @@ def c_array_pointer(py_list: Union[List[Any], np.ndarray], c_type):
516501
obx_opt_model_bytes = c_fn_rc('obx_opt_model_bytes', [OBX_store_options_p, ctypes.c_void_p, ctypes.c_size_t])
517502

518503
# OBX_C_API obx_err obx_opt_model_bytes_direct(OBX_store_options* opt, const void* bytes, size_t size);
519-
obx_opt_model_bytes_direct = c_fn_rc('obx_opt_model_bytes_direct', [OBX_store_options_p, ctypes.c_void_p, ctypes.c_size_t])
504+
obx_opt_model_bytes_direct = c_fn_rc('obx_opt_model_bytes_direct',
505+
[OBX_store_options_p, ctypes.c_void_p, ctypes.c_size_t])
520506

521507
# OBX_C_API void obx_opt_validate_on_open_pages(OBX_store_options* opt, size_t page_limit, uint32_t flags);
522-
obx_opt_validate_on_open_pages = c_fn('obx_opt_validate_on_open_pages', None, [OBX_store_options_p, ctypes.c_size_t, OBXValidateOnOpenPagesFlags])
508+
obx_opt_validate_on_open_pages = c_fn('obx_opt_validate_on_open_pages', None,
509+
[OBX_store_options_p, ctypes.c_size_t, OBXValidateOnOpenPagesFlags])
523510

524511
# OBX_C_API void obx_opt_validate_on_open_kv(OBX_store_options* opt, uint32_t flags);
525512
obx_opt_validate_on_open_kv = c_fn('obx_opt_validate_on_open_kv', None, [OBX_store_options_p, OBXValidateOnOpenKvFlags])
@@ -546,43 +533,53 @@ def c_array_pointer(py_list: Union[List[Any], np.ndarray], c_type):
546533
obx_opt_async_max_queue_length = c_fn('obx_opt_async_max_queue_length', None, [OBX_store_options_p, ctypes.c_size_t])
547534

548535
# OBX_C_API void obx_opt_async_throttle_at_queue_length(OBX_store_options* opt, size_t value);
549-
obx_opt_async_throttle_at_queue_length = c_fn('obx_opt_async_throttle_at_queue_length', None, [OBX_store_options_p, ctypes.c_size_t])
536+
obx_opt_async_throttle_at_queue_length = c_fn('obx_opt_async_throttle_at_queue_length', None,
537+
[OBX_store_options_p, ctypes.c_size_t])
550538

551539
# OBX_C_API void obx_opt_async_throttle_micros(OBX_store_options* opt, uint32_t value);
552540
obx_opt_async_throttle_micros = c_fn('obx_opt_async_throttle_micros', None, [OBX_store_options_p, ctypes.c_uint32])
553541

554542
# OBX_C_API void obx_opt_async_max_in_tx_duration(OBX_store_options* opt, uint32_t micros);
555-
obx_opt_async_max_in_tx_duration = c_fn('obx_opt_async_max_in_tx_duration', None, [OBX_store_options_p, ctypes.c_uint32])
543+
obx_opt_async_max_in_tx_duration = c_fn('obx_opt_async_max_in_tx_duration', None,
544+
[OBX_store_options_p, ctypes.c_uint32])
556545

557546
# OBX_C_API void obx_opt_async_max_in_tx_operations(OBX_store_options* opt, uint32_t value);
558-
obx_opt_async_max_in_tx_operations = c_fn('obx_opt_async_max_in_tx_operations', None, [OBX_store_options_p, ctypes.c_uint32])
547+
obx_opt_async_max_in_tx_operations = c_fn('obx_opt_async_max_in_tx_operations', None,
548+
[OBX_store_options_p, ctypes.c_uint32])
559549

560550
# OBX_C_API void obx_opt_async_pre_txn_delay(OBX_store_options* opt, uint32_t delay_micros);
561551
obx_opt_async_pre_txn_delay = c_fn('obx_opt_async_pre_txn_delay', None, [OBX_store_options_p, ctypes.c_uint32])
562552

563553
# OBX_C_API void obx_opt_async_pre_txn_delay4(OBX_store_options* opt, uint32_t delay_micros, uint32_t delay2_micros, size_t min_queue_length_for_delay2);
564-
obx_opt_async_pre_txn_delay4 = c_fn('obx_opt_async_pre_txn_delay4', None, [OBX_store_options_p, ctypes.c_uint32, ctypes.c_uint32, ctypes.c_size_t])
554+
obx_opt_async_pre_txn_delay4 = c_fn('obx_opt_async_pre_txn_delay4', None,
555+
[OBX_store_options_p, ctypes.c_uint32, ctypes.c_uint32, ctypes.c_size_t])
565556

566557
# OBX_C_API void obx_opt_async_post_txn_delay(OBX_store_options* opt, uint32_t delay_micros);
567558
obx_opt_async_post_txn_delay = c_fn('obx_opt_async_post_txn_delay', None, [OBX_store_options_p, ctypes.c_uint32])
568559

569560
# OBX_C_API void obx_opt_async_post_txn_delay5(OBX_store_options* opt, uint32_t delay_micros, uint32_t delay2_micros, size_t min_queue_length_for_delay2, bool subtract_processing_time);
570-
obx_opt_async_post_txn_delay5 = c_fn('obx_opt_async_post_txn_delay5', None, [OBX_store_options_p, ctypes.c_uint32, ctypes.c_uint32, ctypes.c_size_t, ctypes.c_bool])
561+
obx_opt_async_post_txn_delay5 = c_fn('obx_opt_async_post_txn_delay5', None,
562+
[OBX_store_options_p, ctypes.c_uint32, ctypes.c_uint32, ctypes.c_size_t,
563+
ctypes.c_bool])
571564

572565
# OBX_C_API void obx_opt_async_minor_refill_threshold(OBX_store_options* opt, size_t queue_length);
573-
obx_opt_async_minor_refill_threshold = c_fn('obx_opt_async_minor_refill_threshold', None, [OBX_store_options_p, ctypes.c_size_t])
566+
obx_opt_async_minor_refill_threshold = c_fn('obx_opt_async_minor_refill_threshold', None,
567+
[OBX_store_options_p, ctypes.c_size_t])
574568

575569
# OBX_C_API void obx_opt_async_minor_refill_max_count(OBX_store_options* opt, uint32_t value);
576-
obx_opt_async_minor_refill_max_count = c_fn('obx_opt_async_minor_refill_max_count', None, [OBX_store_options_p, ctypes.c_uint32])
570+
obx_opt_async_minor_refill_max_count = c_fn('obx_opt_async_minor_refill_max_count', None,
571+
[OBX_store_options_p, ctypes.c_uint32])
577572

578573
# OBX_C_API void obx_opt_async_max_tx_pool_size(OBX_store_options* opt, size_t value);
579574
obx_opt_async_max_tx_pool_size = c_fn('obx_opt_async_max_tx_pool_size', None, [OBX_store_options_p, ctypes.c_size_t])
580575

581576
# OBX_C_API void obx_opt_async_object_bytes_max_cache_size(OBX_store_options* opt, uint64_t value);
582-
obx_opt_async_object_bytes_max_cache_size = c_fn('obx_opt_async_object_bytes_max_cache_size', None, [OBX_store_options_p, ctypes.c_uint64])
577+
obx_opt_async_object_bytes_max_cache_size = c_fn('obx_opt_async_object_bytes_max_cache_size', None,
578+
[OBX_store_options_p, ctypes.c_uint64])
583579

584580
# OBX_C_API void obx_opt_async_object_bytes_max_size_to_cache(OBX_store_options* opt, uint64_t value);
585-
obx_opt_async_object_bytes_max_size_to_cache = c_fn('obx_opt_async_object_bytes_max_size_to_cache', None, [OBX_store_options_p, ctypes.c_uint64])
581+
obx_opt_async_object_bytes_max_size_to_cache = c_fn('obx_opt_async_object_bytes_max_size_to_cache', None,
582+
[OBX_store_options_p, ctypes.c_uint64])
586583

587584
#typedef void obx_log_callback(OBXLogLevel log_level, const char* message, size_t message_size, void* user_data);
588585
obx_log_callback_fn = ctypes.CFUNCTYPE(None, OBXLogLevel, ctypes.c_char_p, ctypes.c_size_t, ctypes.c_voidp)
@@ -591,7 +588,8 @@ def c_array_pointer(py_list: Union[List[Any], np.ndarray], c_type):
591588
obx_opt_log_callback = c_fn('obx_opt_log_callback', None, [OBX_store_options_p, obx_log_callback_fn, ctypes.c_voidp])
592589

593590
# OBX_C_API void obx_opt_backup_restore(OBX_store_options* opt, const char* backup_file, uint32_t flags);
594-
obx_opt_backup_restore = c_fn('obx_opt_backup_restore', None, [OBX_store_options_p, ctypes.c_char_p, OBXBackupRestoreFlags])
591+
obx_opt_backup_restore = c_fn('obx_opt_backup_restore', None,
592+
[OBX_store_options_p, ctypes.c_char_p, OBXBackupRestoreFlags])
595593

596594
# OBX_C_API const char* obx_opt_get_directory(OBX_store_options* opt);
597595
obx_opt_get_directory = c_fn('obx_opt_get_directory', ctypes.c_char_p, [OBX_store_options_p])

0 commit comments

Comments
 (0)