Writing Data#
Concourse provides several methods for writing data to records. All write operations are atomic by default — when operating outside of an explicit transaction, each individual write is automatically committed.
Adding Values#
The add method appends a value to a field if and only if that
value does not already exist in the field. A field in Concourse can
hold multiple distinct values simultaneously, so add is the
primary way to build up multi-valued fields.
Effectively equal values of different types are not distinct. For
example, if a field holds 18, adding 18.0 returns false. See
Effectively Equal Values.
Add to a New Record#
To add a value and automatically create a new record, omit the record parameter. Concourse assigns a unique record ID and returns it.
1 2 | |
1 2 | |
Add to an Existing Record#
To add a value to a specific record, pass the record ID. The method
returns true if the value was added, or false if it already
exists.
1 2 | |
1 2 | |
Add to Multiple Records#
You can add a value across several records at once. The method
returns a Map associating each record ID to a boolean indicating
whether the add succeeded for that record.
1 2 3 4 | |
Setting Values#
The set method atomically removes all the other values for a
key in a record and adds a new value if the field doesn’t already
contain it. Use set when a field should contain exactly one
value. If the field already contains only that value, set
doesn’t change anything.
If the field holds a value that is effectively equal to the new
value but not identical, set replaces it. For example, if the
field holds 18, setting it to 18.0 leaves it holding 18.0.
1 2 | |
1 2 | |
Set Across Multiple Records#
You can set a value for a key in several records at once.
1 2 3 | |
Removing Values#
The remove method removes a specific value from a field if it
currently exists. Unlike clear, which removes all values,
remove targets a single value.
remove also succeeds when the field holds a value that is
effectively equal to the one you supply. For example, removing
18.0 from a field that holds 18 removes the 18.
1 2 | |
1 2 | |
Remove from Multiple Records#
1 2 3 | |
Clearing Values#
The clear method atomically removes all values stored for one
or more keys in one or more records. Use this to completely wipe
a field or an entire record.
Clear a Single Key#
1 2 | |
1 2 | |
Clear Multiple Keys#
1 2 3 | |
Clear an Entire Record#
Omit the key parameter to remove all values for every key in the record.
1 2 | |
Clear Across Multiple Records#
All clear variants support collections of records as well.
1 2 3 4 | |
Inserting Data#
The insert method atomically writes a collection of key/value
associations into one or more records. This is the most efficient
way to load structured data into Concourse.
Insert from JSON#
Pass a JSON string containing a top-level object to insert a single record, or a top-level array of objects to insert multiple records. The method returns the set of record IDs where the data was inserted.
1 2 3 4 5 | |
1 2 | |
To insert multiple records at once, pass a JSON array:
1 2 3 | |
Insert from a Map#
1 2 3 4 5 | |
Insert from a Multimap#
Use a Multimap when a key should have multiple values.
1 2 3 4 5 6 7 | |
Insert into Existing Records#
You can insert data into specific existing records. The insert will fail for a given record if any of the key/value associations already exist in that record.
1 2 3 4 5 6 7 | |
Resolvable Links in Insert Data#
When inserting JSON or Map data, you can include resolvable link
instructions that automatically create links to all records
matching a criteria. Use Link.toWhere(criteria) to generate the
instruction.
1 2 3 4 5 6 | |
Do not add resolvable links directly
Resolvable links should only be used within insert
operations (JSON, Map, or Multimap). Do not use add to
write resolvable links because the evaluation and linking
would not be atomic.
Linking Records#
The link method creates a directed pointer from one record to
another. Links are a special data type that
enables Concourse’s graph features.
1 2 3 | |
1 2 | |
Link to Multiple Destinations#
1 2 3 | |
Removing Links#
Use unlink to remove a link between two records.
1 2 | |
1 2 | |
Atomic Write Operations#
Concourse provides several compound write operations that execute atomically without requiring an explicit transaction.
verifyAndSwap#
Atomically replace a value if and only if the expected value is currently stored in the field. This is the classic compare-and-swap (CAS) operation.
1 2 3 4 | |
1 2 | |
verifyOrSet#
verifyOrSet is deprecated. Use set
instead, which has the same guarantee.
reconcile#
Make the necessary changes so that a field contains exactly the specified collection of values. Concourse computes the minimal diff and only applies the adds and removes needed.
A value that the field holds counts as one of the specified values if the two are effectively equal, so it stays in place with the type it was written with.
1 2 3 | |
findAndSet, selectAndSet and getAndSet#
Atomically find the records that match a condition and set a
key as a value in each of them. Because the lookup and the set
happen together, no other client can change those records in
between. These operations support sorting and pagination, so you
can update only the first few matches.
findAndSetreturns the records that were updated.selectAndSetreturns all the values that each updated record held for one or more keys (or every key) before theset.getAndSetreturns the most recent value that each updated record held for one or more keys (or every key) before theset.
When you read a single key instead of a collection of keys, the
result maps each record straight to that key’s values, as select
and get of one key do. Like get, a getAndSet of one key
leaves out a record that has no value for it, even though it
still updates that record.
1 2 3 4 5 6 | |
1 2 | |
Simulating a Unique Index#
findOrAdd#
Atomically find the unique record where a key equals a value, or
add the key/value pair to a new record if none exists. Throws a
DuplicateEntryException if more than one record matches.
1 2 3 4 | |
findOrInsert#
Atomically find the unique record matching a criteria, or insert data into a new record if none exists. This is the most flexible way to implement upsert semantics.
1 2 3 4 5 | |
You can also use the Criteria builder:
1 2 3 4 5 6 7 | |
Like insert, the data parameter can be a JSON string, Map, or
Multimap.
DuplicateEntryException
Both findOrAdd and findOrInsert throw a
DuplicateEntryException if more than one record matches the
condition. This ensures the uniqueness guarantee is enforced.
Design your criteria to match at most one record.
Consolidating Records#
The consolidate method merges the data from two or more records
into a single record. After consolidation, the surviving record
contains the union of all values from the merged records, and the
other records are cleared.
1 2 3 | |