Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

cache open dbs, so that we didn't crash when we open the same db twice

  • Loading branch information...
commit 2cfa741aba8a172b6eabf8c95db704bdc9cace8e 1 parent 427ccbe
Peteris Krumins authored
Showing with 26 additions and 8 deletions.
  1. +26 −8 lib/store.js
34 lib/store.js
View
@@ -1,5 +1,8 @@
var SQLite = require('pksqlite').Database;
var EventEmitter = require('events').EventEmitter;
+var normalize = require('path').normalize;
+
+var openDbs = {};
module.exports = Store;
Store.prototype = new EventEmitter;
@@ -20,6 +23,13 @@ function Store(opts, cb) {
if (opts.filename === undefined)
throw new Error('Filename was not specified');
+ if (opts.filename != ':memory:') {
+ if (opts.filename[0] != '/')
+ opts.filename = process.cwd() + '/' + opts.filename;
+
+ opts.filename = normalize(opts.filename);
+ }
+
opts.json = opts.json || false;
self.on('ready', function () {
@@ -30,14 +40,21 @@ function Store(opts, cb) {
delete actionQueue;
});
- var db = new SQLite();
- db.open(opts.filename, function (error) {
- if (error) {
- self.emit('error', error);
- cb(error);
- }
- initStoreTable();
- });
+ console.dir(opts.filename);
+
+ if (openDbs[opts.filename]) {
+ var db = openDbs[opts.filename];
+ }
+ else {
+ var db = new SQLite();
+ db.open(opts.filename, function (error) {
+ if (error) {
+ self.emit('error', error);
+ cb(error);
+ }
+ initStoreTable();
+ });
+ }
function initStoreTable() {
var hadRow = false;
@@ -52,6 +69,7 @@ function Store(opts, cb) {
if (row === undefined) {
if (hadRow) {
self.emit('ready');
+
cb(undefined, self);
}
else {
Please sign in to comment.
Something went wrong with that request. Please try again.