1
0
mirror of https://github.com/nextapps-de/flexsearch.git synced 2025-09-25 21:08:59 +02:00

document resolver, reduce code duplication, refactoring

This commit is contained in:
Thomas Wilkerling
2025-03-19 14:33:22 +01:00
parent 0455fa4b74
commit d9f235ce3b
107 changed files with 11949 additions and 10740 deletions

View File

@@ -7,6 +7,11 @@ const chunk_size_reg = 250000,
chunk_size_ctx = 1000;
/**
* @param {Map} map
* @param {number=} size
* @return {Array<Object>}
*/
function map_to_json(map, size = 0) {
let chunk = [],
json = [];
@@ -25,13 +30,19 @@ function map_to_json(map, size = 0) {
return chunk;
}
/**
* @param {Array<Object>} json
* @param {Map=} map
* @return {Map}
*/
function json_to_map(json, map) {
map || (map = new Map());
for (let i = 0, entry; i < json.length; i++) {
entry = json[i];
map.set(entry[0], entry[1]);
}
return map;
return (/** @type {Map} */map
);
}
function ctx_to_json(ctx, size = 0) {
@@ -89,9 +100,17 @@ function json_to_reg(json, reg) {
}
/**
*
* @param {function(string, string):Promise|void} callback
* @param {string|null|void} field
* @param {string} key
* @param {Array|null} chunk
* @param {number} index_doc
* @param {number} index_obj
* @param {number=} index_prt
* @this {Index|Document}
* @return {Promise}
*/
function save(callback, field, key, chunk, index_doc, index_obj, index_prt = 0) {
const is_arr = chunk && chunk.constructor === Array,
data = is_arr ? chunk.shift() : chunk;
@@ -114,17 +133,17 @@ function save(callback, field, key, chunk, index_doc, index_obj, index_prt = 0)
/**
* @param {function(string,string):Promise|void} callback
* @param {string|null=} field
* @param {number=} index_doc
* @param {number=} index_obj
* @param {!string|null=} _field
* @param {number=} _index_doc
* @param {number=} _index_obj
* @this {Index}
*/
export function exportIndex(callback, field, index_doc, index_obj = 0) {
export function exportIndex(callback, _field, _index_doc = 0, _index_obj = 0) {
let key, chunk;
switch (index_obj) {
switch (_index_obj) {
case 0:
@@ -156,12 +175,12 @@ export function exportIndex(callback, field, index_doc, index_obj = 0) {
return;
}
return save.call(this, callback, field, key, chunk, index_doc, index_obj);
return save.call(this, callback, _field, key, chunk, _index_doc, _index_obj);
}
/**
* @param {string} key
* @param {string|*} data
* @param {string|Array<Object>} data
* @this Index
*/
@@ -170,8 +189,8 @@ export function importIndex(key, data) {
if (!data) {
return;
}
if (is_string(data)) {
data = JSON.parse( /** @type {string} */data);
if ("string" == typeof data) {
data = /** @type {Array<Object>} */JSON.parse( /** @type {string} */data);
}
const split = key.split(".");
@@ -189,7 +208,7 @@ export function importIndex(key, data) {
case "reg":
// fast update isn't supported by export/import
this.fastupdate = /* suggest */ /* append: */ /* enrich */!1;
this.fastupdate = /* suggest */ /* append: */ /* enrich */ /* resolve: */!1;
this.reg = json_to_reg(data, this.reg);
break;
@@ -207,60 +226,60 @@ export function importIndex(key, data) {
/**
* @param {function(string,string):Promise|void} callback
* @param {string|null=} field
* @param {number=} index_doc
* @param {number=} index_obj
* @param {string|null=} _field
* @param {number=} _index_doc
* @param {number=} _index_obj
* @this {Document}
*/
export function exportDocument(callback, field, index_doc = 0, index_obj = 0) {
export function exportDocument(callback, _field, _index_doc = 0, _index_obj = 0) {
if (index_doc < this.field.length) {
const field = this.field[index_doc],
if (_index_doc < this.field.length) {
const field = this.field[_index_doc],
idx = this.index.get(field),
res = idx.export(callback, field, index_doc, index_obj = 1);
res = idx.export(callback, field, _index_doc, _index_obj = 1);
// start from index 1, because document indexes does not additionally store register
if (res && res.then) {
const self = this;
return res.then(function () {
return self.export(callback, field, index_doc + 1);
return self.export(callback, field, _index_doc + 1);
});
}
return this.export(callback, field, index_doc + 1);
return this.export(callback, field, _index_doc + 1);
} else {
let key, chunk;
switch (index_obj) {
switch (_index_obj) {
case 0:
key = "reg";
chunk = reg_to_json(this.reg);
field = null;
_field = null;
break;
case 1:
key = "tag";
chunk = ctx_to_json(this.tag, this.reg.size);
field = null;
_field = null;
break;
case 2:
key = "doc";
chunk = map_to_json(this.store);
field = null;
_field = null;
break;
case 3:
key = "cfg";
chunk = {};
field = null;
_field = null;
break;
default:
@@ -268,13 +287,13 @@ export function exportDocument(callback, field, index_doc = 0, index_obj = 0) {
return;
}
return save.call(this, callback, field, key, chunk, index_doc, index_obj);
return save.call(this, callback, _field, key, chunk, _index_doc, _index_obj);
}
}
/**
* @param key
* @param {string|*} data
* @param {!string} key
* @param {string|Array<Object>} data
* @this {Document}
*/
@@ -283,8 +302,8 @@ export function importDocument(key, data) {
if (!data) {
return;
}
if (is_string(data)) {
data = JSON.parse( /** @type {string} */data);
if ("string" == typeof data) {
data = /** @type {Array<Object>} */JSON.parse( /** @type {string} */data);
}
const split = key.split(".");