Permalink
Browse files

- KiokuJS.Backend now provides a barebone implementation of `insertEn…

…tries` etc (in plural form)

  which call the according singlular methods in parallel
  • Loading branch information...
1 parent b941535 commit c1502789bc967d3a3e3e2710f6b0b1d0b61ab6bc Nickolay Platonov committed Jan 2, 2011
Showing with 165 additions and 70 deletions.
  1. +95 −4 lib/KiokuJS/Backend.js
  2. +41 −59 lib/KiokuJS/Backend/Hash.js
  3. +17 −0 lib/KiokuJS/Exception.js
  4. +1 −1 lib/KiokuJS/Node.js
  5. +9 −4 lib/KiokuJS/Scope.js
  6. +1 −1 lib/KiokuJS/Test/Fixture.js
  7. +1 −1 t/index.js
View
@@ -16,6 +16,8 @@ Class('KiokuJS.Backend', {
has : {
+ andMax : 20,
+
nodeClass : Joose.I.FutureClass('KiokuJS.Node'),
scopeClass : Joose.I.FutureClass('KiokuJS.Scope'),
@@ -83,6 +85,28 @@ Class('KiokuJS.Backend', {
var scope = this.newScope()
return scope.includeNewObjects(wIDs, woIDs)
+ },
+
+
+ launchAndNormalizeResults : function () {
+
+ this.ANDMAX(this.andMax).except(function () {
+
+ // re-throw the 1st caught exception
+ Joose.A.each(arguments, function (ex) {
+ if (ex !== undefined) throw ex
+ })
+
+ }).then(function () {
+
+ // assumes that methods for individual entries always returns a single result
+ var res = Joose.A.map(arguments, function (results) {
+ return results[ 0 ]
+ })
+
+ this.CONTINUE(res)
+
+ }).now()
}
},
@@ -91,23 +115,90 @@ Class('KiokuJS.Backend', {
methods : {
+ getEntry : function (id, mode) {
+ throw "Abstract method 'getEntry' called for " + this
+ },
+
+
get : function (idsToGet, mode) {
- throw "Abstract method 'get' called for " + this
+ var me = this
+
+ Joose.A.each(idsToGet, function (id) {
+
+ me.AND(function () {
+ me.getEntry(id, mode).now()
+ })
+ })
+
+ me.launchAndNormalizeResults()
},
- insert : function (entriesToInsert, mode) {
+ insertEntry : function () {
throw "Abstract method 'insert' called for " + this
},
+ insert : function (entriesToInsert, mode) {
+ var me = this
+
+ Joose.A.each(entriesToInsert, function (id) {
+
+ me.AND(function () {
+ me.insertEntry(id, mode).now()
+ })
+ })
+
+ me.launchAndNormalizeResults()
+ },
+
+
+ removeID : function (ID) {
+ throw "Abstract method 'removeID' called for " + this
+ },
+
+
+ removeEntry : function (entry) {
+ throw "Abstract method 'removeEntry' called for " + this
+ },
+
+
remove : function (idsOrEntriesToRemove) {
- throw "Abstract method 'remove' called for " + this
+ var me = this
+
+ Joose.A.each(idsOrEntriesToRemove, function (entryOrId) {
+
+ me.AND(function () {
+
+ // entry
+ if (entryOrId === Object(entryOrId))
+ this.removeEntry(entryOrId).now()
+ else
+ this.removeID(entryOrId).now()
+ })
+ })
+
+ me.launchAndNormalizeResults()
+ },
+
+
+ existsID : function (id) {
+ throw "Abstract method 'existsID' called for " + this
},
exists : function (idsToCheck) {
- throw "Abstract method 'exists' called for " + this
+ var me = this
+
+ Joose.A.each(idsToCheck, function (id) {
+
+ me.AND(function () {
+
+ me.existsID(id).now()
+ })
+ })
+
+ me.launchAndNormalizeResults()
},
View
@@ -18,91 +18,73 @@ Class('KiokuJS.Backend.Hash', {
methods : {
- get : function (idsToGet, mode) {
- var docs = this.docs
-
- var strings = Joose.A.map(idsToGet, function (id) {
-
- if (docs[ id ]) return docs[ id ]
+ getEntry : function (id) {
+ var docs = this.docs
+ var me = this
+ var CONTINUE = this.getCONTINUE()
+ if (docs[ id ])
+ setTimeout(function () {
+
+ CONTINUE(me.deserialize(docs[ id ]))
+ }, 0)
+ else
throw new KiokuJS.Exception.LookUp({ id : id, backendName : this.meta.name })
-
- }, this)
-
-
- var entries = Joose.A.map(strings, this.deserialize, this)
-
- var CONTINUE = this.getCONTINUE()
-
- setTimeout(function () {
- CONTINUE(entries)
- }, 0)
},
- insert : function (entries, mode) {
+ insertEntry : function (entry, mode) {
var docs = this.docs
- var strings = Joose.A.map(entries, this.serialize, this)
+ var string = this.serialize(entry)
- Joose.A.each(entries, function (entry, index) {
-
- var ID = entry.ID
-
- if (mode == 'insert' && docs[ ID ])
- throw new KiokuJS.Exception.Overwrite({
- id : ID,
- oldValue : docs[ ID ],
- newValue : strings[ index ]
- })
+ var ID = entry.ID
+
+ if (mode == 'insert' && docs[ ID ])
+ throw new KiokuJS.Exception.Overwrite({
+ id : ID,
+ oldValue : docs[ ID ],
+ newValue : string
+ })
+
+ if (mode == 'update' && !docs[ ID ])
+ throw new KiokuJS.Exception.Update({
+ message : "Attempt to update entry with ID = [" + ID + "], value = [" + string + "], but no such entry in storage"
+ })
- if (mode == 'update' && !docs[ ID ])
- throw new KiokuJS.Exception.Update({
- message : "Attempt to update entry with ID = [" + ID + "], value = [" + strings[ index ] + "], but no such entry in storage"
- })
-
-
- docs[ ID ] = strings[ index ]
- return ID
- })
-
- var CONTINUE = this.getCONTINUE()
+ docs[ ID ] = string
- setTimeout(function () {
- CONTINUE(entries)
- }, 0)
+ setTimeout(this.getCONTINUE(), 0)
},
- remove : function (idsOrEntriesToRemove) {
+ removeID : function (id) {
var docs = this.docs
- Joose.A.each(idsOrEntriesToRemove, function (id) {
-
- // if `id` is an entry
- if (id === Object(id)) id = id.ID
-
- delete docs[ id ]
- })
+ delete docs[ id ]
setTimeout(this.getCONTINUE(), 0)
},
- exists : function (idsToCheck) {
+ removeEntry : function (entry) {
var docs = this.docs
- var checks = Joose.A.map(idsToCheck, function (id) {
- return docs[ id ] != null
- })
-
- var CONTINUE = this.getCONTINUE()
+ delete docs[ entry.ID ]
+ setTimeout(this.getCONTINUE(), 0)
+ },
+
+
+ existsID : function (id) {
+ var docs = this.docs
+ var CONTINUE = this.getCONTINUE()
+
setTimeout(function () {
- CONTINUE(checks)
+
+ CONTINUE(docs[ id ] != null)
}, 0)
-
}
}
}
View
@@ -14,4 +14,21 @@ Class('KiokuJS.Exception', {
return this.meta.name + ': ' + this.description + ', ' + this.getMessage()
}
}
+
+// ,
+//
+//
+// my : {
+//
+// has : {
+// HOST : null
+// },
+//
+// methods : {
+//
+// $throw : function (arg) {
+// throw new Error(new this.HOST(arg))
+// }
+// }
+// }
})
View
@@ -176,7 +176,7 @@ Class('KiokuJS.Node', {
},
- consumeEntry : function (entry) {
+ afterInsert : function (entry) {
this.clearEntry()
}
View
@@ -87,8 +87,8 @@ Class('KiokuJS.Scope', {
// if (!this.hasID(nodeID) || this.hasOwnID(nodeID)) {
- if (!node.isLive()) throw "Can pin only live nodes"
- if (!node.isFirstClass()) throw "Can pin only first-class nodes"
+ if (!node.isLive()) throw "Can pin only live nodes"
+ if (!node.isFirstClass()) debugger//throw "Can pin only first-class nodes"
this.nodesByID[ node.ID ] = node
@@ -179,6 +179,11 @@ Class('KiokuJS.Scope', {
},
+ objectToID : function (obj) {
+ return obj.__REFADR__ && this.nodesByREFADR[ obj.__REFADR__ ].ID || null
+ },
+
+
idToObject : function (id) {
var node = this.nodesByID[ id ]
@@ -398,12 +403,12 @@ Class('KiokuJS.Scope', {
})
// saving only first-class nodes - by design they'll contain a description of the whole graph
- backend.insert(self.encodeNodes(firstClassNodes), mode).andThen(function (entries) {
+ backend.insert(self.encodeNodes(firstClassNodes), mode).andThen(function (result) {
// pin nodes only after successfull insert and only those not pinned yet
Joose.A.each(firstClassNodes, function (node, index) {
- node.consumeEntry(entries[ index ])
+ node.afterInsert(result[ index ])
if (!self.nodePinned(node)) self.pinNode(node)
})
@@ -79,7 +79,7 @@ Class('KiokuJS.Test.Fixture', {
}).CATCH(function (e) {
- t.fail('Exception [' + e + '] caught when running fixture: [' + this + ']')
+ t.fail('Exception [' + (e.description || e.message) + '] caught when running fixture: [' + this + ']')
this.CONTINUE()
View
@@ -14,7 +14,7 @@ var INC = [ '../lib', '/jsan' ]
Harness.configure({
title : 'KiokuJS Test Suite',
-// transparentEx : true,
+ transparentEx : true,
// keepResults : true,
// verbosity : 1,

0 comments on commit c150278

Please sign in to comment.