Skip to content

Comment on "Recipes" 1 #183

Open
Open
@janl

Description

@janl

From http://books.couchdb.org/relax/reference/recipes

The downside of this approach is that with an increasing number of reorde=
rings, float precision can become an issue as digits =93grow=94 infinitely.=
One solution is to not care and expect that a single user will not exceed =
any limits. Alternatively, an administrative task can reset the whole list =
to single-decimals when a user is not active.

I think it's unfortunate that a book about couchdb has to resort to
this sort of answer, showing that neither method is without problems.
Furthermore, the administrative task is left as an exercise for the
reader, while it would be a worthwhile example of administrative tasks
and how to scrub entire databases without resorting to transactions.
It would address problems like:

  • How would this task calculate the new floats? (probably in 1/ndocs
    steps)
  • How do you protect against someone inserting a document between two
    documents you just renumbered?

As a more robust solution, how about storing the float as a string?
There is no limit on string size in CouchDB, and you could use a
larger radix by using ASCII or even UTF8. A string is presumed to be
the same as that string + any number of the first character in your
encoding range. Taking a-z as an example range, the first thing you
insert would have "m" as the key. If you have a busy todo list and you
need to insert between "m" and "n", you get "mm".

In a list where you insert millions of records at the top without ever
fixing the sorting, you would end up with strings like
"aaaaaaaaaaaaaaa....aaaaaab", but as long as your buffers don't die
from the big documents it will work.

At the very least I think the book should calculate that when using
the regular floats available to the system, you can do x top-inserts
before it becomes a problem.

Finally, I think it would be worthwhile to point out that you can use
an array as the sort key as well, allowing for hierarchical manual
sorting. A good kick in the prior knowledge :)
token=40859aae6b9645cadaaa9f68d22b1800login=janl

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions