I have elements of a list in couchdb documents. Let’s say these are 3 elements in 3 documents:
{ "id" : "783587346", "type" : "aList", "content" : "joey", "sort" : 100.0 }
{ "id" : "358734ff6", "type" : "aList", "content" : "jill", "sort" : 110.0 }
{ "id" : "abf587346", "type" : "aList", "content" : "jack", "sort" : 120.0 }
A view retrieves all “aList” documents and displays them sorted by “sort”.
Now I want to move the elements, when I want to move “jack” to the middle, I could do this atomic in one write and change it’s sort key to 105.0. The view now returns the documents in the new sort order.
After a lot of sorting I could end up with sort keys like 50.99999 and 50.99998 after some years and in extreme situations run out of digits?
What can you recommend, is there a better way to do this? I’d rather keep the elements in seperate documents. Different users might edit different list elements in parallel.
And users might also change the document order at the same time (which also can get tricky when 2 users want to move two different documents (like joey and jill) to the end with let’s say “sort” = 130.0 at the same time).
Maybe there is a much better way?
Did I miss something on CouchDb transactions?
You are using the common pattern of a real number for user control of sorting.
That is a fine technique, recommended by Damien Katz. To move between adjacent documents
AandB, then you set yoursortfield to the average ofA.sortandB.sort.This question has several parts.
What about floating point precision?
Javascript
Numbers are double-precision IEEE-754 floating point numbers. Theyhave limited precision.
Doubles have a lot of precision. If this is human-initiated activity then it
will be a very long time before dragging and dropping hits the limit.
But you have two choices:
1. Re-normalize the sort values in the background
Remember rewriting your line numbers in BASIC? Same thing.
Have a cron job or some other task (NodeJS is getting popular)
to check for unacceptably close sort values and space them out. This
could use sophisticated heuristics such as:
sortssortssortvalues but which never changethe view result. In other words, if you have
0.001,0.002, and0.003,move the
0.003first to e.g.0.100, then change0.002to0.005. Thatmay have a slight helpful effect in the UI but remember, replication may not
copy these in the same order so the benefit is marginal, perhaps not worth
the complexity.
2. Use a decimal data type with unlimited precision
Instead of
sortstoring a JavascriptNumber, it could store a string frombut not including
"0.0"through"1.0"(say, to 100 digits). Then a string sort isalso a numeric sort. (You have “anchors” at 0.0 and 1.0 which are invalid for documents. To insert a document in the first position, set
sortto the average of 0.0 and the current first document. For the last position,sortis the average of the last document and 1.0.)Next, your client (whoever calculates the
sortvalue) needsarbitrary-precision real number types. Java, Ruby, Python, pretty much all the
languages have them. This post even inspired me to make a quick project,
BigDecimal for Javascript which is the
BigDecimalcode from Google Web Toolkit (which itself came from Apache Harmony). But there
are other implementations too.
I personally like
BigDecimal. In your case however you would have to change yourcode to use a string for
sort. However the benefit is, you never have to re-normalizethe
sorts to work around the precision.What about collisions from concurrent activity?
CouchDB is Relaxed. What will happen is what users expect. CouchDB
documents mimic the real world. As Chris Anderson says, “there are no
transactions in real life.”
For three elements in a UI:
What if I move
AafterCand you moveBafter C? Clearly, the list will either beC B AorC A B. Which should it be? That depends on your application?Either, it doesn’t matter: Great! CouchDB will order
AandBarbitrarily and youwill be fine. Users will infer (or see if your UI is good) that somebody else moved
the other item.
Or,B must come before A because [some reason]: Well then, your
sortvalue is wrong.It should include all relevant data to decide sub-sorts. For example, you can
emit([120.000, doc.userLastName], doc). When users move docs to the same place, thesort becomes alphabetical.
If you say, A cannot be moved so soon after B moved then that is also application
code that must be implemented regardless of the data store. In other words, it’s not
a transactional thing, it is software logic. For dragging and dropping UI elements,
my feeling is, it’s not worth it and the “it doesn’t matter” solution is best.