Skip to content

Commit

Permalink
Minimal group by support. Also fixed a bug where record count was som…
Browse files Browse the repository at this point in the history
…etimes not being persisted.
  • Loading branch information
mkromberg committed Apr 27, 2015
1 parent 44398b5 commit 927a958
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.sublime-*

testdb1/
105 changes: 91 additions & 14 deletions vecdb.dyalog
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
(⎕IO ⎕ML)1 1

:Section Constants
:Field Public Shared Version'0.2.0'
:Field Public Shared Version'0.2.1' With minimal "Group By" support
:Field Public Shared TypeNames,¨'I1' 'I2' 'I4' 'F' 'B' 'C'
:Field Public Shared TypeNums83 163 323 645 11 163
:Field Public Shared SummaryFns'sum' 'max' 'min' 'count'
:Field Public Shared SummaryAPLFns'+/' '⌈/' '⌊/' ''
:EndSection Constants

:Section Instance Fields
Expand Down Expand Up @@ -46,7 +48,7 @@
:Property Count
:Access public
rget
r+/_Count
r+/_Counts.counter
:EndProperty

Expand Down Expand Up @@ -92,17 +94,17 @@
MakeMaps;s;i;types;T;ns;dr;col;sizes
[Re]make all maps
typesTypeNums[TypeNamesTypes]
_Count(Shards),0
_Counts⎕NS¨(Shards)

:For i :In Shards
siShards
(i_Count)645 1 ⎕MAP((iShardFolders),'counters.vecdb')'W' Map record counter
_Counts[i].counter645 1 ⎕MAP((iShardFolders),'counters.vecdb')'W' Map record counter
:For col :In s
(cols).vector(types[col],¯1)⎕MAP(cols).file'W'
:EndFor

:If 1sizess.(vector) mapped vectors have different lengths
:OrIf sizes.<i_Count or shorter than record count
:OrIf sizes.<(i_Counts).counter or shorter than record count
File damaged
:EndIf
:EndFor
Expand Down Expand Up @@ -248,7 +250,7 @@

rClose
:Access Public
⎕EX'Shards' 'symbols' '_Count'
⎕EX'Shards' 'symbols' '_Counts'
risOpen0 record the fact

Expand All @@ -269,21 +271,42 @@
:EndIf
:EndFor
:EndIf

(summary colnames)ParseSummary cols;p
Split column specifications into summaryfn colname

:If 0=cols summarycolnames
:Else
:If 2>cols cols,,cols :EndIf Enclose if simple
pp×(¨cols)pcols¨' ' position of separator
summary(0p-1)¨cols
colnamesp¨cols
:EndIf

rQuery(where cols);col;value;ix;j;s;count;Data;Cols
rQuery args;where;cols;groupby;col;value;ix;j;s;count;Data;Cols;summary;m;i
:Access Public

:If (2=where)where[1]Columns single constraint?
(where cols groupby)3args,(args)
:If 2=where :AndIf where[1]Columns just a single constraint?
where,where
:EndIf

(summary cols)ParseSummary cols
'UNKNOWN SUMMARY FUNCTION'⎕SIGNAL(/summarySummaryFns,'')11

:If 0groupby We are grouping
:If 1=groupby groupby,groupby :EndIf Enclose if simple
m(0¨summary)colsgroupby summary or one of the grouping cols?
'ONLY SUMMARIZED COLUMNS MAY BE SELECTED WHEN GROUPING'⎕SIGNAL(/m)11
:EndIf

r0 20 (shard indices)

:For s :In Shards
ColssShards
counts_Count
count(s_Counts).counter
ix⎕NULL

:For (col value) :In where AND them all together
Expand All @@ -302,21 +325,75 @@
rs ix
:EndFor Shard

:If 0cols rRead r cols :EndIf
:If 0=cols :GoTo 0 Not asked to return anything: Just return indices
:ElseIf 0=groupby no group by statement
rRead r cols
:For i :In (0¨summary)/cols
(ir)((SummaryFnssummary[i])SummaryAPLFns),'i⊃r'
:EndFor
:Else
rSummarize r summary cols groupby
:EndIf

rSummarize(ix summary cols groupby);char;m;num;s;indices;fns;cix;allix;allcols;numrecs;blksize;offset;groupfn;t;multi;split;data
Read and Summarize specified indices of named columns
Very similar to Read, but not public - called by Query

allixColumnsallcolsgroupbycols
:If (1cols)1groupby
'Only one summary and one groupby column right now, sorry'⎕SIGNAL 11
:EndIf

fns(SummaryAPLFns,'')[SummaryFnssummary]
groupfn'{⍺,',(1fns),'⍵}⌸' This needs enhancement to remove that limitation above
r(0,allix)0

:For (s indices) :In ix
offset0
:If indices⎕NULL All records selected
blksizenumrecs(s_Counts).counter
:Else <indices> records selected
blksizenumrecsindices
:EndIf

split0 We did it all at once
:Repeat
:Trap 1 WS FULL
:If indices⎕NULL All records still selected
dataoffset((sShards)[allix].{vector})blksize
:Else
data(sShards)[allix].{vector[]}blksizeoffsetindices
:EndIf

r(1data)groupfn 2data
offset+blksize
:Else Got a WS FULL
∘∘∘ Morten to trace this
split1 We had to go around again
blksizeblksize(÷)2
:EndTrap
:Until offsetnumrecs

:If split
rr[;1]groupfn r[;2]
:EndIf
:EndFor

rRead(ix cols);char;m;num;cix;s;indices
Read specified indices of named columns
:Access Public

:If 1=ix ix1,ix :EndIf Single Shard?
:If 1=⍴⍴ix ix1,ix :EndIf Single Shard?
:If 1=cols cols,cols :EndIf Single simple column name
⎕SIGNAL/ValidateColumns cols
cixColumnscols
r(cix)

:For (s indices) :In ix
rr,¨(sShards)[cix].{vector[]}indices
:If indices⎕NULL rr,¨(s_Counts).counter¨(sShards)[cix].vector
:Else rr,¨(sShards)[cix].{vector[]}indices :EndIf
:EndFor

:If 0char{/}'C'=¨Types[cix] Symbol transation
Expand Down Expand Up @@ -349,7 +426,7 @@
ddata[;shardss]
lengthd # records to be written to *this* Shard
ColssShards Mapped columns in this Shard
counts_Count Active records in this Shard
count(s_Counts).counter Active records in this Shard
sizeCols[cix].vector Current Shard allocation

:If 0canupdatelengthsize-count Updates to existing maps
Expand All @@ -364,7 +441,7 @@
ExtendShard(sShardFolders)Cols growth append
:EndIf

_Count[s]count+length Update (mapped) counter
_Counts[s].counter[1]count+length Update (mapped) counter
:EndFor

r0
Expand Down

0 comments on commit 927a958

Please sign in to comment.