/
serialize.js
274 lines (179 loc) · 4.92 KB
/
serialize.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// TODO return promises instead of inner await
import { IndexInterface, DocumentInterface } from "./type.js";
import { create_object, is_string } from "./common.js";
function async(callback, self, field, key, index_doc, index, data, on_done){
setTimeout(function(){
const res = callback(field ? field + "." + key : key, JSON.stringify(data));
// await isn't supported by ES5
if(res && res["then"]){
res["then"](function(){
self.export(callback, self, field, index_doc, index + 1, on_done);
})
}
else{
self.export(callback, self, field, index_doc, index + 1, on_done);
}
});
}
/**
* @this IndexInterface
*/
export function exportIndex(callback, self, field, index_doc, index, on_done){
let return_value = true
if (typeof on_done === 'undefined') {
return_value = new Promise((resolve) => {
on_done = resolve
})
}
let key, data;
switch(index || (index = 0)){
case 0:
key = "reg";
// fastupdate isn't supported by export
if(this.fastupdate){
data = create_object();
for(let key in this.register){
data[key] = 1;
}
}
else{
data = this.register;
}
break;
case 1:
key = "cfg";
data = {
"doc": 0,
"opt": this.optimize ? 1 : 0
};
break;
case 2:
key = "map";
data = this.map;
break;
case 3:
key = "ctx";
data = this.ctx;
break;
default:
if (typeof field === 'undefined' && on_done) {
on_done();
}
return;
}
async(callback, self || this, field, key, index_doc, index, data, on_done);
return return_value;
}
/**
* @this IndexInterface
*/
export function importIndex(key, data){
if(!data){
return;
}
if(is_string(data)){
data = JSON.parse(data);
}
switch(key){
case "cfg":
this.optimize = !!data["opt"];
break;
case "reg":
// fastupdate isn't supported by import
this.fastupdate = false;
this.register = data;
break;
case "map":
this.map = data;
break;
case "ctx":
this.ctx = data;
break;
}
}
/**
* @this DocumentInterface
*/
export function exportDocument(callback, self, field, index_doc, index, on_done){
let return_value
if (typeof on_done === 'undefined') {
return_value = new Promise((resolve) => {
on_done = resolve
})
}
index || (index = 0);
index_doc || (index_doc = 0);
if(index_doc < this.field.length){
const field = this.field[index_doc];
const idx = this.index[field];
self = this;
setTimeout(function(){
if(!idx.export(callback, self, index ? field/*.replace(":", "-")*/ : "", index_doc, index++, on_done)){
index_doc++;
index = 1;
self.export(callback, self, field, index_doc, index, on_done);
}
});
}
else{
let key, data;
switch(index){
case 1:
key = "tag";
data = this.tagindex;
field = null;
break;
case 2:
key = "store";
data = this.store;
field = null;
break;
// case 3:
//
// key = "reg";
// data = this.register;
// break;
default:
on_done();
return;
}
async(callback, this, field, key, index_doc, index, data, on_done);
}
return return_value
}
/**
* @this DocumentInterface
*/
export function importDocument(key, data){
if(!data){
return;
}
if(is_string(data)){
data = JSON.parse(data);
}
switch(key){
case "tag":
this.tagindex = data;
break;
case "reg":
// fastupdate isn't supported by import
this.fastupdate = false;
this.register = data;
for(let i = 0, index; i < this.field.length; i++){
index = this.index[this.field[i]];
index.register = data;
index.fastupdate = false;
}
break;
case "store":
this.store = data;
break;
default:
key = key.split(".");
const field = key[0];
key = key[1];
if(field && key){
this.index[field].import(key, data);
}
}
}