Skip to content

Commit

Permalink
(#1178) - Make sure all stores are closed if an error occurs with one…
Browse files Browse the repository at this point in the history
… of them.
  • Loading branch information
davedoesdev authored and calvinmetcalf committed Dec 29, 2013
1 parent f9922c7 commit 18177d3
Showing 1 changed file with 37 additions and 30 deletions.
67 changes: 37 additions & 30 deletions lib/adapters/leveldb.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,38 @@ function LevelPouch(opts, callback) {
uuid = fs.readFileSync(uuidPath);
}

function closeStores(callback) {
var dbpath = path.resolve(opts.name);
var stores = [
path.join(dbpath, DOC_STORE),
path.join(dbpath, BY_SEQ_STORE),
path.join(dbpath, ATTACH_STORE),
path.join(dbpath, ATTACH_BINARY_STORE)
];
var closed = 0;
stores.map(function (path) {
var store = STORES[path];
if (store) {
store.close(function () {
delete STORES[path];

if (++closed >= stores.length) {
done();
}
});
}
else {
if (++closed >= stores.length) {
done();
}
}
});

function done() {
call(callback, null);
}
}

function initstore(store_name, encoding) {

var dbpath = path.resolve(path.join(opts.name, store_name));
Expand All @@ -77,10 +109,14 @@ function LevelPouch(opts, callback) {

function setup_store(err, ldb) {
if (stores.err) {
if (ldb) {
ldb.close();
}
return;
}
if (err) {
stores.err = err;
closeStores();
return call(callback, err);
}

Expand Down Expand Up @@ -666,36 +702,7 @@ function LevelPouch(opts, callback) {
if (!opened) {
return call(callback, errors.NOT_OPEN);
}

var dbpath = path.resolve(opts.name);
var stores = [
path.join(dbpath, DOC_STORE),
path.join(dbpath, BY_SEQ_STORE),
path.join(dbpath, ATTACH_STORE),
path.join(dbpath, ATTACH_BINARY_STORE)
];
var closed = 0;
stores.map(function (path) {
var store = STORES[path];
if (store) {
store.close(function () {
delete STORES[path];

if (++closed >= stores.length) {
done();
}
});
}
else {
if (++closed >= stores.length) {
done();
}
}
});

function done() {
call(callback, null);
}
closeStores(callback);
};

api._getRevisionTree = function (docId, callback) {
Expand Down

0 comments on commit 18177d3

Please sign in to comment.