1
0
mirror of https://github.com/nextapps-de/flexsearch.git synced 2025-09-26 05:19:00 +02:00
Files
flexsearch/dist/module-min/db/redis/index.js
Thomas Wilkerling 2431b51a4f update index.d.ts
2025-05-08 22:08:07 +02:00

1 line
6.7 KiB
JavaScript

import{createClient}from"redis";const defaults={host:"localhost",port:"6379",user:null,pass:null},VERSION=1,fields=["map","ctx","reg","tag","doc","cfg"];import StorageInterface from"../interface.js";import{toArray}from"../../common.js";function sanitize(a){return a.toLowerCase().replace(/[^a-z0-9_\-]/g,"")}let DB,TRX;export default function RedisDB(a,b={}){return this&&this.constructor===RedisDB?void("object"==typeof a&&(b=a,a=a.name),!a&&console.info("Default storage space was used, because a name was not passed."),this.id=(a?sanitize(a):"flexsearch")+"|",this.field=b.field?"-"+sanitize(b.field):"",this.type=b.type||"",this.fastupdate=!0,this.db=b.db||DB||null,this.support_tag_search=!0,this.resolution=9,this.resolution_ctx=9,Object.assign(defaults,b),this.db&&delete defaults.db):new RedisDB(a,b)}RedisDB.prototype.mount=function(a){return a.index?a.mount(this):(a.db=this,this.resolution=a.resolution,this.resolution_ctx=a.resolution_ctx,this.open())},RedisDB.prototype.open=async function(){if(this.db)return this.db;if(DB)return this.db=DB;let a=defaults.url;return a||(a=defaults.user?`redis://${defaults.user}:${defaults.pass}@${defaults.host}:${defaults.port}`:`redis://${defaults.host}:${defaults.port}`),this.db=DB=await createClient(a).on("error",a=>console.error(a)).connect()},RedisDB.prototype.close=async function(){return DB&&(await this.db.disconnect()),this.db=DB=null,this},RedisDB.prototype.destroy=function(){return this.clear()},RedisDB.prototype.clear=function(){return this.db.unlink([this.id+"map"+this.field,this.id+"ctx"+this.field,this.id+"tag"+this.field,this.id+"cfg"+this.field,this.id+"doc",this.id+"reg"])};function create_result(a,b,c,d,e){if(c){if("number"===b)for(let c,e,f=0;f<a.length;f++)c=a[f],e="number"===b?parseInt(c.id||c,10):c.id||c,a[f]=d?{id:e,doc:c.doc}:e;return a}else{let c=[];for(let d,f,g,h=0;h<a.length;h++)d=a[h],f="number"===b?parseInt(d.id||d,10):d.id||d,g=e-d.score,c[g]||(c[g]=[]),c[g].push(f);return c}}RedisDB.prototype.get=function(a,b,c=0,d=0,e=!0,f=!1,g){if(g){const h=b?[b,a]:[a];return this.search({depth:!!b},h,c,d,!1,e,f,g)}const h=this.type,i=this;let j;return j=b?this.db[e?"zRange":"zRangeWithScores"](this.id+"ctx"+this.field+":"+b+":"+a,""+d,""+(d+c-1),{REV:!0}):this.db[e?"zRange":"zRangeWithScores"](this.id+"map"+this.field+":"+a,""+d,""+(d+c-1),{REV:!0}),j.then(async function(a){return a.length?(f&&(a=await i.enrich(a)),create_result(a,h,e,f,b?i.resolution_ctx:i.resolution)):a})},RedisDB.prototype.tag=function(a,b=0,c=0,d=!1){const e=this;return this.db.sMembers(this.id+"tag"+this.field+":"+a).then(function(a){if(!a||!a.length||c>=a.length)return[];if(!b&&!c)return a;const f=a.slice(c,c+b);return d?e.enrich(f):f})},RedisDB.prototype.enrich=function(a){return"object"!=typeof a&&(a=[a]),this.db.hmGet(this.id+"doc","number"===this.type?a.map(a=>""+a):a).then(function(b){for(let c=0;c<b.length;c++)b[c]={id:a[c],doc:b[c]&&JSON.parse(b[c])};return b})},RedisDB.prototype.has=function(a){return this.db.sIsMember(this.id+"reg",""+a)},RedisDB.prototype.search=function(a,b,c=100,d=0,e=!1,f=!0,g=!1,h){const i=1<b.length&&a.depth;let j,k=[],l=[];if(i){const c=this.id+"ctx"+this.field+":";let d,e=b[0];for(let f,g=1;g<b.length;g++)d=b[g],f=a.bidirectional&&d>e,k.push(c+(f?d:e)+":"+(f?e:d)),l.push(1),e=d}else{const a=this.id+"map"+this.field+":";for(let c=0;c<b.length;c++)k.push(a+b[c]),l.push(1)}b=k;const m=this.type;let n=this.id+"tmp:"+Math.random();if(e){const a=this.db.multi();if(a.zInterStore(n,b,{AGGREGATE:"SUM"}),b.push(n),l.push(b.length),a.zUnionStore(n,b,{WEIGHTS:l,AGGREGATE:"SUM"}),h){b=[n];for(let a=0;a<h.length;a+=2)b.push(this.id+"tag-"+sanitize(h[a])+":"+h[a+1]);a.zInterStore(n,b,{AGGREGATE:"MAX"})}j=a[f?"zRange":"zRangeWithScores"](n,""+d,""+(d+c-1),{REV:!0}).unlink(n).exec()}else{if(h)for(let a=0;a<h.length;a+=2)b.push(this.id+"tag-"+sanitize(h[a])+":"+h[a+1]);j=this.db.multi().zInterStore(n,b,{AGGREGATE:"MAX"})[f?"zRange":"zRangeWithScores"](n,""+d,""+(d+c-1),{REV:!0}).unlink(n).exec()}const o=this;return j.then(async function(a){return(a=e&&h?a[3]:a[e?2:1],!a.length)?a:(g&&(a=await o.enrich(a)),create_result(a,m,f,g,i?o.resolution_ctx:o.resolution))})},RedisDB.prototype.info=function(){},RedisDB.prototype.transaction=function(a,b){if(TRX)return a.call(this,TRX);TRX=this.db.multi();let c=a.call(this,TRX),d=TRX.exec();return TRX=null,Promise.all([c,d]).then(function(){b&&b()})},RedisDB.prototype.commit=async function(a,b,c){if(b)await this.clear(),a.commit_task=[];else{let d=a.commit_task;a.commit_task=[];for(let a,c=0;c<d.length;c++)if(a=d[c],a.clear){await this.clear(),b=!0;break}else d[c]=""+a.del;b||(!c&&(d=d.concat(toArray(a.reg,!0))),d.length&&(await this.remove(d)))}a.reg.size&&(await this.transaction(function(b){let c=new Map;for(const d of a.map){const a=d[0],e=d[1];for(let d,f=0;f<e.length;f++)if((d=e[f])&&d.length){let e=[];for(let a=0;a<d.length;a++)e.push({score:this.resolution-f,value:""+d[a]});"number"==typeof d[0]&&(this.type="number");const g=this.id+"map"+this.field+":"+a;if(b.zAdd(g,e),this.fastupdate)for(let a,b=0;b<d.length;b++){a=d[b];let e=c.get(a);e||c.set(a,e=[]),e.push(g)}}}if(this.fastupdate)for(const a of c){const c=a[0],d=a[1];b.sAdd(this.id+"ref"+this.field+":"+c,d)}c=new Map;for(const d of a.ctx){const a=d[0],e=d[1];for(const d of e){const e=d[0],f=d[1];for(let d,g=0;g<f.length;g++)if((d=f[g])&&d.length){let f=[];for(let a=0;a<d.length;a++)f.push({score:this.resolution_ctx-g,value:""+d[a]});"number"==typeof d[0]&&(this.type="number");const h=this.id+"ctx"+this.field+":"+a+":"+e;if(b.zAdd(h,f),this.fastupdate)for(let a,b=0;b<d.length;b++){a=d[b];let e=c.get(a);e||c.set(a,e=[]),e.push(h)}}}}if(this.fastupdate)for(const a of c){const c=a[0],d=a[1];b.sAdd(this.id+"ref"+this.field+":"+c,d)}if(a.store)for(const c of a.store.entries()){const a=c[0],d=c[1];d&&b.hSet(this.id+"doc",""+a,JSON.stringify(d))}if(!a.bypass){let c=toArray(a.reg,!0);c.length&&b.sAdd(this.id+"reg",c)}if(a.tag)for(const c of a.tag){const a=c[0],d=c[1];if(!d.length)continue;let e=[];if("number"==typeof d[0])for(let a=0;a<d.length;a++)e[a]=""+d[a];else e=d;b.sAdd(this.id+"tag"+this.field+":"+a,e)}}),a.map.clear(),a.ctx.clear(),a.tag&&a.tag.clear(),a.store&&a.store.clear(),a.document||a.reg.clear())},RedisDB.prototype.remove=function(a){if(a||0===a)return"object"!=typeof a&&(a=[a]),a.length?this.transaction(async function(b){for(;a.length;){let c;if(10000<a.length&&(c=a.slice(10000),a=a.slice(0,10000)),"number"==typeof a[0])for(let b=0;b<a.length;b++)a[b]=""+a[b];const d=await this.db.smIsMember(this.id+"reg",a);for(let c,e=0;e<a.length;e++)if(d[e]){if(c=""+a[e],this.fastupdate){const a=await this.db.sMembers(this.id+"ref"+this.field+":"+c);if(a){for(let d=0;d<a.length;d++)b.zRem(a[d],c);b.unlink(this.id+"ref"+this.field+":"+c)}}else;b.hDel(this.id+"doc",c),b.sRem(this.id+"reg",c)}if(c)a=c;else break}}):void 0};