c++objectboxgreenrobot-objectbox

ObjectBox 4 C++ Initialise in memory database


I have created an ObjectBox database and written some records to tables which are in base/data.mdb

A different program needs to read this database into an in memory instance of ObjectBox.

Creating the store and pointing to the file does not work:

store_(obx::Options(create_obx_model()).directory("memory:/databasePath"))

I tried creating a temporary store then copy the values to the in memory database

tempStore_(obx::Options(create_obx_model()).directory("path/to/base"))
obx::Box<Object> tableTemp(tempStore_);

memStore_(obx::Options(create_obx_model()).directory("memory:/databasePath"))
obx::Box<Object> tableObject(memStore_);

auto all = tableTemp.getAll();
tableObject.put(all);

But get error:

what():  ID is higher or equal to internal ID sequence: 3 (vs. 1). Use ID 0 (zero) to insert new objects.

Is there a way to initialise in memory database from file? Can I change the internal ID sequence to the last of the in memory database, rather than insert dummy objects and then delete them to increase the sequence number.


Solution

  • You have to make the ID property "assignable". Let me quote the property annotation docs for additional details:

    assignable IDs (set as id(assignable)) to switch from the default (ObjectBox assigns object IDs during insert, following auto-increment order). This will allow putting an object with any valid ID. You can still set the ID to zero to let ObjectBox auto-assign a new ID.

    For example, in the fbs file (assuming you are using ObjectBox Generator) it should look like this:

    table MyEntity {
        /// objectbox:id(assignable)
        id:ulong;
    }