Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

lots more work done

  • Loading branch information...
commit 407b56eff7ae2189702aae6b5408424470452498 1 parent b402a95
@chrisdew authored
View
6 docs/app-test.html
@@ -4,6 +4,12 @@
<span class="p">,</span> <span class="nx">bdb</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;../lib/barricane-db&#39;</span><span class="p">)</span>
<span class="p">;</span>
+
+
+
+
+
+
<span class="kd">var</span> <span class="nx">suite</span> <span class="o">=</span> <span class="nx">vows</span><span class="p">.</span><span class="nx">describe</span><span class="p">(</span><span class="s1">&#39;Setup&#39;</span><span class="p">).</span><span class="nx">addBatch</span><span class="p">(</span>
<span class="p">{</span> <span class="s2">&quot;dummy&quot;</span>
<span class="o">:</span> <span class="p">{</span> <span class="nx">topic</span>
View
74 docs/db.html
@@ -1,22 +1,78 @@
<!DOCTYPE html> <html> <head> <title>db.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <div id="jump_to"> Jump To &hellip; <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="barricane-db.html"> barricane-db.js </a> <a class="source" href="bdb-disk-manager.html"> bdb-disk-manager.js </a> <a class="source" href="db.html"> db.js </a> <a class="source" href="disk-io.html"> disk-io.js </a> <a class="source" href="old-barricane-db.html"> old-barricane-db.js </a> <a class="source" href="serialisation.html"> serialisation.js </a> <a class="source" href="swoj-codec.html"> swoj-codec.js </a> <a class="source" href="util.html"> util.js </a> <a class="source" href="app-test.html"> app-test.js </a> <a class="source" href="demo-model.html"> demo-model.js </a> <a class="source" href="hello-world-test.html"> hello-world-test.js </a> <a class="source" href="mock.html"> mock.js </a> <a class="source" href="old-disk-manager.html"> old-disk-manager.js </a> <a class="source" href="old-serialisation.html"> old-serialisation.js </a> <a class="source" href="swoj-codec-test.html"> swoj-codec-test.js </a> </div> </div> </div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> db.js </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">&#182;</a> </div> <p>Copyright (c) 2010 Barricane Technology Ltd., All Rights Reserved.
Released under the MIT open source licence.</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">&#182;</a> </div> <p>This module contains the database code.
It specifically eschews serialisation and io concerns.</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">var</span> <span class="nx">uuid</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;node-uuid&#39;</span><span class="p">)</span>
- <span class="p">,</span> <span class="nx">dio</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;./disk-io&#39;</span><span class="p">);</span></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">&#182;</a> </div> <p>The DB constructor represents the database. In almost all circumstances only
+ <span class="p">,</span> <span class="nx">dio</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;./disk-io&#39;</span><span class="p">)</span>
+ <span class="p">,</span> <span class="nx">util</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;./util&#39;</span><span class="p">)</span>
+ <span class="p">;</span></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">&#182;</a> </div> <p>The DB constructor represents the database. In almost all circumstances only
one database will exist per process. More can be used, if needed, but the
application will become more complex, as it cannot use
<code>process.db</code> in constructors.</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">function</span> <span class="nx">DB</span><span class="p">(</span><span class="nx">options</span><span class="p">)</span> <span class="p">{</span>
- <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">options</span><span class="p">)</span> <span class="p">{</span>
- <span class="nx">options</span> <span class="o">=</span> <span class="p">{};</span>
- <span class="p">}</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p><code>magic</code> is a string which must not normally occur in your application's
-property names. Double underscore is the default.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">magic</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">magic</span> <span class="o">||</span> <span class="s2">&quot;__&quot;</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">&#182;</a> </div> <p><code>uuid</code> is the function used to generate unique IDs for each
+ <span class="nx">options</span> <span class="o">=</span> <span class="nx">options</span> <span class="o">||</span> <span class="p">{};</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p><code>magic</code> is a string which must not normally occur in your application's
+property names. Double underscore is the default.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">magic</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">magic</span> <span class="o">||</span> <span class="s2">&quot;__&quot;</span><span class="p">;</span>
+ </pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">&#182;</a> </div> <p>Set up some almost constants.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">CONSTRUCTOR_TAG</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">magic</span> <span class="o">+</span> <span class="s1">&#39;constructor&#39;</span> <span class="o">+</span> <span class="k">this</span><span class="p">.</span><span class="nx">magic</span><span class="p">;</span>
+ <span class="k">this</span><span class="p">.</span><span class="nx">UUID_TAG</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">magic</span> <span class="o">+</span> <span class="s1">&#39;uuid&#39;</span> <span class="o">+</span> <span class="k">this</span><span class="p">.</span><span class="nx">magic</span><span class="p">;</span>
+ <span class="k">this</span><span class="p">.</span><span class="nx">DATE_TAG</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">magic</span> <span class="o">+</span> <span class="s1">&#39;date&#39;</span> <span class="o">+</span> <span class="k">this</span><span class="p">.</span><span class="nx">magic</span><span class="p">;</span>
+ </pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">&#182;</a> </div> <p><code>uuid</code> is the function used to generate unique IDs for each
managed object. It is overridable for testing purposes, where the IDs
-need to be deterministic.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">uuid</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">uuid</span> <span class="o">||</span> <span class="nx">uuid</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">&#182;</a> </div> <p>The <code>io</code> manager is responsible for IO. By default BarricaneDB
+need to be deterministic.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">uuid</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">uuid</span> <span class="o">||</span> <span class="nx">uuid</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>The <code>io</code> manager is responsible for IO. By default BarricaneDB
uses simple disk-based persistence. The <code>io</code> manager is
separated so that it can be optionally replaced with ReplicationIO at
-some point in the future.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">io</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">io</span> <span class="o">||</span> <span class="k">new</span> <span class="nx">dio</span><span class="p">.</span><span class="nx">DiskIO</span><span class="p">(</span><span class="nx">options</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><code>instances</code> contains all of the <em>managed</em> objects, indexed
-by uuid.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">instances</span> <span class="o">=</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><code>stores</code> contains all of the <em>managed</em> objects' data, indexed
+some point in the future.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">io</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">io</span> <span class="o">||</span> <span class="k">new</span> <span class="nx">dio</span><span class="p">.</span><span class="nx">DiskIO</span><span class="p">(</span><span class="nx">options</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>The database needs to keep track of constructors, in order to be able
+to reconstruct objects when opening a database.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">constructors</span> <span class="o">=</span> <span class="p">{};</span>
+ </pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-9">&#182;</a> </div> <p><code>instances</code> contains all of the <em>managed</em> objects, indexed
+by uuid.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">instances</span> <span class="o">=</span> <span class="p">{};</span>
+ </pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-10">&#182;</a> </div> <p><code>stores</code> contains all of the <em>managed</em> objects' data, indexed
by uuid. The data is separate as the properties in the instances have
custom getters/setters, via <code><strong>defineSetter</strong></code>.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">stores</span> <span class="o">=</span> <span class="p">{};</span>
-<span class="p">}</span></pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-9">&#182;</a> </div> <p>Export the symbols.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">exports</span><span class="p">.</span><span class="nx">DB</span> <span class="o">=</span> <span class="nx">DB</span><span class="p">;</span>
+ </pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-11">&#182;</a> </div> <p>The <code>persist</code> flag is made false during database
+reconstruction, otherwise we would get into a frightful pickle.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">persist</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
+<span class="p">}</span></pre></div> </td> </tr> <tr id="section-12"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-12">&#182;</a> </div> <p>This opens the database for use. It only exists because I'm not sure about
+the validity of calling instance members from within the constructor.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">DB</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">openSync</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
+ <span class="k">this</span><span class="p">.</span><span class="nx">persist</span> <span class="o">=</span> <span class="kc">false</span><span class="p">;</span>
+
+ <span class="k">this</span><span class="p">.</span><span class="nx">io</span><span class="p">.</span><span class="nx">openSync</span><span class="p">();</span>
+
+ <span class="k">this</span><span class="p">.</span><span class="nx">persist</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
+<span class="p">}</span></pre></div> </td> </tr> <tr id="section-13"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-13">&#182;</a> </div> <p>This must be called once for every instance which needs to be persisted.
+It is idiomatic to do this in the constructor, using a global
+<code>process.db</code> database.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">DB</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">registerInstance</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="kd">var</span> <span class="nx">that</span> <span class="o">=</span> <span class="k">this</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="k">this</span><span class="p">.</span><span class="nx">options</span><span class="p">.</span><span class="nx">uuid</span><span class="p">();</span>
+
+ <span class="k">this</span><span class="p">.</span><span class="nx">instances</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">instance</span><span class="p">;</span>
+ <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">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">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">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="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>
+ <span class="nx">instance</span><span class="p">.</span><span class="nx">__defineGetter__</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="p">{</span>
+ <span class="k">return</span> <span class="nx">store</span><span class="p">[</span><span class="nx">p</span><span class="p">];</span>
+ <span class="p">});</span>
+ <span class="p">});</span>
+
+ </pre></div> </td> </tr> <tr id="section-14"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-14">&#182;</a> </div> <p>Finally add the object to the log, if the database is not in the middle
+of reconstruction.</p> </td> <td class="code"> <div class="highlight"><pre> <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">this</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></pre></div> </td> </tr> <tr id="section-15"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-15">&#182;</a> </div> <p>The database needs to keep track of constructors, so that it can call them
+when loading a database.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">DB</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">registerConstructors</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
+ <span class="kd">var</span> <span class="nx">args</span> <span class="o">=</span> <span class="nb">Array</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">slice</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">arguments</span><span class="p">);</span>
+ <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">i</span> <span class="k">in</span> <span class="nx">args</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">this</span><span class="p">.</span><span class="nx">constructors</span><span class="p">[</span><span class="nx">args</span><span class="p">[</span><span class="nx">i</span><span class="p">].</span><span class="nx">name</span><span class="p">]</span> <span class="o">=</span> <span class="nx">args</span><span class="p">[</span><span class="nx">i</span><span class="p">];</span>
+ <span class="p">}</span>
+<span class="p">};</span></pre></div> </td> </tr> <tr id="section-16"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-16">&#182;</a> </div> <p>Just a wrapper.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">DB</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">deleteSync</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
+ <span class="k">this</span><span class="p">.</span><span class="nx">io</span><span class="p">.</span><span class="nx">deleteSync</span><span class="p">();</span>
+<span class="p">}</span></pre></div> </td> </tr> <tr id="section-17"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-17">&#182;</a> </div> <p>Just a wrapper.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">DB</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">endSync</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
+ <span class="k">this</span><span class="p">.</span><span class="nx">io</span><span class="p">.</span><span class="nx">endSync</span><span class="p">();</span>
+<span class="p">}</span></pre></div> </td> </tr> <tr id="section-18"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-18">&#182;</a> </div> <p>Export the symbols.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">exports</span><span class="p">.</span><span class="nx">DB</span> <span class="o">=</span> <span class="nx">DB</span><span class="p">;</span>
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
View
3  docs/demo-model.html
@@ -1,7 +1,8 @@
<!DOCTYPE html> <html> <head> <title>demo-model.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <div id="jump_to"> Jump To &hellip; <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="barricane-db.html"> barricane-db.js </a> <a class="source" href="bdb-disk-manager.html"> bdb-disk-manager.js </a> <a class="source" href="db.html"> db.js </a> <a class="source" href="disk-io.html"> disk-io.js </a> <a class="source" href="old-barricane-db.html"> old-barricane-db.js </a> <a class="source" href="serialisation.html"> serialisation.js </a> <a class="source" href="swoj-codec.html"> swoj-codec.js </a> <a class="source" href="util.html"> util.js </a> <a class="source" href="app-test.html"> app-test.js </a> <a class="source" href="demo-model.html"> demo-model.js </a> <a class="source" href="hello-world-test.html"> hello-world-test.js </a> <a class="source" href="mock.html"> mock.js </a> <a class="source" href="old-disk-manager.html"> old-disk-manager.js </a> <a class="source" href="old-serialisation.html"> old-serialisation.js </a> <a class="source" href="swoj-codec-test.html"> swoj-codec-test.js </a> </div> </div> </div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> demo-model.js </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">&#182;</a> </div> <p>Copyright (c) 2010 Barricane Technology Ltd., All Rights Reserved.
Released under the MIT open source licence.</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">&#182;</a> </div> <p>This is demonstration model code, as used in test suites.</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">&#182;</a> </div> <p>A house constructor.</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">function</span> <span class="nx">House</span><span class="p">(</span><span class="nx">address</span><span class="p">)</span> <span class="p">{</span>
<span class="k">this</span><span class="p">.</span><span class="nx">address</span> <span class="o">=</span> <span class="nx">address</span><span class="p">;</span>
- </pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p>Managed objects need to be registered - this causes them to be persisted.
+ </pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p>Managed objects need to be registered - this causes them to be persisted
+whenever they are modified.
Registering in the constructor is just convenient, not required.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">process</span><span class="p">.</span><span class="nx">db</span><span class="p">.</span><span class="nx">registerInstance</span><span class="p">(</span><span class="k">this</span><span class="p">);</span>
<span class="p">}</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">&#182;</a> </div> <p>A pointless method, just because.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">House</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">logConstructionMaterial</span> <span class="o">=</span> <span class="kd">function</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="k">this</span><span class="p">.</span><span class="nx">address</span> <span class="o">+</span> <span class="s1">&#39; is made of stone&#39;</span><span class="p">);</span>
View
42 docs/disk-io.html
@@ -1,15 +1,39 @@
<!DOCTYPE html> <html> <head> <title>disk-io.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <div id="jump_to"> Jump To &hellip; <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="barricane-db.html"> barricane-db.js </a> <a class="source" href="bdb-disk-manager.html"> bdb-disk-manager.js </a> <a class="source" href="db.html"> db.js </a> <a class="source" href="disk-io.html"> disk-io.js </a> <a class="source" href="old-barricane-db.html"> old-barricane-db.js </a> <a class="source" href="serialisation.html"> serialisation.js </a> <a class="source" href="swoj-codec.html"> swoj-codec.js </a> <a class="source" href="util.html"> util.js </a> <a class="source" href="app-test.html"> app-test.js </a> <a class="source" href="demo-model.html"> demo-model.js </a> <a class="source" href="hello-world-test.html"> hello-world-test.js </a> <a class="source" href="mock.html"> mock.js </a> <a class="source" href="old-disk-manager.html"> old-disk-manager.js </a> <a class="source" href="old-serialisation.html"> old-serialisation.js </a> <a class="source" href="swoj-codec-test.html"> swoj-codec-test.js </a> </div> </div> </div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> disk-io.js </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">&#182;</a> </div> <p>Copyright (c) 2010 Barricane Technology Ltd., All Rights Reserved.
/ Released under the MIT open source licence.</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">&#182;</a> </div> <p>This module contains the DiskIO code.</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">&#182;</a> </div> <p>The DiskIO constructor creates an object which can be used by DB to interact
with the outside world. Other IOs will be possible in future, including
-ReplicationIO.</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">function</span> <span class="nx">DiskIO</span><span class="p">(</span><span class="nx">options</span><span class="p">)</span> <span class="p">{</span>
- <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">options</span><span class="p">)</span> <span class="p">{</span>
- <span class="nx">options</span> <span class="o">=</span> <span class="p">{};</span>
- <span class="p">}</span>
- <span class="k">this</span><span class="p">.</span><span class="nx">path</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">path</span> <span class="o">||</span> <span class="s1">&#39;/tmp/barricane.db&#39;</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p>Writes must be counted, so we only close the file whwn all has been
+ReplicationIO.</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">var</span> <span class="nx">codec</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;./swoj-codec&#39;</span><span class="p">)</span>
+ <span class="p">,</span> <span class="nx">fs</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;fs&#39;</span><span class="p">)</span>
+ <span class="p">;</span>
+
+<span class="kd">function</span> <span class="nx">DiskIO</span><span class="p">(</span><span class="nx">options</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">options</span> <span class="o">=</span> <span class="nx">options</span> <span class="o">||</span> <span class="p">{};</span>
+
+ <span class="k">this</span><span class="p">.</span><span class="nx">path</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">path</span> <span class="o">||</span> <span class="s1">&#39;/tmp/barricane.db&#39;</span><span class="p">;</span>
+ <span class="k">this</span><span class="p">.</span><span class="nx">codec</span> <span class="o">=</span> <span class="nx">options</span><span class="p">.</span><span class="nx">codec</span> <span class="o">||</span> <span class="k">new</span> <span class="nx">codec</span><span class="p">.</span><span class="nx">Codec</span><span class="p">(</span><span class="nx">options</span><span class="p">.</span><span class="nx">magic</span><span class="p">);</span>
+
+ <span class="k">this</span><span class="p">.</span><span class="nx">writeStream</span> <span class="o">=</span> <span class="kc">null</span><span class="p">;</span>
+ </pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p>Writes must be counted, so we only close the file when all has been
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>
-<span class="p">}</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">&#182;</a> </div> <p>This writes the serialise 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">write</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">serialisedObject</span><span class="p">)</span> <span class="p">{</span>
-<span class="p">};</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">&#182;</a> </div> <p>Read serialises objects back from disk, and
-<code>callback(err, result)</code> for each one.</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">read</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">callback</span><span class="p">)</span> <span class="p">{</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>Export the symbols.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">exports</span><span class="p">.</span><span class="nx">DiskIO</span> <span class="o">=</span> <span class="nx">DiskIO</span><span class="p">;</span>
+ </pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">&#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-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">&#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">write</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="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="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">ending</span><span class="p">(</span><span class="kc">false</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">);</span>
+ <span class="p">}</span>
+ <span class="p">});</span>
+<span class="p">};</span>
+
+<span class="nx">DiskIO</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">openSync</span> <span class="o">=</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</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>This deletes any database files.</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">deleteSync</span> <span class="o">=</span> <span class="kd">function</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>First get the contents of the path.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">contents</span> <span class="o">=</span> <span class="nx">fs</span><span class="p">.</span><span class="nx">readdirSync</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">path</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="s1">&#39;contents:&#39;</span><span class="p">,</span> <span class="nx">contents</span><span class="p">);</span></pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-9">&#182;</a> </div> <p>&lt;-- CONTINUE</p> </td> <td class="code"> <div class="highlight"><pre><span class="p">}</span>
+
+<span class="nx">DiskIO</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">end</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">callback</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">this</span><span class="p">.</span><span class="nx">ending</span> <span class="o">=</span> <span class="nx">callback</span><span class="p">;</span>
+<span class="p">}</span></pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-10">&#182;</a> </div> <p>Export the symbols.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">exports</span><span class="p">.</span><span class="nx">DiskIO</span> <span class="o">=</span> <span class="nx">DiskIO</span><span class="p">;</span>
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
View
79 docs/swoj-codec-test.html
@@ -15,7 +15,7 @@
<span class="nx">wilma</span><span class="p">.</span><span class="nx">spouse</span> <span class="o">=</span> <span class="nx">fred</span><span class="p">;</span>
-<span class="kd">var</span> <span class="nx">codec</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">swoj</span><span class="p">.</span><span class="nx">Codec</span><span class="p">(</span><span class="s2">&quot;__&quot;</span><span class="p">);</span>
+<span class="kd">var</span> <span class="nx">codec</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">swoj</span><span class="p">.</span><span class="nx">Codec</span><span class="p">();</span>
<span class="kd">var</span> <span class="nx">suite</span> <span class="o">=</span> <span class="nx">vows</span><span class="p">.</span><span class="nx">describe</span><span class="p">(</span><span class="s1">&#39;Codec&#39;</span><span class="p">).</span><span class="nx">addBatch</span><span class="p">(</span>
<span class="p">{</span> <span class="s2">&quot;serialise house&quot;</span>
@@ -28,13 +28,13 @@
<span class="o">:</span> <span class="p">{</span> <span class="nx">topic</span>
<span class="o">:</span> <span class="nx">fred</span>
<span class="p">,</span> <span class="s2">&quot;serialise&quot;</span>
- <span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">topic</span><span class="p">)</span> <span class="p">{</span> <span class="nx">assert</span><span class="p">.</span><span class="nx">deepEqual</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">topic</span><span class="p">),</span> <span class="s1">&#39;{&quot;__constructor__&quot;:&quot;Person&quot;,&quot;personalName&quot;:&quot;Fred&quot;,&quot;familyName&quot;:&quot;Flintstone&quot;,&quot;spouse&quot;:&quot;__UUID__:2&quot;,&quot;house&quot;:&quot;__UUID__:0&quot;,&quot;__uuid__&quot;:&quot;1&quot;}&#39;</span><span class="p">);</span> <span class="p">}</span>
+ <span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">topic</span><span class="p">)</span> <span class="p">{</span> <span class="nx">assert</span><span class="p">.</span><span class="nx">deepEqual</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">topic</span><span class="p">),</span> <span class="s1">&#39;{&quot;__constructor__&quot;:&quot;Person&quot;,&quot;personalName&quot;:&quot;Fred&quot;,&quot;familyName&quot;:&quot;Flintstone&quot;,&quot;spouse&quot;:&quot;__uuid__2&quot;,&quot;house&quot;:&quot;__uuid__0&quot;,&quot;__uuid__&quot;:&quot;1&quot;}&#39;</span><span class="p">);</span> <span class="p">}</span>
<span class="p">}</span>
<span class="p">,</span> <span class="s2">&quot;serialise wilma&quot;</span>
<span class="o">:</span> <span class="p">{</span> <span class="nx">topic</span>
<span class="o">:</span> <span class="nx">wilma</span>
<span class="p">,</span> <span class="s2">&quot;serialise&quot;</span>
- <span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">topic</span><span class="p">)</span> <span class="p">{</span> <span class="nx">assert</span><span class="p">.</span><span class="nx">deepEqual</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">topic</span><span class="p">),</span> <span class="s1">&#39;{&quot;__constructor__&quot;:&quot;Person&quot;,&quot;personalName&quot;:&quot;Wilma&quot;,&quot;familyName&quot;:&quot;Flintstone&quot;,&quot;spouse&quot;:&quot;__UUID__:1&quot;,&quot;house&quot;:&quot;__UUID__:0&quot;,&quot;__uuid__&quot;:&quot;2&quot;}&#39;</span><span class="p">);</span> <span class="p">}</span>
+ <span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">topic</span><span class="p">)</span> <span class="p">{</span> <span class="nx">assert</span><span class="p">.</span><span class="nx">deepEqual</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">topic</span><span class="p">),</span> <span class="s1">&#39;{&quot;__constructor__&quot;:&quot;Person&quot;,&quot;personalName&quot;:&quot;Wilma&quot;,&quot;familyName&quot;:&quot;Flintstone&quot;,&quot;spouse&quot;:&quot;__uuid__1&quot;,&quot;house&quot;:&quot;__uuid__0&quot;,&quot;__uuid__&quot;:&quot;2&quot;}&#39;</span><span class="p">);</span> <span class="p">}</span>
<span class="p">}</span>
<span class="p">,</span> <span class="s2">&quot;deserialise house&quot;</span>
<span class="o">:</span> <span class="p">{</span> <span class="nx">topic</span>
@@ -42,28 +42,55 @@
<span class="p">,</span> <span class="s2">&quot;deserialise&quot;</span>
<span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">topic</span><span class="p">)</span> <span class="p">{</span> <span class="nx">assert</span><span class="p">.</span><span class="nx">deepEqual</span><span class="p">(</span><span class="nx">codec</span><span class="p">.</span><span class="nx">deserialise</span><span class="p">(</span><span class="nx">topic</span><span class="p">,</span> <span class="p">{</span><span class="s2">&quot;House&quot;</span><span class="o">:</span> <span class="nx">model</span><span class="p">.</span><span class="nx">House</span><span class="p">,</span> <span class="s2">&quot;Person&quot;</span><span class="o">:</span> <span class="nx">model</span><span class="p">.</span><span class="nx">Person</span><span class="p">}),</span> <span class="nx">house</span><span class="p">)</span> <span class="p">}</span>
<span class="p">}</span>
- <span class="cm">/*</span>
-<span class="cm"> , &quot;deserialise all&quot;</span>
-<span class="cm"> : { topic</span>
-<span class="cm"> : function() {</span>
-<span class="cm"> var instances = {};</span>
-<span class="cm"> var ob0 = codec.deserialise(&#39;{&quot;__constructor__&quot;:&quot;House&quot;,&quot;address&quot;:&quot;301 Cobblestone Wy., Bedrock, 70777&quot;,&quot;__uuid__&quot;:&quot;0&quot;}&#39;);</span>
-<span class="cm"> instances[ob0.__uuid__] = ob0;</span>
-<span class="cm"> var ob1 = codec.deserialise(&#39;{&quot;__constructor__&quot;:&quot;Person&quot;,&quot;personalName&quot;:&quot;Fred&quot;,&quot;familyName&quot;:&quot;Flintstone&quot;,&quot;spouse&quot;:&quot;__UUID__:2&quot;,&quot;house&quot;:&quot;__UUID__:0&quot;,&quot;__uuid__&quot;:&quot;1&quot;}&#39;);</span>
-<span class="cm"> instances[ob1.__uuid__] = ob1;</span>
-<span class="cm"> var ob2 = codec.deserialise(&#39;{&quot;__constructor__&quot;:&quot;Person&quot;,&quot;personalName&quot;:&quot;Wilma&quot;,&quot;familyName&quot;:&quot;Flintstone&quot;,&quot;spouse&quot;:&quot;__UUID__:1&quot;,&quot;house&quot;:&quot;__UUID__:0&quot;,&quot;__uuid__&quot;:&quot;2&quot;}&#39;);</span>
-<span class="cm"> instances[ob2.__uuid__] = ob2;</span>
-<span class="cm"> codec.fixRefs(instances);</span>
-<span class="cm"> return instances;</span>
-<span class="cm"> }</span>
-<span class="cm"> , &#39;deserialise&#39;</span>
-<span class="cm"> : function(topic) {</span>
-
-<span class="cm">//DIVIDER</span>
-<span class="cm"> }</span>
-<span class="cm"> }</span>
-<span class="cm"> */</span>
+ <span class="p">,</span> <span class="s2">&quot;deserialise all&quot;</span>
+ <span class="o">:</span> <span class="p">{</span> <span class="nx">topic</span>
+ <span class="o">:</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
+ <span class="kd">var</span> <span class="nx">instances</span> <span class="o">=</span> <span class="p">{};</span>
+ <span class="kd">var</span> <span class="nx">constructors</span> <span class="o">=</span> <span class="p">{</span> <span class="s1">&#39;House&#39;</span><span class="o">:</span> <span class="nx">model</span><span class="p">.</span><span class="nx">House</span><span class="p">,</span> <span class="s1">&#39;Person&#39;</span><span class="o">:</span> <span class="nx">model</span><span class="p">.</span><span class="nx">Person</span> <span class="p">};</span>
+ <span class="kd">var</span> <span class="nx">ob0</span> <span class="o">=</span> <span class="nx">codec</span><span class="p">.</span><span class="nx">deserialise</span><span class="p">(</span><span class="s1">&#39;{&quot;__constructor__&quot;:&quot;House&quot;,&quot;address&quot;:&quot;301 Cobblestone Wy., Bedrock, 70777&quot;,&quot;__uuid__&quot;:&quot;0&quot;}&#39;</span><span class="p">,</span> <span class="nx">constructors</span><span class="p">);</span>
+ <span class="nx">instances</span><span class="p">[</span><span class="nx">ob0</span><span class="p">.</span><span class="nx">__uuid__</span><span class="p">]</span> <span class="o">=</span> <span class="nx">ob0</span><span class="p">;</span>
+ <span class="kd">var</span> <span class="nx">ob1</span> <span class="o">=</span> <span class="nx">codec</span><span class="p">.</span><span class="nx">deserialise</span><span class="p">(</span><span class="s1">&#39;{&quot;__constructor__&quot;:&quot;Person&quot;,&quot;personalName&quot;:&quot;Fred&quot;,&quot;familyName&quot;:&quot;Flintstone&quot;,&quot;spouse&quot;:&quot;__uuid__2&quot;,&quot;house&quot;:&quot;__uuid__0&quot;,&quot;__uuid__&quot;:&quot;1&quot;}&#39;</span><span class="p">,</span> <span class="nx">constructors</span><span class="p">);</span>
+ <span class="nx">instances</span><span class="p">[</span><span class="nx">ob1</span><span class="p">.</span><span class="nx">__uuid__</span><span class="p">]</span> <span class="o">=</span> <span class="nx">ob1</span><span class="p">;</span>
+ <span class="kd">var</span> <span class="nx">ob2</span> <span class="o">=</span> <span class="nx">codec</span><span class="p">.</span><span class="nx">deserialise</span><span class="p">(</span><span class="s1">&#39;{&quot;__constructor__&quot;:&quot;Person&quot;,&quot;personalName&quot;:&quot;Wilma&quot;,&quot;familyName&quot;:&quot;Flintstone&quot;,&quot;spouse&quot;:&quot;__uuid__1&quot;,&quot;house&quot;:&quot;__uuid__0&quot;,&quot;__uuid__&quot;:&quot;2&quot;}&#39;</span><span class="p">,</span> <span class="nx">constructors</span><span class="p">);</span>
+ <span class="nx">instances</span><span class="p">[</span><span class="nx">ob2</span><span class="p">.</span><span class="nx">__uuid__</span><span class="p">]</span> <span class="o">=</span> <span class="nx">ob2</span><span class="p">;</span>
+ <span class="nx">codec</span><span class="p">.</span><span class="nx">fixRefs</span><span class="p">(</span><span class="nx">instances</span><span class="p">);</span>
+ <span class="k">return</span> <span class="nx">instances</span><span class="p">;</span>
+ <span class="p">}</span>
+ <span class="p">,</span> <span class="s1">&#39;check house&#39;</span>
+ <span class="o">:</span> <span class="p">{</span> <span class="nx">topic</span>
+ <span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">topic</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">&#182;</a> </div> <p>console.log("a", JSON.stringify(arguments));</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="nx">topic</span><span class="p">[</span><span class="s1">&#39;0&#39;</span><span class="p">];</span> <span class="c1">// house.__uuid__</span>
+ <span class="p">}</span>
+ <span class="p">,</span> <span class="s1">&#39;address&#39;</span>
+ <span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">topic</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">&#182;</a> </div> <p>console.log("b", JSON.stringify(arguments));</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">assert</span><span class="p">.</span><span class="nx">equal</span><span class="p">(</span><span class="nx">topic</span><span class="p">.</span><span class="nx">address</span><span class="p">,</span> <span class="s1">&#39;301 Cobblestone Wy., Bedrock, 70777&#39;</span><span class="p">);</span>
+ <span class="p">}</span>
+ <span class="p">}</span>
+ <span class="p">,</span> <span class="s1">&#39;check Wilma&#39;</span>
+ <span class="o">:</span> <span class="p">{</span> <span class="nx">topic</span>
+ <span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">topic</span><span class="p">)</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>console.log("a", JSON.stringify(arguments));</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">return</span> <span class="nx">topic</span><span class="p">[</span><span class="s1">&#39;2&#39;</span><span class="p">];</span> <span class="c1">// wilma.__uuid__</span>
+ <span class="p">}</span>
+ <span class="p">,</span> <span class="s1">&#39;personalName&#39;</span>
+ <span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">topic</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">assert</span><span class="p">.</span><span class="nx">equal</span><span class="p">(</span><span class="nx">topic</span><span class="p">.</span><span class="nx">personalName</span><span class="p">,</span> <span class="s1">&#39;Wilma&#39;</span><span class="p">);</span>
+ <span class="p">}</span>
+ <span class="p">,</span> <span class="s1">&#39;familyName&#39;</span>
+ <span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">topic</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">assert</span><span class="p">.</span><span class="nx">equal</span><span class="p">(</span><span class="nx">topic</span><span class="p">.</span><span class="nx">familyName</span><span class="p">,</span> <span class="s1">&#39;Flintstone&#39;</span><span class="p">);</span>
+ <span class="p">}</span>
+ <span class="p">,</span> <span class="s1">&#39;spouse.personalName&#39;</span>
+ <span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">topic</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">assert</span><span class="p">.</span><span class="nx">equal</span><span class="p">(</span><span class="nx">topic</span><span class="p">.</span><span class="nx">spouse</span><span class="p">.</span><span class="nx">personalName</span><span class="p">,</span> <span class="s1">&#39;Fred&#39;</span><span class="p">);</span>
+ <span class="p">}</span>
+ <span class="p">,</span> <span class="s1">&#39;spouse.spouse.personalName&#39;</span>
+ <span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">topic</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">assert</span><span class="p">.</span><span class="nx">equal</span><span class="p">(</span><span class="nx">topic</span><span class="p">.</span><span class="nx">spouse</span><span class="p">.</span><span class="nx">spouse</span><span class="p">.</span><span class="nx">personalName</span><span class="p">,</span> <span class="s1">&#39;Wilma&#39;</span><span class="p">);</span>
+ <span class="p">}</span>
+ <span class="p">,</span> <span class="s1">&#39;house.address&#39;</span>
+ <span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">topic</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">assert</span><span class="p">.</span><span class="nx">equal</span><span class="p">(</span><span class="nx">topic</span><span class="p">.</span><span class="nx">house</span><span class="p">.</span><span class="nx">address</span><span class="p">,</span> <span class="s1">&#39;301 Cobblestone Wy., Bedrock, 70777&#39;</span><span class="p">);</span>
+ <span class="p">}</span>
+ <span class="p">}</span>
+ <span class="p">}</span>
<span class="p">}</span>
-<span class="p">)</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">&#182;</a> </div> <p>TODO add some nested topics here</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">suite</span><span class="p">.</span><span class="kr">export</span><span class="p">(</span><span class="nx">module</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>TODO test unserialisation</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">suite</span><span class="p">.</span><span class="kr">export</span><span class="p">(</span><span class="nx">module</span><span class="p">);</span>
-</pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">&#182;</a> </div> <p>TODO test unserialisation</p> </td> <td class="code"> <div class="highlight"><pre>undefined</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
+</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
View
58 docs/swoj-codec.html
@@ -9,59 +9,71 @@
codecs require.</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">var</span> <span class="nx">util</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="s1">&#39;./util&#39;</span><span class="p">);</span>
<span class="kd">function</span> <span class="nx">SwojCodec</span><span class="p">(</span><span class="nx">magic</span><span class="p">)</span> <span class="p">{</span>
- <span class="k">this</span><span class="p">.</span><span class="nx">magic</span> <span class="o">=</span> <span class="nx">magic</span><span class="p">;</span>
+ <span class="k">this</span><span class="p">.</span><span class="nx">magic</span> <span class="o">=</span> <span class="nx">magic</span> <span class="o">||</span> <span class="s1">&#39;__&#39;</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">&#182;</a> </div> <p>Set up some almost constants.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">this</span><span class="p">.</span><span class="nx">CONSTRUCTOR_TAG</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">magic</span> <span class="o">+</span> <span class="s1">&#39;constructor&#39;</span> <span class="o">+</span> <span class="k">this</span><span class="p">.</span><span class="nx">magic</span><span class="p">;</span>
+ <span class="k">this</span><span class="p">.</span><span class="nx">UUID_TAG</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">magic</span> <span class="o">+</span> <span class="s1">&#39;uuid&#39;</span> <span class="o">+</span> <span class="k">this</span><span class="p">.</span><span class="nx">magic</span><span class="p">;</span>
+ <span class="k">this</span><span class="p">.</span><span class="nx">DATE_TAG</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">magic</span> <span class="o">+</span> <span class="s1">&#39;date&#39;</span> <span class="o">+</span> <span class="k">this</span><span class="p">.</span><span class="nx">magic</span><span class="p">;</span>
<span class="p">}</span>
<span class="nx">SwojCodec</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">name</span> <span class="o">=</span> <span class="s1">&#39;swoj&#39;</span><span class="p">;</span>
<span class="nx">SwojCodec</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">version</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="nx">SwojCodec</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">defaultFileExtension</span> <span class="o">=</span> <span class="s1">&#39;swoj&#39;</span><span class="p">;</span>
-<span class="nx">SwojCodec</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">encoding</span> <span class="o">=</span> <span class="s1">&#39;utf&#39;</span><span class="p">;</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">&#182;</a> </div> <p>This takes a single instance and returns a representation ready to be written
+<span class="nx">SwojCodec</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">encoding</span> <span class="o">=</span> <span class="s1">&#39;utf&#39;</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>This takes a single instance and returns a representation ready to be written
to disk.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">SwojCodec</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">serialise</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="kd">var</span> <span class="nx">tmp</span> <span class="o">=</span> <span class="p">{};</span>
- <span class="nx">tmp</span><span class="p">[</span><span class="k">this</span><span class="p">.</span><span class="nx">wrap</span><span class="p">(</span><span class="s2">&quot;constructor&quot;</span><span class="p">)]</span> <span class="o">=</span> <span class="nx">instance</span><span class="p">.</span><span class="nx">constructor</span><span class="p">.</span><span class="nx">name</span><span class="p">;</span>
+ <span class="nx">tmp</span><span class="p">[</span><span class="k">this</span><span class="p">.</span><span class="nx">CONSTRUCTOR_TAG</span><span class="p">]</span> <span class="o">=</span> <span class="nx">instance</span><span class="p">.</span><span class="nx">constructor</span><span class="p">.</span><span class="nx">name</span><span class="p">;</span>
<span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">p</span> <span class="k">in</span> <span class="nx">instance</span><span class="p">)</span> <span class="p">{</span>
- <span class="k">if</span> <span class="p">(</span><span class="nx">instance</span><span class="p">.</span><span class="nx">hasOwnProperty</span><span class="p">(</span><span class="nx">p</span><span class="p">))</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>Is this property a registered instance</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span> <span class="nx">instance</span><span class="p">[</span><span class="nx">p</span><span class="p">]</span> <span class="o">!==</span> <span class="kc">undefined</span>
+ <span class="k">if</span> <span class="p">(</span><span class="nx">instance</span><span class="p">.</span><span class="nx">hasOwnProperty</span><span class="p">(</span><span class="nx">p</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>Is this property a registered instance</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span> <span class="nx">instance</span><span class="p">[</span><span class="nx">p</span><span class="p">]</span> <span class="o">!==</span> <span class="kc">undefined</span>
<span class="o">&amp;&amp;</span> <span class="nx">instance</span><span class="p">[</span><span class="nx">p</span><span class="p">]</span> <span class="o">!==</span> <span class="kc">null</span>
- <span class="o">&amp;&amp;</span> <span class="nx">instance</span><span class="p">[</span><span class="nx">p</span><span class="p">].</span><span class="nx">hasOwnProperty</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">wrap</span><span class="p">(</span><span class="s2">&quot;uuid&quot;</span><span class="p">))</span>
+ <span class="o">&amp;&amp;</span> <span class="nx">instance</span><span class="p">[</span><span class="nx">p</span><span class="p">].</span><span class="nx">hasOwnProperty</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="p">)</span> <span class="p">{</span>
- <span class="nx">tmp</span><span class="p">[</span><span class="nx">p</span><span class="p">]</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">wrap</span><span class="p">(</span><span class="s2">&quot;UUID&quot;</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot;:&quot;</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="k">this</span><span class="p">.</span><span class="nx">wrap</span><span class="p">(</span><span class="s2">&quot;uuid&quot;</span><span class="p">)];</span>
- <span class="p">}</span> <span class="k">else</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>Treat dates carefully.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">instance</span><span class="p">[</span><span class="nx">p</span><span class="p">]</span> <span class="k">instanceof</span> <span class="nb">Date</span><span class="p">)</span> <span class="p">{</span>
- <span class="nx">tmp</span><span class="p">[</span><span class="nx">p</span><span class="p">]</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">wrap</span><span class="p">(</span><span class="s2">&quot;DATE&quot;</span><span class="p">)</span> <span class="o">+</span> <span class="s2">&quot;:&quot;</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">getTime</span><span class="p">();</span>
- <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
- <span class="nx">tmp</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">tmp</span><span class="p">[</span><span class="nx">p</span><span class="p">]</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">UUID_TAG</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="k">this</span><span class="p">.</span><span class="nx">UUID_TAG</span><span class="p">];</span>
+ <span class="p">}</span> <span class="k">else</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-9">&#182;</a> </div> <p>Treat dates carefully.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="nx">instance</span><span class="p">[</span><span class="nx">p</span><span class="p">]</span> <span class="k">instanceof</span> <span class="nb">Date</span><span class="p">)</span> <span class="p">{</span>
+ <span class="nx">tmp</span><span class="p">[</span><span class="nx">p</span><span class="p">]</span> <span class="o">=</span> <span class="k">this</span><span class="p">.</span><span class="nx">DATE_TAG</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">getTime</span><span class="p">();</span>
+ <span class="p">}</span> <span class="k">else</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-10">&#182;</a> </div> <p>TODO: Add escaping of magic within values.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="nx">tmp</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="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
+
<span class="k">return</span> <span class="nx">JSON</span><span class="p">.</span><span class="nx">stringify</span><span class="p">(</span><span class="nx">tmp</span><span class="p">);</span>
-<span class="p">}</span></pre></div> </td> </tr> <tr id="section-9"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-9">&#182;</a> </div> <p>This takes a JSON string, and returns an object.
+<span class="p">}</span></pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-11">&#182;</a> </div> <p>This takes a JSON string, and returns an object.
The real work is done by fixLinks, the objects </p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">SwojCodec</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">deserialise</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">string</span><span class="p">,</span> <span class="nx">constructors</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">tmp</span> <span class="o">=</span> <span class="nx">JSON</span><span class="p">.</span><span class="nx">parse</span><span class="p">(</span><span class="nx">string</span><span class="p">);</span>
- </pre></div> </td> </tr> <tr id="section-10"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-10">&#182;</a> </div> <p>Find the constructor.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">Constructor</span> <span class="o">=</span> <span class="nx">constructors</span><span class="p">[</span><span class="nx">tmp</span><span class="p">[</span><span class="k">this</span><span class="p">.</span><span class="nx">wrap</span><span class="p">(</span><span class="s2">&quot;constructor&quot;</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;Constructor:&quot;</span><span class="p">,</span> <span class="nx">Constructor</span><span class="p">);</span>
- </pre></div> </td> </tr> <tr id="section-11"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-11">&#182;</a> </div> <p>And the UUID.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">uuid</span> <span class="o">=</span> <span class="nx">tmp</span><span class="p">[</span><span class="k">this</span><span class="p">.</span><span class="nx">wrap</span><span class="p">(</span><span class="s2">&quot;uuid&quot;</span><span class="p">)];</span>
- </pre></div> </td> </tr> <tr id="section-12"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-12">&#182;</a> </div> <p>Create the object - see <a href="http://stackoverflow.com/questions/3362471/how-can-i-call-a-javascript-constructor-using-call-or-apply">Stack Overflow</a>
-for a detailed explanation.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">Tmp</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(){};</span> <span class="c1">// temporary constructor</span>
+ </pre></div> </td> </tr> <tr id="section-12"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-12">&#182;</a> </div> <p>Find the constructor.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">Constructor</span> <span class="o">=</span> <span class="nx">constructors</span><span class="p">[</span><span class="nx">tmp</span><span class="p">[</span><span class="k">this</span><span class="p">.</span><span class="nx">CONSTRUCTOR_TAG</span><span class="p">]];</span>
+ </pre></div> </td> </tr> <tr id="section-13"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-13">&#182;</a> </div> <p>And the UUID.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">uuid</span> <span class="o">=</span> <span class="nx">tmp</span><span class="p">[</span><span class="k">this</span><span class="p">.</span><span class="nx">UUID_TAG</span><span class="p">];</span>
+ </pre></div> </td> </tr> <tr id="section-14"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-14">&#182;</a> </div> <p>Create the object - see <a href="http://stackoverflow.com/questions/3362471/how-can-i-call-a-javascript-constructor-using-call-or-apply">Stack Overflow</a>
+for a detailed explanation of how to call a constructor without 'new'.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">Tmp</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(){};</span> <span class="c1">// temporary constructor</span>
<span class="kd">var</span> <span class="nx">inst</span><span class="p">,</span> <span class="nx">ret</span><span class="p">;</span> <span class="c1">// instance and temporary return value</span>
<span class="kd">var</span> <span class="nx">args</span> <span class="o">=</span> <span class="p">[];</span>
<span class="nx">Tmp</span><span class="p">.</span><span class="nx">prototype</span> <span class="o">=</span> <span class="nx">Constructor</span><span class="p">.</span><span class="nx">prototype</span><span class="p">;</span>
<span class="nx">inst</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Tmp</span><span class="p">;</span>
<span class="nx">ret</span> <span class="o">=</span> <span class="nx">Constructor</span><span class="p">.</span><span class="nx">apply</span><span class="p">(</span><span class="nx">inst</span><span class="p">,</span> <span class="nx">args</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">ob</span> <span class="o">=</span> <span class="nb">Object</span><span class="p">(</span><span class="nx">ret</span><span class="p">)</span> <span class="o">===</span> <span class="nx">ret</span> <span class="o">?</span> <span class="nx">ret</span> <span class="o">:</span> <span class="nx">inst</span><span class="p">;</span>
- <span class="nx">ob</span><span class="p">[</span><span class="k">this</span><span class="p">.</span><span class="nx">wrap</span><span class="p">(</span><span class="s2">&quot;uuid&quot;</span><span class="p">)]</span> <span class="o">=</span> <span class="nx">uuid</span><span class="p">;</span> <span class="c1">// Fix UUID.</span>
+ <span class="nx">ob</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">uuid</span><span class="p">;</span> <span class="c1">// Fix UUID.</span>
<span class="nx">util</span><span class="p">.</span><span class="nx">ownRealKeys</span><span class="p">(</span><span class="nx">tmp</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">key</span><span class="p">,</span> <span class="nx">i</span><span class="p">,</span> <span class="nx">keys</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">ob</span><span class="p">[</span><span class="nx">key</span><span class="p">]</span> <span class="o">=</span> <span class="nx">tmp</span><span class="p">[</span><span class="nx">key</span><span class="p">];</span>
<span class="p">});</span>
<span class="k">return</span> <span class="nx">ob</span><span class="p">;</span>
-<span class="p">}</span></pre></div> </td> </tr> <tr id="section-13"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-13">&#182;</a> </div> <p>This replaces up any remaining values of <strong>UUID</strong>:... with references to the
+<span class="p">}</span></pre></div> </td> </tr> <tr id="section-15"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-15">&#182;</a> </div> <p>This replaces up any remaining values of <strong>UUID</strong>:... with references to the
correct object, and also handles dates, etc.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">SwojCodec</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">fixRefs</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">instances</span><span class="p">)</span> <span class="p">{</span>
-<span class="p">}</span>
-
-<span class="nx">SwojCodec</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">wrap</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">string</span><span class="p">)</span> <span class="p">{</span>
- <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">magic</span> <span class="o">+</span> <span class="nx">string</span> <span class="o">+</span> <span class="k">this</span><span class="p">.</span><span class="nx">magic</span><span class="p">;</span>
-<span class="p">}</span></pre></div> </td> </tr> <tr id="section-14"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-14">&#182;</a> </div> <p>Anonomise the exported name, so that codec can be used interchangeably, </p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">exports</span><span class="p">.</span><span class="nx">Codec</span> <span class="o">=</span> <span class="nx">SwojCodec</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="nx">util</span><span class="p">.</span><span class="nx">ownKeys</span><span class="p">(</span><span class="nx">instances</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">uuid</span><span class="p">,</span> <span class="nx">i</span><span class="p">,</span> <span class="nx">uuids</span><span class="p">)</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">instances</span><span class="p">[</span><span class="nx">uuid</span><span class="p">],</span> <span class="nx">that</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">key</span><span class="p">,</span> <span class="nx">i</span><span class="p">,</span> <span class="nx">keys</span><span class="p">)</span> <span class="p">{</span>
+ <span class="kd">var</span> <span class="nx">value</span> <span class="o">=</span> <span class="nx">instances</span><span class="p">[</span><span class="nx">uuid</span><span class="p">][</span><span class="nx">key</span><span class="p">];</span></pre></div> </td> </tr> <tr id="section-16"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-16">&#182;</a> </div> <p>TODO: Add escaping of magic within values.</p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span><span class="k">typeof</span> <span class="nx">value</span> <span class="o">==</span> <span class="s1">&#39;string&#39;</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">if</span> <span class="p">(</span><span class="nx">value</span><span class="p">.</span><span class="nx">startsWith</span><span class="p">(</span><span class="nx">that</span><span class="p">.</span><span class="nx">UUID_TAG</span><span class="p">))</span> <span class="p">{</span>
+ <span class="nx">instances</span><span class="p">[</span><span class="nx">uuid</span><span class="p">][</span><span class="nx">key</span><span class="p">]</span> <span class="o">=</span> <span class="nx">instances</span><span class="p">[</span><span class="nx">value</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span>
+ <span class="nx">that</span><span class="p">.</span><span class="nx">UUID_TAG</span><span class="p">.</span><span class="nx">length</span><span class="p">)];</span>
+ <span class="p">}</span>
+ <span class="k">if</span> <span class="p">(</span><span class="nx">value</span><span class="p">.</span><span class="nx">startsWith</span><span class="p">(</span><span class="nx">that</span><span class="p">.</span><span class="nx">DATE_TAG</span><span class="p">))</span> <span class="p">{</span>
+ <span class="nx">instances</span><span class="p">[</span><span class="nx">uuid</span><span class="p">][</span><span class="nx">key</span><span class="p">]</span> <span class="o">=</span> <span class="k">new</span> <span class="nb">Date</span><span class="p">(</span>
+ <span class="nb">parseInt</span><span class="p">(</span><span class="nx">value</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="nx">that</span><span class="p">.</span><span class="nx">DATE_TAG</span><span class="p">.</span><span class="nx">length</span><span class="p">),</span><span class="mi">10</span><span class="p">));</span>
+ <span class="p">}</span>
+ <span class="p">}</span>
+ <span class="p">});</span>
+ <span class="p">});</span>
+<span class="p">}</span></pre></div> </td> </tr> <tr id="section-17"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-17">&#182;</a> </div> <p>Anonomise the exported name, so that codecs can be used interchangeably, </p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">exports</span><span class="p">.</span><span class="nx">Codec</span> <span class="o">=</span> <span class="nx">SwojCodec</span><span class="p">;</span>
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
View
16 docs/util.html
@@ -1,8 +1,15 @@
<!DOCTYPE html> <html> <head> <title>util.js</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <div id="jump_to"> Jump To &hellip; <div id="jump_wrapper"> <div id="jump_page"> <a class="source" href="barricane-db.html"> barricane-db.js </a> <a class="source" href="bdb-disk-manager.html"> bdb-disk-manager.js </a> <a class="source" href="db.html"> db.js </a> <a class="source" href="disk-io.html"> disk-io.js </a> <a class="source" href="old-barricane-db.html"> old-barricane-db.js </a> <a class="source" href="serialisation.html"> serialisation.js </a> <a class="source" href="swoj-codec.html"> swoj-codec.js </a> <a class="source" href="util.html"> util.js </a> <a class="source" href="app-test.html"> app-test.js </a> <a class="source" href="demo-model.html"> demo-model.js </a> <a class="source" href="hello-world-test.html"> hello-world-test.js </a> <a class="source" href="mock.html"> mock.js </a> <a class="source" href="old-disk-manager.html"> old-disk-manager.js </a> <a class="source" href="old-serialisation.html"> old-serialisation.js </a> <a class="source" href="swoj-codec-test.html"> swoj-codec-test.js </a> </div> </div> </div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> util.js </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">&#182;</a> </div> <p>Copyright (c) 2010 Barricane Technology Ltd., All Rights Reserved.
Released under the MIT open source licence.</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">&#182;</a> </div> <p>This is the obligatory dumping ground modules for all those bits which don't
-fit well elsewhere.</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">&#182;</a> </div> <p>This finds all the objects keys, which do not contain the magic marker.</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">function</span> <span class="nx">ownRealKeys</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">magic</span><span class="p">)</span> <span class="p">{</span>
+fit well elsewhere.</p> </td> <td class="code"> <div class="highlight"><pre></pre></div> </td> </tr> <tr id="section-3"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-3">&#182;</a> </div> <p>This finds all the objects keys.</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">function</span> <span class="nx">ownKeys</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">accumulator</span> <span class="o">=</span> <span class="p">[];</span>
- <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">propertyName</span> <span class="k">in</span> <span class="nx">o</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p>FIXME: we should find a way of checking that the trailing magic is
+ <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">propertyName</span> <span class="k">in</span> <span class="nx">o</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">if</span> <span class="p">(</span><span class="nx">o</span><span class="p">.</span><span class="nx">hasOwnProperty</span><span class="p">(</span><span class="nx">propertyName</span><span class="p">))</span> <span class="p">{</span>
+ <span class="nx">accumulator</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">propertyName</span><span class="p">);</span>
+ <span class="p">}</span>
+ <span class="p">}</span>
+ <span class="k">return</span> <span class="nx">accumulator</span><span class="p">;</span>
+<span class="p">}</span></pre></div> </td> </tr> <tr id="section-4"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-4">&#182;</a> </div> <p>This finds all the objects keys, which do not contain the magic marker.</p> </td> <td class="code"> <div class="highlight"><pre><span class="kd">function</span> <span class="nx">ownRealKeys</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span> <span class="nx">magic</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-5"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-5">&#182;</a> </div> <p>console.log('o', JSON.stringify(o), 'magic', magic);</p> </td> <td class="code"> <div class="highlight"><pre> <span class="kd">var</span> <span class="nx">accumulator</span> <span class="o">=</span> <span class="p">[];</span>
+ <span class="k">for</span> <span class="p">(</span><span class="kd">var</span> <span class="nx">propertyName</span> <span class="k">in</span> <span class="nx">o</span><span class="p">)</span> <span class="p">{</span></pre></div> </td> </tr> <tr id="section-6"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-6">&#182;</a> </div> <p>FIXME: we should find a way of checking that the trailing magic is
present too. </p> </td> <td class="code"> <div class="highlight"><pre> <span class="k">if</span> <span class="p">(</span> <span class="nx">o</span><span class="p">.</span><span class="nx">hasOwnProperty</span><span class="p">(</span><span class="nx">propertyName</span><span class="p">)</span>
<span class="o">&amp;&amp;</span> <span class="nx">propertyName</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nx">magic</span><span class="p">.</span><span class="nx">length</span><span class="p">)</span> <span class="o">!==</span> <span class="nx">magic</span>
<span class="p">)</span> <span class="p">{</span>
@@ -10,8 +17,9 @@
<span class="p">}</span>
<span class="p">}</span>
<span class="k">return</span> <span class="nx">accumulator</span><span class="p">;</span>
-<span class="p">}</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>Clobber the string prototype - nasty but convenient.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nb">String</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">startsWith</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(</span><span class="nx">text</span><span class="p">)</span> <span class="p">{</span>
+ <span class="k">return</span> <span class="k">this</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="nx">text</span><span class="p">.</span><span class="nx">length</span><span class="p">)</span> <span class="o">===</span> <span class="nx">text</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>Export the functions.</p> </td> <td class="code"> <div class="highlight"><pre><span class="nx">exports</span><span class="p">.</span><span class="nx">ownKeys</span> <span class="o">=</span> <span class="nx">ownKeys</span>
<span class="nx">exports</span><span class="p">.</span><span class="nx">ownRealKeys</span> <span class="o">=</span> <span class="nx">ownRealKeys</span>
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
View
50 examples/create-database.js
@@ -0,0 +1,50 @@
+// Copyright (c) 2010 Barricane Technology Ltd., All Rights Reserved.
+// 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.
+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);
+
+// 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
+// database.
+db.deleteSync();
+process.exit(0);
+
+// Open the database for business. Most once-per-process methods of DB are
+// synchronous, as it's not really an issue and makes applications simpler.
+// All persistence is done asynchronously.
+db.openSync();
+
+// Construct a simple model from which objects can be persisted.
+var house = new model.House("301 Cobblestone Wy., Bedrock, 70777");
+var fred = new model.Person("Fred", "Flintstone");
+fred.house = house;
+var wilma = new model.Person("Wilma", "Flintstone");
+wilma.house = house;
+fred.spouse = wilma;
+wilma.spouse = fred;
+
+// No save() needed - persistence is transparent.
+
+// This synchronously waits for all writes to complete.
+db.end(function(err, result) {
+ if (!err) {
+ console.log('database has persisted successfully');
+ } else {
+ console.log('error:', error);
+ }
+});
View
34 examples/example-model.js
@@ -0,0 +1,34 @@
+// Copyright (c) 2010 Barricane Technology Ltd., All Rights Reserved.
+// Released under the MIT open source licence.
+
+// This is demonstration model code, as used in the examples.
+
+// A house constructor.
+function House(address) {
+ this.address = address;
+
+ // Managed objects need to be registered - this causes them to be persisted.
+ // Registering in the constructor is just convenient, not required.
+ process.db.registerInstance(this);
+}
+// A pointless method, just because.
+House.prototype.logConstructionMaterial = function() {
+ console.log(this.address + ' is made of stone');
+}
+
+// A person constructor.
+function Person(personalName, familyName) {
+ this.personalName = personalName;
+ this.familyName = familyName;
+ // MUST declare any field which needs to trigger persistence.
+ // ONLY registered constructors, plain objects, arrays, strings, numbers and
+ // dates will be persisted.
+ this.spouse = null;
+ this.house = null;
+
+ process.db.registerInstance(this);
+}
+
+// Export the symbols.
+exports.House = House;
+exports.Person = Person;
View
36 examples/read-database.js
@@ -0,0 +1,36 @@
+// Copyright (c) 2010 Barricane Technology Ltd., All Rights Reserved.
+// 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> every time you create an object.
+process.db = db;
+
+// Open the database for business.
+db.openSync();
+
+// Find Fred
+var fred = db.find('personalName', 'Fred')[0];
+
+// Print out the values we read.
+console.log(fred.familyName);
+/* --> 'Flintstone' */
+
+console.log(fred.spouse.personalName);
+/* --> 'Wilma' */
+
+console.log(fred.spouse.spouse.personalName);
+/* --> 'Fred' */
+
+console.log(fred.house.address);
+/* --> '301 Cobblestone Wy., Bedrock, 70777' */
+
+// This synchronously waits for all writes to complete.
+db.endSync();
View
89 lib/db.js
@@ -5,35 +5,116 @@
// It specifically eschews serialisation and io concerns.
var uuid = require('node-uuid')
- , dio = require('./disk-io');
+ , dio = require('./disk-io')
+ , util = require('./util')
+ ;
// The DB constructor represents the database. In almost all circumstances only
// one database will exist per process. More can be used, if needed, but the
// application will become more complex, as it cannot use
// <code>process.db</code> in constructors.
function DB(options) {
- if (!options) {
- options = {};
- }
+ options = options || {};
+
// <code>magic</code> is a string which must not normally occur in your application's
// property names. Double underscore is the default.
this.magic = options.magic || "__";
+
+ // Set up some almost constants.
+ this.CONSTRUCTOR_TAG = this.magic + 'constructor' + this.magic;
+ this.UUID_TAG = this.magic + 'uuid' + this.magic;
+ this.DATE_TAG = this.magic + 'date' + this.magic;
+
// <code>uuid</code> is the function used to generate unique IDs for each
// managed object. It is overridable for testing purposes, where the IDs
// need to be deterministic.
this.uuid = options.uuid || uuid;
+
// The <code>io</code> manager is responsible for IO. By default BarricaneDB
// uses simple disk-based persistence. The <code>io</code> manager is
// separated so that it can be optionally replaced with ReplicationIO at
// some point in the future.
this.io = options.io || new dio.DiskIO(options);
+
+ // The database needs to keep track of constructors, in order to be able
+ // to reconstruct objects when opening a database.
+ this.constructors = {};
+
// <code>instances</code> contains all of the *managed* objects, indexed
// by uuid.
this.instances = {};
+
// <code>stores</code> contains all of the *managed* objects' data, indexed
// by uuid. The data is separate as the properties in the instances have
// custom getters/setters, via <code>__defineSetter__</code>.
this.stores = {};
+
+ // The <code>persist</code> flag is made false during database
+ // reconstruction, otherwise we would get into a frightful pickle.
+ this.persist = true;
+}
+
+// This opens the database for use. It only exists because I'm not sure about
+// the validity of calling instance members from within the constructor.
+DB.prototype.openSync = function() {
+ this.persist = false;
+
+ this.io.openSync();
+
+ this.persist = true;
+}
+
+
+// This must be called once for every instance which needs to be persisted.
+// It is idiomatic to do this in the constructor, using a global
+// <code>process.db</code> database.
+DB.prototype.registerInstance = function(instance) {
+ var that = this;
+
+ instance[this.UUID_TAG] = this.options.uuid();
+
+ this.instances[instance[this.UUID_TAG]] = instance;
+ var store = {}; // the backing store for the instance data
+ this.stores[instance[this.UUID_TAG]] = store;
+
+ util.ownRealKeys(instance, this.magic).forEach(function(p, i, all) {
+ store[p] = instance[p];
+ instance.__defineSetter__(p, function(val) {
+ store[p] = val;
+ if (this.persist) {
+ that.io.persist(instance);
+ }
+ });
+ instance.__defineGetter__(p, function() {
+ return store[p];
+ });
+ });
+
+
+ // Finally add the object to the log, if the database is not in the middle
+ // of reconstruction.
+ if (this.persist) {
+ this.io.persist(instance);
+ }
+};
+
+// The database needs to keep track of constructors, so that it can call them
+// when loading a database.
+DB.prototype.registerConstructors = function() {
+ var args = Array.prototype.slice.call(arguments);
+ for (var i in args) {
+ this.constructors[args[i].name] = args[i];
+ }
+};
+
+// Just a wrapper.
+DB.prototype.deleteSync = function() {
+ this.io.deleteSync();
+}
+
+// Just a wrapper.
+DB.prototype.endSync = function() {
+ this.io.endSync();
}
// Export the symbols.
View
50 lib/disk-io.js
@@ -6,25 +6,55 @@
// The DiskIO constructor creates an object which can be used by DB to interact
// with the outside world. Other IOs will be possible in future, including
// ReplicationIO.
+
+var codec = require('./swoj-codec')
+ , fs = require('fs')
+ ;
+
function DiskIO(options) {
- if (!options) {
- options = {};
- }
+ options = options || {};
+
this.path = options.path || '/tmp/barricane.db';
- // Writes must be counted, so we only close the file whwn all has been
+ this.codec = options.codec || new codec.Codec(options.magic);
+
+ this.writeStream = null;
+
+ // Writes must be counted, so we only close the file when all has been
// written.
this.outstandingWrites = 0;
+
+ // If end is requested, this will contain a callback.
+ this.ending = false;
}
-// This writes the serialise object to disk.
-DiskIO.prototype.write = function (serialisedObject) {
+// This writes an object to disk.
+DiskIO.prototype.write = function (instance) {
+ var that = this;
+ this.outstandingWrites++;
+
+ var serialised = this.codec.serialise(instance);
+ this.writeStream.write(serialised, function(err, result) {
+ that.outstandingWrites--;
+ if (that.outstandingWrites === 0 && that.ending) {
+ that.ending(false, '');
+ }
+ });
};
-// Read serialises objects back from disk, and
-// <code>callback(err, result)</code> for each one.
-DiskIO.prototype.read = function (callback) {
-};
+DiskIO.prototype.openSync = function() {
+}
+// This deletes any database files.
+DiskIO.prototype.deleteSync = function() {
+ // First get the contents of the path.
+ var contents = fs.readdirSync(this.path);
+ console.log('contents:', contents);
+ // <-- CONTINUE
+}
+
+DiskIO.prototype.end = function(callback) {
+ this.ending = callback;
+}
// Export the symbols.
exports.DiskIO = DiskIO;
View
49 lib/swoj-codec.js
@@ -19,7 +19,12 @@
var util = require('./util');
function SwojCodec(magic) {
- this.magic = magic;
+ this.magic = magic || '__';
+
+ // Set up some almost constants.
+ this.CONSTRUCTOR_TAG = this.magic + 'constructor' + this.magic;
+ this.UUID_TAG = this.magic + 'uuid' + this.magic;
+ this.DATE_TAG = this.magic + 'date' + this.magic;
}
SwojCodec.prototype.name = 'swoj';
@@ -31,26 +36,28 @@ SwojCodec.prototype.encoding = 'utf';
// to disk.
SwojCodec.prototype.serialise = function(instance) {
var tmp = {};
- tmp[this.wrap("constructor")] = instance.constructor.name;
+ tmp[this.CONSTRUCTOR_TAG] = instance.constructor.name;
for (var p in instance) {
if (instance.hasOwnProperty(p)) {
// Is this property a registered instance
if ( instance[p] !== undefined
&& instance[p] !== null
- && instance[p].hasOwnProperty(this.wrap("uuid"))
+ && instance[p].hasOwnProperty(this.UUID_TAG)
) {
- tmp[p] = this.wrap("UUID") + ":" + instance[p][this.wrap("uuid")];
+ tmp[p] = this.UUID_TAG + instance[p][this.UUID_TAG];
} else {
// Treat dates carefully.
if (instance[p] instanceof Date) {
- tmp[p] = this.wrap("DATE") + ":" + instance[p].getTime();
+ tmp[p] = this.DATE_TAG + instance[p].getTime();
} else {
+ // TODO: Add escaping of magic within values.
tmp[p] = instance[p];
}
}
}
}
+
return JSON.stringify(tmp);
}
@@ -60,14 +67,13 @@ SwojCodec.prototype.deserialise = function(string, constructors) {
var tmp = JSON.parse(string);
// Find the constructor.
- var Constructor = constructors[tmp[this.wrap("constructor")]];
- console.log("Constructor:", Constructor);
+ var Constructor = constructors[tmp[this.CONSTRUCTOR_TAG]];
// And the UUID.
- var uuid = tmp[this.wrap("uuid")];
+ var uuid = tmp[this.UUID_TAG];
// Create the object - see [Stack Overflow](http://stackoverflow.com/questions/3362471/how-can-i-call-a-javascript-constructor-using-call-or-apply)
- // for a detailed explanation.
+ // for a detailed explanation of how to call a constructor without 'new'.
var Tmp = function(){}; // temporary constructor
var inst, ret; // instance and temporary return value
var args = [];
@@ -75,7 +81,7 @@ SwojCodec.prototype.deserialise = function(string, constructors) {
inst = new Tmp;
ret = Constructor.apply(inst, args);
var ob = Object(ret) === ret ? ret : inst;
- ob[this.wrap("uuid")] = uuid; // Fix UUID.
+ ob[this.UUID_TAG] = uuid; // Fix UUID.
util.ownRealKeys(tmp, this.magic).forEach(function(key, i, keys) {
ob[key] = tmp[key];
@@ -87,12 +93,25 @@ SwojCodec.prototype.deserialise = function(string, constructors) {
// This replaces up any remaining values of __UUID__:... with references to the
// correct object, and also handles dates, etc.
SwojCodec.prototype.fixRefs = function(instances) {
-}
-
-SwojCodec.prototype.wrap = function(string) {
- return this.magic + string + this.magic;
+ var that = this;
+ util.ownKeys(instances).forEach(function(uuid, i, uuids) {
+ util.ownRealKeys(instances[uuid], that.magic).forEach(function(key, i, keys) {
+ var value = instances[uuid][key];
+ // TODO: Add escaping of magic within values.
+ if (typeof value == 'string') {
+ if (value.startsWith(that.UUID_TAG)) {
+ instances[uuid][key] = instances[value.slice(
+ that.UUID_TAG.length)];
+ }
+ if (value.startsWith(that.DATE_TAG)) {
+ instances[uuid][key] = new Date(
+ parseInt(value.slice(that.DATE_TAG.length),10));
+ }
+ }
+ });
+ });
}
-// Anonomise the exported name, so that codec can be used interchangeably,
+// Anonomise the exported name, so that codecs can be used interchangeably,
exports.Codec = SwojCodec;
View
19 lib/util.js
@@ -4,8 +4,20 @@
// This is the obligatory dumping ground modules for all those bits which don't
// fit well elsewhere.
+// This finds all the objects keys.
+function ownKeys(o) {
+ var accumulator = [];
+ for (var propertyName in o) {
+ if (o.hasOwnProperty(propertyName)) {
+ accumulator.push(propertyName);
+ }
+ }
+ return accumulator;
+}
+
// This finds all the objects keys, which do not contain the magic marker.
function ownRealKeys(o, magic) {
+ //console.log('o', JSON.stringify(o), 'magic', magic);
var accumulator = [];
for (var propertyName in o) {
// FIXME: we should find a way of checking that the trailing magic is
@@ -19,4 +31,11 @@ function ownRealKeys(o, magic) {
return accumulator;
}
+// Clobber the string prototype - nasty but convenient.
+String.prototype.startsWith = function(text) {
+ return this.slice(0, text.length) === text;
+}
+
+// Export the functions.
+exports.ownKeys = ownKeys
exports.ownRealKeys = ownRealKeys
View
6 test/app-test.js
@@ -6,6 +6,12 @@ var vows = require('vows')
, bdb = require('../lib/barricane-db')
;
+
+
+
+
+
+
var suite = vows.describe('Setup').addBatch(
{ "dummy"
: { topic
View
3  test/demo-model.js
@@ -7,7 +7,8 @@
function House(address) {
this.address = address;
- // Managed objects need to be registered - this causes them to be persisted.
+ // Managed objects need to be registered - this causes them to be persisted
+ // whenever they are modified.
// Registering in the constructor is just convenient, not required.
process.db.registerInstance(this);
}
View
56 test/swoj-codec-test.js
@@ -26,7 +26,7 @@ fred.spouse = wilma;
wilma.spouse = fred;
-var codec = new swoj.Codec("__");
+var codec = new swoj.Codec();
var suite = vows.describe('Codec').addBatch(
{ "serialise house"
@@ -39,13 +39,13 @@ var suite = vows.describe('Codec').addBatch(
: { topic
: fred
, "serialise"
- : function(topic) { assert.deepEqual(codec.serialise(topic), '{"__constructor__":"Person","personalName":"Fred","familyName":"Flintstone","spouse":"__UUID__:2","house":"__UUID__:0","__uuid__":"1"}'); }
+ : function(topic) { assert.deepEqual(codec.serialise(topic), '{"__constructor__":"Person","personalName":"Fred","familyName":"Flintstone","spouse":"__uuid__2","house":"__uuid__0","__uuid__":"1"}'); }
}
, "serialise wilma"
: { topic
: wilma
, "serialise"
- : function(topic) { assert.deepEqual(codec.serialise(topic), '{"__constructor__":"Person","personalName":"Wilma","familyName":"Flintstone","spouse":"__UUID__:1","house":"__UUID__:0","__uuid__":"2"}'); }
+ : function(topic) { assert.deepEqual(codec.serialise(topic), '{"__constructor__":"Person","personalName":"Wilma","familyName":"Flintstone","spouse":"__uuid__1","house":"__uuid__0","__uuid__":"2"}'); }
}
, "deserialise house"