Is Inc really atomic in Mongoid? -- Doesn't look like

69 vues
Accéder directement au premier message non lu

brahmana

non lue,
31 déc. 2010, 17:37:0831/12/2010
à Mongoid
Hi all,

First of all, Happy new year. :)

The question now :

Is the Inc modifier in Mongoid really atomic? Because it uses
write_attribute which marks the attribute being incremented as dirty
and will included for a "$set" again later when the document is saved.

Class Post
include Mongoid::Document

field :author
field :views, :type => Integer
...........
...........
...........
end

p = Post.first(:conditions => {_id : <some_id>})

p.inc(:views)
# Results in an update query to mongodb -
blogdb['posts'].update({_id : <some_id>}, {"$inc" : {views : 1}})
# view is marked dirty

# Do some more updates to p and save it
p.save

# Results in an update query to mongodb with "$set" on the "views"
field too (assume previous value of "views" was 0)
# blogdb['posts'].update({_id : <some_id>}, {"$set" : {views : 1}})

The second operation ("$set") effectively overrides the first ("$inc")
there by making the atomic increment sort of useless. Also, more
importantly if there was another increment from another Rails instance
(another app-server process may be) before this save, the second
increment is lost.

Shouldn't the Inc modifier directly update the @attributes hash on the
document instead of using write_attribute?
This way the field is not marked dirty.

Thoughts?

Regards,
Brahmana

Durran Jordan

non lue,
1 janv. 2011, 08:30:0201/01/2011
à mon...@googlegroups.com
Agreed - Ill get that fixed.

2010/12/31 brahmana <om.br...@gmail.com>

brahmana

non lue,
2 janv. 2011, 12:54:2902/01/2011
à Mongoid


On Jan 1, 6:30 pm, Durran Jordan <dur...@gmail.com> wrote:
> Agreed - Ill get that fixed.

Thank you Durran.
>
> 2010/12/31 brahmana <om.brahm...@gmail.com>
Répondre à tous
Répondre à l'auteur
Transférer
0 nouveau message