Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/compute_MTG/columnarize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function columnarize!(mtg::Node)
attrs isa ColumnarAttrs ||
error("columnarize! expects nodes with ColumnarAttrs attributes.")
source_store = _store_for_node_attrs(attrs)
raw = _isbound(attrs) ? Dict{Symbol,Any}(pairs(attrs)) : attrs.staged
raw = _isbound(attrs) ? Dict{Symbol,Any}(pairs(attrs)) : _staged_attributes(attrs)
_add_node_with_attrs!(store, node_id(n), symbol(n), raw)
if root_store !== nothing && source_store === root_store
_restore_root_row_absences!(store, root_store, node_id(n))
Expand All @@ -39,7 +39,7 @@ function columnarize!(mtg::Node)
attrs = node_attributes(n)::ColumnarAttrs
attrs.ref.store = store
attrs.ref.node_id = node_id(n)
empty!(attrs.staged)
attrs.staged = nothing
end
return mtg
end
Expand Down
2 changes: 1 addition & 1 deletion src/compute_MTG/node_funs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ function _merge_columnar_subtree_into_store!(node::Node, target_store::MTGAttrib
end
attrs.ref.store = target_store
attrs.ref.node_id = node_id(node)
empty!(attrs.staged)
attrs.staged = nothing
if _maybe_traversal_cache(node) !== nothing
source_store === nothing || source_store === target_store ||
_unregister_traversal_cache!(source_store, node)
Expand Down
46 changes: 32 additions & 14 deletions src/types/Attributes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -231,18 +231,23 @@ end

mutable struct ColumnarAttrs <: AbstractDict{Symbol,Any}
ref::NodeAttrRef
staged::Dict{Symbol,Any}
staged::Union{Nothing,Dict{Symbol,Any}}
end

ColumnarAttrs() = ColumnarAttrs(NodeAttrRef(nothing, 0), Dict{Symbol,Any}())
ColumnarAttrs() = ColumnarAttrs(NodeAttrRef(nothing, 0), nothing)
function ColumnarAttrs(d::AbstractDict)
isempty(d) && return ColumnarAttrs()
staged = Dict{Symbol,Any}()
for (k, v) in d
staged[Symbol(k)] = v
end
ColumnarAttrs(NodeAttrRef(nothing, 0), staged)
end

# Unbound empty attributes need no mutable staging dictionary.
@inline _staged_attributes(attrs::ColumnarAttrs) =
isnothing(attrs.staged) ? NamedTuple() : attrs.staged

@inline _isbound(attrs::ColumnarAttrs) = attrs.ref.store !== nothing && attrs.ref.node_id > 0

function _assert_columnar_attrs_unbound(attrs::ColumnarAttrs)
Expand Down Expand Up @@ -478,6 +483,10 @@ end
end

function _add_node_with_attrs!(store::MTGAttributeStore, node_id::Int, symbol::Symbol, attrs::NamedTuple)
if isempty(attrs)
_add_node_row!(store, node_id, symbol)
return nothing
end
bid = get(store.symbol_to_bucket, symbol, 0)
if bid == 0 || !all(key -> haskey(store.buckets[bid].col_index, key), keys(attrs))
# Keep the existing schema insertion order when a new key appears. Once
Expand Down Expand Up @@ -665,10 +674,10 @@ end
function init_columnar_root!(attrs::ColumnarAttrs, node_id::Int, symbol::Symbol)
_assert_columnar_attrs_unbound(attrs)
store = MTGAttributeStore()
_add_node_with_attrs!(store, node_id, symbol, attrs.staged)
_add_node_with_attrs!(store, node_id, symbol, _staged_attributes(attrs))
attrs.ref.store = store
attrs.ref.node_id = node_id
empty!(attrs.staged)
attrs.staged = nothing
return attrs
end

Expand All @@ -677,7 +686,7 @@ function bind_columnar_child!(
child_attrs::ColumnarAttrs,
node_id::Int,
symbol::Symbol,
values=child_attrs.staged,
values=_staged_attributes(child_attrs),
)
_assert_columnar_attrs_unbound(child_attrs)
store = _store_for_node_attrs(parent_attrs)
Expand All @@ -686,7 +695,7 @@ function bind_columnar_child!(
_add_node_with_attrs!(store, node_id, symbol, values)
child_attrs.ref.store = store
child_attrs.ref.node_id = node_id
empty!(child_attrs.staged)
child_attrs.staged = nothing
return child_attrs
end

Expand Down Expand Up @@ -720,7 +729,7 @@ end

function Base.length(attrs::ColumnarAttrs)
if !_isbound(attrs)
return length(attrs.staged)
return length(_staged_attributes(attrs))
end
store, bid, row = _bound_store_bid_row(attrs.ref)
bucket = store.buckets[bid]
Expand All @@ -733,7 +742,7 @@ end

function Base.keys(attrs::ColumnarAttrs)
if !_isbound(attrs)
return collect(keys(attrs.staged))
return collect(Symbol, keys(_staged_attributes(attrs)))
end
store, bid, row = _bound_store_bid_row(attrs.ref)
bucket = store.buckets[bid]
Expand All @@ -751,7 +760,7 @@ end

function Base.haskey(attrs::ColumnarAttrs, key::Symbol)
if !_isbound(attrs)
return haskey(attrs.staged, key)
return haskey(_staged_attributes(attrs), key)
end
store, bid, row = _bound_store_bid_row(attrs.ref)
bucket = store.buckets[bid]
Expand All @@ -763,6 +772,7 @@ Base.haskey(attrs::ColumnarAttrs, key) = haskey(attrs, _normalize_attr_key(key))

function Base.getindex(attrs::ColumnarAttrs, key::Symbol)
if !_isbound(attrs)
isnothing(attrs.staged) && throw(KeyError(key))
return attrs.staged[key]
end
store, bid, row = _bound_store_bid_row(attrs.ref)
Expand All @@ -783,7 +793,7 @@ end

@inline function _get_typed_numeric(attrs::ColumnarAttrs, key::Symbol, default::T) where {T<:Number}
if !_isbound(attrs)
v = get(attrs.staged, key, default)
v = get(_staged_attributes(attrs), key, default)
v === nothing && return nothing
return v isa T ? v : (convert(T, v)::T)
end
Expand All @@ -809,7 +819,7 @@ end

function Base.get(attrs::ColumnarAttrs, key::Symbol, default::T) where {T}
if !_isbound(attrs)
return get(attrs.staged, key, default)
return get(_staged_attributes(attrs), key, default)
end
store, bid, row = _bound_store_bid_row(attrs.ref)
bucket = store.buckets[bid]
Expand Down Expand Up @@ -853,7 +863,12 @@ end

function Base.setindex!(attrs::ColumnarAttrs, value::T, key::Symbol) where {T}
if !_isbound(attrs)
attrs.staged[key] = value
staged = attrs.staged
if isnothing(staged)
staged = Dict{Symbol,Any}()
attrs.staged = staged
end
staged[key] = value
return value
end
store, bid, row = _bound_store_bid_row(attrs.ref)
Expand Down Expand Up @@ -885,7 +900,8 @@ end

function Base.iterate(attrs::ColumnarAttrs, state=nothing)
if !_isbound(attrs)
return state === nothing ? iterate(attrs.staged) : iterate(attrs.staged, state)
staged = _staged_attributes(attrs)
return state === nothing ? iterate(staged) : iterate(staged, state)
end
store, bid, row = _bound_store_bid_row(attrs.ref)
bucket = store.buckets[bid]
Expand Down Expand Up @@ -922,6 +938,7 @@ end
function Base.pop!(attrs::ColumnarAttrs, key)
key_ = _normalize_attr_key(key)
if !_isbound(attrs)
isnothing(attrs.staged) && throw(KeyError(key_))
return pop!(attrs.staged, key_)
end
return _pop_bound!(attrs, key_, nothing, true)
Expand All @@ -930,6 +947,7 @@ end
function Base.pop!(attrs::ColumnarAttrs, key, default)
key_ = _normalize_attr_key(key)
if !_isbound(attrs)
isnothing(attrs.staged) && return default
return pop!(attrs.staged, key_, default)
end
return _pop_bound!(attrs, key_, default, false)
Expand All @@ -942,7 +960,7 @@ end

function Base.empty!(attrs::ColumnarAttrs)
if !_isbound(attrs)
empty!(attrs.staged)
attrs.staged = nothing
return attrs
end
store, bid, row = _bound_store_bid_row(attrs.ref)
Expand Down
4 changes: 3 additions & 1 deletion src/types/Node.jl
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ function _to_columnar_attrs(attributes::AbstractDict)
ColumnarAttrs(attributes)
end

_to_columnar_attrs(::NamedTuple{(),Tuple{}}) = ColumnarAttrs()

function _to_columnar_attrs(attributes::NamedTuple)
ColumnarAttrs(Dict{Symbol,Any}(pairs(attributes)))
end
Expand All @@ -137,7 +139,7 @@ function _to_columnar_attrs(attributes)
end

function Node(id::Int, parent::Node{M,ColumnarAttrs}, MTG::M, attributes::ColumnarAttrs) where {M<:AbstractNodeMTG}
_node_with_columnar_attrs(id, parent, MTG, attributes, attributes.staged)
_node_with_columnar_attrs(id, parent, MTG, attributes, _staged_attributes(attributes))
end

function Node(id::Int, parent::Node{M,ColumnarAttrs}, MTG::M, values::NamedTuple) where {M<:AbstractNodeMTG}
Expand Down
4 changes: 2 additions & 2 deletions test/test-columnar-reads.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ using MultiScaleTreeGraph

function historical_get(attrs, key::Symbol, default::T) where {T}
if !MultiScaleTreeGraph._isbound(attrs)
return get(attrs.staged, key, default)
return isnothing(attrs.staged) ? default : get(attrs.staged, key, default)
end
store, bid, row = MultiScaleTreeGraph._bound_store_bid_row(attrs.ref)
bucket = store.buckets[bid]
Expand All @@ -33,7 +33,7 @@ using MultiScaleTreeGraph

function historical_numeric_get(attrs, key::Symbol, default::T) where {T<:Number}
if !MultiScaleTreeGraph._isbound(attrs)
value = get(attrs.staged, key, default)
value = isnothing(attrs.staged) ? default : get(attrs.staged, key, default)
value === nothing && return nothing
return value isa T ? value : (convert(T, value)::T)
end
Expand Down
39 changes: 37 additions & 2 deletions test/test-columnar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,8 @@ end
@test haskey(children(direct_root)[1], :nullable)
@test !haskey(children(direct_root)[3], :nullable)
@test children(direct_root)[6][:temperature] === 20.0
@test node_attributes(children(direct_root)[1]).staged !== node_attributes(children(direct_root)[2]).staged
@test node_attributes(children(direct_root)[1]).ref !== node_attributes(children(direct_root)[2]).ref
@test all(node -> isempty(node_attributes(node).staged), children(direct_root))
@test all(node -> isnothing(node_attributes(node).staged), children(direct_root))

source = Dict{Any,Any}("value" => 9, :label => "dictionary")
saved_source = copy(source)
Expand Down Expand Up @@ -525,3 +524,39 @@ leaf_widths_after = descendants(mtg, :Width, symbol=:Leaf, ignore_nothing=true)
descendants_strategy!(mtg, :pointer)
@test descendants_strategy(mtg) == :pointer
@test descendants(mtg, :Width, symbol=:Leaf, ignore_nothing=true) == leaf_widths_after


@testset "Empty attributes stay independent through binding and removal" begin
first = MultiScaleTreeGraph.ColumnarAttrs()
second = MultiScaleTreeGraph.ColumnarAttrs(Dict{Symbol,Any}())
@test isempty(first)
@test keys(first) == Symbol[]
@test collect(pairs(first)) == Pair{Symbol,Any}[]
@test get(first, :value, 3f0) === 3f0
@test pop!(first, :value, :absent) === :absent
@test_throws KeyError first[:value]
@test_throws KeyError pop!(first, :value)
first[:value] = 2f0
@test first[:value] === 2f0
@test !haskey(second, :value)
empty!(first)
@test isempty(first)
@test isnothing(first.staged)

root = Node(1, NodeMTG(:/, :Plant, 1, 1))
leaf_a = Node(2, root, NodeMTG(:/, :Leaf, 1, 2), NamedTuple())
leaf_b = Node(3, root, NodeMTG(:+, :Leaf, 2, 2))
@test all(n -> isnothing(node_attributes(n).staged), (root, leaf_a, leaf_b))
leaf_a[:value] = 4f0
@test leaf_a[:value] === 4f0
@test !haskey(leaf_b, :value)
saved = node_attributes(leaf_a)
delete_node!(leaf_a)
@test isempty(saved)
saved[:detached] = :annotation
@test saved[:detached] === :annotation
@test !haskey(leaf_b, :detached)
@test columnarize!(root) === root
@test !haskey(leaf_b, :value)
@test !haskey(leaf_b, :detached)
end
Loading