Permalink
Browse files

initial code refactor, no protobuf

  • Loading branch information...
1 parent f506324 commit 2c5b6c0e20febe58482bbfc0f2ff1a3f4dcb7187 dstrek committed Feb 13, 2013
Showing with 9,419 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +74 −0 errtests.js
  3. +289 −0 index.js
  4. +34 −0 package.json
  5. +4 −0 readme.md
  6. +278 −0 rethinkdb/connection.js
  7. +155 −0 rethinkdb/connection_tcp.js
  8. +92 −0 rethinkdb/cursor.js
  9. +76 −0 rethinkdb/database.js
  10. +58 −0 rethinkdb/errors.js
  11. +54 −0 rethinkdb/expressions/array.js
  12. +37 −0 rethinkdb/expressions/attr.js
  13. +37 −0 rethinkdb/expressions/boolean.js
  14. +46 −0 rethinkdb/expressions/builtin.js
  15. +34 −0 rethinkdb/expressions/error.js
  16. +1,125 −0 rethinkdb/expressions/expression.js
  17. +33 −0 rethinkdb/expressions/function.js
  18. +54 −0 rethinkdb/expressions/get.js
  19. +56 −0 rethinkdb/expressions/if.js
  20. +29 −0 rethinkdb/expressions/implicit_var.js
  21. +35 −0 rethinkdb/expressions/js.js
  22. +28 −0 rethinkdb/expressions/js_function.js
  23. +24 −0 rethinkdb/expressions/json.js
  24. +69 −0 rethinkdb/expressions/let.js
  25. +39 −0 rethinkdb/expressions/number.js
  26. +75 −0 rethinkdb/expressions/object.js
  27. +51 −0 rethinkdb/expressions/slice.js
  28. +35 −0 rethinkdb/expressions/string.js
  29. +34 −0 rethinkdb/expressions/var.js
  30. +49 −0 rethinkdb/fn.js
  31. +281 −0 rethinkdb/old/connection.js
  32. +93 −0 rethinkdb/old/cursor.js
  33. +289 −0 rethinkdb/old/database.js
  34. +137 −0 rethinkdb/old/errors.js
  35. +1,883 −0 rethinkdb/old/expression.js
  36. +199 −0 rethinkdb/old/httpconnection.js
  37. +43 −0 rethinkdb/old/net.js
  38. +171 −0 rethinkdb/old/query.js
  39. +102 −0 rethinkdb/old/querybase.js
  40. +26 −0 rethinkdb/old/rethinkdb.js
  41. +594 −0 rethinkdb/old/table.js
  42. +250 −0 rethinkdb/old/tcpconnection.js
  43. +752 −0 rethinkdb/old/test.js
  44. +68 −0 rethinkdb/queries/admin.js
  45. +71 −0 rethinkdb/queries/create_table.js
  46. +43 −0 rethinkdb/queries/delete.js
  47. +46 −0 rethinkdb/queries/drop_table.js
  48. +51 −0 rethinkdb/queries/for_each.js
  49. +71 −0 rethinkdb/queries/insert.js
  50. +56 −0 rethinkdb/queries/mutate.js
  51. +53 −0 rethinkdb/queries/point_delete.js
  52. +71 −0 rethinkdb/queries/point_mutate.js
  53. +71 −0 rethinkdb/queries/point_update.js
  54. +86 −0 rethinkdb/queries/query.js
  55. +28 −0 rethinkdb/queries/read.js
  56. +56 −0 rethinkdb/queries/update.js
  57. BIN rethinkdb/query_language.desc
  58. +6 −0 rethinkdb/query_language.js
  59. +363 −0 rethinkdb/query_language.proto
  60. +95 −0 rethinkdb/table.js
  61. +174 −0 rethinkdb/utils.js
  62. +177 −0 test-driver.js
  63. +7 −0 test.js
View
@@ -0,0 +1,2 @@
+node_modules
+.DS_Store
View
@@ -0,0 +1,74 @@
+// Copyright 2010-2012 RethinkDB, all rights reserved.
+var r = require('./rethinkdb');
+
+var one = r.expr(1);
+var two = r.expr(2);
+var zero = r.expr(0);
+
+var arr = r.expr([1,2,3]);
+
+var c = r.connect({}, function() {
+ c.setErrorHandler(function(err) {
+ console.log(err.name+': '+err.message);
+ });
+
+ r.expr('a').add(1).runp();
+ arr.map(function(row) { return row('a'); }).runp();
+ arr.reduce('a', function(acc, row) {return row.add(acc)}).runp();
+ r.expr([{a:1}]).filter({a:one.div(zero)}).runp();
+
+ r.branch(r.expr(false), r.expr(1), r.error()).runp();
+ r.table('test').orderby('bob').runp();
+ r.expr([{id:0},{id:1}]).distinct().runp();
+
+ r.expr([1,2,3]).groupedMapReduce(function(row) {
+ return row;
+ }, function(row) {
+ return row;
+ }, r.error(), function(acc, row) {
+ return r.error();
+ }).runp();
+
+ r.expr({id:r.error()}).contains('b').runp();
+ r.expr({id:1}).pick('a').runp();
+ r.expr({a:r.error()}).extend({b:1}).runp();
+ r.expr({a:1}).extend({b:r.error()}).runp();
+ r.expr([1,2]).concatMap(function(a) {return r.expr([r.error()])}).runp();
+
+ r.expr([1,2,r.error()]).arrayToStream().runp();
+
+ r.db('test').create('test').runp();
+ r.db('bob').create({
+ tableName: 'test',
+ cacheSize: '1234'
+ }).runp();
+
+ r.db('test').drop('alphabet').runp();
+ r.db('test').table('lakne').runp();
+ r.db('test').table('test').get(r.error()).runp();
+ r.db('test').table('test').insert({id:r.error()}).runp();
+ r.db('test').table('bob').del().runp();
+ r.table('test').get(r.error()).del().runp();
+
+ r.table('bo').update(function(a) {return r.error()}).runp();
+ r.table('test').get(1).update(function(row) {return r.error()}).runp();
+ r.table('bo').replace(function(a) {return r.error()}).runp();
+ r.table('test').get(1).replace(function(row) {return r.error()}).runp();
+
+ r.table('test').forEach(function(a) {
+ return r.table('bo').insert(r.error());
+ }).runp();
+
+ r.table('test').between(0, 1, 'bob').runp();
+
+ r.table('test').insert(r.expr([{id:{}}]).arrayToStream()).runp();
+
+ r.table('test').get(0).update(function(val) {return val.extend({'bob':1})}).runp();
+
+ r.table('test').get(1, 'bob').update(function(row) {return row.extend({bob:3})}).runp();
+ r.table('test').get(1, 'bob').replace(function(row) {return row.extend({bob:3})}).runp();
+ r.table('test').get(1, 'bob').del().runp();
+ r.table('test').insert({id:1}).runp();
+
+ //c.close();
+});
View
289 index.js
@@ -0,0 +1,289 @@
+var _ = require('underscore');
+
+var ql = require('./rethinkdb/query_language');
+var r_util = require('./rethinkdb/utils');
+var Database = require('./rethinkdb/database');
+var Table = require('./rethinkdb/table');
+var TcpConnection = require('./rethinkdb/connection_tcp');
+
+var ArrayExpression = require('./rethinkdb/expressions/array');
+var BooleanExpression = require('./rethinkdb/expressions/boolean');
+var ErrorExpression = require('./rethinkdb/expressions/error');
+var Expression = require('./rethinkdb/expressions/expression');
+var ImplicitVarExpression = require('./rethinkdb/expressions/implicit_var');
+var IfExpression = require('./rethinkdb/expressions/if');
+var JSExpression = require('./rethinkdb/expressions/js');
+var JSONExpression = require('./rethinkdb/expressions/json');
+var LetExpression = require('./rethinkdb/expressions/let');
+var ObjectExpression = require('./rethinkdb/expressions/object');
+var NumberExpression = require('./rethinkdb/expressions/number');
+var StringExpression = require('./rethinkdb/expressions/string');
+var VarExpression = require('./rethinkdb/expressions/var');
+
+var AdminQuery = require('./rethinkdb/query_admin');
+var Query = require('./rethinkdb/query');
+
+function rethinkdb(jsobj) {
+ return rethinkdb.expr(jsobj);
+};
+
+/**
+ * Constructs a primitive ReQL type from a javascript value.
+ * @param {*} value The value to wrap.
+ * @returns {rethinkdb.Expression}
+ * @export
+ */
+rethinkdb.expr = function(value) {
+ if (value instanceof Expression) return value;
+ else if (value === null || _.isNumber(value)) return r_util.newExpr_(NumberExpression, value);
+ else if (_.isBoolean(value)) return r_util.newExpr_(BooleanExpression, value);
+ else if (_.isString(value)) return r_util.newExpr_(StringExpression, value);
+ else if (_.isArray(value)) return r_util.newExpr_(ArrayExpression, value);
+ else if (_.isObject(value)) return r_util.newExpr_(ObjectExpression, value);
+ else return r_util.newExpr_(JSONExpression, value);
+};
+
+/**
+ * Construct a ReQL JS expression from a JavaScript code string.
+ * @param {string} jsExpr A JavaScript code string
+ * @returns {rethinkdb.Expression}
+ * @export
+ */
+rethinkdb.js = function(jsExpr) {
+ r_util.argCheck_(arguments, 1);
+ r_util.typeCheck_(jsExpr, 'string');
+ return r_util.newExpr_(JSExpression, jsExpr);
+};
+
+/**
+ * Construct a table reference
+ * @param {string} tableName A string giving a table name
+ * @param {boolean=} opt_allowOutdated Accept potentially outdated data to reduce latency
+ * @returns {rethinkdb.Expression}
+ * @export
+ */
+rethinkdb.table = function(tableName, opt_allowOutdated) {
+ r_util.argCheck_(arguments, 1);
+ r_util.typeCheck_(tableName, 'string');
+ r_util.typeCheck_(opt_allowOutdated, 'boolean');
+
+ return r_util.newExpr_(Table, tableName, null, opt_allowOutdated);
+};
+
+/**
+ * Concatenate two or more sequences.
+ * @param {...rethinkdb.Expression} var_args
+ * @export
+ */
+rethinkdb.union = function(var_args) {
+ r_util.argCheck_(arguments, 2);
+ var one = r_util.wrapIf_(arguments[0]);
+ return Expression.prototype.union.apply(one, Array.prototype.slice.call(arguments, 1));
+};
+
+/**
+ * Construct a database reference.
+ * @param {string} dbName
+ * @return {rethinkdb.Database}
+ * @export
+ */
+rethinkdb.db = function(dbName) {
+ r_util.argCheck_(arguments, 1);
+ r_util.typeCheck_(dbName, 'string');
+ return new Database(dbName);
+};
+
+/**
+ * Create a new database with the given name.
+ * @param {string} dbName
+ * @export
+ */
+rethinkdb.dbCreate = function(dbName) {
+ r_util.argCheck_(arguments, 1);
+ r_util.typeCheck_(dbName, 'string');
+ return new AdminQuery(MetaQuery.MetaQueryType.CREATE_DB, dbName);
+};
+
+/**
+ * Drop the given database.
+ * @param {string} dbName
+ * @export
+ */
+rethinkdb.dbDrop = function(dbName) {
+ r_util.argCheck_(arguments, 1);
+ r_util.typeCheck_(dbName, 'string');
+ return new AdminQuery(MetaQuery.MetaQueryType.DROP_DB, dbName);
+};
+
+/**
+ * List all databases.
+ * @export
+ */
+rethinkdb.dbList = function() {
+ return new AdminQuery(MetaQuery.MetaQueryType.LIST_DBS);
+};
+
+/**
+ * Throw a runtime error on the server
+ * @return {rethinkdb.Expression}
+ * @export
+ */
+rethinkdb.error = function() {
+ return new ErrorExpression();
+};
+
+/**
+ * Specify ascending ordering for a given attribute passed to orderBy
+ * @param {string} attr The attribute to orderBy
+ * @returns {Array}
+ * @export
+ */
+rethinkdb.asc = function(attr) {
+ return [attr, true];
+};
+
+/**
+ * Specify descending ordering for a given attribute passed to orderBy
+ * @param {string} attr The attribute to orderBy
+ * @returns {Array}
+ * @export
+ */
+rethinkdb.desc = function(attr) {
+ return [attr, false];
+};
+
+/** Predefined groupby reductions */
+
+/**
+ * A predefined reduction for groupby the counts the number of members in the group.
+ * @export
+ */
+rethinkdb.count = {
+ 'mapping': function(row) { return rethinkdb.expr(1); },
+ 'base': 0,
+ 'reduction': function(acc, val) { return acc['add'](val); }
+};
+
+/**
+ * Constructs a reduction for groupby that sums the values of the given field.
+ * @export
+ */
+rethinkdb.sum = function(attr) {
+ r_util.argCheck_(arguments, 1);
+ r_util.typeCheck_(attr, 'string');
+ return {
+ 'mapping': function(row) { return row(attr); },
+ 'base': 0,
+ 'reduction': function(acc, val) { return acc['add'](val); }
+ };
+};
+
+/**
+ * Constructs a reduction for groupby that averages across the values of the given field.
+ * @export
+ */
+rethinkdb.avg = function(attr) {
+ r_util.argCheck_(arguments, 1);
+ r_util.typeCheck_(attr, 'string');
+ return {
+ 'mapping': function(row) { return rethinkdb.expr([row(attr), 1]); },
+ 'base': [0, 0],
+ 'reduction': function(acc, val) {
+ return rethinkdb.expr([acc['nth'](0)['add'](val['nth'](0)), acc['nth'](1)['add'](val['nth'](1))]);
+ },
+ 'finalizer': function(res) { return res['nth'](0)['div'](res['nth'](1)); }
+ };
+};
+
+/**
+ * A Reference to the implicit variable
+ * @export
+ */
+rethinkdb.row = r_util.newExpr_(ImplicitVarExpression);
+
+/**
+ * Reference a variable bound by an enclosing let statement
+ * @param {string} varString The variable to reference. subfields can be
+ * referenced with dot notation.
+ * @param {string} variable.
+ * @export
+ */
+rethinkdb.letVar = function(varString) {
+ r_util.typeCheck_(varString, 'string');
+ return r_util.newExpr_(VarExpression, varString);
+};
+
+/**
+ * Evaluate test, then evaluate and return the result of trueBranch if test evaluates to true,
+ * or evaluate and return the result of falseBranch of test evaluates to false.
+ * @param {rethinkdb.Expression} test ReQL expression that evaluates to a boolean.
+ * @param {rethinkdb.Expression} trueBranch Expression to evaluate if true.
+ * @param {rethinkdb.Expression} falseBranch Expression to evaluate if false.
+ * @return {rethinkdb.Expression}
+ * @export
+ */
+rethinkdb.branch = function(test, trueBranch, falseBranch) {
+ r_util.typeCheck_(trueBranch, Query);
+ r_util.typeCheck_(falseBranch, Query);
+ test = r_util.wrapIf_(test);
+ return r_util.newExpr_(IfExpression, test, trueBranch, falseBranch);
+};
+
+/**
+ * Bind the result of an ReQL expression to a variable within an expression.
+ * @param {Object} bindings An object giving name:expression pairs.
+ * @param {rethinkdb.Query} body The ReQL query to evauluate in context.
+ * @return {rethinkdb.Expression}
+ * @export
+ */
+rethinkdb.let = function(bindings, body) {
+ r_util.typeCheck_(body, Query);
+
+ for (var key in bindings) {
+ if (bindings.hasOwnProperty(key)) {
+ r_util.typeCheck_(bindings[key], Expression);
+ }
+ }
+
+ return r_util.newExpr_(LetExpression, bindings, body);
+};
+
+/**
+ * This shortcut function invokes either the {@link rethinkdb.TcpConnection}
+ * constructor or the {@link rethinkdb.HttpConnection} constructor. Prefers
+ * TcpConnection if available.
+ * @param {?string|Object} host Either a string giving the host to connect to or
+ * an object specifying host and/or port and/or db for the default database to
+ * use on this connection. Any key not supplied will revert to the default.
+ * @param {function(...)=} onConnect
+ * @param {function(...)=} onFailure
+ * @see rethinkdb.HttpConnection
+ * @see rethinkdb.TcpConnection
+ * @export
+ */
+rethinkdb.connect = function(host, onConnect, onFailure) {
+ if (TcpConnection.isAvailable()) {
+ return new TcpConnection(host, onConnect, onFailure);
+ } else {
+ return new HttpConnection(host, onConnect, onFailure);
+ }
+}
+
+/**
+ * Reference to the last created connection.
+ * @type {?rethinkdb.Connection}
+ * @ignore
+ */
+rethinkdb.last_connection_ = null;
+
+/**
+ * Get the last created connection.
+ * @return {rethinkdb.Connection}
+ * @export
+ */
+rethinkdb.lastConnection = function() {
+ return rethinkdb.last_connection_;
+};
+
+
+module.exports = rethinkdb;
Oops, something went wrong.

0 comments on commit 2c5b6c0

Please sign in to comment.