You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 datumr.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?
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.
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.
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.)
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.
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.
Activity
nviennot commentedon Dec 19, 2014
When using
updatewith atomic operations (like increments), the operation is performed as many times as the document appears in the multi index. This is not great.danielmewes commentedon Dec 19, 2014
That's an interesting point, thanks for bringing it up @nviennot.
You can work around it by using
foreachafter thedistinct: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,deleteandreplacethat enables this behavior in cases where you expect a smallish set of matches.Pinging @mlucy for opinions
nviennot commentedon Dec 19, 2014
This workaround looks like a hack :)
danielmewes commentedon Dec 19, 2014
I agree, just wanted to mention it if you have to get this working now.
mlucy commentedon Dec 19, 2014
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.)
danielmewes commentedon Dec 20, 2014
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.
mlucy commentedon Dec 20, 2014
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.