Permalink
Browse files

first working version

  • Loading branch information...
1 parent 9b503fc commit 1d6617624b1f698c0bfaa4aea1dc93da0a0c276c rezoner committed Oct 11, 2012
Showing with 180 additions and 26 deletions.
  1. +50 −24 memoria.js
  2. +103 −0 npm-debug.log
  3. +27 −2 test-node.js
View
@@ -128,7 +128,9 @@
Memoria.saveInterval = 1000;
var Database = function(name, callback) {
- this.tables = {};
+ this.data = { };
+ this.tables = { };
+
var self = this;
this.filePath = name + ".memoria";
@@ -146,13 +148,16 @@
Database.prototype = {
table: function(name) {
- return new Table(this, name);
+ if(!this.tables[name]) {
+ this.tables[name] = new Table(this, name);
+ }
+ return this.tables[name];
},
setupTable: function(name, fields) {
fields.unshift("id");
- this.tables[name] = {
+ this.data[name] = {
autoincrement: 1,
structure: fields,
keyToIndex: _.inverse(fields),
@@ -161,29 +166,41 @@
},
open: function() {
- fs.readFile(this.filePath, function(err, data) {
+ var data = fs.readFileSync(this.filePath);
+ this.data = JSON.parse(data);
+ },
+
+ save: function() {
+ var self = this;
+
+ if(this.saving) {
+ return false;
+ }
+
+ this.saving = true;
+
+ fs.writeFile(this.filePath, JSON.stringify(this.data), function(err) {
if(err) {
throw err;
} else {
- this.tables = JSON.parse(_.unpack(data));
+ self.saving = false;
}
- });
- },
-
- save: function() {
- fs.writeFile(this.filePath, _.pack(JSON.stringify(this.tables)), function(err) {
- if(err) throw err;
+
});
}
};
var Table = function(db, name) {
this.db = db;
this.name = name;
- this.indexing = {};
+ this.indexing = { };
if(this.db) {
- _.extend(this, db.tables[name]);
+ _.extend(this, db.data[name]);
+ }
+
+ for(var i = 0; i < this.items.length; i++) {
+ this.indexing[this.items[i][0]] = this.items[i];
}
};
@@ -204,7 +221,7 @@
insert: function(data) {
var items = arguments;
-/*
+ /*
if(!(data instanceof Array)) {
items = [data];
}
@@ -240,24 +257,32 @@
}
} else if(query instanceof Object) {
var add = true;
- for(var property in query) {
- var keyIndex = this.table.keyToIndex[property];
- if(this.table.items[i][keyIndex] !== query[property]) {
- add = false;
- break;
+ for(var i = 0, len = this.table.items.length; i < len; i++) {
+ for(var property in query) {
+ var keyIndex = this.table.keyToIndex[property];
+ if(this.table.items[i][keyIndex] !== query[property]) {
+ add = false;
+ break;
+ }
}
- }
- if(add) {
- this.items.push(this.table.items[i]);
+ if(add) {
+ this.items.push(this.table.items[i]);
+ }
}
} else {
- this.items = [this.indexing[query]];
+ var item = this.table.indexing[query];
+ if(item) {
+ this.items = [item];
+ }
}
if(this.items.length) {
if(this.single) {
- this.result = _.unfold(this.items[0], this.table.structure);
+ if(this.items.length) {
+ console.log(this.items);
+ this.result = _.unfold(this.items[0], this.table.structure);
+ }
} else {
this.result = [];
for(var i = 0; i < this.items.length; i++) {
@@ -288,4 +313,5 @@
module.exports = Memoria;
}
+
})();
View
@@ -0,0 +1,103 @@
+0 info it worked if it ends with ok
+1 verbose cli [ '/usr/local/bin/node',
+1 verbose cli '/usr/local/bin/npm',
+1 verbose cli 'publish',
+1 verbose cli './npm/' ]
+2 info using npm@1.1.62
+3 info using node@v0.8.11
+4 verbose publish [ './npm/' ]
+5 verbose read json /home/rezoner/Workshop/memoria/npm/package.json
+6 verbose cache add [ './npm/', null ]
+7 silly cache add name=undefined spec="./npm/" args=["./npm/",null]
+8 verbose parsed url { pathname: './npm/', path: './npm/', href: './npm/' }
+9 silly lockFile 2bf482c2-npm ./npm/
+10 verbose lock ./npm/ /home/rezoner/.npm/2bf482c2-npm.lock
+11 verbose read json npm/package.json
+12 verbose tar pack [ '/home/rezoner/tmp/npm-32085/1349910068448-0.9679954596795142/tmp.tgz',
+12 verbose tar pack './npm/' ]
+13 verbose tarball /home/rezoner/tmp/npm-32085/1349910068448-0.9679954596795142/tmp.tgz
+14 verbose folder ./npm/
+15 info prepublish memoria@0.0.5
+16 silly lockFile 26117bb1-68448-0-9679954596795142-tmp-tgz /home/rezoner/tmp/npm-32085/1349910068448-0.9679954596795142/tmp.tgz
+17 verbose lock /home/rezoner/tmp/npm-32085/1349910068448-0.9679954596795142/tmp.tgz /home/rezoner/.npm/26117bb1-68448-0-9679954596795142-tmp-tgz.lock
+18 silly lockFile 26117bb1-68448-0-9679954596795142-tmp-tgz /home/rezoner/tmp/npm-32085/1349910068448-0.9679954596795142/tmp.tgz
+19 verbose tar unpack /home/rezoner/tmp/npm-32085/1349910068448-0.9679954596795142/tmp.tgz
+20 silly lockFile b3050e98-68448-0-9679954596795142-package /home/rezoner/tmp/npm-32085/1349910068448-0.9679954596795142/package
+21 verbose lock /home/rezoner/tmp/npm-32085/1349910068448-0.9679954596795142/package /home/rezoner/.npm/b3050e98-68448-0-9679954596795142-package.lock
+22 silly gunzTarPerm modes [ '755', '644' ]
+23 silly gunzTarPerm extractEntry package.json
+24 silly gunzTarPerm modified mode [ 'package.json', 438, 420 ]
+25 silly gunzTarPerm extractEntry README.md
+26 silly gunzTarPerm modified mode [ 'README.md', 436, 420 ]
+27 silly gunzTarPerm extractEntry memoria.js
+28 silly gunzTarPerm modified mode [ 'memoria.js', 436, 420 ]
+29 verbose read json /home/rezoner/tmp/npm-32085/1349910068448-0.9679954596795142/package/package.json
+30 silly lockFile b3050e98-68448-0-9679954596795142-package /home/rezoner/tmp/npm-32085/1349910068448-0.9679954596795142/package
+31 verbose from cache /home/rezoner/tmp/npm-32085/1349910068448-0.9679954596795142/package/package.json
+32 verbose tar pack [ '/home/rezoner/.npm/memoria/0.0.5/package.tgz',
+32 verbose tar pack '/home/rezoner/tmp/npm-32085/1349910068448-0.9679954596795142/package' ]
+33 verbose tarball /home/rezoner/.npm/memoria/0.0.5/package.tgz
+34 verbose folder /home/rezoner/tmp/npm-32085/1349910068448-0.9679954596795142/package
+35 silly lockFile 8a6c4725-er-npm-memoria-0-0-5-package-tgz /home/rezoner/.npm/memoria/0.0.5/package.tgz
+36 verbose lock /home/rezoner/.npm/memoria/0.0.5/package.tgz /home/rezoner/.npm/8a6c4725-er-npm-memoria-0-0-5-package-tgz.lock
+37 silly lockFile 8a6c4725-er-npm-memoria-0-0-5-package-tgz /home/rezoner/.npm/memoria/0.0.5/package.tgz
+38 silly lockFile 2c794c2c-ezoner-npm-memoria-0-0-5-package /home/rezoner/.npm/memoria/0.0.5/package
+39 verbose lock /home/rezoner/.npm/memoria/0.0.5/package /home/rezoner/.npm/2c794c2c-ezoner-npm-memoria-0-0-5-package.lock
+40 silly lockFile 2c794c2c-ezoner-npm-memoria-0-0-5-package /home/rezoner/.npm/memoria/0.0.5/package
+41 verbose tar unpack /home/rezoner/.npm/memoria/0.0.5/package.tgz
+42 silly lockFile 2c794c2c-ezoner-npm-memoria-0-0-5-package /home/rezoner/.npm/memoria/0.0.5/package
+43 verbose lock /home/rezoner/.npm/memoria/0.0.5/package /home/rezoner/.npm/2c794c2c-ezoner-npm-memoria-0-0-5-package.lock
+44 silly gunzTarPerm modes [ '755', '644' ]
+45 silly gunzTarPerm extractEntry package.json
+46 silly gunzTarPerm extractEntry README.md
+47 silly gunzTarPerm extractEntry memoria.js
+48 verbose read json /home/rezoner/.npm/memoria/0.0.5/package/package.json
+49 silly lockFile 2c794c2c-ezoner-npm-memoria-0-0-5-package /home/rezoner/.npm/memoria/0.0.5/package
+50 silly shasum updated bytes 2859
+51 info shasum c67b9c1471b5347e54e8e81830421ef222b432e9
+51 info shasum /home/rezoner/.npm/memoria/0.0.5/package.tgz
+52 verbose from cache /home/rezoner/.npm/memoria/0.0.5/package/package.json
+53 verbose chmod /home/rezoner/.npm/memoria/0.0.5/package.tgz 644
+54 verbose chown /home/rezoner/.npm/memoria/0.0.5/package.tgz [ 1000, 1000 ]
+55 silly lockFile 2bf482c2-npm ./npm/
+56 silly publish { author:
+56 silly publish { name: 'Rezoner',
+56 silly publish email: 'rezoner1337@gmail.com',
+56 silly publish url: 'http://rezoner.net' },
+56 silly publish name: 'memoria',
+56 silly publish description: 'a simple javascript database for browser based games',
+56 silly publish version: '0.0.5',
+56 silly publish repository: { type: 'git', url: 'git://github.com/rezoner/Memoria.git' },
+56 silly publish main: './memoria.js',
+56 silly publish scripts: {},
+56 silly publish engines: { node: '>=0.6.0' },
+56 silly publish keywords: [ 'database', 'nosql' ],
+56 silly publish dependencies: {},
+56 silly publish devDependencies: {},
+56 silly publish license: 'MIT',
+56 silly publish readme: 'Memoria\n=======\n\nSimple database for node.js\n---------------------------\n\nThe library is in early development - some features are not implemented yet - but these are marked.\n\nMemoria is designed especially for browser based games without much of relational data.\nEverything happens in memory, but the data is saved to a file.\nIt is meant to be used with applications that doesn\'t share database with other instances.\n\n### Install:\n\n npm install memoria\n\n### Create database:\n\nLooks for file `test.memoria` to restore data.\nIf file doesn\'t exists it will be created.\n\n var db = Memoria("test");\n\nIt is possible to check if file was found using:\n\n if(db.exists) {\n \n /* some creational functions */\n\n }\n\n### Create table:\n\n db("users", ["name", "age", "salary"]);\n\n### Insertion:\n\n db("users").insert({ name: "Vennril", age: 15, salary: 1000 });\n\nor\n\n db("users").insert({ ... }, { ... }, { ... });\n \n### Querrying:\n\n db("users").one(selector).result;\n db("users").all(selector).result;\n\n### Selectors:\n\nNumber (returns entry by ID):\n\n db("users").one(32).result;\n\nObject (equal check):\n\n db("users").all({ age: 16 }).result;\n \nFunction (flexible querries):\n\n /* get all items matching age > 32 */\n\n db("users").all(function(r, i) {\n return r[i.age] > 32;\n }).result; \n \nNote that properties have to be accessed using additional array with indexes. It is not really convenient but gives a huge boost to performance.\n\n### Updating:\n\n db("users").all({ age: 32 }).update({ salary: 1600 });\n \nor\n\n db("users").all({ age: 32 }).update(\n function(r, i) {\n r[i.salary] *= 2;\n });\n \n \n \n### Removing *not implemented*:\n\n db("users").all(selector).remove();\n',
+56 silly publish _id: 'memoria@0.0.5',
+56 silly publish dist: { shasum: 'c67b9c1471b5347e54e8e81830421ef222b432e9' } }
+57 verbose url raw memoria
+58 verbose url resolving [ 'https://registry.npmjs.org/', './memoria' ]
+59 verbose url resolved https://registry.npmjs.org/memoria
+60 info trying registry request attempt 1 at 01:01:08
+61 http PUT https://registry.npmjs.org/memoria
+62 http 409 https://registry.npmjs.org/memoria
+63 verbose url raw memoria
+64 verbose url resolving [ 'https://registry.npmjs.org/', './memoria' ]
+65 verbose url resolved https://registry.npmjs.org/memoria
+66 info trying registry request attempt 1 at 01:01:10
+67 http GET https://registry.npmjs.org/memoria
+68 http 200 https://registry.npmjs.org/memoria
+69 error publish fail Cannot publish over existing version.
+69 error publish fail Bump the 'version' field, set the --force flag, or
+69 error publish fail npm unpublish 'memoria@0.0.5'
+69 error publish fail and try again
+70 error System Linux 3.2.0-29-generic
+71 error command "/usr/local/bin/node" "/usr/local/bin/npm" "publish" "./npm/"
+72 error cwd /home/rezoner/Workshop/memoria
+73 error node -v v0.8.11
+74 error npm -v 1.1.62
+75 error code EPUBLISHCONFLICT
+76 verbose exit [ 1, true ]
View
@@ -1,3 +1,13 @@
+var benchmark = {
+ start: function() {
+ this.startTime = (new Date).getTime();
+ },
+
+ stop: function() {
+ console.log((new Date).getTime() - this.startTime + "ms");
+ }
+}
+
Memoria = require("./memoria.js");
db = Memoria("test");
@@ -22,8 +32,17 @@ if(!db.exists) {
{ "name": "Armen138", "age": 11, "salary": 2400 }
);
+ for(var i = 0; i < 500000; i++) {
+ db("users").insert({
+ name: Math.random().toString(36).substring(2),
+ age: Math.random() * 100 | 0,
+ salary: Math.random() * 10000 | 0
+ });
+ }
+
}
+
/* update sallary by age */
db("users").all().update(function(r, i) {
@@ -36,6 +55,12 @@ db("users").all(function(r, i) {
return r[i.salary] > 1000;
}).result;
-/* alienate Hinton */
+/* Find Hinton */
+
+db("users").one({ name: "Hinton" });
-db("users").one({ name: "Hinton" }).remove();
+benchmark.start();
+ var rc = db("users").all(function(r, i) {
+ return Math.random() > 0.5;
+ }).result;
+benchmark.stop();

0 comments on commit 1d66176

Please sign in to comment.