FAQ
We use #only in our app, and
per https://groups.google.com/forum/?fromgroups=#!searchin/mongoid/only$20identity$20map/mongoid/QsyYKiA0qYI/uZrOokIsX2EJ
we wrap all #only queries in a unit of work like this:

Mongoid.unit_of_work(:disable => :current) do
user = User.only(:email).find(user_id)
end

However, this disables reading from the identity map as well as writing to
the identity map. That is, this makes two database calls:

# Loads User into the identity map
user = User.find(user_id)
# Ignores the identity map, so makes another database call
Mongoid.unit_of_work(:disable => :current) do
user = User.only(:email).find(user_id)
end

Is there a way to disable writing to the identity map so #only queries
don't go into it, but enable the use of the identity map for #only queries?
We have a pretty large application and I'm pretty sure that given this
behavior, we're making unnecessary database calls somewhere. This is fairly
easy to do, too: One class might operate on a User object whereas another
class operates on some object that belongs_to User that only needs access
to certain fields so it uses #only when it gets the User.

Search Discussions

  • Arthur Neves at Jan 7, 2013 at 1:03 am
    I was just reading the other thread about .only and identity map, and I guess this is a bug that should be fixed. I am saying that because having to disable identity map for every .only query is really painful, i guess. Just taking active record as example (https://github.com/rails/rails/blob/3-2-stable/activerecord/lib/active_record/identity_map.rb#L93) , they disable add method when all the attributes are not there. So in my perspective, this should be implement it as well in mongoid.

    Just to answer your question, there is no way to disable identity map per Document/Model. the only way to disable is using unit_of_work . If you want to have another behaviour you could extend IdentityMap and implement what ever you need, also you would have to implement the UnitOfWork module to use your identity map instead of the main one.

    --
    Arthur Neves
    @arthurnn (http://twitter.com/arthurnn)

    On Sunday, 6 January, 2013 at 6:56 PM, Jon wrote:

    We use #only in our app, and per https://groups.google.com/forum/?fromgroups=#!searchin/mongoid/only$20identity$20map/mongoid/QsyYKiA0qYI/uZrOokIsX2EJ we wrap all #only queries in a unit of work like this:

    Mongoid.unit_of_work(:disable => :current) do
    user = User.only(:email).find(user_id)
    end

    However, this disables reading from the identity map as well as writing to the identity map. That is, this makes two database calls:

    # Loads User into the identity map
    user = User.find(user_id)
    # Ignores the identity map, so makes another database call
    Mongoid.unit_of_work(:disable => :current) do
    user = User.only(:email).find(user_id)
    end


    Is there a way to disable writing to the identity map so #only queries don't go into it, but enable the use of the identity map for #only queries? We have a pretty large application and I'm pretty sure that given this behavior, we're making unnecessary database calls somewhere. This is fairly easy to do, too: One class might operate on a User object whereas another class operates on some object that belongs_to User that only needs access to certain fields so it uses #only when it gets the User.
  • Jon at Jan 8, 2013 at 4:28 pm
    Durran, what do you think the right way to fix this should be? I see two
    paths:

    1. Documents retrieved via #only do not get added to the IdentityMap. This
    way no one has to remember to wrap all #only calls in a unit_of_work.
    2. Create a new unit_of_work option that lets you read from the identity
    map but not add any documents to it.

    (1) seems like the right call to me since if we agree in both principle and
    in practice that partial documents should never be added to the
    IdentityMap, the ODM should enforce it instead of requiring developers to
    in their code.

    Thoughts?
    On Sunday, January 6, 2013 8:03:29 PM UTC-5, Arthur Neves wrote:

    I was just reading the other thread about .only and identity map, and I
    guess this is a bug that should be fixed. I am saying that because having
    to disable identity map for every .only query is really painful, i guess.
    Just taking active record as example<https://github.com/rails/rails/blob/3-2-stable/activerecord/lib/active_record/identity_map.rb#L93> ,
    they disable add method when all the attributes are not there. So in my
    perspective, this should be implement it as well in mongoid.

    Just to answer your question, there is no way to disable identity map per
    Document/Model. the only way to disable is using unit_of_work . If you want
    to have another behaviour you could extend IdentityMap and implement what
    ever you need, also you would have to implement the UnitOfWork module to
    use your identity map instead of the main one.

    --
    Arthur Neves
    *@arthurnn <http://twitter.com/arthurnn>*

    On Sunday, 6 January, 2013 at 6:56 PM, Jon wrote:

    We use #only in our app, and per
    https://groups.google.com/forum/?fromgroups=#!searchin/mongoid/only$20identity$20map/mongoid/QsyYKiA0qYI/uZrOokIsX2EJwe wrap all #only queries in a unit of work like this:

    Mongoid.unit_of_work(:disable => :current) do
    user = User.only(:email).find(user_id)
    end

    However, this disables reading from the identity map as well as writing to
    the identity map. That is, this makes two database calls:

    # Loads User into the identity map
    user = User.find(user_id)
    # Ignores the identity map, so makes another database call
    Mongoid.unit_of_work(:disable => :current) do
    user = User.only(:email).find(user_id)
    end

    Is there a way to disable writing to the identity map so #only queries
    don't go into it, but enable the use of the identity map for #only queries?
    We have a pretty large application and I'm pretty sure that given this
    behavior, we're making unnecessary database calls somewhere. This is fairly
    easy to do, too: One class might operate on a User object whereas another
    class operates on some object that belongs_to User that only needs access
    to certain fields so it uses #only when it gets the User.

  • Arthur Neves at Jan 8, 2013 at 4:23 pm
    I agree in solution #1 too.. also to follow the pattern that active record had imposed already.

    --
    Arthur Nogueira Neves
    @arthurnn

    On Tuesday, 8 January, 2013 at 11:21 AM, Jon wrote:

    Durran, what do you think the right way to fix this should be? I see two paths:

    1. Documents retrieved via #only do not get added to the IdentityMap. This way no one has to remember to wrap all #only calls in a unit_of_work.
    2. Create a new unit_of_work option that lets you read from the identity map but not add any documents to it.

    (1) seems like the right call to me since if we agree in both principle and in practice that partial documents should never be added to the IdentityMap, the ODM should enforce it instead of requiring developers to in their code.

    Thoughts?
    On Sunday, January 6, 2013 8:03:29 PM UTC-5, Arthur Neves wrote:
    I was just reading the other thread about .only and identity map, and I guess this is a bug that should be fixed. I am saying that because having to disable identity map for every .only query is really painful, i guess. Just taking active record as example (https://github.com/rails/rails/blob/3-2-stable/activerecord/lib/active_record/identity_map.rb#L93) , they disable add method when all the attributes are not there. So in my perspective, this should be implement it as well in mongoid.

    Just to answer your question, there is no way to disable identity map per Document/Model. the only way to disable is using unit_of_work . If you want to have another behaviour you could extend IdentityMap and implement what ever you need, also you would have to implement the UnitOfWork module to use your identity map instead of the main one.

    --
    Arthur Neves
    @arthurnn (http://twitter.com/arthurnn)

    On Sunday, 6 January, 2013 at 6:56 PM, Jon wrote:

    We use #only in our app, and per https://groups.google.com/forum/?fromgroups=#!searchin/mongoid/only$20identity$20map/mongoid/QsyYKiA0qYI/uZrOokIsX2EJ we wrap all #only queries in a unit of work like this:

    Mongoid.unit_of_work(:disable => :current) do
    user = User.only(:email).find(user_id)
    end

    However, this disables reading from the identity map as well as writing to the identity map. That is, this makes two database calls:

    # Loads User into the identity map
    user = User.find(user_id)
    # Ignores the identity map, so makes another database call
    Mongoid.unit_of_work(:disable => :current) do
    user = User.only(:email).find(user_id)
    end


    Is there a way to disable writing to the identity map so #only queries don't go into it, but enable the use of the identity map for #only queries? We have a pretty large application and I'm pretty sure that given this behavior, we're making unnecessary database calls somewhere. This is fairly easy to do, too: One class might operate on a User object whereas another class operates on some object that belongs_to User that only needs access to certain fields so it uses #only when it gets the User.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupmongoid @
categoriesmongodb, rubyonrails
postedJan 6, '13 at 11:56p
activeJan 8, '13 at 4:28p
posts4
users2
websitemongoid.org
irc#mongoid

2 users in discussion

Jon: 2 posts Arthur Neves: 2 posts

People

Translate

site design / logo © 2022 Grokbase