One of the ways to do that is using evaluation API. Evaluation/candidate classes are serialized and sent to the server. That means that if we will put the selection criteria and update code inside evaluation class, we will have that code on the server and executing a query using evaluation on the client side will run the update code on the server side.
For easier query execution we can use database singleton - a class that have only one instance saved in the database. That actually can be the class calling the query itself.
Let's fill up our server database:
01public static void setObjects(){ 02
new File(Util.YAPFILENAME).delete(); 03
ObjectContainer db = Db4o.openFile(Util.YAPFILENAME); 04
try { 05
for (int i = 0; i < 5; i++) { 06
Car car = new Car("car"+i); 07
db.set(car); 08
} 09
db.set(new RemoteExample()); 10
} finally { 11
db.close(); 12
} 13
checkCars(); 14
}
Now we can update the cars using specially designed singleton:
01public static void updateCars(){ 02
// triggering mass updates with a singleton 03
// complete server-side execution 04
ObjectServer server=Db4o.openServer(Util.YAPFILENAME,0); 05
server.ext().configure().messageLevel(0); 06
try { 07
ObjectContainer client=server.openClient(); 08
Query q = client.query(); 09
q.constrain(RemoteExample.class); 10
q.constrain(new Evaluation() { 11
public void evaluate(Candidate candidate) { 12
// evaluate method is executed on the server 13
// use it to run update code 14
ObjectContainer objectContainer = candidate.objectContainer(); 15
Query q2 = objectContainer.query(); 16
q2.constrain(Car.class); 17
ObjectSet objectSet = q2.execute(); 18
while(objectSet.hasNext()){ 19
Car car = (Car)objectSet.next(); 20
car.setModel( "Update1-"+ car.getModel()); 21
objectContainer.set(car); 22
} 23
objectContainer.commit(); 24
} 25
}); 26
q.execute(); 27
client.close(); 28
} finally { 29
server.close(); 30
} 31
checkCars(); 32
}
This method has its pros and cons.
Pros:
Cons: