Skip to content

Commit

Permalink
- KiokuJS.Backend now provides a barebone implementation of `insertEn…
Browse files Browse the repository at this point in the history
…tries` etc (in plural form)

  which call the according singlular methods in parallel
  • Loading branch information
Nickolay Platonov committed Jan 2, 2011
1 parent b941535 commit c150278
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 70 deletions.
99 changes: 95 additions & 4 deletions lib/KiokuJS/Backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Class('KiokuJS.Backend', {


has : {
andMax : 20,

nodeClass : Joose.I.FutureClass('KiokuJS.Node'),
scopeClass : Joose.I.FutureClass('KiokuJS.Scope'),

Expand Down Expand Up @@ -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()
}
},

Expand All @@ -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()
},


Expand Down
100 changes: 41 additions & 59 deletions lib/KiokuJS/Backend/Hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

}
}
}
Expand Down
17 changes: 17 additions & 0 deletions lib/KiokuJS/Exception.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
// }
// }
// }
})
2 changes: 1 addition & 1 deletion lib/KiokuJS/Node.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ Class('KiokuJS.Node', {
},


consumeEntry : function (entry) {
afterInsert : function (entry) {
this.clearEntry()
}

Expand Down
13 changes: 9 additions & 4 deletions lib/KiokuJS/Scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 ]

Expand Down Expand Up @@ -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)
})
Expand Down
2 changes: 1 addition & 1 deletion lib/KiokuJS/Test/Fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion t/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var INC = [ '../lib', '/jsan' ]
Harness.configure({
title : 'KiokuJS Test Suite',

// transparentEx : true,
transparentEx : true,
// keepResults : true,
// verbosity : 1,

Expand Down

0 comments on commit c150278

Please sign in to comment.