Skip to main content

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<T>(key, value);
ParametersTypeDescription
keystringThe compare-exchange item key. This string can be up to 512 bytes.
valueTThe associated value to store for the key
Return ValueDescription
CompareExchangeValue<T>The new compare-exchange item is returned

The CompareExchangeValue

ParametersTypeDescription
keystringThe compare-exchange item key. This string can be up to 512 bytes.
valueTThe value associated with the key
indexlongIndex for concurrency control

Get compare-exchange

Get single value

session.Advanced.ClusterTransaction.GetCompareExchangeValue<T>(key);
ParametersTypeDescription
keystringThe key to retrieve
Return ValueDescription
CompareExchangeValue<T>If the key doesn't exist it will return null

Get multiple values

session.Advanced.ClusterTransaction.GetCompareExchangeValues<T>(keys);
ParametersTypeDescription
keysstring[]Array of keys to retrieve
Return ValueDescription
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);
ParametersTypeDescription
keystringThe key to retrieve
keysstring[]Array of keys to retrieve
Return ValueDescription
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);
ParametersTypeDescription
keystringThe key of the compare-exchange item to delete
indexlongThe index of this compare-exchange item
itemCompareExchangeValue<T>The compare-exchange item to delete