Skip to content

Commit

Permalink
fix: Use data dir for lmdb forks
Browse files Browse the repository at this point in the history
In case tmpdir() is not available, rely on the data dir for creating
forks of the store.
  • Loading branch information
spalladino committed Aug 14, 2024
1 parent 7cc47a6 commit b047110
Showing 1 changed file with 6 additions and 5 deletions.
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

0 comments on commit b047110

Please sign in to comment.