Permalink
Browse files

Merge branch 'master' of github.com:chrisdew/barricane-db

  • Loading branch information...
2 parents d123e3a + 92d6c8e commit 1e8abc97b9309fd48ccb42ac847fd8786ce5ba63 chrisdew committed Feb 3, 2011
Showing with 132 additions and 41 deletions.
  1. +51 −11 lib/barricane-db.js
  2. +27 −18 lib/bdb-disk-manager.js
  3. +51 −11 test/disk-manager.js
  4. +3 −1 test/serialisation.js
View
@@ -20,7 +20,7 @@ function ownKeys(o) {
return accumulator;
}
-function ownRealKeys(o) {
+DB.prototype.ownRealKeys = function(o) {
var accumulator = [];
for (var propertyName in o) {
if (o.hasOwnProperty(propertyName) && propertyName.slice(0,this.options.magic.length) !== this.options.magic) {
@@ -56,7 +56,7 @@ function DB(options) {
//var args = Array.prototype.slice.call(arguments);
-DB.prototype.serialise = function(ob) { // change this to a plain function
+DB.prototype.serialise = function(ob) {
var ret = {};
ret.__constructor = ob.constructor.name;
@@ -77,16 +77,23 @@ DB.prototype.serialise = function(ob) { // change this to a plain function
return ret;
}
+DB.prototype.registerConstructors = function() {
+ var args = Array.prototype.slice.call(arguments);
+ for (var i in args) {
+ this.constructors[args[i].name] = args[i];
+ }
+}
+
DB.prototype.register = function(instance) {
var that = this;
instance.__uuid = this.options.uuid();
- constructor = instance.constructor;
- // is it the first time we;ve seen an object of this type?
- if (this.constructors[constructor.name] === undefined) {
- this.constructors[constructor.name] = constructor;
- }
+ //constructor = instance.constructor;
+ //// is it the first time we;ve seen an object of this type?
+ //if (this.constructors[constructor.name] === undefined) {
+ // this.constructors[constructor.name] = constructor;
+ //}
this.instances[instance.__uuid] = instance;
var store = {}; // the backing store for the instance data
this.stores[instance.__uuid] = store;
@@ -107,15 +114,15 @@ DB.prototype.register = function(instance) {
//}
// FIXME
- ownRealKeys(instance).forEach(function(p, i, all) {
+ this.ownRealKeys(instance).forEach(function(p, i, all) {
//var args = Array.prototype.slice.call(arguments);
//console.log("Setter0", JSON.stringify(instance), instance[p], p);
//var old = instance[p];
store[p] = instance[p];
instance.__defineSetter__(p, function(val) {
//console.log("Setter", JSON.stringify(instance), instance[p], p, val);
store[p] = val;
- that.options.write(that.serialise(instance));
+ that.options.write(JSON.stringify(that.serialise(instance)) + "\n");
});
instance.__defineGetter__(p, function() {
return store[p];
@@ -124,20 +131,53 @@ DB.prototype.register = function(instance) {
// finally add the object to the log
- this.options.write(that.serialise(instance));
+ //console.log("write0", this.options.write);
+ this.options.write(JSON.stringify(that.serialise(instance)) + "\n");
}
-DB.prototype.toJson = function() {
+DB.prototype.toJSON = function() {
var ret = {};
for (var p in this.instances) {
if (this.instances.hasOwnProperty(p)) {
ret[p] = this.serialise(this.instances[p]);
}
}
+ //console.log(JSON.stringify(ret));
return ret;
}
+// This method sets up the instances from their serialised form.
+DB.prototype.fromJSON = function(json) {
+ for(var uuid in json) {
+ if (json.hasOwnProperty(uuid)) {
+ // FIXME create the object using it's constructor
+ // findout about modifying the constructors to automatically add a 'register' call.
+ this.instances[uuid] = json[uuid];
+ }
+ }
+ for(var uuid in this.instances) {
+ if (this.instances.hasOwnProperty(uuid)) {
+ var ob = this.instances[uuid];
+ for(var p in ob) {
+ if (ob.hasOwnProperty(p)) {
+ var value = json[p];
+ if (value instanceof String) {
+ // FIXME
+ // if it's a __ID__ or __DATE__
+ // substitute in the real link/date
+ //
+ }
+ }
+ }
+ }
+ }
+}
+
+// This method amends the instances from the transaction log.
+DB.prototype.fromLog = function(log) {
+
+}
exports.DB = DB;
View
@@ -4,6 +4,8 @@ var bdb = require('barricane-db');
var fs = require('fs');
function BDBDiskManager(options) {
+ var that = this;
+
this.options = options;
if (this.options === undefined) {
this.options = {};
@@ -14,25 +16,32 @@ function BDBDiskManager(options) {
if (this.options.name === undefined) {
this.options.name = "unnamed_db";
}
- this.bdb = null;
- this.txnlog = {};
+ this.db = new bdb.DB(this.options);
+ this.txnlog = null;
+ this.txnlogPath = this.options.path + "/" + this.options.name + ".log";
+ this.snapshot = null;
+ this.snapshotPath = this.options.path + "/" + this.options.name + ".bdb";
+
+ try { // FIXME: do this properly
+ fs.statSync(this.snapshotPath);
+ console.log("loadDB");
+ } catch (e) {
+ console.log("createDB");
+ this.snapshot_fd = fs.openSync(this.snapshotPath, 'w');
+ this.txnlog_fd = fs.openSync(this.txnlogPath, 'w');
+ this.db.options.write = function(arg) {
+ //console.log(arg);
+ fs.write(that.txnlog_fd, arg);
+ }
+ fs.writeSync(this.snapshot_fd, JSON.stringify(this.db));
+ fs.closeSync(this.snapshot_fd);
+ }
}
-BDBDiskManager.prototype.openDB = function(name) {
- fs.statSync()
+BDBDiskManager.prototype.end = function() {
+ this.db.options.write = function() {};
+ fs.closeSync(this.txnlog_fd);
+ //this.txnlog.destroy();
}
-
-BDBDiskManager.prototype._createDB = function(name) {
- this.txnlog =
- this.bdb = new bdb.DB(
- { name: this.options.name
- , manager: this
- , write: this.txnlog.write
- }
- );
-}
-
-BDBDiskManager.prototype._openDB = function(name) {
-
-}
+exports.BDBDiskManager = BDBDiskManager;
View
@@ -1,27 +1,67 @@
var vows = require('vows')
, assert = require('assert')
, bdb = require('barricane-db')
+ , bdm = require('../lib/bdb-disk-manager')
, fs = require('fs')
;
+require('datejs');
+
// mock up uuids for testing
var nextMockUuid = 0;
function mockUuid() {
return "" + nextMockUuid++;
}
-// remove old files, if they exist
-try {
- fs.unlinkSync('/tmp/testdb.snapshot');
- fs.unlinkSync('/tmp/testdb.txnlog');
-} catch (e) {
- console.log("unable to remove files");
+
+
+var db = null;
+var dm = null;
+function setup() {
+ // remove old files, if they exist
+ try {
+ fs.unlinkSync('/tmp/unnamed_db.bdb');
+ fs.unlinkSync('/tmp/unnamed_db.log');
+ } catch (e) {
+ console.log("unable to remove files");
+ }
+
+ var dm = new bdm.BDBDiskManager({uuid: mockUuid});
+ db = dm.db;
+
+ function Customer(personal_name, family_name, dob) {
+ this.personal_name = personal_name;
+ this.family_name = family_name;
+ this.dob = dob;
+ db.register(this);
+ }
+
+ db.registerConstructors(Customer);
+
+ var customer = new Customer("Fred", "Smith", Date.parse("2011-02-02"));
+ customer.family_name = "Flintstone";
}
-vows.describe('Disk Manager Tests').addBatch({
- 'Setting Up': { topic: 'hello'
- , 'hello': function(topic) {
- assert.deepEqual("hello", "hello");
+setup();
+
+var suite = vows.describe('disk-manager').addBatch({
+ '.bdb file': { topic: function() {
+ fs.readFile('/tmp/unnamed_db.bdb', 'utf8', this.callback);
+ }
+ , 'content': function(err, result) {
+ assert.equal(result, '{}');
}
}
-}).export(module);
+ , '.log file': { topic: function() {
+ fs.readFile('/tmp/unnamed_db.log', 'utf8', this.callback);
+ }
+ , 'content': function(err, result) {
+ assert.equal(result, '{"__constructor":"Customer","personal_name":"Fred","family_name":"Smith","dob":"__DATE__1296604800000","__uuid":"0"}\n{"__constructor":"Customer","personal_name":"Fred","family_name":"Flintstone","dob":"__DATE__1296604800000","__uuid":"0"}\n');
+ }
+ }
+})
+suite.export(module);
+
+setTimeout(function() {
+ dm.end();
+}, 1000);
View
@@ -32,6 +32,8 @@ function Account(bank, customer, balance) {
db.register(this);
}
+db.registerConstructors(Bank, Customer, Account);
+
var lloyds = new Bank('lloyds', "01-02-03");
lloyds.name = "LloydsTSB";
var barclays = new Bank('barclays', "02-03-04",
@@ -59,7 +61,7 @@ vows.describe('The First Test').addBatch({
} );
}
, 'serialisation_has_worked': function(topic) {
- assert.deepEqual(topic.toJson(),
+ assert.deepEqual(topic.toJSON(),
{
0: {
__constructor: 'Bank',

0 comments on commit 1e8abc9

Please sign in to comment.