Skip to main content

Get Multiple Compare-Exchange Items


Create sample compare-exchange items

Let’s create some sample compare-exchange items to use in the examples below.
To learn about ALL the available methods for creating a compare-exchange item, see Create compare-exchange item.

store.Operations.Send(
new PutCompareExchangeValueOperation<string>("employees/1", "someValue1", 0));
store.Operations.Send(
new PutCompareExchangeValueOperation<string>("employees/2", "someValue2", 0));
store.Operations.Send(
new PutCompareExchangeValueOperation<string>("employees/3", "someValue3", 0));
store.Operations.Send(
new PutCompareExchangeValueOperation<string>("customers/1", "someValue4", 0));
store.Operations.Send(
new PutCompareExchangeValueOperation<string>("customers/2", "someValue5", 0));

Get compare-exchange items by list of keys

  • To retrieve multiple compare-exchange items by specifying a list of unique keys, use either:

    • Cluster-wide session:
      Use the GetCompareExchangeValues session method – which also supports lazy retrieval.
      If one of the specified keys does not exist, its corresponding entry in the returned dictionary will be null.
      No exception is thrown.

    • Store operation:
      Use the GetCompareExchangeValuesOperation store operation.
      If one of the specified keys does not exist, the .Value property of the corresponding entry in the returned dictionary is null.
      No exception is thrown.

  • Examples:

    Get compare-exchange items by list of keys

    // The session must be opened in cluster-wide mode.
    // An `InvalidOperationException` is thrown if the session is not opened in cluster-wide mode.
    using (var session = store.OpenSession(
    new SessionOptions { TransactionMode = TransactionMode.ClusterWide }))
    {
    // Define the list of keys of the compare-exchange items to retrieve
    var keys = new[] { "employees/1", "employees/2", "customers/2", "non-existing-key" };

    // Call 'GetCompareExchangeValues', pass the list of keys
    Dictionary<string, CompareExchangeValue<string>> items =
    session.Advanced.ClusterTransaction.GetCompareExchangeValues<string>(keys);

    // Check results
    Console.WriteLine($"Number of compare-exchange items retrieved: {items.Count}"); // Expecting 4

    // Access a retrieved item - an existing key
    if (items.TryGetValue("employees/1", out var item))
    {
    string value = item.Value; // "someValue1"
    long version = item.Index;
    }

    // The entry of a non-existing key will be null
    if (items.TryGetValue("non-existing-key", out var nonExistingItem))
    {
    Console.WriteLine(nonExistingItem == null); // true
    }
    }

    Get compare-exchange items by list of keys - lazily

    A list of compare-exchange items can be retrieved lazily when working within a cluster-wide session.

    // The session must be opened in cluster-wide mode.
    // An `InvalidOperationException` is thrown if the session is not opened in cluster-wide mode.
    using (var session = store.OpenSession(
    new SessionOptions { TransactionMode = TransactionMode.ClusterWide }))
    {
    // Define the list of keys of the compare-exchange items to retrieve
    var keys = new[] { "employees/1", "employees/2", "customers/2", "non-existing-key" };

    // Call 'Lazily.GetCompareExchangeValues', pass the list of keys
    var lazyItems =
    session.Advanced.ClusterTransaction.Lazily.GetCompareExchangeValues<string>(keys);

    Dictionary<string, CompareExchangeValue<string>> items = lazyItems.Value;

    // Check results
    Console.WriteLine($"Number of compare-exchange items retrieved: {items.Count}"); // Expecting 4

    // Access a retrieved item - an existing key
    if (items.TryGetValue("employees/1", out var item))
    {
    string value = item.Value; // "someValue1"
    long version = item.Index; // e.g. 321
    }

    // The entry of a non-existing key will be null
    if (items.TryGetValue("non-existing-key", out var nonExistingItem))
    {
    Console.WriteLine(nonExistingItem == null); // true
    }
    }

Get compare-exchange items by prefix

  • You can retrieve compare-exchange items whose keys start with a specific prefix.
    You can also control the maximum number of items to return and the starting position for paging.

  • Examples:

    // The session must be opened in cluster-wide mode.
    // An `InvalidOperationException` is thrown if the session is not opened in cluster-wide mode.
    using var session = store.OpenSession(
    new SessionOptions { TransactionMode = TransactionMode.ClusterWide }))
    {
    // Call 'GetCompareExchangeValues', pass:
    // * startWith: The common key prefix
    // * start: The start position (optional, default is 0)
    // * pageSize: Max items to get (optional, default is 25)
    Dictionary<string, CompareExchangeValue<string>> items =
    session.Advanced.ClusterTransaction.GetCompareExchangeValues<string>(
    startsWith: "employees", start: 0, pageSize: 10);

    // Results will include only compare-exchange items with keys that start with "employees"
    Console.WriteLine($"Number of compare-exchange items with prefix 'employees': {items.Count}");
    // Should be 3
    }

Get compare-exchange items count

Use GetDetailedStatisticsOperation to get the total number of existing compare-exchange items.
This operation does not retrieve any actual items, only the total count.

var stats = store.Maintenance.Send(new GetDetailedStatisticsOperation());
var itemsCount = stats.CountOfCompareExchange;

Syntax


GetCompareExchangeValuesOperation

Get multiple compare-exchange items using a store operation:

// Available overloads:
GetCompareExchangeValuesOperation(string[] keys);
GetCompareExchangeValuesOperation(string startWith, int? start = null, int? pageSize = null)
ParameterTypeDescription
keysstring[]Keys of the compare-exchange items to retrieve.
startWithstringThe common key prefix of the items to retrieve.
startint?The number of items that should be skipped. Default is 0
pageSizeint?The maximum number of values that will be retrieved. Default is int.MaxValue
Returned objectDescription
Dictionary<string, CompareExchangeValue<T>>A Dictionary with a compare-exchange item per key

GetCompareExchangeValues

Get multiple compare-exchange items using cluster-wide session

GetCompareExchangeValues<T>(string[] keys);
GetCompareExchangeValues<T>(string startsWith, int start = 0, int pageSize = 25);
ParameterTypeDescription
keysstring[]Keys of the compare-exchange items to retrieve.
startWithstringThe common key prefix of the items to retrieve.
startint?The number of items that should be skipped. Default is 0
pageSizeint?The maximum number of values that will be retrieved. Default is 25
Returned objectDescription
Dictionary<string, CompareExchangeValue<T>>A Dictionary with a compare-exchange item per key

// The CompareExchangeValue object:
// ================================

public class CompareExchangeValue<T>
{
// The unique identifier of the compare-exchange item.
public string Key { get; }

// The existing `value` of the returned compare-exchange item.
public T Value { get; set; }

// The compare-exchange item's version.
public long Index { get; internal set; }

// The existing `metadata` of the returned compare-exchange item.
public IMetadataDictionary Metadata;
}