Skip to content

Commit

Permalink
added script loading mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
arunoda committed Mar 1, 2013
1 parent 0ca6c64 commit af0afbb
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 34 deletions.
16 changes: 14 additions & 2 deletions README.md
Expand Up @@ -16,7 +16,8 @@ node-redis-scripto

~~~js
var Scripto = require('redis-scripto');
var scriptManager = new Scripto(redisClient, '/path/to/lua/scripts');
var scriptManager = new Scripto(redisClient);
scriptManager.loadFromDir('/path/to/lua/scripts');

var keys = ['keyOne', 'keyTwo'];
var values = [10, 20];
Expand All @@ -42,14 +43,25 @@ node-redis-scripto
});
~~~

* If you just need to load a single script, see following example

~~~js
var scriptManager = new Scripto(redisClient);
scriptManager.loadFromFile('script-one', '/path/to/the/file');
scriptManager.run('script-one', [], [], function(err, result) {

});
~~~

* If you need to load scripts just using JavaScript (without loading from the filesystem), see following example.

~~~js
var scripts = {
'script-one': 'return 1000'
};

var scriptManager = new Scripto(redisClient, scripts);
var scriptManager = new Scripto(redisClient);
scriptManager.load(scripts);
scriptManager.run('script-one', [], [], function(err, result) {

});
Expand Down
48 changes: 32 additions & 16 deletions lib/scripto.js
Expand Up @@ -2,20 +2,30 @@ var fs = require('fs');
var path = require('path');
var debug = require('debug')('scripto');

function Scripto (redisClient, scriptsDir) {
function Scripto (redisClient) {

var scripts = [];
if(typeof(scriptsDir) == 'string') {
scripts = loadScripts(scriptsDir);
} else if(scriptsDir instanceof Object) {
scripts = scriptsDir;
} else {
throw new Error('2nd argument should be either a directory path or a map of scripts');
}

var scripts = {};
var scriptShas = this._scriptShas = {};
debug('loading script initially');
loadScriptsIntoRedis(redisClient, scripts, afterShasLoaded);

this.load = function load(scriptObject) {

mergeObjects(scripts, scriptObject);
loadScriptsIntoRedis(redisClient, scriptObject, afterShasLoaded);
};

this.loadFromFile = function loadFromFile(name, filepath) {

var loadedScripts = {};
loadedScripts[name] = fs.readFileSync(filepath, 'utf8');
this.load(loadedScripts);
};

this.loadFromDir = function loadFromDir(scriptsDir) {

var loadedScripts = loadScriptsFromDir(scriptsDir);
this.load(loadedScripts);
};


this.run = function run(scriptName, keys, args, callback) {

Expand Down Expand Up @@ -70,19 +80,25 @@ function Scripto (redisClient, scriptsDir) {
function afterShasLoaded(err, shas) {

if(err) {
debug('resetting scriptShas due to redis command error: ' + err.toString());
scriptShas = {};
debug('scripts loading failed due to redis command error: ' + err.toString());
} else {
debug('loaded scriptShas');
scriptShas = shas;
mergeObjects(scriptShas, shas);
}
}

function mergeObjects (obj1, obj2) {

for(var key in obj2) {
obj1[key] = obj2[key];
}
}

}

module.exports = Scripto;

function loadScripts(scriptsDir) {
function loadScriptsFromDir(scriptsDir) {

var names = fs.readdirSync(scriptsDir);
var scripts = {};
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "redis-scripto",
"version": "0.1.1",
"version": "0.1.2",
"description": "Redis Lua Script Manager for NodeJS",
"keywords": [
"redis",
Expand Down
47 changes: 32 additions & 15 deletions test/scripto.js
Expand Up @@ -11,7 +11,9 @@ suite('Scripto', function() {
suite('eval', function() {

test('running normally', _clean(function(done) {
var s = new Scripto(redisClient, scriptDir);

var s = new Scripto(redisClient);
s.loadFromDir(scriptDir);
s.eval('read-write', ['helloKey'], [200], function(err, result) {

assert.equal(err, null);
Expand All @@ -21,7 +23,9 @@ suite('Scripto', function() {
}));

test('running non-existing script', _clean(function(done) {
var s = new Scripto(redisClient, scriptDir);

var s = new Scripto(redisClient);
s.loadFromDir(scriptDir);
s.eval('no-such-script', ['helloKey'], [200], function(err, result) {

assert.equal(err.message, 'NO_SUCH_SCRIPT');
Expand All @@ -34,7 +38,9 @@ suite('Scripto', function() {
suite('evalSha', function() {

test('failed at initial call', _clean(function(done) {
var s = new Scripto(redisClient, scriptDir);

var s = new Scripto(redisClient);
s.loadFromDir(scriptDir);
s.evalSha('read-write', ['helloKey'], [200], function(err, result) {

assert.equal(err.message, 'NO_SUCH_SCRIPT_SHA');
Expand All @@ -44,7 +50,9 @@ suite('Scripto', function() {
}));

test('success at runs after script loaded (some millis later)', _clean(function(done) {
var s = new Scripto(redisClient, scriptDir);

var s = new Scripto(redisClient);
s.loadFromDir(scriptDir);

setTimeout(function() {

Expand All @@ -63,7 +71,9 @@ suite('Scripto', function() {
suite('run', function() {

test('success at initial call', _clean(function(done) {
var s = new Scripto(redisClient, scriptDir);

var s = new Scripto(redisClient);
s.loadFromDir(scriptDir);
s.run('read-write', ['helloKey'], [200], function(err, result) {

assert.equal(err, undefined);
Expand All @@ -73,7 +83,9 @@ suite('Scripto', function() {
}));

test('success at runs after script loaded (some millis later, then uses sha)', _clean(function(done) {
var s = new Scripto(redisClient, scriptDir);

var s = new Scripto(redisClient);
s.loadFromDir(scriptDir);

setTimeout(function() {

Expand All @@ -89,10 +101,11 @@ suite('Scripto', function() {
}));
});

test('load scripts as an array', _clean(function(done) {
test('load scripts from an object', _clean(function(done) {

var scripts = { 'script-one': 'return 1000;' };
var s = new Scripto(redisClient, scripts);
var s = new Scripto(redisClient);
s.load(scripts);

s.run('script-one', [], [], function(err, result) {

Expand All @@ -103,15 +116,19 @@ suite('Scripto', function() {

}));

test('failed when called without a proper 2nd argument', function(done) {
test('load a script from file', _clean(function(done) {

try {
var s = new Scripto(redisClient);
} catch(ex) {
assert.equal(ex.message, '2nd argument should be either a directory path or a map of scripts');
var s = new Scripto(redisClient);
s.loadFromFile('read-write', path.resolve(scriptDir, 'read-write.lua'));

s.run('read-write', ['helloKey'], [200], function(err, result) {

assert.equal(err, null);
assert.equal(result, 200);
done();
}
});
});

}));

});

Expand Down

0 comments on commit af0afbb

Please sign in to comment.