From 79242a524ce75a69b5716e855cf238c1ca444535 Mon Sep 17 00:00:00 2001 From: Zef Hemel Date: Wed, 30 Aug 2023 21:12:33 +0200 Subject: [PATCH] Full text refactor step 1 --- plugs/search/engine.test.ts | 22 ++++++++++++++++------ plugs/search/engine.ts | 20 ++++++++++---------- plugs/search/search.ts | 2 +- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/plugs/search/engine.test.ts b/plugs/search/engine.test.ts index be307b48..5cf7d18b 100644 --- a/plugs/search/engine.test.ts +++ b/plugs/search/engine.test.ts @@ -1,22 +1,32 @@ import { assertEquals } from "../../test_deps.ts"; import { BatchKVStore, SimpleSearchEngine } from "./engine.ts"; -class InMemoryBatchKVStore implements BatchKVStore { - private store = new Map(); +class InMemoryBatchKVStore implements BatchKVStore { + private store = new Map(); - get(keys: K[]): Promise<(V | undefined)[]> { - const results: (V | undefined)[] = keys.map((key) => this.store.get(key)); + get(keys: string[]): Promise<(any | undefined)[]> { + const results: (any | undefined)[] = keys.map((key) => this.store.get(key)); return Promise.resolve(results); } - set(entries: Map): Promise { + queryPrefix(prefix: string): Promise<[string, any][]> { + const results: [string, any][] = []; + for (const [key, value] of this.store.entries()) { + if (key.startsWith(prefix)) { + results.push([key, value]); + } + } + return Promise.resolve(results); + } + + set(entries: Map): Promise { for (const [key, value] of entries) { this.store.set(key, value); } return Promise.resolve(); } - delete(keys: K[]): Promise { + delete(keys: string[]): Promise { for (const key of keys) { this.store.delete(key); } diff --git a/plugs/search/engine.ts b/plugs/search/engine.ts index 10fc5117..6840e9a6 100644 --- a/plugs/search/engine.ts +++ b/plugs/search/engine.ts @@ -5,10 +5,10 @@ export type Document = { text: string; }; -export interface BatchKVStore { - get(keys: K[]): Promise<(V | undefined)[]>; - set(entries: Map): Promise; - delete(keys: K[]): Promise; +export interface BatchKVStore { + get(keys: string[]): Promise<(any | undefined)[]>; + set(entries: Map): Promise; + delete(keys: string[]): Promise; } type ResultObject = { @@ -20,8 +20,8 @@ export class SimpleSearchEngine { private stopWords = ["and", "or", "the", "a", "an"]; constructor( - public index: BatchKVStore, - public reverseIndex: BatchKVStore, + public index: BatchKVStore, + public reverseIndex: BatchKVStore, ) { } @@ -86,7 +86,7 @@ export class SimpleSearchEngine { const filteredWords = this.removeStopWords(words); const stemmedWords = filteredWords.map((word) => this.stem(word)); - const wordIdsArray = await this.index.get(stemmedWords); + const wordIdsArray: string[][] = await this.index.get(stemmedWords); const matchCounts: Map = new Map(); wordIdsArray.forEach((wordIds) => { @@ -110,13 +110,13 @@ export class SimpleSearchEngine { // Delete a document from the index public async deleteDocument(documentId: string): Promise { - const words = await this.reverseIndex.get([documentId]); + const words: string[][] = await this.reverseIndex.get([documentId]); if (words && words[0]) { - const currentIdsArray = await this.index.get(words[0]); + const currentIdsArray: string[][] = await this.index.get(words[0]); const deleteKeys: string[] = []; const updateMap = new Map(); - words[0].forEach((word, i) => { + words[0].forEach((word: string, i: number) => { const currentIds = currentIdsArray[i]; if (currentIds) { const updatedIds = currentIds.filter((id) => id !== documentId); diff --git a/plugs/search/search.ts b/plugs/search/search.ts index 7627cc64..a3880337 100644 --- a/plugs/search/search.ts +++ b/plugs/search/search.ts @@ -8,7 +8,7 @@ import { PromiseQueue } from "$sb/lib/async.ts"; const searchPrefix = "🔍 "; -class StoreKVStore implements BatchKVStore { +class StoreKVStore implements BatchKVStore { constructor(private prefix: string) { } get(keys: string[]): Promise<(string[] | undefined)[]> {