Compare Exchange in Cluster-Wide Session



Create compare-exchange

Example

// The session must be first opened with cluster-wide mode
const session = documentStore.openSession({
    transactionMode: "ClusterWide"
});

session.advanced.clusterTransaction.createCompareExchangeValue(
    "Best NoSQL Transactional Database", "RavenDB"  // key, value
);

await session.saveChanges();
  • An InvalidOperationException exception is thrown when:
    • The session was Not opened as cluster-wide.
    • The key already exists in the database.

Syntax

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

The compare exchange object returned

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

Get compare-exchange

Get single value

await session.advanced.clusterTransaction.getCompareExchangeValue(key);
Parameters Type Description
key string The key to retrieve
Return value Description
object The compare-exchange item is returned.
Returns null if key doesn't exist.

Get multiple values

await session.advanced.clusterTransaction.getCompareExchangeValues(keys);
Parameters Type Description
keys string[] Array of keys to retrieve
Return value Description
Record<string, object> If a key doesn't exists the associate value will be null

Get compare-exchange lazily

// Single item
const lazyItem = session.advanced.clusterTransaction.lazily.getCompareExchangeValue(key);
const item = await lazyItem.getValue();

// Multiple items
const lazyItems = session.advanced.clusterTransaction.lazily.getCompareExchangeValues(keys);
const items = await lazyItems.getValue();
Parameters Type Description
key string The key to retrieve
keys string[] Array of keys to retrieve
Return value - after calling getValue Description
object For single item:
If the key doesn't exist it will return null
Record<string, object> For multiple items:
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 number The index of this compare-exchange item
item object The compare-exchange item to delete