1
1
import { compileSingleRowExpression } from "../query/compiler/evaluators.js"
2
2
import { comparisonFunctions } from "../query/builder/functions.js"
3
3
import { DEFAULT_COMPARE_OPTIONS , deepEquals } from "../utils.js"
4
+ import type { RangeQueryOptions } from "./btree-index.js"
4
5
import type { CompareOptions } from "../query/builder/types.js"
5
- import type { BasicExpression } from "../query/ir.js"
6
+ import type { BasicExpression , OrderByDirection } from "../query/ir.js"
6
7
7
8
/**
8
9
* Operations that indexes can support, imported from available comparison functions
@@ -24,12 +25,57 @@ export interface IndexStats {
24
25
readonly lastUpdated : Date
25
26
}
26
27
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
+
27
73
/**
28
74
* Base abstract class that all index types extend
29
75
*/
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
+ {
33
79
public readonly id : number
34
80
public readonly name ?: string
35
81
public readonly expression : BasicExpression
@@ -65,7 +111,20 @@ export abstract class BaseIndex<
65
111
from ?: TKey ,
66
112
filterFn ?: ( key : TKey ) => boolean
67
113
) : Array < TKey >
114
+ abstract takeReversed (
115
+ n : number ,
116
+ from ?: TKey ,
117
+ filterFn ?: ( key : TKey ) => boolean
118
+ ) : Array < TKey >
68
119
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 > >
69
128
70
129
// Common methods
71
130
supports ( operation : IndexOperation ) : boolean {
@@ -80,8 +139,31 @@ export abstract class BaseIndex<
80
139
)
81
140
}
82
141
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
+ */
83
146
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
85
167
}
86
168
87
169
getStats ( ) : IndexStats {
0 commit comments