1
0
mirror of https://github.com/nextapps-de/flexsearch.git synced 2025-10-01 07:36:40 +02:00

more tests, more fixes

This commit is contained in:
Thomas Wilkerling
2025-03-24 18:33:20 +01:00
parent 63531ef4f1
commit e878ce5f99
113 changed files with 6722 additions and 5621 deletions

View File

@@ -1,4 +1,4 @@
import pg_promise from"pg-promise";import StorageInterface from"../interface.js";import{concat,toArray}from"../../common.js";const defaults={schema:"flexsearch",user:"postgres",pass:"postgres",name:"postgres",host:"localhost",port:"5432"},pgp=pg_promise({noWarnings:!!1}),VERSION=1,MAXIMUM_QUERY_VARS=16000,fields=["map","ctx","reg","tag","cfg"],types={text:"text",char:"text",varchar:"text",string:"text",number:"int",numeric:"int",integer:"int",smallint:"int",tinyint:"int",mediumint:"int",int:"int",int8:"int",uint8:"int",int16:"int",uint16:"int",int32:"int",uint32:"bigint",int64:"bigint",bigint:"bigint"};function sanitize(a){return a.toLowerCase().replace(/[^a-z0-9_]/g,"")}let DB,TRX;export default function PostgresDB(a,b={}){if(!this)return new PostgresDB(a,b);if("object"==typeof a&&(b=a,a=a.name),a||console.info("Default storage space was used, because a name was not passed."),this.id=(b.schema?sanitize(b.schema):defaults.schema)+(a?"_"+sanitize(a):""),this.field=b.field?"_"+sanitize(b.field):"",this.type=b.type?types[b.type.toLowerCase()]:"text",this.support_tag_search=!0,!this.type)throw new Error("Unknown type of ID '"+b.type+"'");this.db=DB||(DB=b.db||null),Object.assign(defaults,b),this.db&&delete defaults.db}PostgresDB.prototype.mount=function(a){return a.encoder?(a.db=this,this.open()):a.mount(this)},PostgresDB.prototype.open=async function(){this.db||(this.db=DB||(DB=pgp(`postgres://${defaults.user}:${encodeURIComponent(defaults.pass)}@${defaults.host}:${defaults.port}/${defaults.name}`)));const a=await this.db.oneOrNone(`
import pg_promise from"pg-promise";import StorageInterface from"../interface.js";import{concat,toArray}from"../../common.js";const defaults={schema:"flexsearch",user:"postgres",pass:"postgres",name:"postgres",host:"localhost",port:"5432"},pgp=pg_promise(),VERSION=1,MAXIMUM_QUERY_VARS=16000,fields=["map","ctx","reg","tag","cfg"],types={text:"text",char:"text",varchar:"text",string:"text",number:"int",numeric:"int",integer:"int",smallint:"int",tinyint:"int",mediumint:"int",int:"int",int8:"int",uint8:"int",int16:"int",uint16:"int",int32:"int",uint32:"bigint",int64:"bigint",bigint:"bigint"};function sanitize(a){return a.toLowerCase().replace(/[^a-z0-9_]/g,"")}let DB,TRX;export default function PostgresDB(a,b={}){if(!this)return new PostgresDB(a,b);if("object"==typeof a&&(b=a,a=b.name),a||console.info("Default storage space was used, because a name was not passed."),this.id=(b.schema?sanitize(b.schema):defaults.schema)+(a?"_"+sanitize(a):""),this.field=b.field?"_"+sanitize(b.field):"",this.type=b.type?types[b.type.toLowerCase()]:"text",this.support_tag_search=!0,!this.type)throw new Error("Unknown type of ID '"+b.type+"'");this.db=DB||(DB=b.db||null),Object.assign(defaults,b),this.db&&delete defaults.db}PostgresDB.prototype.mount=function(a){return a.encoder?(a.db=this,this.open()):a.mount(this)},PostgresDB.prototype.open=async function(){this.db||(this.db=DB||(DB=pgp(`postgres://${defaults.user}:${encodeURIComponent(defaults.pass)}@${defaults.host}:${defaults.port}/${defaults.name}`)));const a=await this.db.oneOrNone(`
SELECT EXISTS (
SELECT 1
FROM information_schema.schemata
@@ -50,7 +50,7 @@ import pg_promise from"pg-promise";import StorageInterface from"../interface.js"
CREATE TABLE IF NOT EXISTS ${this.id}.cfg${this.field}(
cfg text NOT NULL
);
`);}}return this.db},PostgresDB.prototype.close=function(){return this.db.close&&this.db.close(),this.db=DB=null,this},PostgresDB.prototype.destroy=function(){return this.db.none(`
`);}}return this.db},PostgresDB.prototype.close=function(){return this.db=null,this},PostgresDB.prototype.destroy=function(){return this.db.none(`
DROP TABLE IF EXISTS ${this.id}.map${this.field};
DROP TABLE IF EXISTS ${this.id}.ctx${this.field};
DROP TABLE IF EXISTS ${this.id}.tag${this.field};
@@ -96,13 +96,13 @@ import pg_promise from"pg-promise";import StorageInterface from"../interface.js"
${c?"OFFSET "+c:""}`,[a]);return d||f.then(function(a){return create_result(a,!0,!1)}),f},PostgresDB.prototype.enrich=async function(a){let b=[];"object"!=typeof a&&(a=[a]);for(let c=0;c<a.length;){const d=a.length-c>MAXIMUM_QUERY_VARS?a.slice(c,c+MAXIMUM_QUERY_VARS):c?a.slice(c):a;c+=d.length;let e="";for(let a=1;a<=d.length;a++)e+=(e?",":"")+"$"+a;const f=await this.db.any(`
SELECT id, doc
FROM ${this.id}.reg
WHERE id IN (${e})`,a);if(f&&f.length){for(let a,b=0;b<f.length;b++)(a=f[b].doc)&&(f[b].doc=JSON.parse(a));b.push(f)}}return 1===b.length?b[0]:1<b.length?concat(b):b},PostgresDB.prototype.has=function(a){return this.db.oneOrNone("SELECT EXISTS(SELECT 1 FROM "+this.id+".reg WHERE id = $1)",[a])},PostgresDB.prototype.search=function(a,b,c=100,d=0,e=!1,f=!0,g=!1,h){let i;if(1<b.length&&a.depth){let j,k="",l=[],m=b[0],n=1;for(let c=1;c<b.length;c++){j=b[c];const d=a.bidirectional&&j>m;k+=(k?" OR ":"")+`(ctx = $${n++} AND key = $${n++})`,l.push(d?j:m,d?m:j),m=j}if(h){k="("+k+")";for(let a=0;a<h.length;a+=2)k+=` AND id IN (SELECT id FROM ${this.id}.tag_${sanitize(h[a])} WHERE tag = $${n++})`,l.push(h[a+1])}i=this.db.any(`
WHERE id IN (${e})`,a);if(f&&f.length){for(let a,b=0;b<f.length;b++)(a=f[b].doc)&&(f[b].doc=JSON.parse(a));b.push(f)}}return 1===b.length?b[0]:1<b.length?concat(b):b},PostgresDB.prototype.has=function(a){return this.db.oneOrNone("SELECT EXISTS(SELECT 1 FROM "+this.id+".reg WHERE id = $1)",[a]).then(function(a){return!!(a&&a.exists)})},PostgresDB.prototype.search=function(a,b,c=100,d=0,e=!1,f=!0,g=!1,h){let i;if(1<b.length&&a.depth){let j,k="",l=[],m=b[0],n=1;for(let c=1;c<b.length;c++){j=b[c];const d=a.bidirectional&&j>m;k+=(k?" OR ":"")+`(ctx = $${n++} AND key = $${n++})`,l.push(d?j:m,d?m:j),m=j}if(h){k="("+k+")";for(let a=0;a<h.length;a+=2)k+=` AND id IN (SELECT id FROM ${this.id}.tag_${sanitize(h[a])} WHERE tag = $${n++})`,l.push(h[a+1])}i=this.db.any(`
SELECT r.id
${f?"":", res"}
${g?", doc":""}
FROM (
SELECT id, count(*) as count,
${e?"SUM":"MIN"}(res) as res
${e?"SUM":"SUM"}(res) as res
FROM ${this.id}.ctx${this.field}
WHERE ${k}
GROUP BY id
@@ -120,7 +120,7 @@ import pg_promise from"pg-promise";import StorageInterface from"../interface.js"
${g?", doc":""}
FROM (
SELECT id, count(*) as count,
${e?"SUM":"MIN"}(res) as res
${e?"SUM":"SUM"}(res) as res
FROM ${this.id}.map${this.field}
WHERE ${a}
GROUP BY id
@@ -132,4 +132,4 @@ import pg_promise from"pg-promise";import StorageInterface from"../interface.js"
ORDER BY ${e?"count DESC, res":"res"}
${c?"LIMIT "+c:""}
${d?"OFFSET "+d:""}
`,b)}return i.then(function(a){return create_result(a,f,g)})},PostgresDB.prototype.info=function(){},PostgresDB.prototype.transaction=function(a){if(TRX)return a.call(this,TRX);const b=this;return(TRX||this.db).tx(function(c){return a.call(b,TRX=c)}).finally(function(){TRX=null})},PostgresDB.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))),d.length&&(await this.remove(d)))}a.reg.size&&(await this.transaction(function(b){const c=[];if(a.store){let d=[],e=new pgp.helpers.ColumnSet(["id","doc"],{table:this.id+".reg"});for(const f of a.store.entries()){const a=f[0],g=f[1];if(d.push({id:a,doc:g&&JSON.stringify(g)}),d.length===MAXIMUM_QUERY_VARS){let a=pgp.helpers.insert(d,e);c.push(b.none(a.replace(/^(insert into )"([^"]+)"/,"$1 $2"))),d=[]}}if(d.length){let a=pgp.helpers.insert(d,e);c.push(b.none(a.replace(/^(insert into )"([^"]+)"/,"$1 $2")))}}else if(!a.bypass){let d=[],e=new pgp.helpers.ColumnSet(["id"],{table:this.id+".reg"});for(const f of a.reg.keys())if(d.push({id:f}),d.length===MAXIMUM_QUERY_VARS){let a=pgp.helpers.insert(d,e);c.push(b.none(a.replace(/^(insert into )"([^"]+)"/,"$1 $2"))),d=[]}if(d.length){let a=pgp.helpers.insert(d,e);c.push(b.none(a.replace(/^(insert into )"([^"]+)"/,"$1 $2")))}}if(a.map.size){let d=[],e=new pgp.helpers.ColumnSet(["key","res","id"],{table:this.id+".map"+this.field});for(const f of a.map){const a=f[0],g=f[1];for(let f,h=0;h<g.length;h++)if((f=g[h])&&f.length)for(let g=0;g<f.length;g++)if(d.push({key:a,res:h,id:f[g]}),d.length===MAXIMUM_QUERY_VARS){let a=pgp.helpers.insert(d,e);c.push(b.none(a.replace(/^(insert into )"([^"]+)"/,"$1 $2"))),d=[]}}if(d.length){let a=pgp.helpers.insert(d,e);c.push(b.none(a.replace(/^(insert into )"([^"]+)"/,"$1 $2")))}}if(a.ctx.size){let d=[],e=new pgp.helpers.ColumnSet(["ctx","key","res","id"],{table:this.id+".ctx"+this.field});for(const f of a.ctx){const a=f[0],g=f[1];for(const f of g){const g=f[0],h=f[1];for(let f,k=0;k<h.length;k++)if((f=h[k])&&f.length)for(let h=0;h<f.length;h++)if(d.push({ctx:a,key:g,res:k,id:f[h]}),d.length===MAXIMUM_QUERY_VARS){let a=pgp.helpers.insert(d,e);c.push(b.none(a.replace(/^(insert into )"([^"]+)"/,"$1 $2"))),d=[]}}}if(d.length){let a=pgp.helpers.insert(d,e);c.push(b.none(a.replace(/^(insert into )"([^"]+)"/,"$1 $2")))}}if(a.tag){let d=[],e=new pgp.helpers.ColumnSet(["tag","id"],{table:this.id+".tag"+this.field});for(const f of a.tag){const a=f[0],g=f[1];if(g.length)for(let f=0;f<g.length;f++)if(d.push({tag:a,id:g[f]}),d.length===MAXIMUM_QUERY_VARS){let a=pgp.helpers.insert(d,e);c.push(b.none(a.replace(/^(insert into )"([^"]+)"/,"$1 $2"))),d=[]}}if(d.length){let a=pgp.helpers.insert(d,e);c.push(b.none(a.replace(/^(insert into )"([^"]+)"/,"$1 $2")))}}return c.length?b.batch(c):void 0}),a.map.clear(),a.ctx.clear(),a.tag&&a.tag.clear(),a.store&&a.store.clear(),a.document||a.reg.clear())},PostgresDB.prototype.remove=function(a){if(a||0===a)return"object"!=typeof a&&(a=[a]),a.length?this.transaction(function(b){return a=[a],b.batch([b.none({text:"DELETE FROM "+this.id+".map"+this.field+" WHERE id = ANY ($1)",rowMode:"array"},a),b.none({text:"DELETE FROM "+this.id+".ctx"+this.field+" WHERE id = ANY ($1)",rowMode:"array"},a),b.none({text:"DELETE FROM "+this.id+".tag"+this.field+" WHERE id = ANY ($1)",rowMode:"array"},a),b.none({text:"DELETE FROM "+this.id+".reg WHERE id = ANY ($1)",rowMode:"array"},a)])}):void 0};
`,b)}return i.then(function(a){return create_result(a,f,g)})},PostgresDB.prototype.info=function(){},PostgresDB.prototype.transaction=function(a){const b=this;return this.db.tx(function(c){return a.call(b,c)})},PostgresDB.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))),d.length&&(await this.remove(d)))}a.reg.size&&(await this.transaction(function(b){const c=[];if(a.store){let d=[],e=new pgp.helpers.ColumnSet(["id","doc"],{table:this.id+".reg"});for(const f of a.store.entries()){const a=f[0],g=f[1];if(d.push({id:a,doc:g&&JSON.stringify(g)}),d.length===MAXIMUM_QUERY_VARS){let a=pgp.helpers.insert(d,e);c.push(b.none(a.replace(/^(insert into )"([^"]+)"/,"$1 $2"))),d=[]}}if(d.length){let a=pgp.helpers.insert(d,e);c.push(b.none(a.replace(/^(insert into )"([^"]+)"/,"$1 $2")))}}else if(!a.bypass){let d=[],e=new pgp.helpers.ColumnSet(["id"],{table:this.id+".reg"});for(const f of a.reg.keys())if(d.push({id:f}),d.length===MAXIMUM_QUERY_VARS){let a=pgp.helpers.insert(d,e);c.push(b.none(a.replace(/^(insert into )"([^"]+)"/,"$1 $2"))),d=[]}if(d.length){let a=pgp.helpers.insert(d,e);c.push(b.none(a.replace(/^(insert into )"([^"]+)"/,"$1 $2")))}}if(a.map.size){let d=[],e=new pgp.helpers.ColumnSet(["key","res","id"],{table:this.id+".map"+this.field});for(const f of a.map){const a=f[0],g=f[1];for(let f,h=0;h<g.length;h++)if((f=g[h])&&f.length)for(let g=0;g<f.length;g++)if(d.push({key:a,res:h,id:f[g]}),d.length===MAXIMUM_QUERY_VARS){let a=pgp.helpers.insert(d,e);c.push(b.none(a.replace(/^(insert into )"([^"]+)"/,"$1 $2"))),d=[]}}if(d.length){let a=pgp.helpers.insert(d,e);c.push(b.none(a.replace(/^(insert into )"([^"]+)"/,"$1 $2")))}}if(a.ctx.size){let d=[],e=new pgp.helpers.ColumnSet(["ctx","key","res","id"],{table:this.id+".ctx"+this.field});for(const f of a.ctx){const a=f[0],g=f[1];for(const f of g){const g=f[0],h=f[1];for(let f,k=0;k<h.length;k++)if((f=h[k])&&f.length)for(let h=0;h<f.length;h++)if(d.push({ctx:a,key:g,res:k,id:f[h]}),d.length===MAXIMUM_QUERY_VARS){let a=pgp.helpers.insert(d,e);c.push(b.none(a.replace(/^(insert into )"([^"]+)"/,"$1 $2"))),d=[]}}}if(d.length){let a=pgp.helpers.insert(d,e);c.push(b.none(a.replace(/^(insert into )"([^"]+)"/,"$1 $2")))}}if(a.tag){let d=[],e=new pgp.helpers.ColumnSet(["tag","id"],{table:this.id+".tag"+this.field});for(const f of a.tag){const a=f[0],g=f[1];if(g.length)for(let f=0;f<g.length;f++)if(d.push({tag:a,id:g[f]}),d.length===MAXIMUM_QUERY_VARS){let a=pgp.helpers.insert(d,e);c.push(b.none(a.replace(/^(insert into )"([^"]+)"/,"$1 $2"))),d=[]}}if(d.length){let a=pgp.helpers.insert(d,e);c.push(b.none(a.replace(/^(insert into )"([^"]+)"/,"$1 $2")))}}return c.length?b.batch(c):void 0}),a.map.clear(),a.ctx.clear(),a.tag&&a.tag.clear(),a.store&&a.store.clear(),a.document||a.reg.clear())},PostgresDB.prototype.remove=function(a){if(a||0===a)return"object"!=typeof a&&(a=[a]),a.length?this.transaction(function(b){return a=[a],b.batch([b.none({text:"DELETE FROM "+this.id+".map"+this.field+" WHERE id = ANY ($1)",rowMode:"array"},a),b.none({text:"DELETE FROM "+this.id+".ctx"+this.field+" WHERE id = ANY ($1)",rowMode:"array"},a),b.none({text:"DELETE FROM "+this.id+".tag"+this.field+" WHERE id = ANY ($1)",rowMode:"array"},a),b.none({text:"DELETE FROM "+this.id+".reg WHERE id = ANY ($1)",rowMode:"array"},a)])}):void 0};