Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

cherry node_Redisql checkin

  • Loading branch information...
commit b9e82134657bff86198ac3e219157fdd1be1d7b1 0 parents
@JakSprats JakSprats authored
11 README
@@ -0,0 +1,11 @@
+
+This is the Node.js (http://nodejs.org/) client for Redisql (http://code.google.com/p/redisql/)
+
+To install you must first install
+1.) node
+2.) npm (http://github.com/isaacs/npm)
+3.) Redisql
+
+Then type "npm install Redisql"
+
+To test, type "node examples/redisql_examples.js"
121 examples/pop_denorm.js
@@ -0,0 +1,121 @@
+var redisql = require("./redisql");
+var client = redisql.createClient();
+var init = true;
+var verbose = false;
+
+redisql.debug_mode = true;
+
+function print_response(obj) {
+ var lines = [];
+ Object.keys(obj).forEach(function (key) {
+ lines.push(" " + key + ": " + obj[key].toString());
+ });
+ console.log("Response hash: \n" + lines.join("\n"));
+}
+
+function run_test() {
+ if (init) {
+ console.log("Initializing");
+ client.flushdb();
+ if (verbose) {
+ console.log("First populate user:id:[name,age,status]");
+ }
+ client.set("user:1:name", "bill", redisql.print);
+ client.set("user:1:age", "33", redisql.print);
+ client.set("user:1:status", "member", redisql.print);
+
+ if (verbose) {
+ console.log("Then populate user:id:address[street,city,zipcode]");
+ }
+ client.set("user:1:address:street", "12345 main st", redisql.print);
+ client.set("user:1:address:city", "capitol city", redisql.print);
+ client.set("user:1:address:zipcode", "55566", redisql.print);
+
+ if (verbose) {
+ console.log("Then populate user:id:payment[type,account]");
+ }
+ client.set("user:1:payment:type", "credit card", redisql.print);
+ client.set("user:1:payment:account", "1234567890", redisql.print);
+
+ client.set("user:2:name", "jane", redisql.print);
+ client.set("user:2:age", "22", redisql.print);
+ client.set("user:2:status", "premium", redisql.print);
+ client.set("user:2:address:street", "345 side st", redisql.print);
+ client.set("user:2:address:city", "capitol city", redisql.print);
+ client.set("user:2:address:zipcode", "55566", redisql.print);
+ client.set("user:2:payment:type", "checking", redisql.print);
+ client.set("user:2:payment:account", "44441111", redisql.print);
+
+ client.set("user:3:name", "ken", redisql.print);
+ client.set("user:3:age", "44", redisql.print);
+ client.set("user:3:status", "guest", redisql.print);
+ client.set("user:3:address:street", "876 big st", redisql.print);
+ client.set("user:3:address:city", "houston", redisql.print);
+ client.set("user:3:address:zipcode", "87654", redisql.print);
+ client.set("user:3:payment:type", "cash", redisql.print);
+
+ if (verbose) {
+ console.log("Keys are now populated");
+ console.log("");
+ console.log("Finally search through all redis keys using ");
+ console.log(" the primary wildcard:\"user\" ");
+ console.log(" and then search through those results using:");
+ console.log(" 1.) the secondary wildcard: \"*:address\" ");
+ console.log(" 2.) the secondary wildcard: \"*:payment\" ");
+ console.log(" 3.) non matching stil match the primary wildcard");
+ console.log("");
+ console.log("The 3 results will be normalised into the tables:");
+ console.log(" 1.) user_address");
+ console.log(" 2.) user_payment");
+ console.log(" 3.) user");
+ }
+ }
+
+ client.norm("user", "address,payment", function (err, res) {
+ if (err) { throw err; }
+ console.log("Response: " + res);
+ });
+
+ client.select("user.pk,user.name,user.status,user_address.city,user_address.street,user_address.pk,user_address.zipcode", "user,user_address", "user.pk = user_address.pk AND user.pk BETWEEN 1 AND 5", redisql.print);
+
+ if (verbose) {
+ console.log("\n\n");
+ console.log("If pure lookup speed of a SINGLE column is the dominant use case");
+ console.log("We can now denorm the redisql tables into redis hash-tables");
+ console.log("which are faster for this use-case");
+ console.log("");
+ console.log("denorm user \user:* ");
+ }
+ client.denorm("user", 'user:*', redisql.print);
+
+ console.log("HGETALL user:1 ");
+ client.hgetall("user:1", function (err, res) {
+ if (err) { throw err; }
+ console.log("Printing response for user:1");
+ print_response(res);
+ });
+
+ console.log("denorm user_payment \user:*:payment ");
+ client.denorm("user_payment", 'user:*:payment', redisql.print);
+ console.log("HGETALL user:2:payment ");
+ client.hgetall("user:2:payment", function (err, res) {
+ if (err) { throw err; }
+ print_response(res);
+ });
+
+ console.log("denorm user \user:*:address ");
+ client.denorm("user_address", 'user:*:address', redisql.print);
+ console.log("HGETALL user:3:address ");
+ client.hgetall("user:3:address", function (err, res) {
+ if (err) { throw err; }
+ print_response(res);
+ client.quit();
+ });
+}
+
+console.log("Connecting to RediSQL server...");
+client.on("connect", run_test);
+client.on("error", function (e) {
+ console.warn("Error connecting to RediSQL server: " + e);
+ process.exit(1);
+});
103 examples/redisql_examples.js
@@ -0,0 +1,103 @@
+var redisql = require("./redisql");
+var client = redisql.createClient();
+
+function last_command(err, res) {
+ console.log("Last result: " + res.toString());
+ client.quit();
+}
+
+
+function init_worker(redisql, client) {
+ client.create_table("worker", "id int,division int,health int,salary TEXT, name TEXT", redisql.print);
+ client.create_index("worker:division:index", "worker", "division", redisql.print);
+ client.create_index("worker:health:index", "worker", "health", redisql.print);
+}
+
+function insert_worker(redisql, client) {
+ client.insert("worker", "(1,11,2,60000.66,jim)", redisql.print);
+ client.insert("worker", "(2,22,1,30000.33,jack)", redisql.print);
+ client.insert("worker", "(3,33,4,90000.99,bob)", redisql.print);
+ client.insert("worker", "(4,44,3,70000.77,bill)", redisql.print);
+ client.insert("worker", "(6,66,1,12000.99,jan)", redisql.print);
+ client.insert("worker", "(7,66,1,11000.99,beth)", redisql.print);
+ client.insert("worker", "(8,11,2,68888.99,mac)", redisql.print);
+ client.insert("worker", "(9,22,1,31111.99,ken)", redisql.print);
+ client.insert_return_size("worker", "(10,33,4,111111.99,seth)", redisql.print);
+}
+
+function scanselect_worker(redisql, client) {
+ client.scanselect("*", "worker", redisql.print);
+ client.scanselect("*", "worker", "name=bill", redisql.print);
+}
+
+function update_worker(redisql, client) {
+ client.select("*", "worker", "id=1", redisql.print);
+ client.update("worker", "name=JIM", "id = 1", redisql.print);
+ client.select("*", "worker", "id = 1", redisql.print);
+}
+
+function delete_worker(redisql, client) {
+ client.select("*", "worker", "id = 2", redisql.print);
+ client.delete("worker", "id = 2", redisql.print);
+ client.select("*", "worker", "id = 2", redisql.print);
+}
+
+function desc_worker(redisql, client) {
+ client.desc("worker", redisql.print);
+}
+
+function dump_worker(redisql, client) {
+ client.dump("worker", redisql.print);
+ client.dump_to_mysql("worker", redisql.print);
+}
+
+function drop_index_worker(redisql, client) {
+ client.drop_index("worker:health:index", redisql.print);
+ client.drop_index("worker:division:index", redisql.print);
+}
+
+function drop_table_worker(redisql, client) {
+ client.drop_table("worker", redisql.print);
+}
+
+function istore_worker_name_list(redisql, client) {
+ client.select_store("name", "worker", "division BETWEEN 11 AND 33",
+ "RPUSH", "l_worker_name", redisql.print);
+ client.lrange("l_worker_name", 0, -1, redisql.print);
+}
+
+function create_table_from_redis_object_example(redisql, client) {
+ client.zadd("zset_example", 3.3, "bob", redisql.print);
+ client.zadd("zset_example", 1.1, "ted", redisql.print);
+ client.zadd("zset_example", 2.2, "mac", redisql.print);
+ client.create_table_from_redis_object("zset_example_table",
+ "zset_example", redisql.print);
+ client.dump("zset_example_table", redisql.print);
+}
+
+function create_table_as_example(redisql, client) {
+ client.zadd("zset_example", 3.3, "bob", redisql.print);
+ client.zadd("zset_example", 1.1, "ted", redisql.print);
+ client.zadd("zset_example", 4.4, "ken", redisql.print);
+ client.zadd("zset_example", 2.2, "mac", redisql.print);
+ client.create_table_as("zset_beginning",
+ "ZREVRANGE", "zset_example", "0 1", redisql.print);
+ client.dump("zset_beginning", redisql.print);
+}
+
+client.flushdb(redisql.print);
+init_worker(redisql, client);
+insert_worker(redisql, client);
+scanselect_worker(redisql, client);
+update_worker(redisql, client);
+delete_worker(redisql, client);
+desc_worker(redisql, client);
+dump_worker(redisql, client);
+istore_worker_name_list(redisql, client);
+create_table_from_redis_object_example(redisql, client);
+create_table_as_example(redisql, client);
+
+//drop_index_worker(redisql, client);
+//drop_table_worker(redisql, client);
+
+client.ping(last_command); // final command to exit cleanly
197 redisql.js
@@ -0,0 +1,197 @@
+var redis = require("redis");
+
+//redis.debug_mode = true;; // comment out
+exports.debug_mode = redis.debug_mode;
+var dbg = redis.debug_mode;
+var proto = redis.RedisClient.prototype;
+
+function to_array(args) { // helper borrowed from node_redis
+ var i, len = args.length, arr = new Array(len);
+ for (i = 0; i < len; i += 1) {
+ arr[i] = args[i];
+ }
+ return arr;
+}
+
+function check_argc(command, args, argc) {
+ if (argc != args.length - 1) { // dont count the callback
+ throw new Error("cmd: " + command + " argc: " + argc + " not: " + len);
+ }
+}
+
+function rsql_send_command(args, command, obj, sargs, dbg) {
+ sargs.unshift(command); // put command at the beginning
+ if (typeof args[args.length - 1] === "function") {
+ sargs.push(args[args.length - 1]);
+ }
+ if (dbg) console.log("Send: " + command + " args: " + JSON.stringify(sargs));
+ obj.send_command.apply(obj, sargs);
+}
+
+// new commands that we'll be adding that require no modifications
+var commands = ["CHANGEDB", "DUMP", "DESC", "NORM", "DENORM"];
+
+// merge into RedisClient prototype (both upper & lower case)
+commands.forEach(function (command) {
+ proto[command] = function () {
+ rsql_send_command("", command, this, to_array(arguments), dbg);
+ };
+ proto[command.toLowerCase()] = proto[command];
+});
+
+proto["CREATE_TABLE"] = function () {
+ var command = "CREATE";
+ var args = to_array(arguments);
+ check_argc(command, args, 2);
+ var mod_args = "TABLE " + args[0] + " (" + args[1] + ")";
+ rsql_send_command(args, command, this, mod_args.split(' '), dbg);
+};
+proto["create_table"] = proto["CREATE_TABLE"];
+
+proto["CREATE_TABLE_FROM_REDIS_OBJECT"] = function () {
+ var command = "CREATE";
+ var args = to_array(arguments);
+ check_argc("CREATE_TABLE_FROM_REDIS_OBJECT", args, 2);
+ var mod_args = "TABLE " + args[0] + " AS DUMP " + args[1];
+ rsql_send_command(args, command, this, mod_args.split(' '), dbg);
+};
+proto["create_table_from_redis_object"] = proto["CREATE_TABLE_FROM_REDIS_OBJECT"];
+
+proto["CREATE_TABLE_AS"] = function () {
+ var command = "CREATE";
+ var args = to_array(arguments);
+ check_argc("CREATE_TABLE_AS", args, 4);
+ var mod_args = "TABLE " + args[0] + " AS " +
+ args[1] + " " + args[2] + " " + args[3];
+ rsql_send_command(args, command, this, mod_args.split(' '), dbg);
+};
+proto["create_table_as"] = proto["CREATE_TABLE_AS"];
+
+proto["CREATE_INDEX"] = function () {
+ var command = "CREATE";
+ var args = to_array(arguments);
+ check_argc(command, args, 3);
+ var mod_args = "INDEX " + args[0] + " ON " + args[1] + " (" + args[2] + ")";
+ rsql_send_command(args, command, this, mod_args.split(' '), dbg);
+};
+proto["create_index"] = proto["CREATE_INDEX"];
+
+proto["DROP_TABLE"] = function () {
+ var command = "DROP";
+ var args = to_array(arguments);
+ check_argc(command, args, 1);
+ var mod_args = "TABLE " + args[0];
+ rsql_send_command(args, command, this, mod_args.split(' '), dbg);
+};
+proto["drop_table"] = proto["DROP_TABLE"];
+
+proto["DROP_INDEX"] = function () {
+ var command = "DROP";
+ var args = to_array(arguments);
+ check_argc(command, args, 1);
+ var mod_args = "INDEX " + args[0];
+ rsql_send_command(args, command, this, mod_args.split(' '), dbg);
+};
+proto["drop_index"] = proto["DROP_INDEX"];
+
+proto["SELECT"] = function () {
+ var command = "SELECT";
+ var args = to_array(arguments);
+ var mod_args;
+ if (args.length != 1) { // rewrite Redisql SELECT * FROM tbl WHERE id = 4
+ check_argc(command, args, 3);
+ mod_args = args[0] + " FROM " + args[1] + " WHERE " + args[2];
+ } else { // redis SELECT DB
+ mod_args = arguments;
+ }
+ rsql_send_command(args, command, this, mod_args.split(' '), dbg);
+};
+proto["select"] = proto["SELECT"];
+
+proto["SELECT_STORE"] = function () {
+ var command = "SELECT";
+ var args = to_array(arguments);
+ check_argc("SELECT_STORE", args, 5);
+ var mod_args = args[0] + " FROM " + args[1] + " WHERE " + args[2] +
+ " STORE " + args[3] + " " + args[4];
+ rsql_send_command(args, command, this, mod_args.split(' '), dbg);
+};
+proto["select_store"] = proto["SELECT_STORE"];
+
+proto["SCANSELECT"] = function () {
+ var command = "SCANSELECT";
+ var args = to_array(arguments);
+ var mod_args = args[0] + " FROM " + args[1];;
+ if (args.length > 3) {
+ check_argc(command, args, 3);
+ mod_args += " WHERE " + args[2];
+ } else {
+ check_argc(command, args, 2);
+ }
+ rsql_send_command(args, command, this, mod_args.split(' '), dbg);
+};
+proto["scanselect"] = proto["SCANSELECT"];
+
+proto["INSERT"] = function () {
+ var command = "INSERT";
+ var args = to_array(arguments);
+ check_argc(command, args, 2);
+ var mod_args = "INTO " + args[0] + " VALUES";
+ var sargs = mod_args.split(' ');
+ sargs.push(args[1]); // put val_list at end as single argument
+ rsql_send_command(args, command, this, sargs, dbg);
+};
+proto["insert"] = proto["INSERT"];
+
+proto["INSERT_RETURN_SIZE"] = function () {
+ var command = "INSERT";
+ var args = to_array(arguments);
+ check_argc("INSERT_RETURN_SIZE", args, 2);
+ var mod_args = "INTO " + args[0] + " VALUES";
+ var sargs = mod_args.split(' ');
+ sargs.push(args[1]); // put val_list at end as single argument
+ sargs.push("RETURN");
+ sargs.push("SIZE");
+ rsql_send_command(args, command, this, sargs, dbg);
+};
+proto["insert_return_size"] = proto["INSERT_RETURN_SIZE"];
+
+proto["DELETE"] = function () {
+ var command = "DELETE";
+ var args = to_array(arguments);
+ check_argc(command, args, 2);
+ var mod_args = "FROM " + args[0] + " WHERE " + args[1];
+ rsql_send_command(args, command, this, mod_args.split(' '), dbg);
+};
+proto["delete"] = proto["DELETE"];
+
+proto["UPDATE"] = function () {
+ var command = "UPDATE";
+ var args = to_array(arguments);
+ check_argc(command, args, 3);
+ var mod_args = args[0] + " SET"
+ var sargs = mod_args.split(' ');;
+ sargs.push(args[1]); // push val_list at end as single argument
+ sargs.push("WHERE");
+ var wargs = args[2].split(' ');
+ for (var i = 0; i < wargs.length; i++) {
+ sargs.push(wargs[i]);
+ };
+ rsql_send_command(args, command, this, sargs, dbg);
+};
+proto["update"] = proto["UPDATE"];
+
+proto["DUMP_TO_MYSQL"] = function () {
+ var command = "DUMP";
+ var args = to_array(arguments);
+ check_argc("DUMP_TO_MYSQL", args, 1);
+ var mod_args = args[0] + " TO MYSQL";
+ rsql_send_command(args, command, this, mod_args.split(' '), dbg);
+};
+proto["dump_to_mysql"] = proto["DUMP_TO_MYSQL"];
+
+exports.createClient = function (port_arg, host_arg) {
+ return redis.createClient(port_arg, host_arg);
+};
+
+exports.print = redis.print;
Please sign in to comment.
Something went wrong with that request. Please try again.