RVN006 - Multi-map index uses only a single AddMap
-
RVN006 suggests a single-map base class when it finds exactly one
AddMapcall site in a multi-map index.
Review the index's behavior and configuration before converting it. -
Default severity: Info
Automated code fix: No -
In this article:
Description
Multi-map index base classes (AbstractMultiMapIndexCreationTask
and the time-series and counters variants: AbstractMultiMapTimeSeriesIndexCreationTask, AbstractMultiMapCountersIndexCreationTask)
support multiple map definitions, including maps over different collections.
When the index registers only one map, a matching single-map base class can simplify the definition.
Before converting, check that it preserves the index's source scope, result type, and field configuration
(see How to resolve).
RVN006 is a style suggestion: a multi-map index with a single map deploys and works correctly.
Trigger condition
When is RVN006 reported?
RVN006 is reported on the class name when exactly one AddMap call site is found in the constructors of the index class or its base classes,
including a base class from a referenced project.
Calls in methods of those classes also count when a constructor calls the method, directly or through other methods.
RVN006 counts call sites in the bodies it inspects; it does not verify whether each call executes or how often it runs. See Limitations for cases where calls are missed or counted without being executed.
When is RVN006 not reported?
- The only call is the multi-map
AddMapForAll<TBase>method
(AddMapForAll<TBase>("<name>", …)for time-series and counters indexes).
It registers maps for concrete types assignable toTBasein the assembly that definesTBase.
This type-based expansion has no direct single-map equivalent. - The only call is inside a
for,foreach,while, ordoloop in the same constructor or method body,
where it can register a map on each iteration. - No call is found. RVN005 reports this case.
- More than one call to
AddMaporAddMapForAllis found. Both methods count toward the total.
RVN006 is also not reported for the following index classes:
- Abstract index classes. Their subclasses are checked instead.
- Indexes that override
CreateIndexDefinition(). - Index classes that are
private,protected, orprivate protected, or nested in a class with one of these access levels. Generic andprotected internalindex classes are checked when the other trigger conditions are met. - Indexes whose required base-class information cannot be obtained from generated metadata or available source. This can happen when a base class is in a compiled assembly without the index information recorded by the RavenDB analyzer's source generator. See Distribution.
How to resolve
Consider the matching single-map base class below when the index registers only one map at runtime.
Preserve its result type and field configuration when converting.
Keep the multi-map base if one call site registers multiple maps, or if the conversion would change the behavior described below (see also Limitations).
Document indexes
Replace AbstractMultiMapIndexCreationTask with AbstractIndexCreationTask<TDocument>.
Assign Map = … instead of calling AddMap<TDocument>(…).
Use the same document type as the original AddMap type argument for TDocument.
Time-series indexes
Replace AbstractMultiMapTimeSeriesIndexCreationTask with AbstractTimeSeriesIndexCreationTask<TDocument>.
Keep AddMap("<time series name>", …) without the type argument.
Use the original AddMap type argument for TDocument: the document type whose collection owns the indexed time series.
Counters indexes
Replace AbstractMultiMapCountersIndexCreationTask with AbstractCountersIndexCreationTask<TDocument>.
Keep AddMap("<counter name>", …) without the type argument.
Use the original AddMap type argument for TDocument: the document type whose collection owns the indexed counters.
Preserve the result type
If the multi-map base specifies a result type, preserve it as the second type argument of the single-map base: <TDocument, TResult>.
For example, replace AbstractMultiMapTimeSeriesIndexCreationTask<TResult> with AbstractTimeSeriesIndexCreationTask<TDocument, TResult>.
This applies even when the index does not assign Reduce: Index, Store, Analyze, and other typed field options use TResult.
For map-reduce indexes, use the two-argument form with the reduce result type as TResult.
Time-series and counters: AddMap<object>
Keep the multi-map base when AddMap<object>("<name>", …) selects a named time series or counter across all collections.
The single-map AddMap(name, …) method does not support object.
A conversion is possible using AbstractTimeSeriesIndexCreationTask<object> or AbstractCountersIndexCreationTask<object>
(or the corresponding <object, TResult> form to preserve a result type),
and the single-map, non-generic AddMapForAll(…) method with a name filter inside the map expression.
However, this makes the index process every time series or counter in the database before filtering by name;
the multi-map form selects the named source directly.
Time-series and counters: compound fields
Keep the multi-map base if the index uses CompoundField(…).
The current single-map time-series and counters base classes do not copy this configuration into the generated index definition,
so converting silently drops the compound fields.
Before (triggers RVN006)
// Triggers RVN006: only one AddMap call.
public class Orders_ByCompany : AbstractMultiMapIndexCreationTask
{
public Orders_ByCompany()
{
AddMap<Order>(orders => from o in orders select new { o.Company });
}
}
After (correct)
public class Orders_ByCompany : AbstractIndexCreationTask<Order>
{
public Orders_ByCompany()
{
Map = orders => from o in orders select new { o.Company };
}
}
Limitations
RVN006 counts recognized AddMap and AddMapForAll call sites, not the maps registered at runtime.
A call inside a lambda or local function nested in a checked constructor or method also counts, even if that lambda or local function is never invoked.
RVN006 can undercount the maps registered at runtime:
- A helper or local function containing one
AddMapcall can run more than once, for example a generic helper called once per document type. The call site is counted once. - A helper can be called from a loop while its
AddMapcall is in another method body.
That call is counted once, without the loop exclusion. A single call in aList<T>.ForEachcallback is also counted once. - An unqualified
AddMap(…)call with an inferred type argument is not counted.
The equivalentthis.AddMap(…)call is recognized. - A helper defined in a base class from another project is not followed when only a derived constructor calls it.
Registrations reachable from that base class's own constructors still contribute through its recorded information.
These cases can leave one recognized call and cause RVN006 to be reported even though multiple maps are registered.
RVN006 can be suppressed even when only one map is registered:
- Calls in different constructors are added together, so two constructors with one
AddMapcall each are not reported. - Calls in both branches of an
ifare added together, even though only one branch executes. - Helper methods are matched by name only. Calls in all overloads sharing that name are added together, including overloads that are never invoked.
Related C# analyzers
- RVN005
Reports multi-map indexes for which noAddMaporAddMapForAllcall is found.