Skip to content
Browse files

Merge pull request #89 from bogensberger/b/crate

added support for crate.io (https://crate.io)
  • Loading branch information...
2 parents 4a0ab5d + f0e8b0f commit af6a16e84569f46a631d7dac8c24cec684500c4e @JohnMcLear JohnMcLear committed Jan 28, 2016
Showing with 186 additions and 8 deletions.
  1. +2 −1 README.md
  2. +175 −0 crate_db.js
  3. +1 −0 defaultTestSettings.js
  4. +8 −7 package.json
View
3 README.md
@@ -12,6 +12,7 @@ Abstract your databases, make datababies. ueberDB turns every database into a s
* Redis
* Couch
* Cassandra
+* Crate
#Install
@@ -59,7 +60,7 @@ db.init(function (err)
Look at sqlite_db.js and mysql_db.js, your module have to provide the same functions. Call it DATABASENAME_db.js and reimplement the functions for your database. If you think it works, test it with `node benchmark.js DATABASENAME`. Benchmark.js is benchmark and test at the same time. It tries to set 100000 values. You can pipe stderr to a file and will create a csv with benchmark results.
#Limitations
-Only mysql, dirty and mongodb currently support findKeys feature. The following do not yet support the function:
+Only mysql, dirty, mongodb and crate currently support findKeys feature. The following do not yet support the function:
* couch
* leveldb
* redis (Only keys of the format *:*:*)
View
175 crate_db.js
@@ -0,0 +1,175 @@
+// initialize w/ default settings
+var crateSettings = {
+ schema : 'doc',
+ table : 'store',
+ hosts : 'http://localhost:4200'
+}
+
+var insertSQL;
+var removeSQL;
+
+exports.database = function(settings)
+{
+ this.db = crate;
+ this.settings = settings || {};
+
+ // update settings if thex where provided
+ if(this.settings.hosts != null) {
+ crateSettings.hosts = this.settings.hosts;
+ }
+
+ if(this.settings.schema != null) {
+ crateSettings.schema = this.settings.schema;
+ }
+
+ if(this.settings.table != null) {
+ crateSettings.table = this.settings.table;
+ }
+
+ crateSettings.fqn = '"' + crateSettings.schema + '"."' + crateSettings.table + '"'
+}
+
+var crate = require("node-crate");
+
+/**
+ * Initialize the crate client, then crate the table if it not exists
+ */
+exports.database.prototype.init = function(callback)
+{
+ this.db.connect(crateSettings.hosts);
+ insertSQL = 'INSERT INTO ' + crateSettings.fqn +' ("key","value") VALUES (?, ?) ON DUPLICATE KEY UPDATE value = VALUES(value)';
+ removeSQL = 'DELETE FROM ' + crateSettings.fqn + ' WHERE key=? ';
+
+ finish = function(log){
+ callback();
+ }
+
+ var stmt = "CREATE TABLE IF NOT EXISTS "+crateSettings.fqn +
+ " (key string primary key, value string) ";
+ crate.execute(stmt).success(finish).error(finish);
+}
+
+/**
+ * read from database
+ */
+exports.database.prototype.get = function (key, callback)
+{
+ finish = function(log){
+ if(!log.rows.length){
+ callback(undefined, null);
+ } else {
+ callback(undefined, log.rows[0][0]);
+ }
+ }
+ finishError = function(log){
+ callback(log, null);
+ }
+ crate.execute ("select value from "+ crateSettings.fqn+" where key = ?", [key])
+ .success (finish)
+ .error(finishError);
+}
+
+exports.database.prototype.findKeys = function (key, notKey, callback)
+{
+ var query = "SELECT key FROM "+ crateSettings.fqn +" where key LIKE ?",
+ params =[];
+ key=key.replace(/\*/g,'%');
+ params.push(key);
+ if(notKey!=null && notKey != undefined){
+ //not desired keys are notKey, e.g. %:%:%
+ notKey=notKey.replace(/\*/g,'%');
+ query+=" AND key NOT LIKE ?"
+ params.push(notKey);
+ }
+ crate.execute (query, params).
+ success(function (log) {
+ if(!log.rows.length){
+ callback(undefined, []);
+ } else {
+ res = []
+ for (var i=0; i < log.rows.length; i++) {
+ res[i] = log.rows[i][0];
+ }
+ callback(undefined, res);
+ }
+ })
+ .error(function (log) {
+ callback(log, null);
+ });
+}
+
+exports.database.prototype.set = function (key, value, callback)
+{
+ crate.execute (insertSQL, [key, value]).
+ success(refresh(function (log) {
+ callback();
+ }))
+ .error(function (log) {
+ callback(log, null);
+ });
+}
+
+exports.database.prototype.remove = function (key, callback)
+{
+ crate.execute (removeSQL, [key]).
+ success(function (log) {
+ callback();
+ })
+ .error(function (log) {
+ callback(log, null);
+ });
+}
+
+exports.database.prototype.doBulk = function (bulk, callback)
+{
+ var remove = [];
+ var insert = [];
+ for(var i in bulk) {
+ if(bulk[i].type == "set")
+ {
+ insert.push([bulk[i].key, bulk[i].value]);
+ } else if(bulk[i].type == "remove") {
+ remove.push([bulk[i].key]);
+ }
+ }
+ error = function(log){
+ console.error("error: "+log);
+ callback(log, null);
+ }
+ finishUpdate = function(log){
+ if(remove.length){
+ crate.executeBulk(removeSQL, remove)
+ .success(refresh(function (log) {
+ callback();
+ }
+ )).error(error);
+ } else {
+ callback();
+ }
+ }
+
+ if(insert.length){
+ crate.executeBulk(insertSQL, insert)
+ .success(refresh(finishUpdate))
+ .error(error);
+ } else if(remove.length){
+ crate.executeBulk(removeSQL, remove)
+ .success(refresh(function (log) {
+ callback();
+ }))
+ .error(errorRemove);
+ }
+}
+
+function refresh(callback) {
+ crate.execute("refresh table " + crateSettings.fqn).success(callback).error(
+ function (log) {
+ console.error("error: "+log);
+ }
+ );
+}
+
+exports.database.prototype.close = function(callback)
+{
+ callback();
+}
View
1 defaultTestSettings.js
@@ -6,3 +6,4 @@ exports["redis"] = {};
exports["couch"] = {port: 5984, host: 'localhost', database: "etherpadlite", maxListeners: 0};
exports["mongodb"] = {port: 27017, host: "localhost", dbname: "etherpadlite"};
exports["cassandra"] = {hosts: ["127.0.0.1:9160"], keyspace: "etherpadlite", cfName: 'etherpadlite'};
+exports["crate"] = {schema: "etherpadlite", table: "store"};
View
15 package.json
@@ -9,13 +9,14 @@
{ "name": "spcsser"}
],
"dependencies" : {
- "mysql" : "0.9.5",
- "pg" : "0.14.1",
- "dirty" : "0.9.x",
- "async" : "0.1.15",
- "channels" : "0.0.2",
- "node-redis": "0.1.x",
- "helenus" : "0.6.2"
+ "mysql" : "0.9.5",
+ "pg" : "0.14.1",
+ "dirty" : "0.9.x",
+ "async" : "0.1.15",
+ "channels" : "0.0.2",
+ "node-redis" : "0.1.x",
+ "helenus" : "0.6.2",
+ "node-crate" : "1.0.26"
},
"devDependencies": {
"log4js" : "0.4.1",

0 comments on commit af6a16e

Please sign in to comment.
Something went wrong with that request. Please try again.