An idea I got from the CouchDB documentation: use floating point numbers
instead of integers. That is, instead of 1, 2, 3, 4..., you can use 0.1,
0.2, 0.3, 0.4... or 1.0, 2.0, 3.0, 4.0... That can help you to insert a
new node (say, 0.15 or 1.5) in between any two given nodes (say, in
between 0.1 and 0.2 or in between 1.0 and 2.0) without any other
modification.
I hope it will be useful for your case.
CGS
On 12/23/2011 11:47 AM, Jo-Erlend Schinstad wrote:
For quite some time, I've been thinking about creating a Gtk TreeModel
that uses CouchDB as a backend. I've just started investigating it and
I've found some issues I don't know how to solve. I think this would
be useful for all kinds of trees, though, so it wouldn't be specific
to GTK. I hope someone can help.
The general idea is simple. The tree consists of nodes that can have
zero or one parent and zero or more children. So I thought each node
would be its own document with an ID like [model number, root node
number, child node number, child...], for instance,
{ "_id":[0, 0, 1,2] } would be the third child of the second child of
the first root node in the first model. This way, it would be easy to
get a single node or a range of nodes (including children) from a
specific model.
I have two problems with this; inserting, removing and moving nodes.
If I insert a node somewhere in the middle, all the documents after it
would need to get their IDs changed. I guess I could do this by
looping backwards from the end, incrementing the id in order to create
a free slot for the new node, (opposite for removals) but wouldn't
this be extremely slow for large collections? It should also be
possible to reorder rows, or just move something one step up or down.
This would ideally mean simply swapping the IDs of two documents, but
I don't know how to do this. I could give the first document a
temporary value, then change the value for the second to the old value
of the first, and then give the first the old value for the second.
But the temporary value might itself cause problems.
Any ideas or thoughts? I appreciate your insight.
Jo-Erlend Schinstad