Permalink
Browse files

switched to using Math.uuid.js for uuid generation

  • Loading branch information...
1 parent 66f14e7 commit 323d196af3941d45bbc54059509617c94bf06dc3 @mrjjwright committed Mar 18, 2010
Showing with 109 additions and 129 deletions.
  1. +90 −0 Math.uuid.js
  2. +0 −1 README.md
  3. +4 −6 nosqlite.coffee
  4. +10 −4 nosqlite.js
  5. +2 −11 test/test_nosqlite.coffee
  6. +3 −15 test/test_nosqlite.js
  7. +0 −92 uuid.js
View
@@ -0,0 +1,90 @@
+/*!
+Math.uuid.js (v1.4)
+http://www.broofa.com
+mailto:robert@broofa.com
+
+Copyright (c) 2010 Robert Kieffer
+Dual licensed under the MIT and GPL licenses.
+*/
+
+/*
+ * Generate a random uuid.
+ *
+ * USAGE: Math.uuid(length, radix)
+ * length - the desired number of characters
+ * radix - the number of allowable values for each character.
+ *
+ * EXAMPLES:
+ * // No arguments - returns RFC4122, version 4 ID
+ * >>> Math.uuid()
+ * "92329D39-6F5C-4520-ABFC-AAB64544E172"
+ *
+ * // One argument - returns ID of the specified length
+ * >>> Math.uuid(15) // 15 character ID (default base=62)
+ * "VcydxgltxrVZSTV"
+ *
+ * // Two arguments - returns ID of the specified length, and radix. (Radix must be <= 62)
+ * >>> Math.uuid(8, 2) // 8 character ID (base=2)
+ * "01001010"
+ * >>> Math.uuid(8, 10) // 8 character ID (base=10)
+ * "47473046"
+ * >>> Math.uuid(8, 16) // 8 character ID (base=16)
+ * "098F4D35"
+ */
+(function() {
+ // Private array of chars to use
+ var CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
+
+ Math.uuid = function (len, radix) {
+ var chars = CHARS, uuid = [];
+ radix = radix || chars.length;
+
+ if (len) {
+ // Compact form
+ for (var i = 0; i < len; i++) uuid[i] = chars[0 | Math.random()*radix];
+ } else {
+ // rfc4122, version 4 form
+ var r;
+
+ // rfc4122 requires these characters
+ uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
+ uuid[14] = '4';
+
+ // Fill in random data. At i==19 set the high bits of clock sequence as
+ // per rfc4122, sec. 4.1.5
+ for (var i = 0; i < 36; i++) {
+ if (!uuid[i]) {
+ r = 0 | Math.random()*16;
+ uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
+ }
+ }
+ }
+
+ return uuid.join('');
+ };
+
+ // A more performant, but slightly bulkier, RFC4122v4 solution. We boost performance
+ // by minimizing calls to random()
+ Math.uuidFast = function() {
+ var chars = CHARS, uuid = new Array(36), rnd=0, r;
+ for (var i = 0; i < 36; i++) {
+ if (i==8 || i==13 || i==18 || i==23) {
+ uuid[i] = '-';
+ } else {
+ if (rnd <= 0x02) rnd = (0x2000000 + Math.random()*0x1000000)|0;
+ r = rnd & 0xf;
+ rnd = rnd >> 4;
+ uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
+ }
+ }
+ return uuid.join('');
+ };
+
+ // A more compact, but less performant, RFC4122v4 solution:
+ Math.uuidCompact = function() {
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
+ var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
+ return v.toString(16);
+ }).toUpperCase();
+ };
+})();
View
@@ -105,7 +105,6 @@ Global Query Params
The API only reads query params and the HTTP post body so you can map it to any url you want to. But you should pass these URL params:
* __table__ - The table name in SQLite. The NoSQLite API is oriented around one table per object, so you will always be dealing with one table.
-
* __method__ - The method on NoSQLite to call. One of `find`, `save`, or `find_or_save_all``.
View
@@ -1,6 +1,6 @@
require "underscore"
sql: require "./sql"
-process.mixin require "./uuid"
+uuid: require "./uuid"
# NoSQLite - SQLite for Javascript
# ---------------------------------
@@ -146,9 +146,9 @@ class NoSQLite
#augment object with guid unless options say not to
if @options_no_guid is false
if not _.isArray(obj)
- obj.guid: uuid()
+ obj.guid: Math.uuidFast()
else for o in obj
- o.guid: uuid()
+ o.guid: Math.uuidFast()
inserts: []
inserts: sql.insert(table, table_obj, @options.core_data_mode) for table_obj in obj if _.isArray(obj)
@@ -305,6 +305,4 @@ String.prototype.trim: ->
# connect to NoSQLite this way.
exports.connect: (db, options) ->
return new NoSQLite(db, options)
-
-
-
+
View
@@ -1,8 +1,8 @@
(function(){
- var NO_SUCH_COLUMN, NO_SUCH_TABLE, NoSQLite, UNRECOGNIZED_ERROR, sql;
+ var NO_SUCH_COLUMN, NO_SUCH_TABLE, NoSQLite, S4, UNRECOGNIZED_ERROR, guid, sql, uuid;
require("underscore");
sql = require("./sql");
- process.mixin(require("./uuid"));
+ uuid = require("./uuid");
// NoSQLite - SQLite for Javascript
// ---------------------------------
//
@@ -146,12 +146,12 @@
//augment object with guid unless options say not to
if (this.options_no_guid === false) {
if (!_.isArray(obj)) {
- obj.guid = uuid();
+ obj.guid = guid();
} else {
_a = obj;
for (_b = 0, _c = _a.length; _b < _c; _b++) {
o = _a[_b];
- o.guid = uuid();
+ o.guid = guid();
}
}
}
@@ -354,4 +354,10 @@
exports.connect = function connect(db, options) {
return new NoSQLite(db, options);
};
+ S4 = function S4() {
+ return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
+ };
+ guid = function guid() {
+ return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
+ };
})();
View
@@ -181,12 +181,7 @@ test_save_bulk: ->
logs: []
for i in [1..250000]
- l: _.clone(log)
- all_keys: []
- for key of l
- all_keys.push(key)
- l.hash: hashlib.md5(all_keys.join(","))
- logs.push(l)
+ logs.push(_.clone(log))
db.save("log", logs, (err, res) ->
ok(res, "success", "should save 25,000 log messages quickly")
@@ -297,8 +292,4 @@ test_save_web: ->
)
-test_save()
-test_save_multiple()
-test_find()
-test_find_or_save()
-test_save_web()
+test_save_bulk()
View
@@ -1,6 +1,5 @@
(function(){
var nosqlite, remove_file, sqlite, test_find, test_find_or_save, test_save, test_save_bulk, test_save_cd, test_save_multiple, test_save_web;
- var __hasProp = Object.prototype.hasOwnProperty;
nosqlite = require("./nosqlite");
sqlite = require("./sqlite");
remove_file = function remove_file(file) {
@@ -170,7 +169,7 @@
});
};
test_save_bulk = function test_save_bulk() {
- var _a, _b, _c, _d, _e, all_keys, db, db_file, i, key, l, log, logs, options;
+ var _a, _b, _c, _d, db, db_file, i, log, logs, options;
db_file = "./test/test_save_bulk.db";
remove_file(db_file);
options = {};
@@ -198,14 +197,7 @@
logs = [];
_c = 1; _d = 250000;
for (_b = 0, i = _c; (_c <= _d ? i <= _d : i >= _d); (_c <= _d ? i += 1 : i -= 1), _b++) {
- l = _.clone(log);
- all_keys = [];
- _e = l;
- for (key in _e) { if (__hasProp.call(_e, key)) {
- all_keys.push(key);
- }}
- l.hash = hashlib.md5(all_keys.join(","));
- logs.push(l);
+ logs.push(_.clone(log));
}
return db.save("log", logs, function(err, res) {
ok(res, "success", "should save 25,000 log messages quickly");
@@ -329,9 +321,5 @@
});
});
};
- test_save();
- test_save_multiple();
- test_find();
- test_find_or_save();
- test_save_web();
+ test_save_bulk();
})();
View
92 uuid.js
@@ -1,92 +0,0 @@
-
-// ported by Tom Robison
-
-/*
-Based on Math.uuid.js 1.4 by Robert Kieffer
-
-----
-Copyright (c) 2008, Robert Kieffer
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions are met:
-
- * Redistributions of source code must retain the above copyright notice,
- this list of conditions and the following disclaimer.
- * Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in the
- documentation and/or other materials provided with the distribution.
- * Neither the name of Robert Kieffer nor the names of its contributors
- may be used to endorse or promote products derived from this software
- without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
-AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
-LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
-CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
-CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
-ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-POSSIBILITY OF SUCH DAMAGE.
-
-*/
-
-
-
-/*
- * Generate a random uuid.
- *
- * USAGE: uuid.uuid(length, radix)
- * length - the desired number of characters
- * radix - the number of allowable values for each character.
- *
- * EXAMPLES:
- * // No arguments - returns RFC4122, version 4 ID
- * >>> Math.uuid()
- * "92329D39-6F5C-4520-ABFC-AAB64544E172"
- *
- * // One argument - returns ID of the specified length
- * >>> Math.uuid(15) // 15 character ID (default base=62)
- * "VcydxgltxrVZSTV"
- *
- * // Two arguments - returns ID of the specified length, and radix. (Radix must be <= 62)
- * >>> Math.uuid(8, 2) // 8 character ID (base=2)
- * "01001010"
- * >>> Math.uuid(8, 10) // 8 character ID (base=10)
- * "47473046"
- * >>> Math.uuid(8, 16) // 8 character ID (base=16)
- * "098F4D35"
- */
-
-
-var CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
-exports.uuid = function (len, radix) {
- var chars = CHARS, uuid = [], rnd = Math.random;
- radix = radix || chars.length;
-
- if (len) {
- // Compact form
- for (var i = 0; i < len; i++) uuid[i] = chars[0 | rnd()*radix];
- } else {
- // rfc4122, version 4 form
- var r;
-
- // rfc4122 requires these characters
- uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
- uuid[14] = '4';
-
- // Fill in random data. At i==19 set the high bits of clock sequence as
- // per rfc4122, sec. 4.1.5
- for (var i = 0; i < 36; i++) {
- if (!uuid[i]) {
- r = 0 | rnd()*16;
- uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r & 0xf];
- }
- }
- }
-
- return uuid.join('');
-};

0 comments on commit 323d196

Please sign in to comment.