-
Notifications
You must be signed in to change notification settings - Fork 3
Modifying Objects
There are a few methods in the API of the JACIS store to modify the stored objects:
|
Update the object with the passed key to the new passed value. |
|
Remove the object with the passed key from the store. |
The API to modify objects is quite simple since only a method to update the value for a key and a method to remove the value for a key is needed. If we want to create objects we simply use the update
method as well. Regardless which changes we do for an object we simply pass the new value to the update
method. Note that by default there is no automatic dirty check. Therefore all modifications to objects read from the store have to be notified to the store explicitly by calling the update
method. Modifications done to objects without calling the method are simply ignored. Note that the order of calling the update
method and doing the modifications does not matter. If we first call the update
method and then modify the passed instance the changes will also be cloned back to the store on commit. However changes done to the passed instance after commit will have no effect on the store.
For the examples below we use the same object class and the same initial data as in the
Accessing Objects chapter (10 Account
instances with different balance).
// Modification is lost after commit (update missing):
container.withLocalTx(() -> {
Account acc = store.get("account1");
acc.deposit(1000);
});
// Modification succeeds (first modification then update):
container.withLocalTx(() -> {
Account acc = store.get("account1");
acc.deposit(1000);
store.update("account1", acc);
});
// Modification succeeds (first update then modification):
container.withLocalTx(() -> {
Account acc = store.get("account1");
store.update("account1", acc);
acc.deposit(1000);
});
// looping and updating (adding 10% interest to all accounts with a positive balance)
container.withLocalTx(() -> {
store.stream(acc -> acc.getBalance() > 0) //
.forEach(acc -> store.update(acc.getName(), acc.deposit(acc.getBalance() / 10)));
});
Next Chapter: Read Only Objects