Module for reading and writing within a transaction.
Used inside Goblin.transaction/2 (read-write) and Goblin.read/2 (read-only).
Goblin.transaction(db, fn tx ->
counter = Goblin.Tx.get(tx, :counter, default: 0)
tx
|> Goblin.Tx.put(:counter, counter + 1)
|> Goblin.Tx.commit()
end)
Goblin.read(db, fn tx ->
Goblin.Tx.get(tx, :alice)
end)
Summary
Functions
Pipeline-friendly helper function to abort the transaction.
Pipeline-friendly helper function to commit the transaction.
Retrieves a value within a transaction.
Retrieves values for multiple keys within a transaction.
Writes a key-value pair within a transaction.
Writes multiple key-value pairs within a transaction.
Removes a key within a transaction.
Removes multiple keys within a transaction.
Types
@type t() :: %Goblin.Tx{ commits: [{term(), non_neg_integer(), term()}], max_level_key: -1 | non_neg_integer(), mode: :write | :read, mvcc: :ets.table(), sequence: non_neg_integer(), tx_id: non_neg_integer() }
Functions
Pipeline-friendly helper function to abort the transaction.
Parameters
tx- The transaction to abortreply- The reply after aborting (default::error)
Returns
- The abort tuple, i.e.
{:abort, reply}.
Examples
tx
|> Goblin.Tx.put(:alice, "Alice")
|> Goblin.Tx.abort()
Pipeline-friendly helper function to commit the transaction.
Parameters
tx- The transaction to commitreply- The reply after committing (default::ok)
Returns
- The commit tuple, i.e.
{:commit, tx, reply}.
Examples
tx
|> Goblin.Tx.put(:alice, "Alice")
|> Goblin.Tx.commit()
Retrieves a value within a transaction.
Parameters
tx- The transaction structkey- The key to look upopts- A keyword list with the following options (default:[])::tag- Tag the key is namespaced under:default- Value to return ifkeyis not found (default:nil)
Returns
- The value associated with the key, or
defaultif not found
Examples
Goblin.Tx.get(tx, :alice)
# => "Alice"
Goblin.Tx.get(tx, :nonexistent, default: :not_found)
# => :not_found
Retrieves values for multiple keys within a transaction.
Keys not found are excluded from the result.
Parameters
tx- The transaction structkeys- A list of keys to look upopts- A keyword list with the following options (default:[])::tag- Tag the keys are namespaced under
Returns
- A list of
{key, value}tuples for keys found, in unspecified order
Examples
[{:alice, "Alice"}, {:bob, "Bob"}] = Goblin.Tx.get_multi(tx, [:alice, :bob])
Writes a key-value pair within a transaction.
Parameters
tx- The transaction structkey- Any Elixir term to use as the keyvalue- Any Elixir term to storeopts- A keyword list with the following options (default:[])::tag- Tag to namespace the key under
Returns
- Updated transaction struct
Examples
tx = Goblin.Tx.put(tx, :alice, "Alice")
Writes multiple key-value pairs within a transaction.
Parameters
tx- The transaction structpairs- A list of{key, value}tuplesopts- A keyword list with the following options (default:[])::tag- Tag to namespace the keys under
Returns
- Updated transaction struct
Examples
tx = Goblin.Tx.put_multi(tx, [{:alice, "Alice"}, {:bob, "Bob"}])
Removes a key within a transaction.
Parameters
tx- The transaction structkey- The key to removeopts- A keyword list with the following options (default:[])::tag- Tag the key is namespaced under
Returns
- Updated transaction struct
Examples
tx = Goblin.Tx.remove(tx, :alice)
Removes multiple keys within a transaction.
Parameters
tx- The transaction structkeys- A list of keys to removeopts- A keyword list with the following options (default:[])::tag- Tag the keys are namespaced under
Returns
- Updated transaction struct
Examples
tx = Goblin.Tx.remove_multi(tx, [:alice, :bob])