I want to keep a large ordered list (millions of elements) in Google App Engine datastore. Fast insertion is required.
The simplest way would be adding an indexed property (or column) "order_num" representing the order. For example, a list [A, B, C] would be stored like this:
content order_num
--------------------
A 1
B 2
C 3
However, this doesn't give you fast insertion. For example, If I want to insert X after A, I have to renumber B and C to "make room" for X, i.e., let B become 3, C becomes 4, and X be 2. This would be a disaster if I have millions of elements.
I found a feasible solution called "gap approach" described here. This approach keeps a gap between adjacent elements. Like this:
content order_num
--------------------
A 1000
B 2000
C 3000
When I want to insert X after A, I can simply add X with its order_num set to (1000 + 2000) / 2 = 1500, no renumbering required.
But with these gaps becoming smaller, renumbering may be required. My question is, is there any known strategy on renumbering? And deciding the size of gaps?
Thanks!
UPDATE
Here's more detail. Say I have a list of elements in database, and every element has an integer property named my_num. The value of my_num is an arbitrary positive integer. Suppose I have a list [A, B, C, D], and their my_num are
element my_num
---------------------
A 5
B 2
C 10
D 7
Now, let's define an accum() operator:
accum(n) = element[0].my_num + element[1].my_num + ... + element[n-1].my_num
So the accum values for each element are
element my_num accum
----------------------------
A 5 5
B 2 7
C 10 17
D 7 24
But accum values probably should NOT be stored in database because the list is constantly updated. It's better to keep insertion fast.
I want to design a query which input is an integer x:
query(x) = element[i] if accum(i-1) < x <= accum(i)
For example, query(11) is C and query(3) is A.
Is it possible to design a datastore schema to make this query fast? Or the only way is accumulate it one by one at query time which I'm planning to do?