Get Counters Operation
This operation is used to get counters' values for a specific document.
It can be used to get the value of a single counter, multiple counters' values, or all counters' values.
Syntax
Get Single Counter
public GetCountersOperation(String docId, String counter)
public GetCountersOperation(String docId, String counter, boolean returnFullResults)
Parameters | ||
---|---|---|
docId | String | The ID of the document that holds the counters |
counter | String | The name of the counter to get |
returnFullResults | boolean | A flag which indicates if the operation should include a dictionary of counter values per database node in the result |
Return Full Results flag:
If RavenDB is running in a distributed cluster, and the database resides on several nodes,
a counter can have a different local value on each database node, and the total counter value is the
sum of all the local values of this counter from each node.
In order to get the counter values per database node, set the returnFullResults
flag to true
Get Multiple Counters
public GetCountersOperation(String docId, String[] counters)
public GetCountersOperation(String docId, String[] counters, boolean returnFullResults)
Parameters | ||
---|---|---|
docId | String | The ID of the document that holds the counters |
counters | String[] | The names of the counters to get |
returnFullResults | boolean | A flag which indicates if the operation should include a dictionary of counter values per database node in the result |
Get All Counters of a Document
public GetCountersOperation(String docId)
public GetCountersOperation(String docId, boolean returnFullResults)
Parameters | ||
---|---|---|
docId | String | The ID of the document that holds the counters |
returnFullResults | boolean | A flag which indicates if the operation should include a dictionary of counter values per database node in the result |
Return Value
The operation returns a CountersDetail
object, which holds a list of CounterDetail
objects
public class CountersDetail {
private List<CounterDetail> counters;
// getters and setters
}
public class CounterDetail {
private String documentId; // ID of the document that holds the counter
private String counterName; // The counter name
private long totalValue; // Total counter value
private long etag; // Counter Etag
private Map<String, Long> counterValues; // A map of counter values per database node
private String changeVector; // Change vector of the counter
// getters and setters
}
Examples
Assume we have a document "users/1" that holds 3 counters -
"likes", "dislikes" and "downloads" - with values 10, 20 and 30 (respectively)
Example #1 : Get single counter
CountersDetail operationResult = store.operations()
.send(new GetCountersOperation("users/1", "likes"));
Result:
{
"Counters":
[
{
"DocumentId" : "users/1",
"CounterName" : "likes",
"TotalValue" : 10,
"CounterValues" : null
}
]
}
Example #2 : Get multiple counters
CountersDetail operationResult = store.operations()
.send(new GetCountersOperation("users/1", new String[]{ "likes", "dislikes" }));
Result:
{
"Counters":
[
{
"DocumentId" : "users/1",
"CounterName" : "likes",
"TotalValue" : 10,
"CounterValues" : null
},
{
"DocumentId" : "users/1",
"CounterName" : "dislikes",
"TotalValue" : 20,
"CounterValues" : null
}
]
}
Example #3 : Get all counters
CountersDetail operationResult = store.operations()
.send(new GetCountersOperation("users/1"));
Result:
{
"Counters":
[
{
"DocumentId" : "users/1",
"CounterName" : "likes",
"TotalValue" : 10,
"CounterValues" : null
},
{
"DocumentId" : "users/1",
"CounterName" : "dislikes",
"TotalValue" : 20,
"CounterValues" : null
},
{
"DocumentId" : "users/1",
"CounterName" : "downloads",
"TotalValue" : 30,
"CounterValues" : null
}
]
}
Example #4 : Include full values in the result
CountersDetail operationResult = store.operations()
.send(new GetCountersOperation("users/1", "likes", true));
Result:
Assuming a 3-node cluster, the distribution of the counter's value to nodes A, B, and C could be as follows:
{
"Counters":
[
{
"DocumentId" : "users/1",
"CounterName" : "likes",
"TotalValue" : 10,
"CounterValues" :
{
"A:35-UuCp420vs0u+URADcGVURA" : 5,
"B:83-SeCFU29daUOxfjUcAlLiJw" : 3,
"C:27-7i7GP8bOOkGYLNflO/rSeg" : 2,
}
}
]
}