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

Explicit connect / disconnect in level adapter, etc #230

Merged
merged 15 commits into from
Sep 18, 2014
Merged
Show file tree
Hide file tree
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
176 changes: 116 additions & 60 deletions lib/adapters/level/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,12 @@ if (!level && !multilevel) {

_baseConfig = {
db: '/data/level',
sublevel: '',
sublevel: null,
keyPrefix: true,
multilevel: {
port: 3000,
host: 'localhost',
manifest: '',
auth: false
}
port: null,
host: null,
manifest: null,
auth: null
};

var Adapter = function (options) {
Expand All @@ -55,6 +53,8 @@ var Adapter = function (options) {
this.client = null;
this.db = null;

this.isMultilevel = (this.config.port && this.config.host && multilevel);

this.init.apply(this, arguments);
};

Expand All @@ -72,62 +72,61 @@ utils.mixin(Adapter.prototype, new (function () {
if (config.sublevel) {
// Load sublevel, and set db
sublevel = utils.file.requireLocal('level-sublevel');
db = sublevel(db);
db = db.sublevel(config.sublevel);
this._db = sublevel(db);
this.db = this._db.sublevel(config.sublevel);
} else {
this._db = this.db = db;
}

this.db = db;
};

this._initMultilevel = function (config) {
var db
, con
, manifest;

if (config.multilevel.manifest) {
manifest = require(config.multilevel.manifest);
}

db = multilevel.client(manifest);

this.client = net.connect(config.multilevel.port, config.multilevel.host, function (err) {
if (err) throw err;
});
var manifest;

if (config.multilevel.auth) {
db.auth(config.multilevel.auth, function (err) {
if (err) throw err;
});
if (config.manifest) {
manifest = config.manifest;
if (typeof(manifest) === 'string') {
manifest = require(manifest);
}
}

this.client.pipe(db.createRpcStream()).pipe(this.client);

if (config.sublevel) {
this.db = db.sublevel(config.sublevel);
} else {
this.db = db;
}
this._db = multilevel.client(manifest);
};

this._generateKey = function (type, id) {
var keyPrefix = this.config.keyPrefix;
this._generateKey = function (type, id, reverse) {
var keyPrefix = this.config.keyPrefix
, key;

if (keyPrefix === true) {
return type + delimiter + id;

if (reverse) {
key = id.replace(type + delimiter, id);
}
else {
key = type + delimiter + id;
}
}
else if (keyPrefix === false) {
return id;
key = id;
}
else {
return keyPrefix + delimiter + id;
if (reverse) {
key = id.replace(keyPrefix + delimiter, id);
}
else {
key = keyPrefix + delimiter + id;
}
}

return key;
}

this._generateId = function (type, key) {
return this._generateKey(type, key, true);
}

this.init = function () {
var config = this.config;

if (multilevel) {
if (this.isMultilevel) {
this._initMultilevel(config);
} else {
this._initLevel(config);
Expand Down Expand Up @@ -196,6 +195,9 @@ utils.mixin(Adapter.prototype, new (function () {
if (filter(data)) {
inst = query.model.create(item.value, {scenario: query.opts.scenario});
inst.id = item.value.id;
if (!inst.id) {
inst.id = self._generateId(type, item.key);
}
inst._saved = true;
res.push(inst);
}
Expand Down Expand Up @@ -376,17 +378,22 @@ utils.mixin(Adapter.prototype, new (function () {

var drop = function () {
var type
, tableName;
, tableName
, batch;
if ((type = types.shift())) {
// tableName = utils.inflection.pluralize(c);
// tableName = utils.string.snakeize(tableName);

startKey = self._generateKey(type, '');
endKey = self._generateKey(type, '\xFF');
batch = [];

db.createReadStream({start: startKey, end: endKey})
.pipe(db.createWriteStream({ type: 'del' }))
.on('data', function (item) {
batch.push({ key: item.key, type: 'del' });
})
.on('close', function () {
drop();
db.batch(batch, drop);
});
}
else {
Expand All @@ -396,39 +403,88 @@ utils.mixin(Adapter.prototype, new (function () {
drop();
};

this.connect = function (callback) {
this._connectMultilevel = function (cb) {
var self = this
, cb = callback || function () {};
this.client.on('connect', function (err) {
, config
, con;

config = this.config;

function setDb() {
if(config.sublevel) {
self.db = self._db.sublevel(config.sublevel);
}
else {
self.db = self._db;
}
self.emit('connect');
cb();
}

this.client = net.connect(config.port, config.host, function (err) {
if (err) {
self.emit('error', err);
cb(err);
return cb(err);
}

if (config.auth) {
self._db.auth(config.auth, function (err) {
if (err) {
self.emit('error', err);
return cb(err);
}
setDb();
});
}
else {
self.emit('connect');
cb();
setDb();
}
});

this.client.pipe(this._db.createRpcStream()).pipe(this.client);
};

this.connect = function (callback) {
var self = this
, cb = callback || function () {};

if (this.isMultilevel) {
this._connectMultilevel(cb);
}
else {
setTimeout(function () {
self.emit('connect');
cb();
}, 0);
}
};

this.disconnect = function (callback) {
var self = this
, cb = callback || function () {};
this.client.on('close', function (err) {
if (err) {
self.emit('error', err);
cb(err);
}
else {
if (this.isMultilevel) {
process.nextTick(function () {
self._db.close();
self.emit('disconnect');
cb();
}
});
});
}
else {
this._db.close(function (err) {
if (err) {
self.emit('error', err);
cb(err);
}
else {
self.emit('disconnect');
cb();
}
});
}
};

})());

utils.mixin(Adapter.prototype, mr);

module.exports.Adapter = Adapter;

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"leveldb",
"sqlite"
],
"version": "6.0.1",
"version": "6.0.2",
"author": "Matthew Eernisse <mde@fleegix.org> (http://fleegix.org)",
"main": "./lib/index.js",
"scripts": {
Expand All @@ -36,9 +36,10 @@
"pg": "2.5.x",
"mysql": "2.0.x",
"level": "0.17.x",
"multilevel": "6.0.x",
"sqlite3": "2.1.x"
},
"engines": {
"node": "*"
}
}
}
8 changes: 6 additions & 2 deletions test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
, level: {
db: process.env.LEVEL_DATABASE || '/tmp/foo'
}
, multilevel: {
port: process.env.LEVEL_PORT || 3000
, host: process.env.LEVEL_HOST || '127.0.0.1'
}
};

try {
Expand All @@ -37,12 +41,12 @@
else {
existsSync = fs.existsSync;
}

// Check if JSON parsing failed
if(existsSync(userOptsFile)) {
throw new Error("Could not parse user options, check if your file is valid JSON");
}
}

module.exports = config;
}());
7 changes: 6 additions & 1 deletion test/integration/adapters/level/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ tests = {
}

, 'after': function (next) {
adapter.dropTable(['Zooby', 'User'], next);
adapter.dropTable(['Zooby', 'User'], function () {
adapter.disconnect(function (err) {
if (err) { throw err; }
next();
});
});
}

, 'test create adapter': function () {
Expand Down
Loading