You are here: db4o Databases Replication > Selective Replication

Selective Replication

What if the mobile device doesn't have enough memory to store all objects? Or if only a subset of the object need to be in both databases? In this case you can replicate only a subset of all objects.

Filter by Class

You can ask the dRS to replicate only the objects of a certain class. Instead of getting the change-set with all objects, it will return only the changed objects of that class.

ObjectSet changesOnDesktop =
    replicationSession.providerA().objectsChangedSinceLastReplication(Pilot.class);

for (Object changedObjectOnDesktop : changesOnDesktop) {
    replicationSession.replicate(changedObjectOnDesktop);
}

replicationSession.commit();
Db4oReplicationExamples.java: Selective replication by class

Filter With a Condition

For more complex cases a simple condition can be a solution. Add a simple condition within the replication loop to select which objects are replicated and which ones not.

ObjectSet changesOnDesktop = replicationSession.providerA().objectsChangedSinceLastReplication();

for (Object changedObjectOnDesktop : changesOnDesktop) {
    if (changedObjectOnDesktop instanceof Car) {
        if (((Car) changedObjectOnDesktop).getName().startsWith("M")) {
            replicationSession.replicate(changedObjectOnDesktop);
        }
    }            
}

replicationSession.commit();
Db4oReplicationExamples.java: Selective replication with a condition

Filter With a Query

It's also possible to query for the objects and the replicate those objects. However in this case, also unchanged objects are replicated again.

ObjectSet<Car> changesOnDesktop = desktopDatabase.query(new Predicate<Car>() {
    public  boolean match(Car p) {
        return p.getName().startsWith("M");
    }
});

for (Car changedObjectOnDesktop  : changesOnDesktop) {
    replicationSession.replicate(changedObjectOnDesktop);
}

replicationSession.commit();
Db4oReplicationExamples.java: Selective replication with a query

Combination

Of course it possible to use a combination of these three possibilities. For example you can request the changes for a certain class and then filter with a condition.