|
| 1 | +import { |
| 2 | + getStartIndexStringFromLowerBound, |
| 3 | + getStartIndexStringFromUpperBound |
| 4 | +} from '../../custom-index'; |
| 5 | +import type { |
| 6 | + RxDocumentData, |
| 7 | + RxStorageQueryResult |
| 8 | +} from '../../types'; |
| 9 | +import { ensureNotFalsy } from '../../util'; |
| 10 | +import { RxStorageDexieStatics } from '../dexie'; |
| 11 | +import { getFoundationDBIndexName } from './foundationdb-helpers'; |
| 12 | +import type { |
| 13 | + FoundationDBPreparedQuery |
| 14 | +} from './foundationdb-types'; |
| 15 | +import { RxStorageInstanceFoundationDB } from './rx-storage-instance-foundationdb'; |
| 16 | + |
| 17 | +export async function queryFoundationDB<RxDocType>( |
| 18 | + instance: RxStorageInstanceFoundationDB<RxDocType>, |
| 19 | + preparedQuery: FoundationDBPreparedQuery<RxDocType> |
| 20 | +): Promise<RxStorageQueryResult<RxDocType>> { |
| 21 | + const queryPlan = preparedQuery.queryPlan; |
| 22 | + const query = preparedQuery.query; |
| 23 | + const skip = query.skip ? query.skip : 0; |
| 24 | + const limit = query.limit ? query.limit : Infinity; |
| 25 | + const skipPlusLimit = skip + limit; |
| 26 | + const queryPlanFields: string[] = queryPlan.index; |
| 27 | + const mustManuallyResort = !queryPlan.sortFieldsSameAsIndexFields; |
| 28 | + |
| 29 | + const queryMatcher = RxStorageDexieStatics.getQueryMatcher( |
| 30 | + instance.schema, |
| 31 | + preparedQuery |
| 32 | + ); |
| 33 | + const sortComparator = RxStorageDexieStatics.getSortComparator(instance.schema, preparedQuery); |
| 34 | + const dbs = await instance.internals.dbsPromise; |
| 35 | + |
| 36 | + |
| 37 | + const indexForName = queryPlanFields.slice(0); |
| 38 | + indexForName.unshift('_deleted'); |
| 39 | + const indexName = getFoundationDBIndexName(indexForName); |
| 40 | + const indexDB = ensureNotFalsy(dbs.indexes[indexName]).db; |
| 41 | + |
| 42 | + let lowerBound: any[] = queryPlan.startKeys; |
| 43 | + lowerBound = [false].concat(lowerBound); |
| 44 | + const lowerBoundString = getStartIndexStringFromLowerBound( |
| 45 | + instance.schema, |
| 46 | + indexForName, |
| 47 | + lowerBound |
| 48 | + ); |
| 49 | + |
| 50 | + let upperBound: any[] = queryPlan.endKeys; |
| 51 | + upperBound = [false].concat(upperBound); |
| 52 | + const upperBoundString = getStartIndexStringFromUpperBound( |
| 53 | + instance.schema, |
| 54 | + indexForName, |
| 55 | + upperBound |
| 56 | + ); |
| 57 | + let result = await dbs.root.doTransaction(async (tx: any) => { |
| 58 | + const innerResult: RxDocumentData<RxDocType>[] = []; |
| 59 | + const indexTx = tx.at(indexDB.subspace); |
| 60 | + const mainTx = tx.at(dbs.main.subspace); |
| 61 | + |
| 62 | + const range = indexTx.getRangeBatch( |
| 63 | + lowerBoundString, |
| 64 | + upperBoundString, |
| 65 | + { |
| 66 | + // TODO these options seem to be broken in the foundationdb node bindings |
| 67 | + // limit: instance.settings.batchSize, |
| 68 | + // streamingMode: StreamingMode.Exact |
| 69 | + } |
| 70 | + ); |
| 71 | + let done = false; |
| 72 | + while (!done) { |
| 73 | + const next = await range.next(); |
| 74 | + if (next.done) { |
| 75 | + done = true; |
| 76 | + break; |
| 77 | + } |
| 78 | + const docIds = next.value.map((row: string[]) => row[1]); |
| 79 | + const docsData: RxDocumentData<RxDocType>[] = await Promise.all(docIds.map((docId: string) => mainTx.get(docId))); |
| 80 | + docsData.forEach((docData) => { |
| 81 | + if (!done) { |
| 82 | + if ( |
| 83 | + queryMatcher(docData) |
| 84 | + ) { |
| 85 | + innerResult.push(docData); |
| 86 | + } |
| 87 | + } |
| 88 | + if ( |
| 89 | + !mustManuallyResort && |
| 90 | + innerResult.length === skipPlusLimit |
| 91 | + ) { |
| 92 | + done = true; |
| 93 | + range.return(); |
| 94 | + } |
| 95 | + }); |
| 96 | + } |
| 97 | + return innerResult; |
| 98 | + }); |
| 99 | + if (mustManuallyResort) { |
| 100 | + result = result.sort(sortComparator); |
| 101 | + } |
| 102 | + |
| 103 | + // apply skip and limit boundaries. |
| 104 | + result = result.slice(skip, skipPlusLimit); |
| 105 | + |
| 106 | + return { |
| 107 | + documents: result |
| 108 | + }; |
| 109 | +} |
0 commit comments