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();
// The session must be first opened with cluster-wide mode
session.Advanced.ClusterTransaction.CreateCompareExchangeValue(
    key: "Best NoSQL Transactional Database",
    value: "RavenDB"
);

await session.SaveChangesAsync();
  • 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<T>(key, value);
session.Advanced.ClusterTransaction.CreateCompareExchangeValue<T>(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<T>(key);
await session.Advanced.ClusterTransaction.GetCompareExchangeValueAsync<T>(key);
Parameters Type Description
key string The key to retrieve
Return Value Description
CompareExchangeValue<T> If the key doesn't exist it will return null

Get multiple values

session.Advanced.ClusterTransaction.GetCompareExchangeValues<T>(keys);
await session.Advanced.ClusterTransaction.GetCompareExchangeValuesAsync<T>(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<T>(key);

// Multiple values
session.Advanced.ClusterTransaction.Lazily.GetCompareExchangeValues<T>(keys);
// Single value
session.Advanced.ClusterTransaction.Lazily.GetCompareExchangeValueAsync<T>(key);

// Multiple values
session.Advanced.ClusterTransaction.Lazily.GetCompareExchangeValuesAsync<T>(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<T>(item);
// Delete by key & index
session.Advanced.ClusterTransaction.DeleteCompareExchangeValue(key, index);

// Delete by compare-exchange item
session.Advanced.ClusterTransaction.DeleteCompareExchangeValue<T>(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