Skip to content

Commit 9a305b6

Browse files
committed
first draft :)
1 parent 8dfe025 commit 9a305b6

File tree

2 files changed

+207
-0
lines changed

2 files changed

+207
-0
lines changed

run_query.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
var webgme = require('webgme'),
2+
requirejs = requirejs || require('requirejs');
3+
4+
//TODO getting some configuration
5+
var config = {
6+
mongodatabase:'query',
7+
mongoip:'127.0.0.1'
8+
};
9+
webGMEGlobal.setConfig(config);
10+
11+
requirejs(['src/query'],function(QUERY){
12+
var myConfig = webGMEGlobal.getConfig();
13+
var storage = new webgme.serverUserStorage({'host':myConfig.mongoip, 'port':myConfig.mongoport, 'database':myConfig.mongodatabase});
14+
storage.openDatabase(function(err) {
15+
if (!err) {
16+
storage.openProject('querytest', function (err, project) {
17+
if (!err) {
18+
var core = new webgme.core(project, {corerel: 2});
19+
project.getBranchHash("master","#hack",function(err,commitHash){
20+
if(!err){
21+
project.loadObject(commitHash,function(err,commit) {
22+
if (!err && commit) {
23+
core.loadRoot(commit.root, function (err, root) {
24+
if(!err){
25+
var query = QUERY.getQuery(root,core);
26+
query.children().children().parent().parent().toArray(function(err,nodes){
27+
for(var i=0;i<nodes.length;i++){
28+
console.log(core.getAttribute(nodes[i],'name'));
29+
}
30+
storage.closeDatabase();
31+
});
32+
}
33+
});
34+
}
35+
});
36+
}
37+
});
38+
}
39+
});
40+
}
41+
});
42+
});

src/query.js

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
define([], function () {
2+
"use strict";
3+
function multipleNodeOriginatedQuery(nodes,core){
4+
var _object = {};
5+
6+
return _object;
7+
}
8+
9+
function singleNodeOriginatedQuery(node,core){
10+
var self = this,
11+
query = {},
12+
nodesCache = {},
13+
nodes = {},
14+
toRemove = {},
15+
callingQueue = [],
16+
error = null;
17+
18+
nodesCache[core.getPath(node)] = node;
19+
nodes[core.getPath(node)] = true;
20+
21+
//internal functions
22+
var removeNode = function(nodeId) {
23+
toRemove[nodeId] = true;
24+
},
25+
addNode = function(node) {
26+
var nodeId = core.getPath(node);
27+
//handling in the query
28+
nodes[nodeId] = true;
29+
delete toRemove[nodeId];
30+
//handling in the cache
31+
if(!nodesCache[nodeId]){
32+
nodesCache[nodeId] = node;
33+
}
34+
},
35+
nodeArray = function(){
36+
var keys = Object.keys(nodes),
37+
items = [],
38+
i;
39+
for(i=0;i<keys.length;i++){
40+
items.push(keys[i]);
41+
}
42+
return items;
43+
},
44+
addFunctionToQueue = function(functionObject){
45+
if(callingQueue.length === 0){
46+
callingQueue.push(functionObject);
47+
return callNextInQueue();
48+
} else {
49+
callingQueue.push(functionObject)
50+
}
51+
},
52+
callNextInQueue = function(){
53+
var next = callingQueue[0];
54+
if(next && typeof next.function === 'function'){
55+
next.function.apply(self,next.parameters || []);
56+
}
57+
},
58+
finishedInQueue = function(){
59+
//cleaning the toRemove from nodes
60+
var keys = Object.keys(toRemove),
61+
i;
62+
for(i=0;i<keys.length;i++){
63+
delete nodes[keys[i]];
64+
}
65+
toRemove = {};
66+
67+
callingQueue.shift();
68+
},
69+
children = function(){
70+
var startingNodeIds = nodeArray(),
71+
i,
72+
waiting = startingNodeIds.length,
73+
childrenLoaded = function(err,children){
74+
error = error || err;
75+
if(!err){
76+
for(var i=0;i<children.length;i++){
77+
addNode(children[i]);
78+
}
79+
}
80+
if(--waiting === 0){
81+
//we are finished the loading so we can go to the next function in the line
82+
finishedInQueue();
83+
return callNextInQueue();
84+
}
85+
};
86+
87+
88+
//what if there is nothing to do
89+
if(waiting === 0){
90+
finishedInQueue();
91+
return callNextInQueue();
92+
}
93+
94+
for(i=0;i<startingNodeIds.length;i++){
95+
removeNode(startingNodeIds[i]);
96+
core.loadChildren(nodesCache[startingNodeIds[i]],childrenLoaded);
97+
}
98+
},
99+
parent = function(){
100+
var startingNodeIds = nodeArray(),
101+
parent,
102+
i;
103+
for(i=0;i<startingNodeIds.length;i++){
104+
removeNode(startingNodeIds[i]);
105+
parent = core.getParent(nodesCache[startingNodeIds[i]]);
106+
if(parent){
107+
addNode(parent);
108+
}
109+
}
110+
finishedInQueue();
111+
return callNextInQueue();
112+
},
113+
base = function(){
114+
var startingNodeIds = nodeArray(),
115+
base,
116+
i;
117+
for(i=0;i<startingNodeIds.length;i++){
118+
removeNode(startingNodeIds[i]);
119+
base = core.getBase(nodesCache[startingNodeIds[i]]);
120+
if(base){
121+
addNode(base);
122+
}
123+
}
124+
finishedInQueue();
125+
return callNextInQueue();
126+
},
127+
toNodeArray = function(callback){
128+
var nodeIds = nodeArray(),
129+
i,
130+
items=[];
131+
for(i=0;i<nodeIds.length;i++){
132+
items.push(nodesCache[nodeIds[i]]);
133+
}
134+
callback(error,items);
135+
};
136+
137+
138+
//traversing functions
139+
//they all create a new promise type query object
140+
query.children = function(){
141+
addFunctionToQueue({function:children,parameters:[]});
142+
return query;
143+
};
144+
query.parent = function(){
145+
addFunctionToQueue({function:parent,parameters:[]});
146+
return query;
147+
};
148+
query.base = function(){
149+
addFunctionToQueue({function:base,parameters:[]});
150+
return query;
151+
};
152+
153+
//async functions - they didn't return with the query object!!!
154+
query.toArray = function(callback){
155+
addFunctionToQueue({function:toNodeArray,parameters:[callback]});
156+
};
157+
158+
return query;
159+
}
160+
return {
161+
getQuery : singleNodeOriginatedQuery,
162+
getMultiQuery : multipleNodeOriginatedQuery
163+
}
164+
});
165+

0 commit comments

Comments
 (0)