3636#include < optional>
3737#endif
3838
39- static_assert (OBX_VERSION_MAJOR == 4 && OBX_VERSION_MINOR == 0 && OBX_VERSION_PATCH == 0 , // NOLINT
39+ static_assert (OBX_VERSION_MAJOR == 4 && OBX_VERSION_MINOR == 0 && OBX_VERSION_PATCH == 1 , // NOLINT
4040 " Versions of objectbox.h and objectbox.hpp files do not match, please update" );
4141
4242#ifdef __clang__
@@ -625,6 +625,32 @@ class Options {
625625 obx_opt_backup_restore (opt, backupFile, flags);
626626 return *this ;
627627 }
628+
629+ // / Enables Write-ahead logging (WAL); for now this is only supported for in-memory DBs.
630+ // / @param flags WAL itself is enabled by setting flag OBXWalFlags_EnableWal (also the default parameter value).
631+ // / Combine with other flags using bitwise OR or switch off WAL by passing 0.
632+ Options& wal (uint32_t flags = OBXWalFlags_EnableWal) {
633+ obx_opt_wal (opt, flags);
634+ return *this ;
635+ }
636+
637+ // / The WAL file gets consolidated when it reached this size limit when opening the database.
638+ // / This setting is meant for applications that prefer to consolidate on startup,
639+ // / which may avoid consolidations on commits while the application is running.
640+ // / The default is 4096 (4 MB).
641+ Options& walMaxFileSizeOnOpenInKb (uint64_t size_in_kb) {
642+ obx_opt_wal_max_file_size_on_open_in_kb (opt, size_in_kb);
643+ return *this ;
644+ }
645+
646+ // / The WAL file gets consolidated when it reaches this size limit after a commit.
647+ // / As consolidation takes some time, it is a trade-off between accumulating enough data
648+ // / and the time the consolidation takes (longer with more data).
649+ // / The default is 16384 (16 MB).
650+ Options& walMaxFileSizeInKb (uint64_t size_in_kb) {
651+ obx_opt_wal_max_file_size_in_kb (opt, size_in_kb);
652+ return *this ;
653+ }
628654};
629655
630656// / Transactions can be started in read (only) or write mode.
@@ -2120,7 +2146,7 @@ class QueryBase {
21202146 if (!cResult) internal::throwLastError ();
21212147
21222148 std::vector<std::pair<obx_id, double >> result (cResult->count );
2123- for (int i = 0 ; i < cResult->count ; ++i) {
2149+ for (size_t i = 0 ; i < cResult->count ; ++i) {
21242150 std::pair<obx_id, double >& entry = result[i];
21252151 const OBX_id_score& idScore = cResult->ids_scores [i];
21262152 entry.first = idScore.id ;
@@ -2135,8 +2161,23 @@ class QueryBase {
21352161 // / Find object IDs matching the query ordered by their query score (e.g. distance in NN search).
21362162 // / The resulting array is sorted by score in ascending order (unlike findIds()).
21372163 // / Unlike findIdsWithScores(), this method returns a simple vector of IDs without scores.
2138- std::vector<obx_id> findIdsByScore () {
2139- return internal::idVectorOrThrow (obx_query_find_ids_by_score (cQuery_));
2164+ std::vector<obx_id> findIdsByScore () { return internal::idVectorOrThrow (obx_query_find_ids_by_score (cQuery_)); }
2165+
2166+ // / Walk over matching objects one-by-one using the given data visitor (C-style callback function with user data).
2167+ // / Note: if no order conditions is present, the order is arbitrary (sometimes ordered by ID, but never guaranteed
2168+ // / to).
2169+ void visit (obx_data_visitor* visitor, void * userData) {
2170+ OBX_VERIFY_STATE (cQuery_);
2171+ obx_err err = obx_query_visit (cQuery_, visitor, userData);
2172+ internal::checkErrOrThrow (err);
2173+ }
2174+
2175+ // / Walk over matching objects one-by-one using the given data visitor (C-style callback function with user data).
2176+ // / Note: the elements are ordered by the score.
2177+ void visitWithScore (obx_data_score_visitor* visitor, void * userData) {
2178+ OBX_VERIFY_STATE (cQuery_);
2179+ obx_err err = obx_query_visit_with_score (cQuery_, visitor, userData);
2180+ internal::checkErrOrThrow (err);
21402181 }
21412182
21422183 // / Returns the number of matching objects.
@@ -3150,8 +3191,26 @@ struct AsyncTreePutResult {
31503191 [[noreturn]] void throwException () { internal::throwError (status, " Async tree put failed: " + errorMessage); }
31513192};
31523193
3153- using AsyncTreePutCallback = std::function<void (const AsyncTreePutResult& result)>;
3194+ struct AsyncTreeGetResult {
3195+ std::string path; // /< Path of leaf
3196+ obx_err status; // /< OBX_SUCCESS or OBX_NOT_FOUND if operation did not succeed
3197+
3198+ obx_id id; // /< ID of the leaf that was get if operation succeeded or 0 if not found
3199+ OBX_bytes leaf_data; // /< Leaf data if operation succeeded
3200+ OBX_bytes leaf_metadata; // /< Leaf metadata if operation succeeded
3201+
3202+ std::string errorMessage; // /< Non-empty if an error occurred (result is "Undefined")
3203+
3204+ // /@returns true if the operation was successful.
3205+ inline bool isSuccess () { return status == OBX_SUCCESS; }
3206+
3207+ // / Alternative to checking error codes: throw an exception instead.
3208+ // / Note that this will always throw, so you should at least check for a successful outcome, e.g. via isSuccess().
3209+ [[noreturn]] void throwException () { internal::throwError (status, " Async tree get failed: " + errorMessage); }
3210+ };
31543211
3212+ using AsyncTreePutCallback = std::function<void (const AsyncTreePutResult& result)>;
3213+ using AsyncTreeGetCallback = std::function<void (const AsyncTreeGetResult& result)>;
31553214// / @brief Top level tree API representing a tree structure/schema associated with a store.
31563215// /
31573216// / Data is accessed via TreeCursor.
@@ -3192,6 +3251,17 @@ class Tree {
31923251 // / Returns the leaf name of the given path (the string component after the last path delimiter).
31933252 std::string getLeafName (const std::string& path) const ;
31943253
3254+ // / \brief A get operation for a tree leaf,
3255+ // / @param withMetadata Flag if the callback also wants to receive the metadata (also as raw FlatBuffers).
3256+ // / @param callback Once the operation has completed (successfully or not), the callback is called with
3257+ // / AsyncTreeGetResult.
3258+ void getAsync (const char * path, bool withMetadata, AsyncTreeGetCallback callback);
3259+
3260+ // / Like getAsync(), but the callback uses a C function ptr and user data instead of a std::function wrapper.
3261+ // / @param withMetadata Flag if the callback also wants to receive the metadata (also as raw FlatBuffers).
3262+ void getAsyncRawCallback (const char * path, bool withMetadata, obx_tree_async_get_callback* callback,
3263+ void * callback_user_data = nullptr );
3264+
31953265 // / \brief A "low-level" put operation for a tree leaf using given raw FlatBuffer bytes.
31963266 // / Any non-existing branches and meta nodes are put on the fly if an optional meta-leaf FlatBuffers is given.
31973267 // / A typical usage pattern is to first try without the meta-leaf, and if it does not work, create the meta-leaf and
@@ -3263,6 +3333,12 @@ void Tree::putAsyncRawCallback(const char* path, void* data, size_t size, OBXPro
32633333 internal::checkErrOrThrow (err);
32643334}
32653335
3336+ void Tree::getAsyncRawCallback (const char * path, bool withMetadata, obx_tree_async_get_callback* callback,
3337+ void * callback_user_data) {
3338+ obx_err err = obx_tree_async_get_raw (cTree_, path, withMetadata, callback, callback_user_data);
3339+ internal::checkErrOrThrow (err);
3340+ }
3341+
32663342namespace {
32673343void cCallbackTrampolineAsyncTreePut (obx_err status, obx_id id, void * userData) {
32683344 assert (userData);
@@ -3272,6 +3348,17 @@ void cCallbackTrampolineAsyncTreePut(obx_err status, obx_id id, void* userData)
32723348 AsyncTreePutResult result{internal::mapErrorToTreePutResult (status), status, id, std::move (errorMessage)};
32733349 (*callback)(result);
32743350}
3351+ void cCallbackTrampolineAsyncTreeGet (obx_err status, obx_id id, const char * path, const void * leaf_data,
3352+ size_t leaf_data_size, const void * leaf_metadata, size_t leaf_metadata_size,
3353+ void * userData) {
3354+ assert (userData);
3355+ std::unique_ptr<AsyncTreeGetCallback> callback (static_cast <AsyncTreeGetCallback*>(userData));
3356+ std::string errorMessage;
3357+ if (status != OBX_SUCCESS) internal::appendLastErrorText (status, errorMessage);
3358+ AsyncTreeGetResult result{
3359+ path, status, id, {leaf_data, leaf_data_size}, {leaf_metadata, leaf_metadata_size}, std::move (errorMessage)};
3360+ (*callback)(result);
3361+ }
32753362} // namespace
32763363
32773364void Tree::putAsync (const char * path, void * data, size_t size, OBXPropertyType type, void * metadata,
@@ -3290,6 +3377,14 @@ void Tree::consolidateNodeConflictsAsync() {
32903377 internal::checkErrOrThrow (err);
32913378}
32923379
3380+ void Tree::getAsync (const char * path, bool withMetadata,
3381+ std::function<void (const AsyncTreeGetResult& result)> callback) {
3382+ auto funPtr = new std::function<void (const AsyncTreeGetResult&)>(std::move (callback));
3383+ obx_tree_async_get_callback* cCallback = cCallbackTrampolineAsyncTreeGet;
3384+ obx_err err = obx_tree_async_get_raw (cTree_, path, withMetadata, cCallback, funPtr);
3385+ internal::checkErrOrThrow (err);
3386+ }
3387+
32933388#endif
32943389
32953390class TreeCursor ;
0 commit comments