Skip to content

multi index and update #3462

Open
Open

Description

@nviennot

When using a multi index, a get_all may retrieve many times the same document. e.g

r.table(:table).index_create(:field1, :multi => true).run
r.table(:table).insert(:field1 => [1,2,3]).run # insert 1 document
r.table(:table).get_all(1,2,3, :index => :field1).run.to_a # retrieve 3 documents

A quick workaround can be used with distinct. However distinct and delete or update are not compatible. e.g.

r.table(:table).get_all(1,2,3, :index => :field1).distinct.update(:field2 => 2) # raises, expected selection but found datum
r.table(:table).get_all(1,2,3, :index => :field1).update(:field2 => 2) # this works, but returns 3 replaced documents, instead of 1.

Can we have distinct return a selection? If not possible, can we have update() return the number of unique document really updated?

Related: #3345

Activity

  1. nviennot commented on Dec 19, 2014

    @nviennot
    ContributorAuthor

    When using update with atomic operations (like increments), the operation is performed as many times as the document appears in the multi index. This is not great.

  2. danielmewes commented on Dec 19, 2014

    @danielmewes
    Member

    That's an interesting point, thanks for bringing it up @nviennot.

    You can work around it by using foreach after the distinct:
    r.table(:table).get_all(1,2,3, :index => :field1).distinct.foreach(|x| r.table(:table).get(x[:id]).update(:field2 => 2) )

    Returning the number of distinct documents updated would require us to keep their primary keys in memory, which could blow the memory on very large tables. We could possibly have an opt arg for update, delete and replace that enables this behavior in cases where you expect a smallish set of matches.

    Pinging @mlucy for opinions

  3. added this to the subsequent milestone on Dec 19, 2014
  4. nviennot commented on Dec 19, 2014

    @nviennot
    ContributorAuthor

    This workaround looks like a hack :)

  5. danielmewes commented on Dec 19, 2014

    @danielmewes
    Member

    This workaround looks like a hack :)

    I agree, just wanted to mention it if you have to get this working now.

  6. mlucy commented on Dec 19, 2014

    @mlucy
    Member

    This would be hard to do without using a lot of memory. The only way I can think of is to store some information in the btree when we update a row, and then not update when we see it for the second time in the index. We could maybe use the existing timestamps for this. (An alternative would be to do what Daniel said and keep the primary keys around, but to keep them around in a disk-backed queue so that we don't use up all the memory on the machine if you try to do it on a whole table.)

  7. danielmewes commented on Dec 20, 2014

    @danielmewes
    Member

    Making updates conditional on a timestamp might actually become feasible after #2134 .
    We can also consider exposing that as a separate feature where an update is only applied if a given document hasn't been modified since some previous read. I think I had once discussed this with @wojons, but I can't find a separate issue about it feature at the moment.

    That's at least two releases away I think.

  8. mlucy commented on Dec 20, 2014

    @mlucy
    Member

    Tim and I talked about this, and he pointed out that you can actually do this pretty easily by computing the serialized keys and making sure that you only update the first one in the range you're operating on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions