Skip to content
This repository has been archived by the owner on Jan 1, 2025. It is now read-only.

Commit

Permalink
gross hacks
Browse files Browse the repository at this point in the history
  • Loading branch information
mixmix committed Jul 15, 2024
1 parent 52bb0ee commit 65a70bf
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
44 changes: 31 additions & 13 deletions packages/leveldb/src/leveldb/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<RxDocType> implements RxStorageInstance<
Expand Down Expand Up @@ -173,62 +173,80 @@ export class RxStorageIntanceLevelDB<RxDocType> 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<RxDocType>[] = [];
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);
}

Expand Down
3 changes: 2 additions & 1 deletion packages/leveldb/src/leveldb/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export class LevelDBInternal<RxDocType> implements LevelDBStorageInternals<RxDoc
}

async getIndex(key: string): Promise<string[]> {
console.log('getIndex', key)
const db = await this.getInstance()
return db.get(key)
.then(result => result ? JSON.parse(result) : [])
Expand Down Expand Up @@ -128,7 +129,7 @@ export class LevelDBInternal<RxDocType> implements LevelDBStorageInternals<RxDoc
}

async setIndex(key: string, ids: string[]) {
// console.log('setIndex', { key, ids })
console.log('setIndex', key, ids)
// QUESTION: why is the value being store in the key?
// QUESTION: if setIndex + getIndex are misaligned...
// - then how do we know what these functions SHOULD be doing?
Expand Down
2 changes: 2 additions & 0 deletions packages/shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export function compareDocsWithIndex<RxDocType>(
a: DocWithIndexString<RxDocType>,
b: DocWithIndexString<RxDocType>
): 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) {
Expand Down

0 comments on commit 65a70bf

Please sign in to comment.