Skip to main content

Indexes: Indexing Compare Exchange Values

  • Compare exchange values can be loaded within an index using the value's key.

  • The index will update when the compare exchange value is updated, as well as when documents are modified in the indexed collection(s).

  • In this page:

Syntax

When creating an index using AbstractIndexCreationTask, use the method LoadCompareExchangeValue() to load a compare exchange value by its key.

//Load one compare exchange value
T LoadCompareExchangeValue<T>(string key);

//Load multiple compare exchange values
T[] LoadCompareExchangeValue<T>(IEnumerable<string> keys);

For javascript indexes, use the method cmpxchg(<key>).

ParameterTypeDescription
keystringThe key of a particular compare exchange value.
keysIEnumerable<string>The keys of multiple compare exchange values.

Examples

These indexes map the rooms in a hotel, as well as compare exchange values representing the guests in those rooms.

private class Compare_Exchange_Index : AbstractIndexCreationTask<HotelRoom>
{
public class Result
{
public string Room;
public string Guests;
}

public Compare_Exchange_Index()
{
Map = HotelRooms => from room in HotelRooms
let guests = LoadCompareExchangeValue<string>(room.RoomID)
select new Result
{
Room = room.RoomID,
Guests = guests
};
}
}

Querying the Index

// Return all vacant hotel rooms
List<HotelRoom> vacantRooms = session
.Query<HotelRoom, Compare_Exchange_Index>()
.Where(x => RavenQuery.CmpXchg<string>(x.RoomID) == null)
.OfType<HotelRoom>()
.ToList();