see on GitHub

Counters And Other Features



Counters and Indexing

Indexing Counters can speed-up finding them and the documents that contain them.
RavenDB supports Counters-indexing by name, but not by value.

  • Counter-Names indexing
    Re-indexing due to Counter-name modification is normally rare enough to pause no performance issues.
    To index a document's Counters by name, use CounterNamesFor.

  • Counter-Values indexing
    Indexing by Counter-value is not provided, changing the Counter's value will not trigger indexing of the document.
    Counters are designed for high-throughput scenarios and cannot afford the re-indexing cost of each change.


Counters and Queries

Create queries using code, or send the server raw queries for execution.

  • Either way, you can query Counters by name but not by value.
    This is because queries are generally based on indexes, and Counter values are not indexed.
  • Counter values can be projected from query results, as demonstrated in the following examples.
    This way a client can get Counter values from a query without downloading whole documents.

  • Use Session.Query to code queries yourself.

    • Returned Counter Value: Accumulated
      A Counter's value is returned as a single sum, with no specification of the Counter's value on each node.
      using (var session = docStore.OpenSession())
      {
          //Select documents from the "Products" collection, with a Counter named `ProductLikes`.  
          //Querying upon Counter values (e.g. using "Where") is not possible.  
          //In this example the documents that contain the Counter are NOT returned, only Counter values.
          var query = session.Query<Product>()
          .Select(product => RavenQuery.Counter(product, "ProductLikes"));
      
          var queryResults = query.ToList();
      
          //Show ProductLikes's value, or NULL for documents with no such Counter.  
          foreach (var counterValue in queryResults)
          {
              Console.WriteLine("ProductLikes: " + counterValue);
          }
      }
  • Use RawQuery to send the server raw RQL expressions for execution.

    • You can use the counter method.
      Returned Counter Value: Accumulated

    • You can use the counterRaw method.
      Returned Counter Value: Distributed
      A Counter's value is returned as a series of values, each maintained by one of the nodes.

      • It is not expected of you to use this in your application.
        Applications normally use the Counter's overall value, and very rarely refer to the value each node gives it.

    counter and counterRaw samples:

    //Various RQL expressions sent to the server using counter()
    //Returned Counter value is accumulated
    var rawquery1 = session.Advanced
        .RawQuery<CounterResult>("from users as u select counter(u, \"ProductLikes\")")
        .ToList();
    
    var rawquery2 = session.Advanced
        .RawQuery<CounterResult>("from users select counter(\"ProductLikes\") as ProductLikesCount")
        .ToList();
    
    var rawquery3 = session.Advanced
        .RawQuery<CounterResult>("from products where ProductLikes > 500 select ProductSection, counter(\"ProductLikes\")")
        .ToList();
    //An RQL expression sent to the server using counterRaw()
    //Returned Counter value is distributed
    var query = session.Advanced
        .RawQuery<CounterResultRaw>("from users as u select counterRaw(u, \"downloads\")")
        .ToList();


Counters and Revisions

A document revision stores all the document's Counters' names and values.

  • Stored Counter Values: Accumulated
    A revision stores a Counter's value as a single sum, with no specification of the Counter's value on each node.

  • Revisions-creation can be initiated by Counter-name modification.

    • When the Revisions feature is enabled, the creation or deletion of a Counter initiates the creation of a new document revision.
    • Counter value modifications do not cause the creation of new revisions.

Counters and Smuggler

Smuggler is a DocumentStore property, that can be used to export chosen database items to an external file or to import database items from an existing file into the database.

  • Transferred Counter Value: Distributed
    Smuggler transfers the entire series of values that the different nodes maintain for a Counter.
  • To make Smuggler handle Counters, include DatabaseItemType.Counters in OperateOnTypes's list of entities to import or export.
    OperateOnTypes = DatabaseItemType.Indexes
                     | DatabaseItemType.Documents
                     | DatabaseItemType.Counters

Counters and Changes API

Changes API is a Push service, that can inform you of various changes on the Server, including changes in Counter values.
You can target all Counters, or specify the ones you wish to follow.

  • Pushed Counter Value: Accumulated
    Changes API methods return a Counter's value as a single sum, without specifying its value on each node.
  • The service is initiated by Counter Value Modification.

Counters and Ongoing Tasks:

Each ongoing task relates to Counters in its own way.

  • Counters and the Backup task
    There are two backup types: logical-backup and snapshot.
    Both types store and restore all data, including Counters.
    Both types operate as an ongoing backup routine, with a pre-set time schedule.

    • Logical Backup:
      Backed-up Counter values: Distributed
      A logical backup is a higher-level implementation of Smuggler.
      As with Smuggler, Counters are backed-up and restored including their values on all nodes.
    • Snapshot:
      A snapshot stores all data and settings as a single binary image. All components, including Counters, are restored to the exact same state they've been at during backup.
  • Counters and the External Replication task
    The ongoing external replication task replicates all data, including Counters.

    • Replicated Counter Value: Distributed
      Counters are replicated along with their values on all nodes.
    • Replication can be initiated by both Counter-name update and Counter-value modification.
  • Counters and the ETL task
    ETL is used to export data from RavenDB to an external (either Raven or SQL) database.

    • SQL ETL is not supported.
      Counters cannot be exported to an SQL database over SQL ETL.
    • RavenDB ETL is supported.
      Counters are exported over RavenDB ETL.
      • Export can be initiated by both Counter-name update and Counter-value modification.
      • Exported Counter Value: Distributed
        Counters are exported along with their values on all nodes.
      • Counters can be exported using a script.
        Default behavior: When an ETL script is not provided, Counters are exported.
  • Counters and the Data Subscriptions task
    Data Subscriptions can be initiated by document changes, including those caused by Counter Name updates.
    Documents will not be delivered in reaction to Counter Value modification.

Counters and Other Features: Summary

Use this table to find if and how various RavenDB features are triggered by Counters, and how the various features handle Counter values.

  • In the Triggered By column:
    • Document Change - Feature is triggered by a Counter Name update.
    • Countrer Value Modification - Feature is triggered by a Counter Value modification.
    • Time Schedule - Feature is invoked by a pre-set time routine.
    • No Trigger - Feature is executed manually, through the Studio or by a Client.
  • In the Counter Value column:
    • Accumulated - Counter Value is handled as a single accumulated sum.
    • Distributed - Counter Value is handled as a series of values maintained by cluster nodes.
Feature Triggered by Counter Value
Indexing Document Change doesn't handle values
LINQ Queries No trigger Accumulated
Raw Queries No trigger counter() - Accumulated
counterRaw() - Distributed
Smuggler No trigger Distributed
Backup Task Time Schedule Distributed
RavenDB ETL Task Document Change,
Countrer Value Modification
Distributed
External Replication task Document Change,
Countrer Value Modification
Distributed
Data Subscriptions Update Task Document Change
Changes API Countrer Value Modification Accumulated
Revision creation Document Change Accumulated


Including Counters

You can include Counters while loading a document.
An included Counter is retrieved in the same request as its owner-document and is held by the session, so it can be immediately retrieved when needed with no additional remote calls.

  • Including Counters when using Session.Load:

    • Include a single Counter using IncludeCounter.
    • Include multiple Counters using IncludeCounters.

    IncludeCounter and IncludeCounters usage samples:

    //include single Counters
    var productPage = session.Load<Product>("products/1-C", includeBuilder =>
            includeBuilder.IncludeCounter("ProductLikes")
                          .IncludeCounter("ProductDislikes")
                          .IncludeCounter("ProductDownloads"));
    //include multiple Counters
    //note that you can combine the inclusion of Counters and documents.
    var productPage = session.Load<Product>("orders/1-A", includeBuilder =>
            includeBuilder.IncludeDocuments("products/1-C")
                          .IncludeCounters(new[] { "ProductLikes", "ProductDislikes" }));

  • Including Counters when using Session.Query:

    • Include a single Counter using IncludeCounter.
    • Include multiple Counters using IncludeCounters.

    IncludeCounter and IncludeCounters usage samples:

    //include a single Counter
    var query = session.Query<Product>()
            .Include(includeBuilder =>
             includeBuilder.IncludeCounter("ProductLikes"));
    //include multiple Counters
    var query = session.Query<Product>()
            .Include(includeBuilder =>
            includeBuilder.IncludeCounters(new[] { "ProductLikes", "ProductDownloads" }));