Permalink
Browse files

Refactor more.

- Split out hash stuff from class.js
- Fix the plugin to work with sealed prototypes
  • Loading branch information...
1 parent f220858 commit e3f08ebb0b4a4dedd95697caa183ee81bc04791b @creationix committed Aug 19, 2010
Showing with 73 additions and 64 deletions.
  1. +3 −23 lib/class.js
  2. +31 −0 lib/hash.js
  3. +4 −8 lib/nstore.js
  4. +34 −32 lib/nstore/query.js
  5. +1 −1 test/findTest.js
View
@@ -1,38 +1,18 @@
// This is my proto library but without changing Object.prototype
// Then only sub-objects of Class have the special properties.
var Class = module.exports = Object.create(Object.prototype, {
- // Implements a forEach much like the one for Array.prototype.forEach, but for
- // any object.
- forEach: {value: function forEach(callback, thisObject) {
- var keys = Object.keys(this);
- var length = keys.length;
- for (var i = 0; i < length; i++) {
- var key = keys[i];
- callback.call(thisObject, this[key], key, this);
- }
- }},
- // Implements a map much like the one for Array.prototype.map, but for any
- // object. Returns an array, not a generic object.
- map: {value: function map(callback, thisObject) {
- var accum = [];
- var keys = Object.keys(this);
- var length = keys.length;
- for (var i = 0; i < length; i++) {
- var key = keys[i];
- accum[i] = callback.call(thisObject, this[key], key, this);
- }
- return accum;
- }},
// Implement extend for easy prototypal inheritance
extend: {value: function extend(obj) {
obj.__proto__ = this;
+ // Lock the object to be used only as a prototype
+ Object.freeze(obj);
creationix
creationix Aug 19, 2010 Owner

So that people can't accidentally call initialize/constructor on the prototype directly. Also it forces me to write cleaner code.

return obj;
}},
// Implement new for easy self-initializing objects
new: {value: function () {
var obj = Object.create(this);
if (obj.initialize) obj.initialize.apply(obj, arguments);
- // Lock the object down
+ // Lock the object down so the fields are static
Object.seal(obj);
creationix
creationix Aug 19, 2010 Owner

It forces me to not tack stuff on my objects thus allowing V8 to properly optimize.

return obj;
}}
View
@@ -0,0 +1,31 @@
+// A Hash is an interable object
+var Hash = module.exports = Object.create(Object.prototype, {
+ // Make for easy converting objects to hashes.
+ new: {value: function initialize(value) {
+ value = value || {};
+ value.__proto__ = Hash;
+ return value;
+ }},
+ // Implements a forEach much like the one for Array.prototype.forEach.
+ forEach: {value: function forEach(callback, thisObject) {
+ var keys = Object.keys(this),
+ length = keys.length;
+ for (var i = 0; i < length; i++) {
+ var key = keys[i];
+ callback.call(thisObject, this[key], key, this);
+ }
+ }},
+ // Implements a map much like the one for Array.prototype.map.
+ // Returns a normal Array instance.
+ map: {value: function map(callback, thisObject) {
+ var keys = Object.keys(this),
+ length = keys.length,
+ accum = new Array(length);
+ for (var i = 0; i < length; i++) {
+ var key = keys[i];
+ accum[i] = callback.call(thisObject, this[key], key, this);
+ }
+ return accum;
+ }}
+});
+Object.freeze(Hash);
View
@@ -1,8 +1,8 @@
-var EventEmitter = require('events').EventEmitter,
- Path = require('path'),
+var Path = require('path'),
fs = require('fs'),
Step = require('step'),
Class = require('./class'),
+ Hash = require('./hash'),
Queue = require('./queue'),
File = require('./file');
@@ -15,10 +15,9 @@ var nStore = module.exports = Class.extend({
// Set up out local properties on the object
// and load the datafile.
initialize: function initialize(filename, callback) {
- if (this === nStore) throw new Error("Can't call initialize directly");
this.filename = filename;
this.fd = null;
- this.index = {};
+ this.index = Hash.new();
this.writeQueue = Queue.new();
this.stale = 0;
this.dbLength = 0;
@@ -42,7 +41,7 @@ var nStore = module.exports = Class.extend({
loadDatabase: function (callback) {
var buffer = new Buffer(CHUNK_LENGTH);
- var index = {};
+ var index = Hash.new();
var scanned = false;
var self = this;
var stale = 0;
@@ -180,9 +179,6 @@ var nStore = module.exports = Class.extend({
},
- // Saves a document with optional key. The effect if immediate to the
- // running program, but not persistent till after the callback.
- // Pass null as the key to get a generated key.
save: function save(key, doc, callback) {
if (!key) {
key = this.getKey();
View
@@ -1,39 +1,41 @@
// This plugin adds simple queries to nStore
-var nStore = require('../nstore');
+module.exports = {
-// Get all the records as either a single object or a document/key stream
-nStore.all = function all(callback) {
- return this.find(undefined, callback);
-};
+ // Get all the records as either a single object or a document/key stream
+ all: function all(callback) {
+ return this.find(undefined, callback);
+ },
-// Find records using a query
-nStore.find = function find(query, callback) {
- var filter = compileQuery(query);
- var stream;
- var results = {};
- if (!callback) {
- stream = new EventEmitter();
+ // Find records using a query
+ find: function find(query, callback) {
+ var filter = compileQuery(query);
+ var stream;
+ var results = {};
+ if (!callback) {
+ stream = new EventEmitter();
+ }
+ var keys = Object.keys(this.index);
+ var counter = keys.length;
+ keys.forEach(function (key) {
+ this.get(key, function (err, doc, key) {
+ if (err) {
+ if (stream) stream.emit('error', err);
+ if (callback) callback(err);
+ return;
+ }
+ if (filter(doc, key)) {
+ if (stream) stream.emit('document', doc, key);
+ if (callback) results[key] = doc;
+ };
+ counter--;
+ if (counter === 0) {
+ if (stream) stream.emit('end');
+ if (callback) callback(null, results);
+ }
+ });
+ }, this);
}
- var keys = Object.keys(this.index);
- var counter = keys.length;
- keys.forEach(function (key) {
- this.get(key, function (err, doc, key) {
- if (err) {
- if (stream) stream.emit('error', err);
- if (callback) callback(err);
- return;
- }
- if (filter(doc, key)) {
- if (stream) stream.emit('document', doc, key);
- if (callback) results[key] = doc;
- };
- counter--;
- if (counter === 0) {
- if (stream) stream.emit('end');
- if (callback) callback(null, results);
- }
- });
- }, this);
+
};
function compileQuery(query) {
View
@@ -1,7 +1,7 @@
require('./helper');
// Enable the query plugin
-require('nstore/query');
+nStore = nStore.extend(require('nstore/query'));
var queries = [
[{age: 50}, 2],

0 comments on commit e3f08eb

Please sign in to comment.