async + recursive + database-update operation with nodejs

爷,独闯天下 提交于 2019-12-23 05:14:39

问题


Here is the algorithm which returns the position(in form of a callback) at which an item can be inserted and also update the position of the previous items.

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 example :

say - shows empty index.

previous state

0 - - - - 5 6 7 - - 10
a - - - - b c d - - e

input at index : 5,f

new state :

0 - - - - 5 6 7 8 - 10
a - - - - f b c d - e

Noe f's new index is 5.

My requirement is to write a code for the above in combination of async,recursion and IO operation (db update - of the index of items in case of 2.b) in nodejs.

i am expecting next(err,indexToBeInserted); callback that has to called only after all the updation has taken place.

My function block goes here :

toInsert(mongooseModel,next,indexToBeInserted) {
 // async code goes here.
}

ps : if you have any better solution please share.

来源:https://stackoverflow.com/questions/19470994/async-recursive-database-update-operation-with-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!