-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
159 lines (143 loc) · 3.24 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
var mongo = require('mongoskin');
var _ = require('lodash');
var vertexName = {};
var ret = {};
var doc;
module.exports = function(url, docName){
var db = mongo.db(url, {native_parser:true});
doc = db.bind(docName);
ret.delete = function(data, callback){
doc
.remove(data, function(err, result){
callback(err, result);
});
}
ret.update = function(data, update, callback){
doc
.update(data, { $set : update }, { multi : true }, function(err, row){
callback(err, row)
})
}
ret.insert = function(data, callback){
doc
.insert(data, function(err, result){
callback(err, result);
});
}
ret.search = function(dataSearch, callback){
vertexName = {}
var dataLen = dataSearch.length;
var dataArr = [];
for (var i = dataLen - 1; i >= 0; i--) {
dataArr.push(dataSearch[i])
};
traverse(dataLen, dataArr, callback)
}
return ret;
}
function traverse(iRecursive, data, callback){
iRecursive--;
var subj = data[iRecursive].subj;
var pred = data[iRecursive].pred;
var obj = data[iRecursive].obj;
if (subj == undefined) {
subj = '';
delete data[iRecursive].subj
};
if (pred == undefined) {
pred = '';
delete data[iRecursive].pred
};
if (obj == undefined) {
obj = '';
delete data[iRecursive].obj
};
var nulledProp = 0;
var subjName, predName, objName;
if (subj[0] == '$') {
nulledProp += 1;
subjName = subj.substring(1);
if (vertexName[subjName]) {
data[iRecursive].subj = { $in : vertexName[subjName] }
}
else{
vertexName[subjName] = []
delete data[iRecursive].subj
}
}
else if(subj[0] == '!' && subj[1] == '$'){
nulledProp += 1;
subjName = subj.substring(2);
if (vertexName[subjName]) {
data[iRecursive].subj = { $nin : vertexName[subjName] }
}
else{
vertexName[subjName] = []
delete data[iRecursive].subj
}
}
if (pred[0] == '$') {
nulledProp += 2;
predName = pred.substring(1);
if (vertexName[predName]) {
data[iRecursive].pred = { $in : vertexName[predName] }
}
else{
vertexName[predName] = []
delete data[iRecursive].pred
}
}
else if(pred[0] == '!' && pred[1] == '$'){
nulledProp += 2;
predName = pred.substring(2);
if (vertexName[predName]) {
data[iRecursive].pred = { $nin : vertexName[predName] }
}
else{
vertexName[predName] = []
delete data[iRecursive].pred
}
}
if (obj[0] == '$') {
objName = obj.substring(1);
nulledProp += 4;
if (vertexName[objName]) {
data[iRecursive].obj = { $in : vertexName[objName] }
}
else{
vertexName[objName] = []
delete data[iRecursive].obj
}
}
else if(obj[0] == '!' && obj[1] == '$'){
nulledProp += 4;
objName = obj.substring(2);
if (vertexName[objName]) {
data[iRecursive].obj = { $nin : vertexName[objName] }
}
else{
vertexName[objName] = []
delete data[iRecursive].obj
}
}
doc
.find(data[iRecursive])
.toArray(function(err, result){
if(nulledProp & 1){
vertexName[subjName] = _.uniq(_.pluck(result, 'subj'));
}
if(nulledProp & 2){
vertexName[predName] = _.uniq(_.pluck(result, 'pred'));
}
if(nulledProp & 4){
vertexName[objName] = _.uniq(_.pluck(result, 'obj'));
}
if (iRecursive === 0) {
vertexName.value = result;
callback(err, vertexName)
}
else{
traverse(iRecursive, data, callback)
}
})
}