JuliaDB: GroupViews


#1

I’ve found it particularly useful to view JuliaDB tables by chunks (denoted either by their primary keys, or a subset of the indexing key). Is there any plan to implement anything like the following into the library(whether in IndexedTables or JuliaDB)?

struct GroupViews
    jdb
    keyview
    key::Union{Tuple, Array}
    length::Int
end

GroupViews(jdb) = GroupViews(jdb, select( jdb, jdb.pkey ), jdb.pkey, length( jdb ) )
GroupViews(jdb, key) = GroupViews(jdb, select(jdb, key), key, length( jdb ) )

function Base.iterate( iter::GroupViews, state = ( 1, iter.keyview[1] ) )
    index, lastchunkkey = state
    if index == iter.length
        return( nothing )  
    end
    
    inchunk = true
    lastindex = index
    
    while inchunk
        index += 1
        if index == iter.length
            break  
        end
        inchunk = all( values(iter.keyview[index]) .== values(lastchunkkey) )
    end
    return( ( iter.jdb[(lastindex):(index - 1)], ((lastindex),(index - 1))), #Return value(jdb view, (index_i, index_f))
            ( index , iter.keyview[ index ] ) )#State
end