Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Use data dir for lmdb forks #7973

Merged
merged 1 commit into from
Aug 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions yarn-project/kv-store/src/lmdb/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createDebugLogger } from '@aztec/foundation/log';
import { mkdtemp } from 'fs/promises';
import { type Database, type Key, type RootDatabase, open } from 'lmdb';
import { tmpdir } from 'os';
import { join } from 'path';
import { dirname, join } from 'path';

import { type AztecArray } from '../interfaces/array.js';
import { type AztecCounter } from '../interfaces/counter.js';
Expand All @@ -25,7 +25,7 @@ export class AztecLmdbStore implements AztecKVStore {
#data: Database<unknown, Key>;
#multiMapData: Database<unknown, Key>;

constructor(rootDb: RootDatabase, public readonly isEphemeral: boolean) {
constructor(rootDb: RootDatabase, public readonly isEphemeral: boolean, private path?: string) {
this.#rootDb = rootDb;

// big bucket to store all the data
Expand Down Expand Up @@ -61,18 +61,19 @@ export class AztecLmdbStore implements AztecKVStore {
): AztecLmdbStore {
log.info(`Opening LMDB database at ${path || 'temporary location'}`);
const rootDb = open({ path, noSync: ephemeral });
return new AztecLmdbStore(rootDb, ephemeral);
return new AztecLmdbStore(rootDb, ephemeral, path);
}

/**
* Forks the current DB into a new DB by backing it up to a temporary location and opening a new lmdb db.
* @returns A new AztecLmdbStore.
*/
async fork() {
const forkPath = join(await mkdtemp(join(tmpdir(), 'aztec-store-fork-')), 'root.mdb');
const baseDir = this.path ? dirname(this.path) : tmpdir();
const forkPath = join(await mkdtemp(join(baseDir, 'aztec-store-fork-')), 'root.mdb');
await this.#rootDb.backup(forkPath, false);
const forkDb = open(forkPath, { noSync: this.isEphemeral });
return new AztecLmdbStore(forkDb, this.isEphemeral);
return new AztecLmdbStore(forkDb, this.isEphemeral, forkPath);
}

/**
Expand Down
Loading