1
0
mirror of https://github.com/nextapps-de/flexsearch.git synced 2025-10-03 16:41:50 +02:00

bundle pre-release

This commit is contained in:
Thomas Wilkerling
2025-03-07 17:44:10 +01:00
parent 5bcbc72dac
commit 25e4b5d712
219 changed files with 44901 additions and 16106 deletions

View File

@@ -1,168 +1,77 @@
import { IndexInterface, DocumentInterface } from "./type.js";
import { create_object, is_object } from "./common.js";
import Index from "./index.js";
import Document from "./document.js";
/**
* @param {string|Object} query
* @param {number|Object=} limit
* @param {Object=} options
* @this {Index|Document}
* @returns {Array<number|string>|Promise}
*/
export function searchCache(query, limit, options) {
query = ("object" == typeof query ? "" + query.query : "" + query).toLowerCase();
//let encoded = this.encoder.encode(query).join(" ");
let cache = this.cache.get(query);
if (!cache) {
cache = this.search(query, limit, options);
if (cache instanceof Promise) {
const self = this;
cache.then(function (cache) {
self.cache.set(query, cache);
});
}
this.cache.set(query, cache);
}
return cache;
}
/**
* @param {boolean|number=} limit
* @constructor
*/
function CacheClass(limit) {
export default function CacheClass(limit) {
/** @private */
this.limit = !0 !== limit && limit;
this.limit = !limit || !0 === limit ? 1000 : limit;
/** @private */
this.cache = create_object();
this.cache = new Map();
/** @private */
this.queue = [];
//this.clear();
this.last = "";
}
export default CacheClass;
/**
* @param {string|Object} query
* @param {number|Object=} limit
* @param {Object=} options
* @this {IndexInterface}
* @returns {Array<number|string>}
*/
export function searchCache(query, limit, options) {
if (is_object(query)) {
query = query.query;
}
let cache = this.cache.get(query);
if (!cache) {
cache = this.search(query, limit, options);
this.cache.set(query, cache);
}
return cache;
}
// CacheClass.prototype.clear = function(){
//
// /** @private */
// this.cache = create_object();
//
// /** @private */
// this.queue = [];
// };
CacheClass.prototype.set = function (key, value) {
if (!this.cache[key]) {
// it is just a shame that native function array.shift() performs so bad
// const length = this.queue.length;
//
// this.queue[length] = key;
//
// if(length === this.limit){
//
// delete this.cache[this.queue.shift()];
// }
// the same bad performance
// this.queue.unshift(key);
//
// if(this.queue.length === this.limit){
//
// this.queue.pop();
// }
// fast implementation variant
// let length = this.queue.length;
//
// if(length === this.limit){
//
// length--;
//
// delete this.cache[this.queue[0]];
//
// for(let x = 0; x < length; x++){
//
// this.queue[x] = this.queue[x + 1];
// }
// }
//
// this.queue[length] = key;
// current fastest implementation variant
// theoretically that should not perform better compared to the example above
let length = this.queue.length;
if (length === this.limit) {
delete this.cache[this.queue[length - 1]];
} else {
length++;
if (!this.cache.has(key)) {
this.cache.set(this.last = key, value);
if (this.limit && this.cache.size > this.limit) {
this.cache.delete(this.cache.keys().next().value);
}
for (let x = length - 1; 0 < x; x--) {
this.queue[x] = this.queue[x - 1];
}
this.queue[0] = key;
}
this.cache[key] = value;
};
CacheClass.prototype.get = function (key) {
const cache = this.cache[key];
if (this.limit && cache) {
// probably the indexOf() method performs faster when matched content is on front (left-to-right)
// using lastIndexOf() does not help, it performs almost slower
const pos = this.queue.indexOf(key);
// if(pos < this.queue.length - 1){
//
// const tmp = this.queue[pos];
// this.queue[pos] = this.queue[pos + 1];
// this.queue[pos + 1] = tmp;
// }
if (pos) {
const tmp = this.queue[pos - 1];
this.queue[pos - 1] = this.queue[pos];
this.queue[pos] = tmp;
}
const cache = this.cache.get(key);
if (cache && this.limit && this.last !== key) {
this.cache.delete(key);
this.cache.set(this.last = key, cache);
}
return cache;
};
CacheClass.prototype.del = function (id) {
CacheClass.prototype.remove = function (id) {
for (const item of this.cache) {
const key = item[0],
value = item[1];
for (let i = 0, item, key; i < this.queue.length; i++) {
key = this.queue[i];
item = this.cache[key];
if (item.includes(id)) {
this.queue.splice(i--, 1);
delete this.cache[key];
if (value.includes(id)) {
this.cache.delete(key);
}
}
};
CacheClass.prototype.clear = function () {
this.cache.clear();
this.last = "";
};