Skip to content

orderBy.limit changefeed positions #5334

Closed

Description

@danielmewes

It would be very useful if we could add positions to the output of orderBy.limit changefeeds.

I propose the following API:

  • A new optarg {includePositions: true} to changes
  • Two new fields in the documents generated by the changefield: old_pos and new_pos
  • The old_pos field exists exactly if old_val exists, and new_pos exists exactly if new_val exists
  • old_pos is null exactly if old_val is null and new_pos is null if new_val is null

This will apply to both initial and subsequent values.

The values should be such that if the user wants to maintain an ordered list, they can follow these two steps in this order in order to update it on a change event:

  1. Erase the element at old_pos from the list if old_pos is set and not null (all subsequent elements will move up)
  2. Insert new_val at new_pos into the list if new_pos is set and not null (all subsequent elements will move down)

The option should only work on orderBy.limit changefeeds.
We can make it work on orderBy.map.limit and orderBy.limit.map as well if it's not too hard, but it's somewhat optional. It might be a bit of extra work since we need to change the definition of what a "trivial" change is (the positions could change without the values changing).
It should not work on orderBy.limit.filter, though it should probably work on orderBy.filter.limit (relevant: #5325).

The complexity for determining the positions on the server might be linear in the size of the limit, but I think that's ok.

I'd like to fast-track the discussion on this because a certain other project is waiting on the feature.
@mlucy @deontologician @coffeemug any opinions on the API?

Activity

  1. added this to the 2.3 milestone on Jan 29, 2016
  2. mlucy commented on Jan 29, 2016

    @mlucy
    Member

    So, I'd like to say one last time that I don't necessarily think this is a good idea. It is inherently inefficient to be inserting and/or deleting in the middle of an array on every change. It would be much better for the certain other project to keep track of the results of an orderBy.limit changefeed in some sort of balanced tree structure ordered by whatever we're ordering by on the server, and then do inserts and deletes on that in logarithmic time instead.

    If that's too hard, though, we can plausibly add this. @danielmewes and I talked a little bit about the implementation earlier. The server is currently storing the top n elements in an ordered map, which means that we can't really compute the offsets efficiently -- we'll have to do work proportional to the size of n on every change in order to generate the offsets. We could fix this by using a balanced tree structure that stores subtree counts, but it looks like neither Boost nor the STL provides one, so we'd have to find one somewhere or write it ourselves. Probably for the first version we should not bother and just accept the O(n) overhead.


    As for the proposal:

    • We should maybe call it includeOffsets instead of includePositions, since we already use the word offset to describe this in other places (like the offsets_of command). old_pos/new_pos would have to change too.
    • Should we error if this optarg is set for non-orderBy.limit changefeeds, since it has no meaning and won't change the output?
    • It seems a little weird to have old_pos/new_pos sometimes be there and sometimes be null when there's no semantic difference, but I don't object strongly to this if it seems more consistent to you.
  3. danielmewes commented on Jan 29, 2016

    @danielmewes
    MemberAuthor

    I think most people will not care about the linear costs of updating the array.
    The cost is also not actually that high, since most interpreted languages use references and won't actually copy the stored data in the array, and since orderBy.limit is meant for smallish result sets.

    I think what most users will do at the moment is not implement an efficient tree structure, but instead just use an array anyway and iterate over it to find the position of the old_val to remove it, and then iterating over it again to insert new_val in the right spot (or something similar to this).
    That is almost strictly worse than us just giving them the positions.

    On your other comments:

    • includeOffsets sounds good to me.
    • using the flag on other types of changefeeds should fail I think
    • I have a slight preference for coupling the existence and null-ness of new_val and new_offset as well as old_val and old_offset. Though I wouldn't be strongly opposed to having them always be null or always be non-existent if there's no old/new value, regardless of whether we have initial values or actual changes.
  4. deontologician commented on Jan 29, 2016

    @deontologician
    Contributor

    I think the value is that it makes explicit what to do with the change. The current change object on orderBy.limit changefeeds is unintuitive to use correctly, and I think the fact that we have to emulate the server's sorting is an annoying burden on end users. So I don't think this is a big problem for that certain other project, since we know the limitations and the correct strategy, but it's pretty imperative for anyone else using the database.

    It's one of those things where you don't realize what a colossal pain in the ass we're thrusting on users until you try to do it yourself. Also, the efficiency argument doesn't seem too big of a deal. The nature of these queries is that the result set is small. To put it in perspective, immediately after resizing and moving the array like this, most frontend frameworks are going to immediately regenerate their entire page's markup, and then run a diff algorithm on it vs. the old version of the page to decide what to change in the DOM. The ease of use of this strategy trumps any minor efficiency gains that might be had by avoiding shifting elements in the original array.

  5. coffeemug commented on Jan 29, 2016

    @coffeemug
    Contributor

    I think it's ok to have an API here that would often result in inefficient use. It's almost always what you want for small resultsets, and as long as users can turn on the current behavior if the dataset gets too big, I think this would be what you'd actually want 99% of the time.

  6. mlucy commented on Jan 29, 2016

    @mlucy
    Member

    OK, let's do this then.

  7. danielmewes commented on Jan 29, 2016

    @danielmewes
    MemberAuthor

    👍
    Let's do the original proposal but with the optarg named includeOffsets and the fields named old_offset/new_offset, and failure if includeOffsets is set on changefeeds that don't support it.

  8. mlucy commented on Feb 4, 2016

    @mlucy
    Member

    This is in CR 3462 by @danielmewes (branch mlucy_5334).

  9. dimitrikochnev commented on Feb 6, 2016

    @dimitrikochnev

    @mlucy Thank you! It's exactly what I was looking for.

  10. mlucy commented on Feb 9, 2016

    @mlucy
    Member

    @Rosendi -- glad to help!

    This is in next, it should be released in RethinkDB 2.3.

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions