@kanunilabs/pivotgrid-core 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ 'use strict';var J=class{size=1;nodeToParent;nodeToVal;nodeToDepth;nodeChildCount;childrenMap;constructor(e){this.nodeToParent=new Int32Array(e).fill(-1),this.nodeToVal=new Int32Array(e).fill(-1),this.nodeToDepth=new Int32Array(e).fill(0),this.nodeChildCount=new Int32Array(e).fill(0),this.childrenMap=new Map;}expandCapacity(){let e=this.nodeToParent.length*2,n=new Int32Array(e).fill(-1);n.set(this.nodeToParent),this.nodeToParent=n;let t=new Int32Array(e).fill(-1);t.set(this.nodeToVal),this.nodeToVal=t;let r=new Int32Array(e).fill(0);r.set(this.nodeToDepth),this.nodeToDepth=r;let i=new Int32Array(e).fill(0);i.set(this.nodeChildCount),this.nodeChildCount=i;}insert(e,n){let t=0;for(let r=0;r<n;r++){let i=e[r],a=this.childrenMap.get(t);a||(a=new Map,this.childrenMap.set(t,a));let u=a.get(i);u===void 0&&(this.size>=this.nodeToParent.length&&this.expandCapacity(),u=this.size++,this.nodeToParent[u]=t,this.nodeToVal[u]=i,this.nodeToDepth[u]=r+1,this.nodeChildCount[t]++,a.set(i,u)),t=u;}return t}};var ee=class{dictionary=[];rowFields=[];colFields=[];dataFields=[];storedColumns={};storedRowCount=0;lastMask=null;isBuilt=false;buildState=null;incrementalFilters=null;incrementalPrefilter=null;rowTrie;colTrie;cardinalityMap={};rowPathStrs=[];colPathStrs=[];rowIndexMapCache={};columnIndexMapCache={};buildVersion=0;groupCount=0;leafGroupRowIds;leafGroupColIds;leafGroupMeasures;leafGroupCounts;leafGroupMins;leafGroupMaxes;leafGroupDistinctSets=new Map;init(e){this.dictionary=e.dictionary,this.rowFields=e.rowFields,this.colFields=e.colFields,this.dataFields=e.dataFields,e.workerBootstrap&&e.workerBootstrap.length>0&&console.warn("[PivotGrid Worker] workerBootstrap is deprecated for security reasons. All calculations are executed on the main thread."),this.storedRowCount=e.rowCount,this.storedColumns={};for(let[n,t]of Object.entries(e.columnarData))t.type==="int32"&&(this.storedColumns[n]={type:"int32",array:new Int32Array(t.buffer)}),t.type==="float64"&&(this.storedColumns[n]={type:"float64",array:new Float64Array(t.buffer)});this.lastMask=null,e.deferBuild?this.isBuilt=false:this.buildFromMask(null);}reconfigure(e){if(this.rowFields=e.rowFields,this.colFields=e.colFields,this.dataFields=e.dataFields,e.dictionaryAppend&&e.dictionaryAppend.length>0)for(let n of e.dictionaryAppend)this.dictionary.push(n);if(e.newColumns)for(let[n,t]of Object.entries(e.newColumns))t.type==="int32"&&(this.storedColumns[n]={type:"int32",array:new Int32Array(t.buffer)}),t.type==="float64"&&(this.storedColumns[n]={type:"float64",array:new Float64Array(t.buffer)});this.buildFromMask(this.lastMask);}initStart(e){this.dictionary=e.dictionary,this.rowFields=e.rowFields,this.colFields=e.colFields,this.dataFields=e.dataFields,e.workerBootstrap&&e.workerBootstrap.length>0&&console.warn("[PivotGrid Worker] workerBootstrap is deprecated for security reasons. All calculations are executed on the main thread."),this.storedRowCount=e.rowCountTotal,this.storedColumns={};for(let[n,t]of Object.entries(e.columnTypes))t==="int32"?this.storedColumns[n]={type:"int32",array:new Int32Array(e.rowCountTotal)}:this.storedColumns[n]={type:"float64",array:new Float64Array(e.rowCountTotal)};this.incrementalFilters=e.filters&&e.filters.length>0?e.filters.map(n=>({fieldId:n.fieldId,filterType:n.filterType,ids:new Set,pending:new Set(n.valueStrings)})):null,this.incrementalPrefilter=e.prefilter&&e.prefilter.conditions.length>0?e.prefilter:null,this.lastMask=this.incrementalFilters||this.incrementalPrefilter?new Uint8Array(e.rowCountTotal):null,this.resolveIncrementalFilterStrings(0),this.isBuilt=false,this.beginBuild();}initChunk(e){if(e.dictionaryAppend&&e.dictionaryAppend.length>0){let n=this.dictionary.length;for(let t of e.dictionaryAppend)this.dictionary.push(t);this.resolveIncrementalFilterStrings(n),this.ensureCardCapacity();}for(let[n,t]of Object.entries(e.columns)){let r=this.storedColumns[n];r&&(r.type==="int32"?r.array.set(new Int32Array(t),e.start):r.array.set(new Float64Array(t),e.start));}this.lastMask&&this.computeMaskRange(e.start,e.end),this.processRange(e.start,e.end,this.lastMask);}initEnd(){this.incrementalFilters=null,this.incrementalPrefilter=null,this.finalizeBuild();}resolveIncrementalFilterStrings(e){let n=this.incrementalFilters;if(!n)return;let t=0;for(let r of n)t+=r.pending.size;if(t!==0)for(let r=e;r<this.dictionary.length;r++){let i=this.dictionary[r];for(let a of n)a.pending.delete(i)&&a.ids.add(r);}}computeMaskRange(e,n){let t=this.lastMask;if(t.fill(1,e,n),this.incrementalFilters)for(let r of this.incrementalFilters){let i=this.storedColumns[r.fieldId];if(!i||i.type!=="int32")continue;let a=i.array,u=r.ids;if(r.filterType==="exclude")for(let o=e;o<n;o++)t[o]&&u.has(a[o])&&(t[o]=0);else for(let o=e;o<n;o++)t[o]&&!u.has(a[o])&&(t[o]=0);}if(this.incrementalPrefilter)for(let r=e;r<n;r++)t[r]&&!this.evaluatePrefilterGroup(this.incrementalPrefilter,r)&&(t[r]=0);}applyFilter(e){if(e.filters.length===0&&!e.prefilter){this.lastMask=null,this.buildFromMask(null);return}let n=this.storedRowCount,t=new Uint8Array(n);t.fill(1);for(let r of e.filters){let i=this.storedColumns[r.fieldId];if(!i||i.type!=="int32")continue;let a=i.array,u=new Set(r.dictIds);if(r.filterType==="exclude")for(let o=0;o<n;o++)t[o]&&u.has(a[o])&&(t[o]=0);else for(let o=0;o<n;o++)t[o]&&!u.has(a[o])&&(t[o]=0);}if(e.prefilter&&e.prefilter.conditions.length>0)for(let r=0;r<n;r++)t[r]&&!this.evaluatePrefilterGroup(e.prefilter,r)&&(t[r]=0);this.lastMask=t,this.buildFromMask(t);}evaluatePrefilterGroup(e,n){if(!e.conditions||e.conditions.length===0)return true;if(e.logicalOperator==="and"){for(let t of e.conditions)if("logicalOperator"in t){if(!this.evaluatePrefilterGroup(t,n))return false}else if(!this.evaluatePrefilterCondition(t,n))return false;return true}else {for(let t of e.conditions)if("logicalOperator"in t){if(this.evaluatePrefilterGroup(t,n))return true}else if(this.evaluatePrefilterCondition(t,n))return true;return false}}evaluatePrefilterCondition(e,n){let t=this.storedColumns[e.fieldId];if(!t)return true;if(t.type==="float64"){let u=t.array[n],o=Number(e.value);switch(e.operator){case "=":return u===o;case "<>":return u!==o;case ">":return u>o;case ">=":return u>=o;case "<":return u<o;case "<=":return u<=o;case "isnull":return isNaN(u)||u===0;case "isnotnull":return !isNaN(u)&&u!==0;default:return true}}let r=t.array[n],i=this.dictionary[r]||"",a=String(e.value??"");switch(e.operator){case "=":return i===a;case "<>":return i!==a;case ">":return i>a;case ">=":return i>=a;case "<":return i<a;case "<=":return i<=a;case "contains":return i.toLowerCase().includes(a.toLowerCase());case "notcontains":return !i.toLowerCase().includes(a.toLowerCase());case "startswith":return i.toLowerCase().startsWith(a.toLowerCase());case "endswith":return i.toLowerCase().endsWith(a.toLowerCase());case "isnull":return i===""||i==="(Blank)";case "isnotnull":return i!==""&&i!=="(Blank)";case "in":return Array.isArray(e.value)?e.value.map(String).includes(i):true;case "notin":return Array.isArray(e.value)?!e.value.map(String).includes(i):true;default:return true}}buildFromMask(e){this.beginBuild(),this.processRange(0,this.storedRowCount,e),this.finalizeBuild();}beginBuild(){let e=this.storedRowCount,n=this.storedColumns,t=this.rowFields.length,r=this.colFields.length,i=new Array(t);for(let l=0;l<t;l++)i[l]=n[this.rowFields[l].id].array;let a=new Array(r);for(let l=0;l<r;l++)a[l]=n[this.colFields[l].id].array;let u=this.dictionary.length,o=this.rowFields.map(l=>{let M=n[l.id];return M&&M.type==="int32"?new Uint8Array(u):null}),w=this.colFields.map(l=>{let M=n[l.id];return M&&M.type==="int32"?new Uint8Array(u):null});this.rowTrie=new J(2e5),this.colTrie=new J(2e5);let v=this.dataFields.length,S=this.dataFields.map(l=>l.summaryType||"sum"),h=S.some(l=>l==="count"||l==="avg"),D=S.some(l=>l==="min"),L=S.some(l=>l==="max"),K=S.some(l=>l==="distinct"),_=new Array(v);for(let l=0;l<v;l++)_[l]=n[this.dataFields[l].id].array;let R=Math.min(e,2e5),N=D?new Float64Array(R*v):new Float64Array(0),x=L?new Float64Array(R*v):new Float64Array(0);this.leafGroupDistinctSets=K?new Map:new Map,D&&N.fill(1/0),L&&x.fill(-1/0),this.buildState={rPath:new Int32Array(t),cPath:new Int32Array(r),rowColArrays:i,colColArrays:a,dataColArrays:_,rowCardSeen:o,colCardSeen:w,rowCardCounts:new Int32Array(t),colCardCounts:new Int32Array(r),summaryTypes:S,allSums:S.every(l=>l==="sum"),needsCounts:h,needsMins:D,needsMaxes:L,groupMap:new Map,leafRowIds:new Int32Array(R),leafColIds:new Int32Array(R),leafMeasures:new Float64Array(R*v),leafCounts:h?new Float64Array(R*v):new Float64Array(0),leafMins:N,leafMaxes:x,gCount:0};}ensureCardCapacity(){let e=this.buildState;if(!e)return;let n=this.dictionary.length,t=r=>{if(r===null||r.length>=n)return r;let i=new Uint8Array(Math.max(n,r.length*2));return i.set(r),i};for(let r=0;r<e.rowCardSeen.length;r++)e.rowCardSeen[r]=t(e.rowCardSeen[r]);for(let r=0;r<e.colCardSeen.length;r++)e.colCardSeen[r]=t(e.colCardSeen[r]);}processRange(e,n,t){let r=this.buildState,{rPath:i,cPath:a,rowColArrays:u,colColArrays:o,dataColArrays:w,rowCardSeen:v,colCardSeen:S,rowCardCounts:h,colCardCounts:D,summaryTypes:L,allSums:K,needsCounts:_,needsMins:R,needsMaxes:N,groupMap:x}=r,l=u.length,M=o.length,y=w.length,X=this.rowTrie,V=this.colTrie,C=r.leafRowIds,b=r.leafColIds,k=r.leafMeasures,z=r.leafCounts,U=r.leafMins,E=r.leafMaxes,Q=r.gCount;for(let W=e;W<n;W++){if(t&&!t[W])continue;for(let s=0;s<l;s++){let d=u[s][W];i[s]=d;let f=v[s];f!==null&&f[d]===0&&(f[d]=1,h[s]++);}let Y=X.insert(i,l);for(let s=0;s<M;s++){let d=o[s][W];a[s]=d;let f=S[s];f!==null&&f[d]===0&&(f[d]=1,D[s]++);}let H=V.insert(a,M),Z=Y*67108864+H,G=x.get(Z);if(G===void 0){if(G=Q++,x.set(Z,G),G>=C.length){let s=G*2,d=new Int32Array(s);d.set(C),C=d;let f=new Int32Array(s);f.set(b),b=f;let I=new Float64Array(s*y);if(I.set(k),k=I,_){let p=new Float64Array(s*y);p.set(z),z=p;}if(R){let p=new Float64Array(s*y);p.fill(1/0),p.set(U),U=p;}if(N){let p=new Float64Array(s*y);p.fill(-1/0),p.set(E),E=p;}}C[G]=Y,b[G]=H;}let c=G*y;if(K)for(let s=0;s<y;s++)k[c+s]+=w[s][W];else for(let s=0;s<y;s++){let d=w[s][W],f=c+s;switch(L[s]){case "sum":k[f]+=d;break;case "count":z[f]+=1;break;case "avg":k[f]+=d,z[f]+=1;break;case "min":d<U[f]&&(U[f]=d);break;case "max":d>E[f]&&(E[f]=d);break;case "distinct":{let I=this.leafGroupDistinctSets.get(f);I||(I=new Set,this.leafGroupDistinctSets.set(f,I)),I.add(d);break}default:k[f]+=d;break}}}r.leafRowIds=C,r.leafColIds=b,r.leafMeasures=k,r.leafCounts=z,r.leafMins=U,r.leafMaxes=E,r.gCount=Q;}finalizeBuild(){let e=this.buildState,{needsCounts:n,needsMins:t,needsMaxes:r,gCount:i}=e,a=e.dataColArrays.length,u=e.rowColArrays.length,o=e.colColArrays.length;this.groupCount=i,this.leafGroupRowIds=e.leafRowIds.slice(0,i),this.leafGroupColIds=e.leafColIds.slice(0,i),this.leafGroupMeasures=e.leafMeasures.slice(0,i*a),this.leafGroupCounts=n?e.leafCounts.slice(0,i*a):e.leafCounts,this.leafGroupMins=t?e.leafMins.slice(0,i*a):e.leafMins,this.leafGroupMaxes=r?e.leafMaxes.slice(0,i*a):e.leafMaxes,this.cardinalityMap={};for(let w=0;w<u;w++)e.rowCardSeen[w]!==null&&(this.cardinalityMap[this.rowFields[w].id]=e.rowCardCounts[w]);for(let w=0;w<o;w++)e.colCardSeen[w]!==null&&(this.cardinalityMap[this.colFields[w].id]=e.colCardCounts[w]);this.buildState=null,this.buildVersion++,this.isBuilt=true,this.rowPathStrs=this.buildPathStrs(this.rowTrie),this.colPathStrs=this.buildPathStrs(this.colTrie),this.rowIndexMapCache=this.buildIndexMap(this.rowPathStrs),this.columnIndexMapCache=this.buildIndexMap(this.colPathStrs);}buildPathStrs(e){let n=new Array(e.size);n[0]="";for(let t=1;t<e.size;t++){let r=e.nodeToParent[t],i=this.dictionary[e.nodeToVal[t]],a=n[r];n[t]=a?a+"|"+i:i;}return n}buildIndexMap(e){let n={};for(let t=0;t<e.length;t++)n[e[t]]=t;return n}compute(e){this.isBuilt||this.buildFromMask(this.lastMask);let n=this.rowPathStrs,t=this.colPathStrs,r=new Uint8Array(this.rowTrie.size),i=new Set(e.expandedPaths),a=i.has(".*")||i.has("*");for(let c=0;c<this.rowTrie.size;c++)r[c]=a||i.has(n[c])?1:0;let u=new Uint8Array(this.colTrie.size),o=new Set(e.expandedColPaths),w=o.has(".*")||o.has("*");for(let c=0;c<this.colTrie.size;c++)u[c]=w||o.has(t[c])?1:0;let v=new Uint8Array(this.rowTrie.size);v[0]=1;for(let c=1;c<this.rowTrie.size;c++){let s=this.rowTrie.nodeToParent[c];(s===0||r[s])&&(v[c]=1);}let S=new Uint8Array(this.colTrie.size);S[0]=1;for(let c=1;c<this.colTrie.size;c++){let s=this.colTrie.nodeToParent[c];(s===0||u[s])&&(S[c]=1);}let h=this.dataFields.length,D=this.dataFields.map(c=>c.summaryType||"sum"),L=this.leafGroupMins.length>0,K=this.leafGroupMaxes.length>0,_=this.leafGroupCounts.length>0,R=this.leafGroupDistinctSets.size>0,N=new Map,x=0,l=1e4,M=new Float64Array(l),y=new Float64Array(l*h),X=new Int32Array(l*h),V=_?new Float64Array(l*h):null,C=L?new Float64Array(l*h):null,b=K?new Float64Array(l*h):null,k=R?new Map:new Map;C&&C.fill(1/0),b&&b.fill(-1/0);let z=new Int32Array(this.rowFields.length+1),U=new Int32Array(this.colFields.length+1),E=(c,s,d,f)=>{let I=0,p=c;for(;p!==-1;)d[p]&&(f[I++]=p),p=s.nodeToParent[p];return I};for(let c=0;c<this.groupCount;c++){let s=this.leafGroupRowIds[c],d=this.leafGroupColIds[c],f=E(s,this.rowTrie,v,z),I=E(d,this.colTrie,S,U);for(let p=0;p<f;p++){let $=z[p];for(let B=0;B<I;B++){let q=U[B],j=$*67108864+q,g=N.get(j);if(g===void 0){if(g=x++,N.set(j,g),g>=M.length){let A=g*2,m=new Float64Array(A);m.set(M),M=m;let P=new Float64Array(A*h);P.set(y),y=P;let T=new Int32Array(A*h);if(T.set(X),X=T,V){let F=new Float64Array(A*h);F.set(V),V=F;}if(C){let F=new Float64Array(A*h);F.fill(1/0),F.set(C),C=F;}if(b){let F=new Float64Array(A*h);F.fill(-1/0),F.set(b),b=F;}}M[g]=j;}for(let A=0;A<h;A++){let m=g*h+A,P=c*h+A;switch(D[A]){case "sum":y[m]+=this.leafGroupMeasures[P];break;case "count":y[m]+=this.leafGroupCounts[P];break;case "avg":y[m]+=this.leafGroupMeasures[P],V[m]+=this.leafGroupCounts[P];break;case "min":{let T=this.leafGroupMins[P];T<C[m]&&(C[m]=T);break}case "max":{let T=this.leafGroupMaxes[P];T>b[m]&&(b[m]=T);break}case "distinct":{let T=this.leafGroupDistinctSets.get(P);if(T){let F=k.get(m);F||(F=new Set,k.set(m,F));for(let ie of T)F.add(ie);}break}default:y[m]+=this.leafGroupMeasures[P];break}X[m]+=1;}}}}for(let c=0;c<x;c++)for(let s=0;s<h;s++){let d=c*h+s;switch(D[s]){case "avg":y[d]=V[d]>0?y[d]/V[d]:0;break;case "min":y[d]=C[d]!==1/0?C[d]:0;break;case "max":y[d]=b[d]!==-1/0?b[d]:0;break;case "distinct":{let f=k.get(d);y[d]=f?f.size:0;break}}}let Q=M.slice(0,x),W=y.slice(0,x*h),Y=X.slice(0,x*h),H=(c,s,d,f,I)=>{let p=[];for(let B=0;B<f.length;B++)p[B]=f[B]?.id||"";let $=B=>{let q=c.childrenMap.get(B);if(!q)return [];let j=[];for(let g of q.values())if(s[g]){let A=c.nodeToDepth[g]-1,m=d[g]===1;j.push({key:I[g],value:this.dictionary[c.nodeToVal[g]],fieldId:p[A]||"",children:m?$(g):[],isExpanded:m,isTotal:false,depth:A,hasChildren:c.nodeChildCount[g]>0});}return j};return $(0)},Z=H(this.rowTrie,v,r,this.rowFields,n),G=H(this.colTrie,S,u,this.colFields,t);return {id:e.id,rowTree:Z,colTree:G,cellMatrix:{keys:Q,sums:W,counts:Y},rowIndexMap:this.rowIndexMapCache,columnIndexMap:this.columnIndexMapCache,cardinalityMap:this.cardinalityMap,indexMapsVersion:this.buildVersion}}};var O=new ee,ne=-1;self.onmessage=te=>{let e=te.data;try{if(e.action==="init")O.init(e),self.postMessage({id:e.id,isInitComplete:!0});else if(e.action==="initStart")O.initStart(e);else if(e.action==="initChunk")O.initChunk(e);else if(e.action==="initEnd")O.initEnd(),self.postMessage({id:e.id,isInitComplete:!0});else if(e.action==="filter")O.applyFilter(e),self.postMessage({id:e.id,isFilterComplete:!0});else if(e.action==="reconfigure")O.reconfigure(e),self.postMessage({id:e.id,isReconfigureComplete:!0});else if(e.action==="compute"){let n=O.compute(e),t=[n.cellMatrix.keys.buffer,n.cellMatrix.sums.buffer,n.cellMatrix.counts.buffer];if(n.indexMapsVersion!==void 0&&n.indexMapsVersion===ne){let r={...n,rowIndexMap:void 0,columnIndexMap:void 0,cardinalityMap:void 0,indexMapsOmitted:!0};postMessage(r,t);}else n.indexMapsVersion!==void 0&&(ne=n.indexMapsVersion),postMessage(n,t);}}catch(n){self.postMessage({id:e.id,error:n instanceof Error?n.message:String(n)});}};
@@ -0,0 +1 @@
1
+ export { I as FlatCellMatrix, a1 as WorkerComputeRequest, a2 as WorkerFilterRequest, a4 as WorkerInitRequest, ax as WorkerReconfigureRequest, a5 as WorkerResponse } from './pivot.worker-DQFl4fMu.cjs';
@@ -0,0 +1 @@
1
+ export { I as FlatCellMatrix, a1 as WorkerComputeRequest, a2 as WorkerFilterRequest, a4 as WorkerInitRequest, ax as WorkerReconfigureRequest, a5 as WorkerResponse } from './pivot.worker-DQFl4fMu.js';
@@ -0,0 +1 @@
1
+ var J=class{size=1;nodeToParent;nodeToVal;nodeToDepth;nodeChildCount;childrenMap;constructor(e){this.nodeToParent=new Int32Array(e).fill(-1),this.nodeToVal=new Int32Array(e).fill(-1),this.nodeToDepth=new Int32Array(e).fill(0),this.nodeChildCount=new Int32Array(e).fill(0),this.childrenMap=new Map;}expandCapacity(){let e=this.nodeToParent.length*2,n=new Int32Array(e).fill(-1);n.set(this.nodeToParent),this.nodeToParent=n;let t=new Int32Array(e).fill(-1);t.set(this.nodeToVal),this.nodeToVal=t;let r=new Int32Array(e).fill(0);r.set(this.nodeToDepth),this.nodeToDepth=r;let i=new Int32Array(e).fill(0);i.set(this.nodeChildCount),this.nodeChildCount=i;}insert(e,n){let t=0;for(let r=0;r<n;r++){let i=e[r],a=this.childrenMap.get(t);a||(a=new Map,this.childrenMap.set(t,a));let u=a.get(i);u===void 0&&(this.size>=this.nodeToParent.length&&this.expandCapacity(),u=this.size++,this.nodeToParent[u]=t,this.nodeToVal[u]=i,this.nodeToDepth[u]=r+1,this.nodeChildCount[t]++,a.set(i,u)),t=u;}return t}};var ee=class{dictionary=[];rowFields=[];colFields=[];dataFields=[];storedColumns={};storedRowCount=0;lastMask=null;isBuilt=false;buildState=null;incrementalFilters=null;incrementalPrefilter=null;rowTrie;colTrie;cardinalityMap={};rowPathStrs=[];colPathStrs=[];rowIndexMapCache={};columnIndexMapCache={};buildVersion=0;groupCount=0;leafGroupRowIds;leafGroupColIds;leafGroupMeasures;leafGroupCounts;leafGroupMins;leafGroupMaxes;leafGroupDistinctSets=new Map;init(e){this.dictionary=e.dictionary,this.rowFields=e.rowFields,this.colFields=e.colFields,this.dataFields=e.dataFields,e.workerBootstrap&&e.workerBootstrap.length>0&&console.warn("[PivotGrid Worker] workerBootstrap is deprecated for security reasons. All calculations are executed on the main thread."),this.storedRowCount=e.rowCount,this.storedColumns={};for(let[n,t]of Object.entries(e.columnarData))t.type==="int32"&&(this.storedColumns[n]={type:"int32",array:new Int32Array(t.buffer)}),t.type==="float64"&&(this.storedColumns[n]={type:"float64",array:new Float64Array(t.buffer)});this.lastMask=null,e.deferBuild?this.isBuilt=false:this.buildFromMask(null);}reconfigure(e){if(this.rowFields=e.rowFields,this.colFields=e.colFields,this.dataFields=e.dataFields,e.dictionaryAppend&&e.dictionaryAppend.length>0)for(let n of e.dictionaryAppend)this.dictionary.push(n);if(e.newColumns)for(let[n,t]of Object.entries(e.newColumns))t.type==="int32"&&(this.storedColumns[n]={type:"int32",array:new Int32Array(t.buffer)}),t.type==="float64"&&(this.storedColumns[n]={type:"float64",array:new Float64Array(t.buffer)});this.buildFromMask(this.lastMask);}initStart(e){this.dictionary=e.dictionary,this.rowFields=e.rowFields,this.colFields=e.colFields,this.dataFields=e.dataFields,e.workerBootstrap&&e.workerBootstrap.length>0&&console.warn("[PivotGrid Worker] workerBootstrap is deprecated for security reasons. All calculations are executed on the main thread."),this.storedRowCount=e.rowCountTotal,this.storedColumns={};for(let[n,t]of Object.entries(e.columnTypes))t==="int32"?this.storedColumns[n]={type:"int32",array:new Int32Array(e.rowCountTotal)}:this.storedColumns[n]={type:"float64",array:new Float64Array(e.rowCountTotal)};this.incrementalFilters=e.filters&&e.filters.length>0?e.filters.map(n=>({fieldId:n.fieldId,filterType:n.filterType,ids:new Set,pending:new Set(n.valueStrings)})):null,this.incrementalPrefilter=e.prefilter&&e.prefilter.conditions.length>0?e.prefilter:null,this.lastMask=this.incrementalFilters||this.incrementalPrefilter?new Uint8Array(e.rowCountTotal):null,this.resolveIncrementalFilterStrings(0),this.isBuilt=false,this.beginBuild();}initChunk(e){if(e.dictionaryAppend&&e.dictionaryAppend.length>0){let n=this.dictionary.length;for(let t of e.dictionaryAppend)this.dictionary.push(t);this.resolveIncrementalFilterStrings(n),this.ensureCardCapacity();}for(let[n,t]of Object.entries(e.columns)){let r=this.storedColumns[n];r&&(r.type==="int32"?r.array.set(new Int32Array(t),e.start):r.array.set(new Float64Array(t),e.start));}this.lastMask&&this.computeMaskRange(e.start,e.end),this.processRange(e.start,e.end,this.lastMask);}initEnd(){this.incrementalFilters=null,this.incrementalPrefilter=null,this.finalizeBuild();}resolveIncrementalFilterStrings(e){let n=this.incrementalFilters;if(!n)return;let t=0;for(let r of n)t+=r.pending.size;if(t!==0)for(let r=e;r<this.dictionary.length;r++){let i=this.dictionary[r];for(let a of n)a.pending.delete(i)&&a.ids.add(r);}}computeMaskRange(e,n){let t=this.lastMask;if(t.fill(1,e,n),this.incrementalFilters)for(let r of this.incrementalFilters){let i=this.storedColumns[r.fieldId];if(!i||i.type!=="int32")continue;let a=i.array,u=r.ids;if(r.filterType==="exclude")for(let o=e;o<n;o++)t[o]&&u.has(a[o])&&(t[o]=0);else for(let o=e;o<n;o++)t[o]&&!u.has(a[o])&&(t[o]=0);}if(this.incrementalPrefilter)for(let r=e;r<n;r++)t[r]&&!this.evaluatePrefilterGroup(this.incrementalPrefilter,r)&&(t[r]=0);}applyFilter(e){if(e.filters.length===0&&!e.prefilter){this.lastMask=null,this.buildFromMask(null);return}let n=this.storedRowCount,t=new Uint8Array(n);t.fill(1);for(let r of e.filters){let i=this.storedColumns[r.fieldId];if(!i||i.type!=="int32")continue;let a=i.array,u=new Set(r.dictIds);if(r.filterType==="exclude")for(let o=0;o<n;o++)t[o]&&u.has(a[o])&&(t[o]=0);else for(let o=0;o<n;o++)t[o]&&!u.has(a[o])&&(t[o]=0);}if(e.prefilter&&e.prefilter.conditions.length>0)for(let r=0;r<n;r++)t[r]&&!this.evaluatePrefilterGroup(e.prefilter,r)&&(t[r]=0);this.lastMask=t,this.buildFromMask(t);}evaluatePrefilterGroup(e,n){if(!e.conditions||e.conditions.length===0)return true;if(e.logicalOperator==="and"){for(let t of e.conditions)if("logicalOperator"in t){if(!this.evaluatePrefilterGroup(t,n))return false}else if(!this.evaluatePrefilterCondition(t,n))return false;return true}else {for(let t of e.conditions)if("logicalOperator"in t){if(this.evaluatePrefilterGroup(t,n))return true}else if(this.evaluatePrefilterCondition(t,n))return true;return false}}evaluatePrefilterCondition(e,n){let t=this.storedColumns[e.fieldId];if(!t)return true;if(t.type==="float64"){let u=t.array[n],o=Number(e.value);switch(e.operator){case "=":return u===o;case "<>":return u!==o;case ">":return u>o;case ">=":return u>=o;case "<":return u<o;case "<=":return u<=o;case "isnull":return isNaN(u)||u===0;case "isnotnull":return !isNaN(u)&&u!==0;default:return true}}let r=t.array[n],i=this.dictionary[r]||"",a=String(e.value??"");switch(e.operator){case "=":return i===a;case "<>":return i!==a;case ">":return i>a;case ">=":return i>=a;case "<":return i<a;case "<=":return i<=a;case "contains":return i.toLowerCase().includes(a.toLowerCase());case "notcontains":return !i.toLowerCase().includes(a.toLowerCase());case "startswith":return i.toLowerCase().startsWith(a.toLowerCase());case "endswith":return i.toLowerCase().endsWith(a.toLowerCase());case "isnull":return i===""||i==="(Blank)";case "isnotnull":return i!==""&&i!=="(Blank)";case "in":return Array.isArray(e.value)?e.value.map(String).includes(i):true;case "notin":return Array.isArray(e.value)?!e.value.map(String).includes(i):true;default:return true}}buildFromMask(e){this.beginBuild(),this.processRange(0,this.storedRowCount,e),this.finalizeBuild();}beginBuild(){let e=this.storedRowCount,n=this.storedColumns,t=this.rowFields.length,r=this.colFields.length,i=new Array(t);for(let l=0;l<t;l++)i[l]=n[this.rowFields[l].id].array;let a=new Array(r);for(let l=0;l<r;l++)a[l]=n[this.colFields[l].id].array;let u=this.dictionary.length,o=this.rowFields.map(l=>{let M=n[l.id];return M&&M.type==="int32"?new Uint8Array(u):null}),w=this.colFields.map(l=>{let M=n[l.id];return M&&M.type==="int32"?new Uint8Array(u):null});this.rowTrie=new J(2e5),this.colTrie=new J(2e5);let v=this.dataFields.length,S=this.dataFields.map(l=>l.summaryType||"sum"),h=S.some(l=>l==="count"||l==="avg"),D=S.some(l=>l==="min"),L=S.some(l=>l==="max"),K=S.some(l=>l==="distinct"),_=new Array(v);for(let l=0;l<v;l++)_[l]=n[this.dataFields[l].id].array;let R=Math.min(e,2e5),N=D?new Float64Array(R*v):new Float64Array(0),x=L?new Float64Array(R*v):new Float64Array(0);this.leafGroupDistinctSets=K?new Map:new Map,D&&N.fill(1/0),L&&x.fill(-1/0),this.buildState={rPath:new Int32Array(t),cPath:new Int32Array(r),rowColArrays:i,colColArrays:a,dataColArrays:_,rowCardSeen:o,colCardSeen:w,rowCardCounts:new Int32Array(t),colCardCounts:new Int32Array(r),summaryTypes:S,allSums:S.every(l=>l==="sum"),needsCounts:h,needsMins:D,needsMaxes:L,groupMap:new Map,leafRowIds:new Int32Array(R),leafColIds:new Int32Array(R),leafMeasures:new Float64Array(R*v),leafCounts:h?new Float64Array(R*v):new Float64Array(0),leafMins:N,leafMaxes:x,gCount:0};}ensureCardCapacity(){let e=this.buildState;if(!e)return;let n=this.dictionary.length,t=r=>{if(r===null||r.length>=n)return r;let i=new Uint8Array(Math.max(n,r.length*2));return i.set(r),i};for(let r=0;r<e.rowCardSeen.length;r++)e.rowCardSeen[r]=t(e.rowCardSeen[r]);for(let r=0;r<e.colCardSeen.length;r++)e.colCardSeen[r]=t(e.colCardSeen[r]);}processRange(e,n,t){let r=this.buildState,{rPath:i,cPath:a,rowColArrays:u,colColArrays:o,dataColArrays:w,rowCardSeen:v,colCardSeen:S,rowCardCounts:h,colCardCounts:D,summaryTypes:L,allSums:K,needsCounts:_,needsMins:R,needsMaxes:N,groupMap:x}=r,l=u.length,M=o.length,y=w.length,X=this.rowTrie,V=this.colTrie,C=r.leafRowIds,b=r.leafColIds,k=r.leafMeasures,z=r.leafCounts,U=r.leafMins,E=r.leafMaxes,Q=r.gCount;for(let W=e;W<n;W++){if(t&&!t[W])continue;for(let s=0;s<l;s++){let d=u[s][W];i[s]=d;let f=v[s];f!==null&&f[d]===0&&(f[d]=1,h[s]++);}let Y=X.insert(i,l);for(let s=0;s<M;s++){let d=o[s][W];a[s]=d;let f=S[s];f!==null&&f[d]===0&&(f[d]=1,D[s]++);}let H=V.insert(a,M),Z=Y*67108864+H,G=x.get(Z);if(G===void 0){if(G=Q++,x.set(Z,G),G>=C.length){let s=G*2,d=new Int32Array(s);d.set(C),C=d;let f=new Int32Array(s);f.set(b),b=f;let I=new Float64Array(s*y);if(I.set(k),k=I,_){let p=new Float64Array(s*y);p.set(z),z=p;}if(R){let p=new Float64Array(s*y);p.fill(1/0),p.set(U),U=p;}if(N){let p=new Float64Array(s*y);p.fill(-1/0),p.set(E),E=p;}}C[G]=Y,b[G]=H;}let c=G*y;if(K)for(let s=0;s<y;s++)k[c+s]+=w[s][W];else for(let s=0;s<y;s++){let d=w[s][W],f=c+s;switch(L[s]){case "sum":k[f]+=d;break;case "count":z[f]+=1;break;case "avg":k[f]+=d,z[f]+=1;break;case "min":d<U[f]&&(U[f]=d);break;case "max":d>E[f]&&(E[f]=d);break;case "distinct":{let I=this.leafGroupDistinctSets.get(f);I||(I=new Set,this.leafGroupDistinctSets.set(f,I)),I.add(d);break}default:k[f]+=d;break}}}r.leafRowIds=C,r.leafColIds=b,r.leafMeasures=k,r.leafCounts=z,r.leafMins=U,r.leafMaxes=E,r.gCount=Q;}finalizeBuild(){let e=this.buildState,{needsCounts:n,needsMins:t,needsMaxes:r,gCount:i}=e,a=e.dataColArrays.length,u=e.rowColArrays.length,o=e.colColArrays.length;this.groupCount=i,this.leafGroupRowIds=e.leafRowIds.slice(0,i),this.leafGroupColIds=e.leafColIds.slice(0,i),this.leafGroupMeasures=e.leafMeasures.slice(0,i*a),this.leafGroupCounts=n?e.leafCounts.slice(0,i*a):e.leafCounts,this.leafGroupMins=t?e.leafMins.slice(0,i*a):e.leafMins,this.leafGroupMaxes=r?e.leafMaxes.slice(0,i*a):e.leafMaxes,this.cardinalityMap={};for(let w=0;w<u;w++)e.rowCardSeen[w]!==null&&(this.cardinalityMap[this.rowFields[w].id]=e.rowCardCounts[w]);for(let w=0;w<o;w++)e.colCardSeen[w]!==null&&(this.cardinalityMap[this.colFields[w].id]=e.colCardCounts[w]);this.buildState=null,this.buildVersion++,this.isBuilt=true,this.rowPathStrs=this.buildPathStrs(this.rowTrie),this.colPathStrs=this.buildPathStrs(this.colTrie),this.rowIndexMapCache=this.buildIndexMap(this.rowPathStrs),this.columnIndexMapCache=this.buildIndexMap(this.colPathStrs);}buildPathStrs(e){let n=new Array(e.size);n[0]="";for(let t=1;t<e.size;t++){let r=e.nodeToParent[t],i=this.dictionary[e.nodeToVal[t]],a=n[r];n[t]=a?a+"|"+i:i;}return n}buildIndexMap(e){let n={};for(let t=0;t<e.length;t++)n[e[t]]=t;return n}compute(e){this.isBuilt||this.buildFromMask(this.lastMask);let n=this.rowPathStrs,t=this.colPathStrs,r=new Uint8Array(this.rowTrie.size),i=new Set(e.expandedPaths),a=i.has(".*")||i.has("*");for(let c=0;c<this.rowTrie.size;c++)r[c]=a||i.has(n[c])?1:0;let u=new Uint8Array(this.colTrie.size),o=new Set(e.expandedColPaths),w=o.has(".*")||o.has("*");for(let c=0;c<this.colTrie.size;c++)u[c]=w||o.has(t[c])?1:0;let v=new Uint8Array(this.rowTrie.size);v[0]=1;for(let c=1;c<this.rowTrie.size;c++){let s=this.rowTrie.nodeToParent[c];(s===0||r[s])&&(v[c]=1);}let S=new Uint8Array(this.colTrie.size);S[0]=1;for(let c=1;c<this.colTrie.size;c++){let s=this.colTrie.nodeToParent[c];(s===0||u[s])&&(S[c]=1);}let h=this.dataFields.length,D=this.dataFields.map(c=>c.summaryType||"sum"),L=this.leafGroupMins.length>0,K=this.leafGroupMaxes.length>0,_=this.leafGroupCounts.length>0,R=this.leafGroupDistinctSets.size>0,N=new Map,x=0,l=1e4,M=new Float64Array(l),y=new Float64Array(l*h),X=new Int32Array(l*h),V=_?new Float64Array(l*h):null,C=L?new Float64Array(l*h):null,b=K?new Float64Array(l*h):null,k=R?new Map:new Map;C&&C.fill(1/0),b&&b.fill(-1/0);let z=new Int32Array(this.rowFields.length+1),U=new Int32Array(this.colFields.length+1),E=(c,s,d,f)=>{let I=0,p=c;for(;p!==-1;)d[p]&&(f[I++]=p),p=s.nodeToParent[p];return I};for(let c=0;c<this.groupCount;c++){let s=this.leafGroupRowIds[c],d=this.leafGroupColIds[c],f=E(s,this.rowTrie,v,z),I=E(d,this.colTrie,S,U);for(let p=0;p<f;p++){let $=z[p];for(let B=0;B<I;B++){let q=U[B],j=$*67108864+q,g=N.get(j);if(g===void 0){if(g=x++,N.set(j,g),g>=M.length){let A=g*2,m=new Float64Array(A);m.set(M),M=m;let P=new Float64Array(A*h);P.set(y),y=P;let T=new Int32Array(A*h);if(T.set(X),X=T,V){let F=new Float64Array(A*h);F.set(V),V=F;}if(C){let F=new Float64Array(A*h);F.fill(1/0),F.set(C),C=F;}if(b){let F=new Float64Array(A*h);F.fill(-1/0),F.set(b),b=F;}}M[g]=j;}for(let A=0;A<h;A++){let m=g*h+A,P=c*h+A;switch(D[A]){case "sum":y[m]+=this.leafGroupMeasures[P];break;case "count":y[m]+=this.leafGroupCounts[P];break;case "avg":y[m]+=this.leafGroupMeasures[P],V[m]+=this.leafGroupCounts[P];break;case "min":{let T=this.leafGroupMins[P];T<C[m]&&(C[m]=T);break}case "max":{let T=this.leafGroupMaxes[P];T>b[m]&&(b[m]=T);break}case "distinct":{let T=this.leafGroupDistinctSets.get(P);if(T){let F=k.get(m);F||(F=new Set,k.set(m,F));for(let ie of T)F.add(ie);}break}default:y[m]+=this.leafGroupMeasures[P];break}X[m]+=1;}}}}for(let c=0;c<x;c++)for(let s=0;s<h;s++){let d=c*h+s;switch(D[s]){case "avg":y[d]=V[d]>0?y[d]/V[d]:0;break;case "min":y[d]=C[d]!==1/0?C[d]:0;break;case "max":y[d]=b[d]!==-1/0?b[d]:0;break;case "distinct":{let f=k.get(d);y[d]=f?f.size:0;break}}}let Q=M.slice(0,x),W=y.slice(0,x*h),Y=X.slice(0,x*h),H=(c,s,d,f,I)=>{let p=[];for(let B=0;B<f.length;B++)p[B]=f[B]?.id||"";let $=B=>{let q=c.childrenMap.get(B);if(!q)return [];let j=[];for(let g of q.values())if(s[g]){let A=c.nodeToDepth[g]-1,m=d[g]===1;j.push({key:I[g],value:this.dictionary[c.nodeToVal[g]],fieldId:p[A]||"",children:m?$(g):[],isExpanded:m,isTotal:false,depth:A,hasChildren:c.nodeChildCount[g]>0});}return j};return $(0)},Z=H(this.rowTrie,v,r,this.rowFields,n),G=H(this.colTrie,S,u,this.colFields,t);return {id:e.id,rowTree:Z,colTree:G,cellMatrix:{keys:Q,sums:W,counts:Y},rowIndexMap:this.rowIndexMapCache,columnIndexMap:this.columnIndexMapCache,cardinalityMap:this.cardinalityMap,indexMapsVersion:this.buildVersion}}};var O=new ee,ne=-1;self.onmessage=te=>{let e=te.data;try{if(e.action==="init")O.init(e),self.postMessage({id:e.id,isInitComplete:!0});else if(e.action==="initStart")O.initStart(e);else if(e.action==="initChunk")O.initChunk(e);else if(e.action==="initEnd")O.initEnd(),self.postMessage({id:e.id,isInitComplete:!0});else if(e.action==="filter")O.applyFilter(e),self.postMessage({id:e.id,isFilterComplete:!0});else if(e.action==="reconfigure")O.reconfigure(e),self.postMessage({id:e.id,isReconfigureComplete:!0});else if(e.action==="compute"){let n=O.compute(e),t=[n.cellMatrix.keys.buffer,n.cellMatrix.sums.buffer,n.cellMatrix.counts.buffer];if(n.indexMapsVersion!==void 0&&n.indexMapsVersion===ne){let r={...n,rowIndexMap:void 0,columnIndexMap:void 0,cardinalityMap:void 0,indexMapsOmitted:!0};postMessage(r,t);}else n.indexMapsVersion!==void 0&&(ne=n.indexMapsVersion),postMessage(n,t);}}catch(n){self.postMessage({id:e.id,error:n instanceof Error?n.message:String(n)});}};
package/package.json ADDED
@@ -0,0 +1,70 @@
1
+ {
2
+ "name": "@kanunilabs/pivotgrid-core",
3
+ "version": "1.0.0",
4
+ "description": "Framework-agnostic PivotGrid engine with Web Worker, columnar storage, trie-based grouping, and headless controller",
5
+ "license": "SEE LICENSE IN LICENSE",
6
+ "type": "module",
7
+ "exports": {
8
+ ".": {
9
+ "import": {
10
+ "types": "./dist/index.d.ts",
11
+ "default": "./dist/index.js"
12
+ },
13
+ "require": {
14
+ "types": "./dist/index.d.cts",
15
+ "default": "./dist/index.cjs"
16
+ }
17
+ },
18
+ "./dist/pivot.worker.js": {
19
+ "import": "./dist/pivot.worker.js",
20
+ "require": "./dist/pivot.worker.cjs"
21
+ },
22
+ "./dist/export.worker.js": {
23
+ "import": "./dist/export.worker.js",
24
+ "require": "./dist/export.worker.cjs"
25
+ },
26
+ "./dist/import.worker.js": {
27
+ "import": "./dist/import.worker.js",
28
+ "require": "./dist/import.worker.cjs"
29
+ }
30
+ },
31
+ "main": "./dist/index.cjs",
32
+ "module": "./dist/index.js",
33
+ "types": "./dist/index.d.ts",
34
+ "files": [
35
+ "dist",
36
+ "LICENSE"
37
+ ],
38
+ "publishConfig": {
39
+ "access": "public"
40
+ },
41
+ "peerDependencies": {
42
+ "file-saver": ">=2.0.0",
43
+ "xlsx": ">=0.18.0"
44
+ },
45
+ "devDependencies": {
46
+ "@types/file-saver": "^2.0.7"
47
+ },
48
+ "keywords": [
49
+ "pivotgrid",
50
+ "pivot-table",
51
+ "pivot-engine",
52
+ "olap",
53
+ "analytics",
54
+ "data-analysis",
55
+ "web-worker",
56
+ "columnar-storage",
57
+ "headless",
58
+ "framework-agnostic"
59
+ ],
60
+ "repository": {
61
+ "type": "git",
62
+ "url": "https://github.com/kanunilabs/pivotgrid",
63
+ "directory": "packages/core"
64
+ },
65
+ "scripts": {
66
+ "build": "tsup",
67
+ "build:check": "tsc --noEmit",
68
+ "test": "vitest run"
69
+ }
70
+ }