1- import type { LevelDatabase } from './create-level.js' ;
2- import type { AbstractBatchDelOperation , AbstractBatchOperation , AbstractIteratorOptions } from 'abstract-level' ;
31import type { Filter , RangeFilter } from '../core/types.js' ;
2+ import type { LevelWrapperBatchOperation , LevelWrapperIteratorOptions } from './level-wrapper.js' ;
43
5- import { abortOr } from '../utils/abort.js' ;
6- import { createLevelDatabase } from './create-level.js' ;
74import { flatten } from '../utils/object.js' ;
5+ import { createLevelDatabase , LevelWrapper } from './level-wrapper.js' ;
86
97export type Entry = {
108 _id : string ,
@@ -21,38 +19,30 @@ export interface IndexLevelOptions {
2119export class IndexLevel {
2220 config : IndexLevelConfig ;
2321
24- db : LevelDatabase < string , string > ;
22+ db : LevelWrapper < string > ;
2523
2624 constructor ( config ?: IndexLevelConfig ) {
2725 this . config = {
2826 createLevelDatabase,
2927 ...config
3028 } ;
29+
30+ this . db = new LevelWrapper < string > ( { ...this . config , valueEncoding : 'utf8' } ) ;
3131 }
3232
3333 async open ( ) : Promise < void > {
34- await this . createLevelDatabase ( ) ;
35-
3634 return this . db . open ( ) ;
3735 }
3836
3937 async close ( ) : Promise < void > {
40- if ( ! this . db ) {
41- return ;
42- }
43-
4438 return this . db . close ( ) ;
4539 }
4640
4741 async put ( entry : Entry , options ?: IndexLevelOptions ) : Promise < void > {
48- options ?. signal ?. throwIfAborted ( ) ;
49-
50- await abortOr ( options ?. signal , this . createLevelDatabase ( ) ) ;
51-
5242 entry = flatten ( entry ) as Entry ;
5343 const { _id } = entry ;
5444
55- const ops : AbstractBatchOperation < LevelDatabase < string , string > , string , string > [ ] = [ ] ;
45+ const ops : LevelWrapperBatchOperation < string > [ ] = [ ] ;
5646 const prefixes : string [ ] = [ ] ;
5747 for ( const property in entry ) {
5848 if ( property === '_id' ) {
@@ -67,14 +57,10 @@ export class IndexLevel {
6757 }
6858 ops . push ( { type : 'put' , key : `__${ _id } __prefixes` , value : JSON . stringify ( prefixes ) } ) ;
6959
70- await abortOr ( options ?. signal , this . db . batch ( ops ) ) ;
60+ return this . db . batch ( ops , options ) ;
7161 }
7262
7363 async query ( filter : Filter , options ?: IndexLevelOptions ) : Promise < Array < string > > {
74- options ?. signal ?. throwIfAborted ( ) ;
75-
76- await abortOr ( options ?. signal , this . createLevelDatabase ( ) ) ;
77-
7864 const requiredProperties = new Set < string > ( ) ;
7965 const missingPropertiesForID : { [ id : string ] : Set < string > } = { } ;
8066 const promises : Promise < Matches > [ ] = [ ] ;
@@ -116,32 +102,28 @@ export class IndexLevel {
116102 }
117103
118104 async delete ( id : string , options ?: IndexLevelOptions ) : Promise < void > {
119- options ?. signal ?. throwIfAborted ( ) ;
120-
121- await abortOr ( options ?. signal , this . createLevelDatabase ( ) ) ;
122-
123- const prefixes = await abortOr ( options ?. signal , this . db . get ( `__${ id } __prefixes` ) ) ;
105+ const prefixes = await this . db . get ( `__${ id } __prefixes` , options ) ;
124106 if ( ! prefixes ) {
125107 return ;
126108 }
127109
128- const ops : AbstractBatchDelOperation < LevelDatabase < string , string > , string > [ ] = [ ] ;
110+ const ops : LevelWrapperBatchOperation < string > [ ] = [ ] ;
129111 for ( const prefix of JSON . parse ( prefixes ) ) {
130112 ops . push ( { type : 'del' , key : this . join ( prefix , id ) } ) ;
131113 }
132114 ops . push ( { type : 'del' , key : `__${ id } __prefixes` } ) ;
133115
134- await abortOr ( options ?. signal , this . db . batch ( ops ) ) ;
116+ return this . db . batch ( ops , options ) ;
135117 }
136118
137- clear ( ) : Promise < void > {
119+ async clear ( ) : Promise < void > {
138120 return this . db . clear ( ) ;
139121 }
140122
141123 private async findExactMatches ( propertyName : string , propertyValue : unknown , options ?: IndexLevelOptions ) : Promise < Matches > {
142124 const propertyKey = this . join ( propertyName , this . encodeValue ( propertyValue ) ) ;
143125
144- const iteratorOptions : AbstractIteratorOptions < string , string > = {
126+ const iteratorOptions : LevelWrapperIteratorOptions < string > = {
145127 gt : propertyKey
146128 } ;
147129
@@ -151,7 +133,7 @@ export class IndexLevel {
151133 private async findRangeMatches ( propertyName : string , range : RangeFilter , options ?: IndexLevelOptions ) : Promise < Matches > {
152134 const propertyKey = this . join ( propertyName ) ;
153135
154- const iteratorOptions : AbstractIteratorOptions < string , string > = { } ;
136+ const iteratorOptions : LevelWrapperIteratorOptions < string > = { } ;
155137 for ( const comparator in range ) {
156138 iteratorOptions [ comparator ] = this . join ( propertyName , this . encodeValue ( range [ comparator ] ) ) ;
157139 }
@@ -171,7 +153,7 @@ export class IndexLevel {
171153
172154 private async findMatches (
173155 propertyName : string ,
174- iteratorOptions : AbstractIteratorOptions < string , string > ,
156+ iteratorOptions : LevelWrapperIteratorOptions < string > ,
175157 options ?: IndexLevelOptions
176158 ) : Promise < Matches > {
177159 // Since we will stop iterating if we encounter entries that do not start with the `propertyName`, we need to always start from the upper bound.
@@ -181,9 +163,7 @@ export class IndexLevel {
181163 }
182164
183165 const matches = new Map < string , string > ;
184- for await ( const [ key , value ] of this . db . iterator ( iteratorOptions ) ) {
185- options ?. signal ?. throwIfAborted ( ) ;
186-
166+ for await ( const [ key , value ] of this . db . iterator ( iteratorOptions , options ) ) {
187167 if ( ! key . startsWith ( propertyName ) ) {
188168 break ;
189169 }
@@ -206,10 +186,6 @@ export class IndexLevel {
206186 private join ( ...values : unknown [ ] ) : string {
207187 return values . join ( `\x00` ) ;
208188 }
209-
210- private async createLevelDatabase ( ) : Promise < void > {
211- this . db ??= await this . config . createLevelDatabase < string , string > ( this . config . location , { keyEncoding : 'utf8' , valueEncoding : 'utf8' } ) ;
212- }
213189}
214190
215191type IndexLevelConfig = {
0 commit comments