blockstore: add ensure method to create directories

This commit is contained in:
Braydon Fuller 2019-03-07 11:27:15 -08:00
parent 0b0dd58a91
commit 89d3253f29
No known key found for this signature in database
GPG Key ID: F24F232D108B3AD4
5 changed files with 39 additions and 4 deletions

View File

@ -30,6 +30,16 @@ class AbstractBlockStore {
this.logger = Logger.global.context('blockstore');
}
/**
* This method ensures that resources are available
* before opening.
* @returns {Promise}
*/
async ensure() {
throw new Error('Abstract method.');
}
/**
* This method opens any necessary resources and
* initializes the store to be ready to be queried.

View File

@ -129,6 +129,17 @@ class FileBlockStore extends AbstractBlockStore {
}
}
/**
* This method ensures that both the block storage directory
* and index directory exist.
* before opening.
* @returns {Promise}
*/
async ensure() {
return fs.mkdirp(this.indexLocation);
}
/**
* Opens the file block store. It will regenerate necessary block
* indexing if the index is missing or inconsistent.

View File

@ -7,6 +7,7 @@
'use strict';
const bdb = require('bdb');
const fs = require('bfile');
const AbstractBlockStore = require('./abstract');
const layout = require('./layout');
const {types} = require('./common');
@ -37,6 +38,16 @@ class LevelBlockStore extends AbstractBlockStore {
});
}
/**
* This method ensures that the storage directory exists
* before opening.
* @returns {Promise}
*/
async ensure() {
return fs.mkdirp(this.location);
}
/**
* Opens the block storage.
* @returns {Promise}

View File

@ -57,6 +57,7 @@ class Node extends EventEmitter {
this.workers = null;
this.spv = false;
this.blocks = null;
this.chain = null;
this.fees = null;
this.mempool = null;
@ -135,6 +136,9 @@ class Node extends EventEmitter {
if (this.memory)
return undefined;
if (this.blocks)
await this.blocks.ensure();
return fs.mkdirp(this.config.prefix);
}

View File

@ -10,7 +10,6 @@ const common = require('./util/common');
const {resolve} = require('path');
const fs = require('bfile');
const {rimraf} = require('./util/common');
const {mkdirp} = require('bfile');
const random = require('bcrypto/lib/random');
const vectors = [
@ -347,13 +346,13 @@ describe('BlockStore', function() {
beforeEach(async () => {
await rimraf(location);
await mkdirp(location);
store = new FileBlockStore({
location: location,
maxFileLength: 1024
});
await store.ensure();
await store.open();
});
@ -657,13 +656,13 @@ describe('BlockStore', function() {
beforeEach(async () => {
await rimraf(location);
await mkdirp(location);
store = new FileBlockStore({
location: location,
maxFileLength: 1024 * 1024
});
await store.ensure();
await store.open();
});
@ -709,12 +708,12 @@ describe('BlockStore', function() {
beforeEach(async () => {
await rimraf(location);
await mkdirp(location);
store = new LevelBlockStore({
location: location
});
await store.ensure();
await store.open();
});