Skip to content

Db4oUuidExample

Fast ACID NoSQL Application Database edited this page Sep 18, 2019 · 8 revisions
package com.db4odoc.disconnectedobj.idexamples;

import com.db4o.ObjectContainer;
import com.db4o.config.ConfigScope;
import com.db4o.config.EmbeddedConfiguration;
import com.db4o.ext.Db4oUUID;


public class Db4oUuidExample implements IdExample<Db4oUUID> {

    public static Db4oUuidExample create(){
        return new Db4oUuidExample();
    }
    public Db4oUUID idForObject(Object obj, ObjectContainer container) {
        // #example: get the db4o-uuid
        Db4oUUID uuid = container.ext().getObjectInfo(obj).getUUID();
        // #end example
        return uuid;
    }

    public Object objectForID(Db4oUUID idForObject, ObjectContainer container) {
        // #example: get an object by a db4o-uuid
        Object objectForId = container.ext().getByUUID(idForObject);
        // getting by uuid doesn't activate the object
        // so you need to do it manually
        container.ext().activate(objectForId);
         // #end example
        return objectForId;
    }


    public void configure(EmbeddedConfiguration configuration) {
        // #example: db4o-uuids need to be activated
        configuration.file().generateUUIDs(ConfigScope.GLOBALLY);
        // #end example
    }

    public void registerEventOnContainer(ObjectContainer container) {
        // no events required for internal ids
    }
}

Internal IDs

Each object, except value objects like ints, floats or string, do have an internal id. This id is unique within on db4o database and db4o uses it internally for managing the objects. However you also can access this id or retrieve objects by the internal id.

You can get the internal id of an object from the extended object container.

And you can retrieve an object by the internal id. Note that when you get an object by its internal id that it won't be activated. Therefore you have to explicitly activate the object.

db4o UUIDs

db4o can also generate a UUIDs for each object. The UUIDs main purpose is to enable replication. By default db4o doesn't assign a UUID to each object. You have to enable this globally or for certain types. For example:

A db4o UUID consists of two parts. The first part is the database signature which is unique to the database.

The second part a unique id within the object-container for the object. Both parts together represent a unique id.

You can get the db4o uuid from the extended object container.

And you can get an object by its UUID. Note that when you get an object by its UUID, that it won't be activated. Therefore you have to explicitly activate the object.

Java Version: getByUUID(Class, objectId) will automatically activate the Object.

NET Version: GetByUUID<Class>(objectId) will automatically activate the Object.

https://stackoverflow.com/questions/2541789/db4o-mvc-application-architecture However InternalIDs may change when you defragment the database. The db4o-UUIDs can be used as ids.

just don't defragment the file when using InternalID

Clone this wiki locally