-
-
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! refactor: move the internal id to orama
- Loading branch information
Showing
13 changed files
with
163 additions
and
113 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
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
50 changes: 27 additions & 23 deletions
50
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 |
---|---|---|
@@ -1,66 +1,70 @@ | ||
import { Orama } from '../types.js'; | ||
|
||
export type DocumentID = string | number; | ||
export type InternalDocumentID = number; | ||
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; | ||
}; | ||
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; | ||
const { internalIdToId } = raw as InternalDocumentIDStore | ||
|
||
orama.internalDocumentIDStore.idToInternalId.clear(); | ||
orama.internalDocumentIDStore.internalIdToId = []; | ||
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]); | ||
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); | ||
const internalId = store.idToInternalId.get(id) | ||
|
||
if (internalId) { | ||
return internalId; | ||
return internalId | ||
} | ||
|
||
const currentId = store.idToInternalId.size + 1; | ||
const currentId = store.idToInternalId.size + 1 | ||
|
||
store.idToInternalId.set(id, currentId); | ||
store.internalIdToId.push(id); | ||
store.idToInternalId.set(id, currentId) | ||
store.internalIdToId.push(id) | ||
|
||
return currentId | ||
} | ||
|
||
return currentId; | ||
if (id > store.internalIdToId.length) { | ||
return getInternalDocumentId(store, id.toString()) | ||
} | ||
|
||
return id as number; | ||
return id | ||
} | ||
|
||
export function getDocumentIdFromInternalId(store: InternalDocumentIDStore, internalId: InternalDocumentID): string { | ||
if (store.internalIdToId.length < internalId) { | ||
throw new Error(`Invalid internalId ${internalId}`); | ||
throw new Error(`Invalid internalId ${internalId}`) | ||
} | ||
|
||
return store.internalIdToId[internalId - 1]; | ||
return store.internalIdToId[internalId - 1] | ||
} | ||
|
Oops, something went wrong.