1
0
mirror of https://github.com/nextapps-de/flexsearch.git synced 2025-10-01 23:56:41 +02:00

fast-boot serialization for server-side-rendering

This commit is contained in:
Thomas Wilkerling
2025-03-10 16:54:01 +01:00
parent da475effee
commit 3a00dba465
27 changed files with 2055 additions and 1223 deletions

View File

@@ -276,4 +276,165 @@ export function importDocument(key, data) {
this.index[field].import(key, data);
}
}
}
}
/*
reg: "1,2,3,4,5,6,7,8,9"
map: "gulliver:1,2,3|4,5,6|7,8,9;"
ctx: "gulliver+travel:1,2,3|4,5,6|7,8,9;"
*/
/**
* @this Index
* @param {boolean} withFunctionWrapper
* @return {string}
*/
export function serialize(withFunctionWrapper = !0) {
if (!this.reg.size) return "";
let reg = '',
type = "";
for (const key of this.reg.keys()) {
type || (type = typeof key);
reg += (reg ? ',' : '') + ("string" == type ? '"' + key + '"' : key);
}
reg = 'index.reg=new Set([' + reg + ']);';
let map = '';
for (const item of this.map.entries()) {
const key = item[0],
value = item[1];
let res = '';
for (let i = 0, ids; i < value.length; i++) {
ids = value[i] || [''];
let str = '';
for (let j = 0; j < ids.length; j++) {
str += (str ? ',' : '') + ("string" == type ? '"' + ids[j] + '"' : ids[j]);
}
str = '[' + str + ']';
res += (res ? ',' : '') + str;
}
res = '["' + key + '",[' + res + ']]';
map += (map ? ',' : '') + res;
}
map = "index.map=new Map([" + map + "]);";
let ctx = '';
for (const context of this.ctx.entries()) {
const key_ctx = context[0],
value_ctx = context[1];
for (const item of value_ctx.entries()) {
const key = item[0],
value = item[1];
let res = '';
for (let i = 0, ids; i < value.length; i++) {
ids = value[i] || [''];
let str = '';
for (let j = 0; j < ids.length; j++) {
str += (str ? ',' : '') + ("string" == type ? '"' + ids[j] + '"' : ids[j]);
}
str = '[' + str + ']';
res += (res ? ',' : '') + str;
}
res = 'new Map([["' + key + '",[' + res + ']]])';
res = '["' + key_ctx + '",' + res + ']';
ctx += (ctx ? ',' : '') + res;
}
}
ctx = "index.ctx=new Map([" + ctx + "]);";
return withFunctionWrapper ? "function inject(index){" + reg + map + ctx + "}" : reg + map + ctx;
}
// export function exportSnapshot(flexsearch){
//
// if(!flexsearch.reg.size) return;
//
// let reg = '';
// let type = "";
// for(const key of flexsearch.reg.keys()){
// type || (type = typeof key);
// reg += (reg ? ',' : '') + (type === "string" ? '"' + key + '"' : key);
// }
// reg = "f.reg=[" + reg + "];";
//
// let map = '';
// for(const item of flexsearch.map.entries()){
// const key = item[0];
// const value = item[1];
// let res = '';
// for(let i = 0, ids; i < value.length; i++){
// ids = value[i] || [''];
// let str = '';
// for(let j = 0; j < ids.length; j++){
// str += (str ? ',' : '') + (type === "string" ? '"' + ids[j] + '"' : ids[j]);
// }
// str = "[
// res += (res ? '|' : '') + str;
// }
// map += (map ? ';' : '') + key + ':' + res;
// }
//
//
// let ctx = '';
// for(const context of flexsearch.ctx.entries()){
// const key_ctx = context[0];
// const value_ctx = context[1];
//
// for(const item of value_ctx.entries()){
// const key = item[0];
// const value = item[1];
//
// let res = '';
// for(let i = 0, ids; i < value.length; i++){
// ids = value[i] || [''];
// let str = '';
// for(let j = 0; j < ids.length; j++){
// str += (str ? ',' : '') + (type === "string" ? '"' + ids[j] + '"' : ids[j]);
// }
// res += (res ? '|' : '') + str;
// }
// ctx += (ctx ? ';' : '') + key_ctx + '+' + key + ':' + res;
// }
// }
//
// return reg + '#' + map + '#' + ctx;
// }
//
// export function importSnapshot(flexsearch, input){
//
// if(!input) return;
//
// let pos_reg = input.indexOf("#");
// const reg = input.substring(0, pos_reg).split(",");
// flexsearch.reg = new Set(reg);
//
// let pos_map = input.indexOf("#", pos_reg + 1);
// const map = input.substring(pos_reg + 1, pos_map).split(";").map(res => {
// const split = res.split(":");
// split[1] = split[1].split("|").map(ids => ids ? ids.split(",") : null);
// return split;
// });
// flexsearch.map = new Map(map);
//
// input = input.substring(pos_map + 1);
// if(input){
// const ctx = input.split(";").map(res => {
// const split = res.split(":");
// const context = split[0].split("+");
// const map = new Map([
// [context[1], split[1].split("|").map(ids => ids ? ids.split(",") : null)]
// ]);
// return [context[0], map]
// });
// flexsearch.ctx = new Map(ctx);
// }
// }