Skip to content

Commit

Permalink
Document changes are now flushed to the database in sequential order
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Kifer committed Oct 26, 2010
1 parent 3043906 commit b035b91
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 25 deletions.
58 changes: 39 additions & 19 deletions lib/congo.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -214,41 +214,61 @@ class Collection extends Model
_id: __type__.Identity

constructor: (initial, next) ->
@__flushing__ = false
@__store_queue__ = []
if initial instanceof Function
next = initial
initial = null
super initial
if not @__values__._id?
Congo.db.insert @constructor.name, @__dehydrate__(), (id) =>
@__values__._id = id
next @
document = @__dehydrate__()
@__queue_store__ (qnext) =>
log '[ Creating ] ' + @constructor.name if process.env.DEBUG?
Congo.db.insert @constructor.name, document, (id) =>
# log 'ID: ' + id
@__values__._id = id
next @ if next?
qnext()
else if next?
next @

__ascend_push__: (key, val, next) ->
@__store_push__ key, val, next

__store_push__: (key, val, next) ->
log '[ Storing ] ' + @constructor.name + '.' + key if process.env.DEBUG?
doc = {}
document = {}
if val instanceof Model
doc[key] = val.__dehydrate__()
document[key] = val.__dehydrate__()
else
doc[key] = val
Congo.db.update @constructor.name, { _id: @__values__._id }, { $push: doc }, next
document[key] = val
@__queue_store__ (qnext) =>
log '[ Storing ] ' + @constructor.name + '.' + key if process.env.DEBUG?
Congo.db.update @constructor.name, { _id: @__values__._id }, { $push: document }, ->
next() if next?
qnext()

__ascend_set__: (key, val, next) ->
@__store_set__ key, val, next

__store_set__: (key, val, next) ->
return if key == '_id'
log '[ Storing ] ' + @constructor.name + '.' + key if process.env.DEBUG?
doc = {}
document = {}
if val instanceof Model
doc[key] = val.__dehydrate__()
document[key] = val.__dehydrate__()
else
document[key] = val
@__queue_store__ (qnext) =>
log '[ Storing ] ' + @constructor.name + '.' + key if process.env.DEBUG?
Congo.db.update @constructor.name, { _id: @__values__._id }, { $set: document }, ->
next() if next?
qnext()

__queue_store__: (op) ->
@__store_queue__.push op
if not @__flushing__
@__flushing__ = true
@__flush_store_queue__()

__flush_store_queue__: ->
if @__store_queue__.length == 0
@__flushing__ = false
else
doc[key] = val
Congo.db.update @constructor.name, { _id: @__values__._id }, { $set: doc }, next
@__store_queue__.shift()(=>
@__flush_store_queue__())

__save__: (next) ->
Congo.db.update @constructor.name, { _id: @__values__._id }, @__dehydrate__(), next
Expand Down
2 changes: 1 addition & 1 deletion lib/mongo.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ try
catch error
util = require 'sys'
log = (args...) -> util.puts(a) for a in args
inspect = (args...) -> log(util.inspect(a)) for a in args
inspect = (args...) -> log(util.inspect(a, true, null)) for a in args
_bson = require './vendor/bson'
binary = require './vendor/binary'
net = require 'net'
Expand Down
10 changes: 5 additions & 5 deletions test/mongo.test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Account.clear ->
result = account.__dehydrate__()
result._id = test_result._id
assert.ok _.isEqual(test_result, result)
setTimeout go, 200
setTimeout go, 400

new Account {}, (account) ->
new Person { name: 'Andrew Jackson' }, (person) ->
Expand All @@ -95,12 +95,12 @@ Account.clear ->
go1 = ->
new Provider { name: 'FedEx', account: 'FEDXXX' }, (provider) ->
person.addresses[0].shipping[0].providers[1] = provider
setTimeout go2, 200
setTimeout go2, 400
go2 = ->
Account.load account._id, (loaded_account) ->
log '--- Setting scalars via assignment'
assert.ok _.isEqual(account.__dehydrate__(), loaded_account.__dehydrate__())
setTimeout go2, 200
setTimeout go2, 400

new Account {}, (account) ->
new Person { name: 'Andrew Jackson' }, (person) ->
Expand All @@ -115,5 +115,5 @@ Account.clear ->
Account.load account._id, (loaded2) ->
log '--- Setting properties after hydration'
assert.ok _.isEqual(loaded1.__dehydrate__(), loaded2.__dehydrate__())
setTimeout go2, 200
setTimeout go, 200
setTimeout go2, 400
setTimeout go, 400

0 comments on commit b035b91

Please sign in to comment.