Atomic Operations
NoBrainer supports atomic operations on document attributes to avoid races
during updates (e.g. incrementing a number).
NoBrainer provides a natural Ruby syntax to describe atomic
operations: assignments ran within a queue_atomic
block are performed
atomically during the next save()
operation.
Examples
The following examples use the following base code:
Incrementing a field
Removing items from an array
Adding items to a set
Swapping two fields
Using different fields to compute some value
Removing a field
Using a RQL value
Operations
- Atomic operations are performed when invoking
save()
. - Multiple atomic operations can be queued.
- Once an attribute has atomic operations queued, validations are no longer performed on that attribute.
- Once an atomic value is queued, the attribute value cannot be read anymore.
The following describes operations that can be performed on various types:
Any types
instance.unset(:field)
removes the specified field from the document.instance.field = value
assignsfield
tovalue
, which can be a RQL expression, or some operation using other fields from the same document.
Array
values1 & values2
performs the set intersection betweenvalues1
andvalues2
.values1 | values2
performs the set union ofvalues1
andvalues2
.values1 + values2
performs the concatenation ofvalues1
andvalues2
.values1 - values2
performs the difference betweenvalues1
andvalues2
.values << value
appendsvalue
tovalues
.values.delete(value)
removesvalue
fromvalues
.
Set
Same as Array
, except all operations are done with Set
semantics.
String
value1 + value2
performs the concatenation of the two strings.
Integer / Float
+ - / %
performs the usual numeric computations.