Compare Exchange in Cluster-Wide Session



Create compare-exchange

Example

// The session must be first opened with cluster-wide mode
$session->advanced()->clusterTransaction()->createCompareExchangeValue(
    key: "Best NoSQL Transactional Database",
    value: "RavenDB"
);

$session->saveChanges();
  • saveChanges() throws a ConcurrencyException if the key already exists.
  • An InvalidOperationException exception is thrown if the session was Not opened as cluster-wide.

Syntax

$session->advanced()->clusterTransaction()->createCompareExchangeValue($key, $value);
Parameters Type Description
key string The compare-exchange item key. This string can be up to 512 bytes.
value T The associated value to store for the key
Return Value Description
CompareExchangeValue<T> The new compare-exchange item is returned

The CompareExchangeValue

Parameters Type Description
key string The compare-exchange item key. This string can be up to 512 bytes.
value T The value associated with the key
index long Index for concurrency control

Get compare-exchange

Get single value

$session->advanced()->clusterTransaction()->getCompareExchangeValue(null, $key);
Parameters Type Description
key string The key to retrieve
Return Value Description
CompareExchangeValue<T> The compare exchange value, or null if it doesn't exist

Get multiple values

$session->advanced()->clusterTransaction()->getCompareExchangeValues(null, $keys);
Parameters Type Description
keys string[] Array of keys to retrieve
Return Value Description
Dictionary<string, CompareExchangeValue<T>> If a key doesn't exists the associate value will be null

Get compare-exchange lazily

// Single value
$session->advanced()->clusterTransaction()->lazily()->getCompareExchangeValue(null, $key);

// Multiple values
$session->advanced()->clusterTransaction()->lazily()->getCompareExchangeValues(null, $keys);
Parameters Type Description
key string The key to retrieve
keys string[] Array of keys to retrieve
Return Value Description
Lazy<CompareExchangeValue<T>> If the key doesn't exist it will return null
Lazy<Dictionary<string, CompareExchangeValue<T>>> If a key doesn't exists the associate value will be null

Delete compare-exchange

// Delete by key & index
$session->advanced()->clusterTransaction()->deleteCompareExchangeValue($key, $index);

// Delete by compare-exchange item
$session->advanced()->clusterTransaction()->deleteCompareExchangeValue($item);
Parameters Type Description
key string The key of the compare-exchange item to delete
index long The index of this compare-exchange item
item CompareExchangeValue<T> The compare-exchange item to delete