-
-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! perf: use internal id instead of string id
- Loading branch information
Showing
20 changed files
with
197 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
packages/orama/src/components/internal-document-id-store.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { Orama } from '../types.js'; | ||
|
||
export type DocumentID = string | number; | ||
export type InternalDocumentID = number; | ||
|
||
export type InternalDocumentIDStore = { | ||
idToInternalId: Map<string, number>; | ||
internalIdToId: string[]; | ||
save: (store: InternalDocumentIDStore) => unknown; | ||
load: (orama: Orama, raw: unknown) => void; | ||
}; | ||
|
||
export function createInternalDocumentIDStore(): InternalDocumentIDStore { | ||
return { | ||
idToInternalId: new Map(), | ||
internalIdToId: [], | ||
save, | ||
load, | ||
}; | ||
} | ||
|
||
export function save(store: InternalDocumentIDStore): unknown { | ||
return { | ||
internalIdToId: store.internalIdToId, | ||
}; | ||
} | ||
|
||
export function load(orama: Orama, raw: unknown): void { | ||
const { internalIdToId } = raw as InternalDocumentIDStore; | ||
|
||
orama.internalDocumentIDStore.idToInternalId.clear(); | ||
orama.internalDocumentIDStore.internalIdToId = []; | ||
|
||
for (let i = 0; i < internalIdToId.length; i++) { | ||
orama.internalDocumentIDStore.idToInternalId.set(internalIdToId[i], i + 1); | ||
orama.internalDocumentIDStore.internalIdToId.push(internalIdToId[i]); | ||
} | ||
} | ||
|
||
export function getInternalDocumentId(store: InternalDocumentIDStore, id: DocumentID): InternalDocumentID { | ||
if (typeof id === "string") { | ||
const internalId = store.idToInternalId.get(id); | ||
|
||
if (internalId) { | ||
return internalId; | ||
} | ||
|
||
const currentId = store.idToInternalId.size + 1; | ||
|
||
store.idToInternalId.set(id, currentId); | ||
store.internalIdToId.push(id); | ||
|
||
return currentId; | ||
} | ||
|
||
return id as number; | ||
} | ||
|
||
export function getDocumentIdFromInternalId(store: InternalDocumentIDStore, internalId: InternalDocumentID): string { | ||
if (store.internalIdToId.length < internalId) { | ||
throw new Error(`Invalid internalId ${internalId}`); | ||
} | ||
|
||
return store.internalIdToId[internalId - 1]; | ||
} | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.