CRUD Operations
DatasetReader and DatasetWriterReader
A
DatasetReader is required to read records from the
dataset, and
DatasetWriterReader allows add/update/delete
operations on a dataset.
DatasetWriterReader<String> writerReader = persons.writerReader(); // <1>
| 1 |
Dataset.writerReader returns the required
DatasetWriterReader for mutative access.
Similarly,
Dataset.reader returns a
DatasetReader.
|
Adding Records
String person1 = "p1";
writerReader.add(person1, // 1
Person.FIRST_NAME.newCell("Marcus"), // 2
Person.LAST_NAME.newCell("Aurelius"),
Person.RATED.newCell(true),
Person.NICENESS.newCell(0.65D));
| 1 | The
DatasetWriterReader API provides the
add method which takes the specified key of
the record,
|
| 2 | And var-args of
Cell. Another variant takes an
Iterable of cells.
|
Accessing Records
Record<String> marcus =
writerReader.get(person1).orElseThrow(AssertionError::new); // <1>
| 1 |
DatasetReader has a
get method which takes a key as the
argument. It returns an
Optional of record. If the dataset doesn't
have a record against the provided key
Optional.isPresent will return false.
|
Update Existing Records
writerReader.update(marcus.getKey(),
UpdateOperation.write(Person.NICENESS, 0.85D)); // <1>
writerReader.update(person2, UpdateOperation.allOf(
UpdateOperation.write(Person.RATED, false),
UpdateOperation.remove(Person.NICENESS))); // <2>
writerReader.update(person3, UpdateOperation.allOf(
UpdateOperation.write(Person.RATED, true),
UpdateOperation.write(Person.NICENESS, 0.92D)));
| 1 |
The
|
| 2 |
For updating multiple cells simultaneously, you can use
|
Deleting Records
writerReader.delete(marcus.getKey()); // <1>
| 1 |
|
Accessor APIs for CRUD
Another way to perform CRUD operations on a dataset is through using
the
ReadWriteRecordAccessor API. It provides more
control over read-write operations on a record with mapping and conditional
reads/writes.
ReadWriteRecordAccessor<String> recordAccessor = writerReader.on(person3); // <1>
recordAccessor.read(record -> record.get(Person.NICENESS).get()); // <2>
recordAccessor.upsert(Person.BIRTH_YEAR.newCell(2000),
Person.PICTURE.newCell(new byte[1024])); // <3>
Optional<Integer> ageDiff = recordAccessor.update(UpdateOperation.write(
Person.BIRTH_YEAR.newCell(1985)), (record1, record2) ->
record1.get(Person.BIRTH_YEAR).get() -
record2.get(Person.BIRTH_YEAR).get()); // <4>
ConditionalReadWriteRecordAccessor<String> conditionalReadWriteRecordAccessor =
recordAccessor.iff(record ->
record.get(Person.BIRTH_YEAR).get().equals(1985)); // <5>
Record<String> record = conditionalReadWriteRecordAccessor.read().get(); // <6>
conditionalReadWriteRecordAccessor.update(
UpdateOperation.write(Person.RATED, false)); // <7>
conditionalReadWriteRecordAccessor.delete(); // <8>
| 1 |
An accessor for a record can be obtained by calling
|
| 2 |
The
|
| 3 |
The
|
| 4 |
There is an advanced
|
| 5 |
Another variant allows conditional read/writes on the
record.
|
| 6 |
If the predicate returns
|
| 7 |
The record will only be updated if the predicate returns
|
| 8 |
Similarly, the deletion succeeds if the predicate was
|
Please refer to the API documentation for more details.