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.

IObjectSet changesOnDesktop = 
    replicationSession.ProviderA().ObjectsChangedSinceLastReplication(typeof (Pilot));

foreach (object changedObjectOnDesktop in changesOnDesktop)
{
    replicationSession.Replicate(changedObjectOnDesktop);
}

replicationSession.Commit();
Db4oReplicationExamples.cs: Selective replication by class
Dim changesOnDesktop As IObjectSet = _
    replicationSession.ProviderA().ObjectsChangedSinceLastReplication(GetType(Pilot))

For Each changedObjectOnDesktop As Object In changesOnDesktop
    replicationSession.Replicate(changedObjectOnDesktop)
Next

replicationSession.Commit()
Db4oReplicationExamples.vb: 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.

IObjectSet changesOnDesktop = replicationSession.ProviderA().ObjectsChangedSinceLastReplication();

foreach (object changedObjectOnDesktop in changesOnDesktop)
{
    if (changedObjectOnDesktop is Car)
    {
        if (((Car) changedObjectOnDesktop).Name.StartsWith("M"))
        {
            replicationSession.Replicate(changedObjectOnDesktop);
        }
    }
}

replicationSession.Commit();
Db4oReplicationExamples.cs: Selective replication with a condition
Dim changesOnDesktop As IObjectSet = _
    replicationSession.ProviderA().ObjectsChangedSinceLastReplication()

For Each changedObjectOnDesktop As Object In changesOnDesktop
    If TypeOf changedObjectOnDesktop Is Car Then
        If DirectCast(changedObjectOnDesktop, Car).Name.StartsWith("M") Then
            replicationSession.Replicate(changedObjectOnDesktop)
        End If
    End If
Next

replicationSession.Commit()
Db4oReplicationExamples.vb: 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.

IList<Car> changesOnDesktop = 
    desktopDatabase.Query(delegate(Car car) { return car.Name.StartsWith("M"); });

foreach (Car changedObjectOnDesktop  in changesOnDesktop)
{
    replicationSession.Replicate(changedObjectOnDesktop);
}

replicationSession.Commit();
Db4oReplicationExamples.cs: Selective replication with a query
Dim changesOnDesktop As IList(Of Car) = _
    desktopDatabase.Query(Function(car As Car) car.Name.StartsWith("M"))

For Each changedObjectOnDesktop As Car In changesOnDesktop
    replicationSession.Replicate(changedObjectOnDesktop)
Next

replicationSession.Commit()
Db4oReplicationExamples.vb: 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.

The IObjectSet is also an IEnumerable, so you can use LINQ to filter the objects.

Consistency with Selective Replication

Note that dRS tries to avoid inconsistent replications. When replicating a object all its referenced objects are replicated as well. This avoids a incomplete and inconsistent object graphs. So for example when you do a selective replication of the Car objects, dRS will also replicate all Pilots which are referenced by the Car objects.