Custom Index Types
A secondary index is a class the optimizer can use to speed up lookups and joins.
You pass an index class to createIndex,
and DortDB optimizes accordingly. Implementing your own means satisfying the
Index interface.
The Index interface
An index implements three methods:
match(expressions): given the expressions appearing in a query (e.g. the operands of an equality check), decide which ones this index can serve. Return the ordered positions it matches, ornullif it cannot help.createAccessor(expressions): for expressions that matched, return aCalculationthat looks up the matching items for given values of those expressions.reindex(values): given the source items and the evaluated index-expression keys, (re)fill the index's data structure.
interface Index {
expressions: Calculation[];
reindex(values: Iterable<{ value: unknown; keys: unknown[] }>): void;
match(expressions: IndexMatchInput[], renameMap?: RenameMap): number[] | null;
createAccessor(expressions: IndexMatchInput[]): Calculation;
}
An index does not have to store anything. The ConnectionIndex,
for example, keeps no data structure of its own; it recognizes join conditions
between nodes and relationships and resolves the connected elements through the
graph data adapter.
Hash-join indices
To let an index accelerate joins of non-indexed data (not just point lookups), implement the
HashJoinIndex extensions and register the class in
executor.hashJoinIndices:
- a static
canIndex(expressions)method, analogous tomatch, that decides whether the index applies to a join's expressions; and - an
allValues()method that iterates every stored value (needed to evaluate full outer joins).
new DortDB({
mainLang: SQL(),
optimizer: { rules: defaultRules },
executor: { hashJoinIndices: [MapIndex, MyIndex] },
});
The built-in MapIndex is the reference implementation of both roles: a hash
index usable for equality lookups and for equality-based hash joins.