1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00

make LevelDb node aware of db ready/not ready to reduce errors.

This commit is contained in:
dceejay 2015-05-21 12:37:48 +01:00
parent 17b1a9a785
commit b03db617e2
2 changed files with 25 additions and 27 deletions

View File

@ -1,5 +1,5 @@
/** /**
* Copyright 2013 IBM Corp. * Copyright 2013,2015 IBM Corp.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -21,13 +21,19 @@ module.exports = function(RED) {
function LevelNode(n) { function LevelNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.dbname = n.db; this.dbname = n.db;
this.ready = false;
var node = this; var node = this;
lvldb(this.dbname, function(err, db) { lvldb(this.dbname, function(err, db) {
if (err) { node.error(err); } if (err) { node.error(err); }
node.db = db; node.db = db;
node.db.on('ready', function() { node.ready = true; });
node.db.on('closing', function() { node.ready = false; });
}); });
this.on('close', function() { node.on('close', function() {
if (node.db) { node.db.close(); } if (node.db) {
node.ready = false;
node.db.close();
}
}); });
} }
RED.nodes.registerType("leveldbase",LevelNode); RED.nodes.registerType("leveldbase",LevelNode);
@ -38,9 +44,9 @@ module.exports = function(RED) {
this.level = n.level; this.level = n.level;
this.levelConfig = RED.nodes.getNode(this.level); this.levelConfig = RED.nodes.getNode(this.level);
if (this.levelConfig) {
var node = this; var node = this;
node.on("input", function(msg) { node.on("input", function(msg) {
if (node.levelConfig && node.levelConfig.ready) {
var key = msg.topic.toString(); var key = msg.topic.toString();
if (key && (key.length > 0)) { if (key && (key.length > 0)) {
node.levelConfig.db.get(msg.topic, function(err, value) { node.levelConfig.db.get(msg.topic, function(err, value) {
@ -53,15 +59,11 @@ module.exports = function(RED) {
node.send(msg); node.send(msg);
}); });
} }
else { else { node.error("Cannot make key string from msg.topic"); }
node.error("Cannot make key string from msg.topic");
} }
else { node.warn("Database not ready"); }
}); });
} }
else {
this.error("LevelDB database name not configured");
}
}
RED.nodes.registerType("leveldb in",LevelDBNodeIn); RED.nodes.registerType("leveldb in",LevelDBNodeIn);
@ -71,9 +73,9 @@ module.exports = function(RED) {
this.operation = n.operation; this.operation = n.operation;
this.levelConfig = RED.nodes.getNode(this.level); this.levelConfig = RED.nodes.getNode(this.level);
if (this.levelConfig) {
var node = this; var node = this;
node.on("input", function(msg) { node.on("input", function(msg) {
if (node.levelConfig && node.levelConfig.ready) {
var key = msg.topic.toString(); var key = msg.topic.toString();
if (key && (key.length > 0)) { if (key && (key.length > 0)) {
if (node.operation === "delete") { if (node.operation === "delete") {
@ -85,14 +87,10 @@ module.exports = function(RED) {
}); });
} }
} }
else { else { node.error("Cannot make key string from msg.topic"); }
node.error("Cannot make key string from msg.topic");
} }
else { node.warn("Database not ready"); }
}); });
} }
else {
this.error("LevelDB database name not configured");
}
}
RED.nodes.registerType("leveldb out",LevelDBNodeOut); RED.nodes.registerType("leveldb out",LevelDBNodeOut);
} }

View File

@ -1,9 +1,9 @@
{ {
"name" : "node-red-node-leveldb", "name" : "node-red-node-leveldb",
"version" : "0.0.2", "version" : "0.0.3",
"description" : "A Node-RED node to read and write to a LevelDB database", "description" : "A Node-RED node to read and write to a LevelDB database",
"dependencies" : { "dependencies" : {
"level" : "0.18.0" "level" : "1.0.0"
}, },
"repository" : { "repository" : {
"type":"git", "type":"git",