-
Notifications
You must be signed in to change notification settings - Fork 1
/
search.js
110 lines (109 loc) · 2.7 KB
/
search.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
var data = {},
index = {},
masterParser = require('./masterParser'),
fs = require('fs'),
pending = 0;
function add(userAddress, obj) {
console.log(obj);
data[userAddress] = {
userAddress: userAddress,
textFields: obj.textFields || {},
images: obj.images || {},
tools: obj.tools || {},
type: 'row'
}
var words = [];
if(data[userAddress].textFields.fullName) {
word = data[userAddress].textFields.fullName.split(' ');
}
if(data[userAddress].textFields.nick) {
words.push(data[userAddress].textFields.nick);
}
for(var i in words) {
for(var j=3; j<=words[i].length; j++) {
var prefix = words[i].substring(0, j);
if(!index[prefix.toLowerCase()]) {
index[prefix.toLowerCase()]={};
}
index[prefix.toLowerCase()][userAddress]=true;
}
}
dumpDb();
}
function dumpDb() {
fs.writeFile("./dump.json", JSON.stringify({data: data, index: index}), function(err) {
if(err) {
console.log(err);
} else {
console.log("The file was saved!");
}
});
}
function readDb() {
fs.readFile("./dump.json", function(err, str) {
if(err) {
console.log(err);
} else {
try {
var parsed = JSON.parse(str);
data = parsed.data;
index = parsed.index;
console.log("The file was loaded!");
} catch(e) {
}
}
});
}
readDb();
var sessions = [];
exports.getSession = function() {
return (function() {
var rowCb = function(row) {
console.log(row);
};
var statusCb = function(status) {
console.log(status);
};
return {
on: function(eventType, cb) {
if(eventType == 'row') {
rowCb = cb;
} else if(eventType == 'status') {
statusCb = cb;
}
},
search: function search(str) {
if(index[str]) {
for(var i in index[str]) {
if(data[i]) {
var obj = data[i];
obj.from = 'index';
obj.query = str;
console.log('result from index');
rowCb(obj);
}
}
}
pending++;
console.log('pending++: '+pending+' '+data);
statusCb(pending>0?'busy':'idle');
masterParser.parse(str, 'input', function(err, obj) {
pending--;
console.log('pending--: '+pending);
statusCb(pending>0?'busy':'idle');
if(obj) {
obj.userAddress = str;
add(str, obj);
obj.from='live';
obj.query=str;
console.log('result from live:');
console.log(obj);
rowCb(obj);
}
});
},
close: function() {
}
};
})();
};