11import { compileSingleRowExpression } from "../query/compiler/evaluators.js"
22import { comparisonFunctions } from "../query/builder/functions.js"
33import { DEFAULT_COMPARE_OPTIONS , deepEquals } from "../utils.js"
4+ import type { RangeQueryOptions } from "./btree-index.js"
45import type { CompareOptions } from "../query/builder/types.js"
5- import type { BasicExpression } from "../query/ir.js"
6+ import type { BasicExpression , OrderByDirection } from "../query/ir.js"
67
78/**
89 * Operations that indexes can support, imported from available comparison functions
@@ -24,12 +25,57 @@ export interface IndexStats {
2425 readonly lastUpdated : Date
2526}
2627
28+ export interface IndexInterface <
29+ TKey extends string | number = string | number ,
30+ > {
31+ add : ( key : TKey , item : any ) => void
32+ remove : ( key : TKey , item : any ) => void
33+ update : ( key : TKey , oldItem : any , newItem : any ) => void
34+
35+ build : ( entries : Iterable < [ TKey , any ] > ) => void
36+ clear : ( ) => void
37+
38+ lookup : ( operation : IndexOperation , value : any ) => Set < TKey >
39+
40+ equalityLookup : ( value : any ) => Set < TKey >
41+ inArrayLookup : ( values : Array < any > ) => Set < TKey >
42+
43+ rangeQuery : ( options : RangeQueryOptions ) => Set < TKey >
44+ rangeQueryReversed : ( options : RangeQueryOptions ) => Set < TKey >
45+
46+ take : (
47+ n : number ,
48+ from ?: TKey ,
49+ filterFn ?: ( key : TKey ) => boolean
50+ ) => Array < TKey >
51+ takeReversed : (
52+ n : number ,
53+ from ?: TKey ,
54+ filterFn ?: ( key : TKey ) => boolean
55+ ) => Array < TKey >
56+
57+ get keyCount ( ) : number
58+ get orderedEntriesArray ( ) : Array < [ any , Set < TKey > ] >
59+ get orderedEntriesArrayReversed ( ) : Array < [ any , Set < TKey > ] >
60+
61+ get indexedKeysSet ( ) : Set < TKey >
62+ get valueMapData ( ) : Map < any , Set < TKey > >
63+
64+ supports : ( operation : IndexOperation ) => boolean
65+
66+ matchesField : ( fieldPath : Array < string > ) => boolean
67+ matchesCompareOptions : ( compareOptions : CompareOptions ) => boolean
68+ matchesDirection : ( direction : OrderByDirection ) => boolean
69+
70+ getStats : ( ) => IndexStats
71+ }
72+
2773/**
2874 * Base abstract class that all index types extend
2975 */
30- export abstract class BaseIndex <
31- TKey extends string | number = string | number ,
32- > {
76+ export abstract class BaseIndex < TKey extends string | number = string | number >
77+ implements IndexInterface < TKey >
78+ {
3379 public readonly id : number
3480 public readonly name ?: string
3581 public readonly expression : BasicExpression
@@ -65,7 +111,20 @@ export abstract class BaseIndex<
65111 from ?: TKey ,
66112 filterFn ?: ( key : TKey ) => boolean
67113 ) : Array < TKey >
114+ abstract takeReversed (
115+ n : number ,
116+ from ?: TKey ,
117+ filterFn ?: ( key : TKey ) => boolean
118+ ) : Array < TKey >
68119 abstract get keyCount ( ) : number
120+ abstract equalityLookup ( value : any ) : Set < TKey >
121+ abstract inArrayLookup ( values : Array < any > ) : Set < TKey >
122+ abstract rangeQuery ( options : RangeQueryOptions ) : Set < TKey >
123+ abstract rangeQueryReversed ( options : RangeQueryOptions ) : Set < TKey >
124+ abstract get orderedEntriesArray ( ) : Array < [ any , Set < TKey > ] >
125+ abstract get orderedEntriesArrayReversed ( ) : Array < [ any , Set < TKey > ] >
126+ abstract get indexedKeysSet ( ) : Set < TKey >
127+ abstract get valueMapData ( ) : Map < any , Set < TKey > >
69128
70129 // Common methods
71130 supports ( operation : IndexOperation ) : boolean {
@@ -80,8 +139,31 @@ export abstract class BaseIndex<
80139 )
81140 }
82141
142+ /**
143+ * Checks if the compare options match the index's compare options.
144+ * The direction is ignored because the index can be reversed if the direction is different.
145+ */
83146 matchesCompareOptions ( compareOptions : CompareOptions ) : boolean {
84- return deepEquals ( this . compareOptions , compareOptions )
147+ const thisCompareOptionsWithoutDirection = {
148+ ...this . compareOptions ,
149+ direction : undefined ,
150+ }
151+ const compareOptionsWithoutDirection = {
152+ ...compareOptions ,
153+ direction : undefined ,
154+ }
155+
156+ return deepEquals (
157+ thisCompareOptionsWithoutDirection ,
158+ compareOptionsWithoutDirection
159+ )
160+ }
161+
162+ /**
163+ * Checks if the index matches the provided direction.
164+ */
165+ matchesDirection ( direction : OrderByDirection ) : boolean {
166+ return this . compareOptions . direction === direction
85167 }
86168
87169 getStats ( ) : IndexStats {
0 commit comments