diff --git a/packages/leveldb/src/leveldb/instance.ts b/packages/leveldb/src/leveldb/instance.ts index 84c2c0e..9c28fa7 100644 --- a/packages/leveldb/src/leveldb/instance.ts +++ b/packages/leveldb/src/leveldb/instance.ts @@ -43,7 +43,7 @@ import type { import { boundGE, boundGT, boundLE, boundLT, compareDocsWithIndex } from '@pluto-encrypted/shared' export function getIndexName(index: string[]): string { - return index.join(','); + return `[${index.join('+')}]`; } export class RxStorageIntanceLevelDB implements RxStorageInstance< @@ -173,62 +173,80 @@ export class RxStorageIntanceLevelDB implements RxStorageInstance< const queryPlanFields: string[] = queryPlan.index; const mustManuallyResort = !queryPlan.sortSatisfiedByIndex; const index: string[] | undefined = queryPlanFields; + const _index = ['key'] + console.log({ _index }) + const lowerBound: any[] = queryPlan.startKeys; const lowerBoundString = getStartIndexStringFromLowerBound( this.schema, - index, + _index, // index, lowerBound ); const upperBound: any[] = queryPlan.endKeys; - const upperBoundString = getStartIndexStringFromUpperBound( this.schema, - index, + _index, // index, upperBound ); - const indexName = getIndexName(index); - // => '_meta.lwt,key' + + // HACK: getIndex(index) is doing the wrong thing ... so mutate the index (after upper/lower bound stuff) + const indexName = getIndexName([this.collectionName, 'key']) + console.log('index (wrong)', index) + console.log('getIndexName =>', indexName) + + // const indexName = getIndexName(index); + // // => '_meta.lwt,key' // QUESTION: this doesn't seem to map to any of the indexes being entered? // with setIndex - const docsWithIndex = await this.internals.getIndex(indexName) + const docsWithIndex = (await this.internals.getIndex(indexName)) - console.log({ - docsWithIndex - // NOTE: this is empty ... which leads to no results - }) + console.log({ docsWithIndex }) + console.log({ lowerBoundString, upperBoundString }) let indexOfLower = (queryPlan.inclusiveStart ? boundGE : boundGT)( - docsWithIndex, + // docsWithIndex, + // HACK: ecchh + docsWithIndex.map(d => { return { indexString: d } }), { indexString: lowerBoundString } as any, compareDocsWithIndex + // NOTE: this does comparisons like "a.indexString < b.indexString" + // QUESTION: Are we expecting docsWithIndex to have an indexString attribute? ); const indexOfUpper = (queryPlan.inclusiveEnd ? boundLE : boundLT)( - docsWithIndex, + // docsWithIndex, + // HACK: ecchh + docsWithIndex.map(d => { return { indexString: d } }), { indexString: upperBoundString } as any, compareDocsWithIndex ); + console.log({ indexOfLower, indexOfUpper }) + // { indexOfLower: 0, indexOfUpper: -1 } // QUESTION: what does -1 encode here? let rows: RxDocumentData[] = []; let done = false; while (!done) { const currentRow = docsWithIndex[indexOfLower]; + console.log({ currentRow }) if ( !currentRow || indexOfLower > indexOfUpper ) { + console.log('break') break; } const [currentDoc] = await this.findDocumentsById([currentRow], false) + console.log({ currentDoc }) if (currentDoc && (!queryMatcher || queryMatcher(currentDoc))) { + console.log('match!') rows.push(currentDoc); } diff --git a/packages/leveldb/src/leveldb/internal.ts b/packages/leveldb/src/leveldb/internal.ts index c02127d..fbe6d3b 100644 --- a/packages/leveldb/src/leveldb/internal.ts +++ b/packages/leveldb/src/leveldb/internal.ts @@ -70,6 +70,7 @@ export class LevelDBInternal implements LevelDBStorageInternals { + console.log('getIndex', key) const db = await this.getInstance() return db.get(key) .then(result => result ? JSON.parse(result) : []) @@ -128,7 +129,7 @@ export class LevelDBInternal implements LevelDBStorageInternals( a: DocWithIndexString, b: DocWithIndexString ): 1 | 0 | -1 { + if (a.indexString === undefined) throw Error(`missing indexString on ${a}`) + if (b.indexString === undefined) throw Error(`missing indexString on ${b}`) if (a.indexString < b.indexString) { return -1 } else if (a.indexString === b.indexString) {