Permalink
Browse files

create-database works

  • Loading branch information...
chrisdew committed Feb 9, 2011
1 parent 2d2a11f commit 41750b19223c66381b15018a2c9d8e8b84520329
Showing with 19 additions and 10 deletions.
  1. +4 −1 docs/db.html
  2. +2 −1 docs/disk-io.html
  3. +3 −6 examples/create-database.js
  4. +4 −0 examples/example-model.js
  5. +4 −1 lib/db.js
  6. +2 −1 lib/disk-io.js
View
@@ -46,11 +46,14 @@
<span class="kd">var</span> <span class="nx">store</span> <span class="o">=</span> <span class="p">{};</span> <span class="c1">// the backing store for the instance data</span>
<span class="k">this</span><span class="p">.</span><span class="nx">stores</span><span class="p">[</span><span class="nx">instance</span><span class="p">[</span><span class="k">this</span><span class="p">.</span><span class="nx">UUID_TAG</span><span class="p">]]</span> <span class="o">=</span> <span class="nx">store</span><span class="p">;</span>
+ <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">&quot;about to create setters&quot;</span><span class="p">);</span>
<span class="nx">util</span><span class="p">.</span><span class="nx">ownRealKeys</span><span class="p">(</span><span class="nx">instance</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">magic</span><span class="p">).</span><span class="nx">forEach</span><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">p</span><span class="p">,</span> <span class="nx">i</span><span class="p">,</span> <span class="nx">all</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">store</span><span class="p">[</span><span class="nx">p</span><span class="p">]</span> <span class="o">=</span> <span class="nx">instance</span><span class="p">[</span><span class="nx">p</span><span class="p">];</span>
+ <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">&quot;creatig setter&quot;</span><span class="p">,</span> <span class="nx">p</span><span class="p">);</span>
<span class="nx">instance</span><span class="p">.</span><span class="nx">__defineSetter__</span><span class="p">(</span><span class="nx">p</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">val</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">&quot;setter called&quot;</span><span class="p">,</span> <span class="nx">JSON</span><span class="p">.</span><span class="nx">stringify</span><span class="p">(</span><span class="nx">that</span><span class="p">.</span><span class="nx">persist</span><span class="p">));</span>
<span class="nx">store</span><span class="p">[</span><span class="nx">p</span><span class="p">]</span> <span class="o">=</span> <span class="nx">val</span><span class="p">;</span>
- <span class="k">if</span> <span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">persist</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">if</span> <span class="p">(</span><span class="nx">that</span><span class="p">.</span><span class="nx">persist</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">that</span><span class="p">.</span><span class="nx">io</span><span class="p">.</span><span class="nx">persist</span><span class="p">(</span><span class="nx">instance</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">});</span>
View
@@ -22,11 +22,12 @@
written.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">outstandingWrites</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</pre></div> </td> </tr> <tr id="section-7"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-7">&#182;</a> </div> <p>If end is requested, this will contain a callback.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">ending</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span>
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-8"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-8">&#182;</a> </div> <p>This writes an object to disk.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">DiskIO</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">persist</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">instance</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="s2">&quot;persisting&quot;</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">that</span> <span class="o">=</span> <span class="k">this</span><span class="p">;</span>
<span class="k">this</span><span class="p">.</span><span class="nx">outstandingWrites</span><span class="o">++</span><span class="p">;</span>
<span class="kd">var</span> <span class="nx">serialised</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">codec</span><span class="p">.</span><span class="nx">serialise</span><span class="p">(</span><span class="nx">instance</span><span class="p">);</span>
- <span class="k">this</span><span class="p">.</span><span class="nx">writeStream</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="nx">serialised</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">result</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">this</span><span class="p">.</span><span class="nx">writeStream</span><span class="p">.</span><span class="nx">write</span><span class="p">(</span><span class="nx">serialised</span> <span class="o">+</span> <span class="s2">&quot;\n&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">err</span><span class="p">,</span> <span class="nx">result</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">that</span><span class="p">.</span><span class="nx">outstandingWrites</span><span class="o">--</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">that</span><span class="p">.</span><span class="nx">outstandingWrites</span> <span class="o">===</span> <span class="mi">0</span> <span class="o">&amp;&amp;</span> <span class="nx">that</span><span class="p">.</span><span class="nx">ending</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">that</span><span class="p">.</span><span class="nx">writeStream</span><span class="p">.</span><span class="nx">end</span><span class="p">();</span> <span class="c1">// FIXME: we need to call the callback only after the end.</span>
@@ -2,21 +2,18 @@
// Released under the MIT open source licence.
var bdb = require('barricane-db')
- , model = require('./example-model')
;
// Create a database instance.
var db = new bdb.DB({path: '/tmp', name: 'test_db'});
// Make the database available globally with the process. If you don't do this,
// you can manually inject the database into appropriate constructors, or call
-// <code>DB.registerInstance(instance)</code> everytime you create an object.
+// <code>DB.registerInstance(instance)</code> every time you create an object.
process.db = db;
-// Register the constructors. We can either do this here, or do this in the
-// model - that approach needs us to create process.db before we require the
-// model.
-db.registerConstructors(model.House, model.Person);
+// Import the model, now that we have defined <code>process.db</code>.
+var model = require('./example-model');
// Delete any database of the same path and name. Most applications will never
// use this. It's only used here so that we know this has created a brand new
@@ -29,6 +29,10 @@ function Person(personalName, familyName) {
process.db.registerInstance(this);
}
+// Register the constructors.
+process.db.registerConstructors(House, Person);
+
+
// Export the symbols.
exports.House = House;
exports.Person = Person;
View
@@ -77,11 +77,14 @@ DB.prototype.registerInstance = function(instance) {
var store = {}; // the backing store for the instance data
this.stores[instance[this.UUID_TAG]] = store;
+ console.log("about to create setters");
util.ownRealKeys(instance, this.magic).forEach(function(p, i, all) {
store[p] = instance[p];
+ console.log("creatig setter", p);
instance.__defineSetter__(p, function(val) {
+ console.log("setter called", JSON.stringify(that.persist));
store[p] = val;
- if (this.persist) {
+ if (that.persist) {
that.io.persist(instance);
}
});
View
@@ -39,11 +39,12 @@ function DiskIO(options) {
// This writes an object to disk.
DiskIO.prototype.persist = function (instance) {
+ console.log("persisting");
var that = this;
this.outstandingWrites++;
var serialised = this.codec.serialise(instance);
- this.writeStream.write(serialised, function(err, result) {
+ this.writeStream.write(serialised + "\n", function(err, result) {
that.outstandingWrites--;
if (that.outstandingWrites === 0 && that.ending) {
that.writeStream.end(); // FIXME: we need to call the callback only after the end.

0 comments on commit 41750b1

Please sign in to comment.