1313# limitations under the License.
1414
1515
16- from objectbox .c import *
16+ import objectbox .c as c
1717import objectbox .transaction
18-
18+ from objectbox .store_options import StoreOptions
19+ import objectbox
20+ from objectbox .model .entity import _Entity
21+ from typing import *
1922
2023class Store :
21- def __init__ (self , c_store : OBX_store_p ):
22- self ._c_store = c_store
24+ def __init__ (self ,
25+ model : Optional [objectbox .model .Model ] = None ,
26+ directory : Optional [str ] = None ,
27+ max_db_size_in_kb : Optional [int ] = None ,
28+ max_data_size_in_kb : Optional [int ] = None ,
29+ file_mode : Optional [int ] = None ,
30+ max_readers : Optional [int ] = None ,
31+ no_reader_thread_locals : Optional [bool ] = None ,
32+ model_bytes : Optional [bytes ] = None ,
33+ model_bytes_direct : Optional [bytes ] = None ,
34+ read_schema : Optional [bool ] = None ,
35+ use_previous_commit : Optional [bool ] = None ,
36+ read_only : Optional [bool ] = None ,
37+ debug_flags : Optional [c .DebugFlags ] = None ,
38+ async_max_queue_length : Optional [int ] = None ,
39+ async_throttle_at_queue_length : Optional [int ] = None ,
40+ async_throttle_micros : Optional [int ] = None ,
41+ async_max_in_tx_duration : Optional [int ] = None ,
42+ async_max_in_tx_operations : Optional [int ] = None ,
43+ async_pre_txn_delay : Optional [int ] = None ,
44+ async_post_txn_delay : Optional [int ] = None ,
45+ async_minor_refill_threshold : Optional [int ] = None ,
46+ async_minor_refill_max_count : Optional [int ] = None ,
47+ async_object_bytes_max_cache_size : Optional [int ] = None ,
48+ async_object_bytes_max_size_to_cache : Optional [int ] = None ,
49+ c_store : Optional [c .OBX_store_p ] = None ):
50+
51+ """Opens an ObjectBox database Store
52+
53+ :param model:
54+ Database schema model.
55+ :param directory:
56+ Store directory. Defaults to "objectbox".
57+ Use prefix "memory:" to open an in-memory database, e.g. "memory:myapp"
58+ :param max_db_size_in_kb:
59+ Maximum database size. Defaults to one gigabyte.
60+ :param max_data_size_in_kb:
61+ Maximum data in database size tracking. Defaults to being disabled.
62+ This is a more involved size tacking.
63+ Recommended only if stricter accurate limit is required.
64+ Data size must be below database size.
65+ :param file_mode:
66+ Unix-style file mode options. Defaults to "int('644',8)".
67+ This option is ignored on Windows platforms.
68+ :param max_readers:
69+ Maximum number of readers (related to read transactions).
70+ Default value (currently 126) is suitable for most applications.
71+ :param no_reader_thread_locals:
72+ Disables the usage of thread locals for "readers" related to read transactions.
73+ This can make sense if you are using a lot of threads that are kept alive.
74+ :param model_bytes:
75+ Database schema model given by flatbuffers bytes serialized model.
76+ :param model_bytes_direct:
77+ Database schema model given by flatbuffers bytes serialized model without copying.
78+ :param read_schema:
79+ Advanced settings.
80+ :param use_previous_commit:
81+ Advanced setting recommended to set with read_only to ensure no data is lost.
82+ :param read_only:
83+ Open store in read-only mode: no schema update, no write transactions. Defaults to false.
84+ :param debug_flags:
85+ Set debug flags. Defaults to DebugFlags.NONE.
86+ :param async_max_queue_length:
87+ Maximum size of the queue before new transactions will be rejected.
88+ :param async_throttle_at_queue_length:
89+ Throttle queue submitter when hitting this water mark.
90+ :param async_throttle_micros:
91+ Sleeping time for throttled queue submitter.
92+ :param async_max_in_tx_duration:
93+ Maximum duration spent in a transaction before queue enforces a commit.
94+ :param async_max_in_tx_operations:
95+ Maximum number of operations performed in a transaction before queye enforces a commit.
96+ :param async_pre_txn_delay:
97+ Delay (in micro seconds) before queue is triggered by new element.
98+ :param async_post_txn_delay:
99+ Delay (in micro seconds) after a transaction was committed.
100+ :param async_minor_refill_threshold:
101+ Number of operations to be considered a "minor refill".
102+ :param async_minor_refill_max_count:
103+ If set, allows "minor refills" with small batches that came in (off by default).
104+ :param async_object_bytes_max_cache_size:
105+ Total cache size. Defaults to 0.5 mega bytes.
106+ :param async_object_bytes_max_size_to_cache:
107+ Maximum size for an object to be cached.
108+ :param c_store:
109+ Internal parameter for deprecated ObjectBox interface. Do not use it; other options would be ignored if passed.
110+ """
111+
112+ self ._c_store = None
113+ if not c_store :
114+ options = StoreOptions ()
115+ try :
116+ if model is not None :
117+ options .model (model )
118+ if directory is not None :
119+ options .directory (directory )
120+ if max_db_size_in_kb is not None :
121+ options .max_db_size_in_kb (max_db_size_in_kb )
122+ if max_data_size_in_kb is not None :
123+ options .max_data_size_in_kb (max_data_size_in_kb )
124+ if file_mode is not None :
125+ options .file_mode (file_mode )
126+ if max_readers is not None :
127+ options .max_readers (max_readers )
128+ if no_reader_thread_locals is not None :
129+ options .no_reader_thread_locals (no_reader_thread_locals )
130+ if model_bytes is not None :
131+ options .model_bytes (model_bytes )
132+ if model_bytes_direct is not None :
133+ options .model_bytes_direct (model_bytes_direct )
134+ if read_schema is not None :
135+ options .read_schema (read_schema )
136+ if use_previous_commit is not None :
137+ options .use_previous_commit (use_previous_commit )
138+ if read_only is not None :
139+ options .read_only (read_only )
140+ if debug_flags is not None :
141+ options .debug_flags (debug_flags )
142+ if async_max_queue_length is not None :
143+ options .async_max_queue_length (async_max_queue_length )
144+ if async_throttle_at_queue_length is not None :
145+ options .async_throttle_at_queue_length (async_throttle_at_queue_length )
146+ if async_throttle_micros is not None :
147+ options .async_throttle_micros (async_throttle_micros )
148+ if async_max_in_tx_duration is not None :
149+ options .async_max_in_tx_duration (async_max_in_tx_duration )
150+ if async_max_in_tx_operations is not None :
151+ options .async_max_in_tx_operations (async_max_in_tx_operations )
152+ if async_pre_txn_delay is not None :
153+ options .async_pre_txn_delay (async_pre_txn_delay )
154+ if async_post_txn_delay is not None :
155+ options .async_post_txn_delay (async_post_txn_delay )
156+ if async_minor_refill_threshold is not None :
157+ options .async_minor_refill_threshold (async_minor_refill_threshold )
158+ if async_minor_refill_max_count is not None :
159+ options .async_minor_refill_max_count (async_minor_refill_max_count )
160+ if async_object_bytes_max_cache_size is not None :
161+ options .async_object_bytes_max_cache_size (async_object_bytes_max_cache_size )
162+ if async_object_bytes_max_size_to_cache is not None :
163+ options .async_object_bytes_max_size_to_cache (async_object_bytes_max_size_to_cache )
164+
165+ except c .CoreException :
166+ options ._free ()
167+ raise
168+ self ._c_store = c .obx_store_open (options ._c_handle )
169+ else :
170+ self ._c_store = c_store
23171
24172 def __del__ (self ):
25173 self .close ()
174+
175+ def box (self , entity : _Entity ) -> 'objectbox.Box' :
176+ """
177+ Open a box for an entity.
178+
179+ :param entity:
180+ Entity type of the model
181+ """
182+ return objectbox .Box (self , entity )
26183
27184 def read_tx (self ):
28185 return objectbox .transaction .read (self )
@@ -34,4 +191,13 @@ def close(self):
34191 c_store_to_close = self ._c_store
35192 if c_store_to_close :
36193 self ._c_store = None
37- obx_store_close (c_store_to_close )
194+ c .obx_store_close (c_store_to_close )
195+
196+ def remove_db_files (dir ):
197+ """
198+ Remove Database files
199+
200+ :param dir:
201+ Path to directory.
202+ """
203+ c .obx_remove_db_files (c .c_str (dir ))
0 commit comments