mirror of
https://github.com/wassname/HackFlowy.git
synced 2026-06-27 16:00:04 +08:00
REVISION CONTROL
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
var _ = require('underscore');
|
||||
var MySnap = require('../models/Snap.js').MySnap
|
||||
module.exports.getAndSendRevHistory = getAndSendRevHistory;
|
||||
|
||||
|
||||
function getAndSendRevHistory(rootId, sock){
|
||||
globalList = [];
|
||||
snapHash = {};
|
||||
timeHash = {};
|
||||
depth = 0;
|
||||
socket = sock;
|
||||
|
||||
var rootId = "53ea3f506dc8d39342bf4f9f";
|
||||
// FetchSelfAndChildrenBack(rootId, asyncLoopGetChildren);
|
||||
asyncLoopGetChildren([rootId]);
|
||||
}
|
||||
|
||||
//This is all the backSnaps for ONE rootId.
|
||||
function updateGlobals(rootSnaps){
|
||||
var rootId = rootSnaps[0].cur_id;
|
||||
snapHash[rootId] = snapHash[rootId] || rootSnaps;
|
||||
//snapHash[rootId] = rootSnaps; //should be equivalent.
|
||||
|
||||
_.each(rootSnaps, function(snap){
|
||||
(timeHash[snap.timestamp] = timeHash[snap.timestamp] || []).push(snap);
|
||||
});
|
||||
|
||||
globalList.push(rootId);
|
||||
}
|
||||
|
||||
//This should only be called once per ID.
|
||||
function FetchSelfAndChildrenBack(rootId, callback){
|
||||
MySnap.find({cur_id: rootId}).sort({timestamp: 1}).exec(function(err, rootSnaps){
|
||||
if(err){console.log("ERROR HERE")}
|
||||
|
||||
updateGlobals(rootSnaps);
|
||||
|
||||
var metaArray = [];
|
||||
_.each(rootSnaps,function(snap){metaArray.push(snap.children)})
|
||||
|
||||
mergeArrays(metaArray, callback); //(mergeArrays, then get children)
|
||||
});
|
||||
}
|
||||
|
||||
function mergeArrays(metaArray, callback){
|
||||
// console.log("MERGE_SNAPs_CHILDREN");
|
||||
// console.log("metaArray")
|
||||
// console.log(metaArray);
|
||||
mergedArray = _.union(_.flatten(metaArray));
|
||||
mergedArray = mergedArray.filter( function(a){if (!this[a]) {this[a] = 1; return a;}},{});
|
||||
// console.log("mergedArray");
|
||||
// console.log(mergedArray);
|
||||
|
||||
callback(mergedArray); //d2temp.push(merged); loopStep();
|
||||
}
|
||||
|
||||
|
||||
//d2=nodes in the next depth.//(maybe been FetchSelfAndChildrenBack'd)
|
||||
//globalList=nodes in this depth. //(have bene fetch'd)
|
||||
//temp = nodes to fetch next.
|
||||
function MergeAndDiffArraysSync(d2){
|
||||
var temp = [];
|
||||
_.each(d2, function(el){
|
||||
if(globalList.indexOf(el) == -1){
|
||||
temp.push(el);
|
||||
}
|
||||
});
|
||||
console.log("d2");
|
||||
console.log(d2);
|
||||
globalList = _.union(globalList, temp);
|
||||
return temp
|
||||
}
|
||||
|
||||
|
||||
//BFS from one node to all of its adjacents.
|
||||
//d1Union has all the unique nodes that have ever been a child of root.
|
||||
function asyncLoopGetChildren(d1Union){
|
||||
var i = 0;
|
||||
var numTimes = d1Union.length;
|
||||
var nextDepthList = [];
|
||||
|
||||
|
||||
if(typeof d1Union == 'undefined' || d1Union.length == 0){//bottomed out recursion = end of bfs
|
||||
console.log("finished!!!!")
|
||||
console.log("snapHash");
|
||||
console.log(snapHash);
|
||||
console.log("timeHash");
|
||||
console.log(timeHash);
|
||||
console.log("globalList");
|
||||
console.log(globalList);
|
||||
|
||||
socket.emit("revHistory", [snapHash, timeHash]);
|
||||
return;
|
||||
}
|
||||
|
||||
function loopStep(){
|
||||
if(i == numTimes){//call completion function
|
||||
d2Union = _.union(_.flatten(nextDepthList)); //array of arrays.
|
||||
|
||||
//these can easily be done using callbacks if this causes a bug.
|
||||
var temp = MergeAndDiffArraysSync(d2Union);
|
||||
asyncLoopGetChildren(temp);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// fn(loopStep);
|
||||
FetchSelfAndChildrenBack(d1Union[i], function(mergedArray){
|
||||
nextDepthList.push(mergedArray);
|
||||
loopStep();
|
||||
});
|
||||
i+=1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
loopStep();
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user