refactor: simplify and add memory indexing
This commit is contained in:
20
src/index/unique.ts
Normal file
20
src/index/unique.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import type { PrimaryKey } from "./primary.ts";
|
||||
|
||||
export class UniqueIndex {
|
||||
readonly #index = new Map<string, PrimaryKey>();
|
||||
|
||||
insert(value: any, pk: PrimaryKey) {
|
||||
if (this.#index.has(value)) {
|
||||
throw new Error(`Unique constraint violation: ${value}`);
|
||||
}
|
||||
this.#index.set(value, pk);
|
||||
}
|
||||
|
||||
lookup(value: any): PrimaryKey | undefined {
|
||||
return this.#index.get(value);
|
||||
}
|
||||
|
||||
delete(value: any) {
|
||||
this.#index.delete(value);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user