これは、アイテムを挿入できる位置を (コールバックの形式で) 返し、前のアイテムの位置を更新するアルゴリズムです。
Put the item into infinite length array (assumed), 0<=index<INFINITY,
and for empty array largest_index_occupied = 0. ALSO largest_index_occupied is dynamically retrieved from database.(assumed). No two item can share same index. No item must be lost during any operation. indexes are in increasing order.
1. if index not provided OR index provided is greater than largest_index_occupied then put item at index : largest_index_occupied+5;
2. If index provided AND less than equals to largest_index_occupied, :
a. if (No other item already exists at that index) : simply put the item at that index
b. otherwise (means if a item already exists at that index) : increase the index of all items by one untill we get an empty index. and put the new item at the index actually passed by user(which must be empty now).
2.b 例:
言う - 空のインデックスを示します。
以前の状態
0 - - - - 5 6 7 - - 10
a - - - - b c d - - e
インデックスでの入力:5,f
新しい状態:
0 - - - - 5 6 7 8 - 10
a - - - - f b c d - e
Noe f の新しいインデックスは 5 です。
私の要件は、nodejs で async、recursion、および IO 操作 (db update - 2.b の場合のアイテムのインデックス) を組み合わせて上記のコードを記述することです。
next(err,indexToBeInserted);
すべての更新が行われた後にのみ呼び出す必要があるコールバックを期待しています。
私の機能ブロックはここにあります:
toInsert(mongooseModel,next,indexToBeInserted) {
// async code goes here.
}
ps : より良い解決策があれば共有してください。