Support explicit read-only RAG archives
This commit is contained in:
parent
b3191f4f2c
commit
bba41efbd6
6 changed files with 36 additions and 8 deletions
|
|
@ -7,20 +7,27 @@ export class SerializedFileStore<T> {
|
|||
public constructor(
|
||||
private readonly filePath: string,
|
||||
private readonly createDefault: () => T,
|
||||
private readonly options: { readOnly?: boolean } = {},
|
||||
) {}
|
||||
|
||||
public async snapshot(): Promise<T> {
|
||||
const store = await this.serialize(() => this.withInterprocessLock(() => this.readUnlocked()));
|
||||
const store = await this.serialize(() =>
|
||||
this.options.readOnly
|
||||
? this.readUnlocked()
|
||||
: this.withInterprocessLock(() => this.readUnlocked()),
|
||||
);
|
||||
return structuredClone(store);
|
||||
}
|
||||
|
||||
public async overwrite(nextValue: T): Promise<void> {
|
||||
this.requireWritable();
|
||||
await this.serialize(async () => {
|
||||
await this.withInterprocessLock(() => this.writeUnlocked(nextValue));
|
||||
});
|
||||
}
|
||||
|
||||
public async mutate<R>(mutator: (current: T) => R | Promise<R>): Promise<R> {
|
||||
this.requireWritable();
|
||||
return this.serialize(async () => {
|
||||
return this.withInterprocessLock(async () => {
|
||||
const current = await this.readUnlocked();
|
||||
|
|
@ -32,6 +39,7 @@ export class SerializedFileStore<T> {
|
|||
}
|
||||
|
||||
public async normalize(): Promise<void> {
|
||||
this.requireWritable();
|
||||
await this.serialize(async () => {
|
||||
await this.withInterprocessLock(async () => {
|
||||
const current = await this.readUnlocked();
|
||||
|
|
@ -49,6 +57,12 @@ export class SerializedFileStore<T> {
|
|||
return pending;
|
||||
}
|
||||
|
||||
private requireWritable(): void {
|
||||
if (this.options.readOnly) {
|
||||
throw new Error(`Read-only file store cannot mutate: ${this.filePath}`);
|
||||
}
|
||||
}
|
||||
|
||||
private async readUnlocked(): Promise<T> {
|
||||
try {
|
||||
const raw = await readFile(this.filePath, "utf8");
|
||||
|
|
|
|||
|
|
@ -56,11 +56,11 @@ interface HistoryArchiveFilters
|
|||
export class FileMessageArchiveRepository {
|
||||
private readonly store: SerializedFileStore<MessageArchiveStore>;
|
||||
|
||||
public constructor(filePath: string) {
|
||||
public constructor(filePath: string, options: { readOnly?: boolean } = {}) {
|
||||
this.store = new SerializedFileStore(filePath, () => ({
|
||||
version: 1,
|
||||
messages: [],
|
||||
}));
|
||||
}), options);
|
||||
}
|
||||
|
||||
public async upsert(messages: ArchivedMessage[]): Promise<MessageArchiveMutationResult> {
|
||||
|
|
|
|||
|
|
@ -57,11 +57,14 @@ export class FileSourceDocumentArchiveRepository {
|
|||
private readonly shardRoot: string;
|
||||
private initialization?: Promise<void>;
|
||||
|
||||
public constructor(private readonly filePath: string) {
|
||||
public constructor(
|
||||
private readonly filePath: string,
|
||||
private readonly options: { readOnly?: boolean } = {},
|
||||
) {
|
||||
this.manifestStore = new SerializedFileStore(filePath, () => ({
|
||||
version: 2,
|
||||
repos: [],
|
||||
}));
|
||||
}), options);
|
||||
this.shardRoot = deriveShardRoot(filePath);
|
||||
}
|
||||
|
||||
|
|
@ -277,7 +280,7 @@ export class FileSourceDocumentArchiveRepository {
|
|||
version: 1,
|
||||
repoName,
|
||||
documents: [],
|
||||
}));
|
||||
}), this.options);
|
||||
}
|
||||
|
||||
private async syncRepoSummary(repoName: string, documentCount: number): Promise<void> {
|
||||
|
|
|
|||
Loading…
Reference in a new issue