Documentation Index
Fetch the complete documentation index at: https://docs.activeviam.com/llms.txt
Use this file to discover all available pages before exploring further.
Modifying the content of the datastore is done in a Datastore Transactions.
The main operations available to modify the content are add, remove, removeWhere, updateWhere.
In the following examples we will use a datastore with one store with the following schema:
final IStoreDescription storeDescription =
StoreDescription.builder()
.withStoreName("Test")
.withField("Id", ILiteralType.LONG)
.asKeyField()
.withField("Product", ILiteralType.STRING)
.withNullableField("Country", ILiteralType.STRING)
.withField("Quantity", ILiteralType.DOUBLE, 0.0)
.build();
And the following initial content:
| Id | Product | Country | Quantity |
|---|
| 1 | Product1 | France | 100.0 |
| 2 | Product2 | France | 20.0 |
| 3 | Product1 | USA | 50.0 |
Note that all these examples use the recommended way to edit the datastore by using the datastore.edit() method so that the transaction is automatically started and committed (or rolled back in case of an error).
Add rows
The simplest operation to insert data into the datastore is to use the add method.
This method will insert a single record into the store.
final Object[] row = new Object[] {4L, "Product4", "Estonia", 10.0};
datastore.edit(t -> t.add("Test", row));
Multiple rows can be inserted at once in a store by using the addAll method.
final List<Object[]> rows =
List.of(
new Object[] {5L, "Product5", "Germany", 30.0},
new Object[] {6L, "Product6", "Italy", 40.0});
datastore.edit(t -> t.addAll("Test", rows));
After these two operations, the content of the store will be:
| Id | Product | Country | Quantity |
|---|
| 1 | Product1 | France | 100.0 |
| 2 | Product2 | France | 20.0 |
| 3 | Product1 | USA | 50.0 |
| 4 | Product4 | Estonia | 10.0 |
| 5 | Product5 | Germany | 30.0 |
| 6 | Product6 | Italy | 40.0 |
Remove rows
Remove with a key
To remove a row with a key, one can use the remove method:
datastore.edit(t -> t.remove("Test", 1L));
Applied to the initial content, this operations will remove the row with the key 1:
| Id | Product | Country | Quantity |
|---|
| 2 | Product2 | France | 20.0 |
| 3 | Product1 | USA | 50.0 |
To remove multiple rows with keys, one can use the removeAll method.
Remove with a condition
More often than key-based removal, one may want to remove rows based on a condition.
This can be done with the removeWhere method.
For instance let’s remove all the products from France:
final ICondition condition = BaseConditions.equal(FieldPath.of("Country"), "France");
datastore.edit(t -> t.removeWhere("Test", condition));
| Id | Product | Country | Quantity |
|---|
| 3 | Product1 | USA | 50.0 |
Update-where procedures
The updateWhere method allows to update the values of a field based on a condition and an update procedure.
For instance, let’s update the quantity of all the products from France by multiplying it by 2.
Let’s first define a condition on which the procedure will be applied:
final ICondition condition = BaseConditions.equal(FieldPath.of("Country"), "France");
We must then define a selection of field used to apply the procedure.
Here we want to multiply the quantity by 2 so we just need the quantity fields.
final ISelection selection =
new Selection("Test", List.of(AliasedField.create("Quantity", FieldPath.of("Quantity"))));
Finally, we define the procedure to apply to the selected fields.
The procedure is defined by implementing the IUpdateWhereProcedure interface which has 2 methods:
init is called once before the update is applied with the format of the selected fields and the target records. It is generally used to retrieve the index of the fields to read and write, or do any heavy initialization.
execute is called for each record that matches the condition. This method provides the selected data in a read-only way and the target record where the data can be written.
final IUpdateWhereProcedure updateWhereProcedure =
new IUpdateWhereProcedure() {
int quantityFieldSelectionPosition;
int quantityFieldRecordPosition;
@Override
public void init(final IRecordFormat selectionFormat, final IRecordFormat recordFormat) {
this.quantityFieldSelectionPosition = selectionFormat.getFieldIndex("Quantity");
this.quantityFieldRecordPosition = recordFormat.getFieldIndex("Quantity");
}
@Override
public void execute(final IArrayReader selectedRecord, final IArrayWriter recordWriter) {
final double quantity = selectedRecord.readDouble(this.quantityFieldSelectionPosition);
recordWriter.writeDouble(this.quantityFieldRecordPosition, quantity * 2);
}
};
And we can apply the update:
datastore.edit(t -> t.updateWhere(selection, condition, updateWhereProcedure));
| Id | Product | Country | Quantity |
|---|
| 1 | Product1 | France | 200.0 |
| 2 | Product2 | France | 40.0 |
| 3 | Product1 | USA | 50.0 |