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

閲覧: 69 回
最初の未読メッセージにスキップ

brahmana

未読、
2010/12/31 17:37:082010/12/31
To: 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

未読、
2011/01/01 8:30:022011/01/01
To: mon...@googlegroups.com
Agreed - Ill get that fixed.

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

brahmana

未読、
2011/01/02 12:54:292011/01/02
To: 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>
全員に返信
投稿者に返信
転送
新着メール 0 件