-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.js
166 lines (154 loc) · 3.59 KB
/
engine.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
160
161
162
163
164
165
166
/* Corpus Engine
provide core interface to make use of ksana corpus
*/
const CorpusROM=require("ksana-corpus-rom");
const Corpus=require("./corpus");
const regcor=require("./regcor");
var opening="";
var pool={};
const close=function(id) {
var engine=pool[id];
if (engine) {
engine.kdb.free();
delete pool[id];
}
}
const createEngine=function(id,kdb,opts,cb){//preload meta and other fields
if (typeof opts=="function") {
cb=opts;
opts={};
}
var engine={kdb:kdb};
engine.get=require("./get"); //install first API
engine.local=kdb.local;
engine.expandVariant=opts.expandVariant||function(t){return t};
if (kdb.fs.mergePostings) { //native mergePostings
engine.mergePostings=kdb.fs.mergePostings.bind(kdb.fs);
}
opts.preload=opts.preload||[]; //user specified preload
var preload=[["meta"],["gfields"],["kfields"],["fields","tocrange"]];
if (!opts.textOnly) {
preload.push(["inverted","book2tpos"]
,["inverted","article2tpos"]
,["inverted","tokens"]
,["inverted","posting_length"]
,["inverted","group2tpos"]
);
}
if(typeof console!=="undefined" && console.time) {
console.log("loading "+id);
console.time("preload")
}
opts.preload.forEach(function(p){preload.push(p)});
engine.get(preload,{recursive:true},function(res){
if(typeof console!=="undefined" && console.time) {
console.timeEnd("preload")
}
engine.meta=res[0];
if (!engine.meta.displayOptions) {
engine.meta.displayOptions={};
}
engine.id=id;
engine.registered=!!regcor[engine.id];
Corpus.init(engine);
cb(0,engine);
});
}
const prepareEngine=function(id,kdb,opts,cb){
createEngine(id,kdb,opts,function(err2,engine){
opening="";
if (err2) cb(err2);
else {
if (engine&&engine.meta){
pool[id]=engine;
cb&& cb(0,engine);
} else {
cb&&cb(id+" is invalid");
}
}
});
}
const open=function(id,opts,cb){
if (typeof opts=="function") {
cb=opts;
opts={};
}
var timer=0;
var engine=pool[id];
if (engine) {
cb&&cb(0,engine);
return engine;
}
if (opening) {
timer=setInterval(function(){
if (!opening) {
clearInterval(timer);
_open(id,opts,cb);
}
} ,200);
} else {
return _open(id,opts,cb);
}
}
const isNode=function(){
return (typeof process!=="undefined") &&
process.versions && process.versions.node;
}
const _open=function(id,opts,cb){
var engine=pool[id];
if (engine) {
cb&&cb(0,engine);
return engine;
}
var fn=id,fn2;
opening=id;
opts=opts||{};
const chromeextension=typeof window!=='undefined' && window.location &&
window.location.protocol=="chrome-extension:";
if (typeof fn=="string" && fn.substr(0,5)!=="blob:") {
if (fn.indexOf(".cor")==-1) fn+=".cor";
if (!chromeextension) {
if ((typeof window!=="undefined" && window.node_modules)||isNode()) {
fn2="../"+id+"-corpus/"+fn; //for nw
} else {
const ofn=fn;
fn=id+"-corpus/"+fn;
fn2=ofn;//web mode try xxx-corpus first, to avoid http warning message
}
}
opening=id;
} else {
//input type File
if (fn.name) {
id=fn.name.replace(/\..+$/,"");
opening=id;
} else {
if (!fn.lastIndexOf) {
debugger;
opening=fn;
} else {
opening=fn.substr(fn.lastIndexOf("/")+1);
}
}
}
new CorpusROM.open(fn,function(err,kdb){
if (err) {
if (typeof fn2=="string") {
new CorpusROM.open(fn2,function(err2,kdb2){
if (err2) {
opening="";
cb&&cb(err2);
} else {
prepareEngine(id,kdb2,opts,cb)
}
})
}
} else {
prepareEngine(id,kdb,opts,cb)
}
});
}
const isBusy=function(){
return !!opening;
}
module.exports={open:open,close:close,isBusy:isBusy};