Skip to content

Commit

Permalink
Merge pull request #34 from DilumAluthge/dpa/mbd_unpack
Browse files Browse the repository at this point in the history
Don't extend `Base.convert`; instead, define a separate `mbd_unpack` function
  • Loading branch information
meggart authored Sep 1, 2022
2 parents a938537 + d2c663b commit 94f4542
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "LMDB"
uuid = "11f193de-5e89-5f17-923a-7d207d56daf9"
authors = ["Art Wild <[email protected]>"]
version = "0.2.1"
version = "1.0.0-DEV"

[deps]
CEnum = "fa961155-64e5-5f13-b03f-caf6b980ea82"
Expand Down
2 changes: 1 addition & 1 deletion src/LMDB.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module LMDB
@isdefined(Docile) && eval(:(@document))

import Base: open, close, getindex, setindex!, put!, reset,
isopen, count, delete!, keys, get, show, convert, show
isopen, count, delete!, keys, get, show, show
import Base.Iterators: drop

export Environment, create, open, close, sync, set!, unset!, getindex, setindex!, path, info, show,
Expand Down
10 changes: 5 additions & 5 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ function MDBValue(val::T) where {T}
return MDB_val(Csize_t(val_size), convert(Ptr{Cvoid},pointer(val)))
end

convert(::Type{T}, mdb_val_ref::Ref{MDB_val}) where {T} = _convert(T, mdb_val_ref[])
function _convert(::Type{String}, mdb_val::MDB_val)
mbd_unpack(::Type{T}, mdb_val_ref::Ref{MDB_val}) where {T} = _mbd_unpack(T, mdb_val_ref[])
function _mbd_unpack(::Type{T}, mdb_val::MDB_val) where {T <: String}
unsafe_string(convert(Ptr{UInt8}, mdb_val.mv_data), mdb_val.mv_size)
end
function _convert(::Type{Vector{T}}, mdb_val::MDB_val) where {T}
function _mbd_unpack(::Type{V}, mdb_val::MDB_val) where {T, V <: Vector{T}}
res = unsafe_wrap(Array, convert(Ptr{UInt8}, mdb_val.mv_data), mdb_val.mv_size)
reinterpret(T,res)
reinterpret(T, res)
end
function _convert(::Type{T}, mdb_val::MDB_val) where {T}
function _mbd_unpack(::Type{T}, mdb_val::MDB_val) where {T}
unsafe_load(convert(Ptr{T}, mdb_val.mv_data))
end

Expand Down
16 changes: 8 additions & 8 deletions src/cur.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ struct ReturnValueSize end

arcopy(x::Array) = copy(x)
arcopy(x) = x
process_returns(::ReturnKeys{K}, mdb_key_ref, _) where K = arcopy(convert(K, mdb_key_ref)),MDB_NEXT
process_returns(::ReturnValues{V}, _, mdb_val_ref) where V = arcopy(convert(V, mdb_val_ref)), MDB_NEXT
process_returns(::ReturnBoth{K,V}, mdb_key_ref, mdb_val_ref) where {K,V} = arcopy((convert(K, mdb_key_ref)) => arcopy(convert(V, mdb_val_ref))), MDB_NEXT
process_returns(::ReturnKeys{K}, mdb_key_ref, _) where K = arcopy(mbd_unpack(K, mdb_key_ref)),MDB_NEXT
process_returns(::ReturnValues{V}, _, mdb_val_ref) where V = arcopy(mbd_unpack(V, mdb_val_ref)), MDB_NEXT
process_returns(::ReturnBoth{K,V}, mdb_key_ref, mdb_val_ref) where {K,V} = arcopy((mbd_unpack(K, mdb_key_ref)) => arcopy(mbd_unpack(V, mdb_val_ref))), MDB_NEXT
process_returns(::ReturnValueSize, _, mdb_val_ref) = mdb_val_ref[].mv_size, MDB_NEXT
function init_values(d::LMDBIterator)
k,op = if !isempty(d.prefix)
Expand All @@ -93,7 +93,7 @@ function Base.iterate(iter::LMDBIterator, refs)
if ret == 0
#Check if we are still in key prefix
if !isempty(iter.prefix)
k = convert(Vector{UInt8}, mdb_key_ref)
k = mbd_unpack(Vector{UInt8}, mdb_key_ref)
if any(i->!=(i...),zip(iter.prefix, k))
return nothing
end
Expand All @@ -118,14 +118,14 @@ function DirectoryLister(; sep = '/', lprefix=0)
end

function process_returns(l::DirectoryLister{K}, mdb_key_ref, _) where K
k = convert(Vector{UInt8}, mdb_key_ref)
k = mbd_unpack(Vector{UInt8}, mdb_key_ref)
nextsep = findnext(==(l.sep),k,l.istart)
if nextsep === nothing
return arcopy(convert(K, mdb_key_ref)),MDB_NEXT
return arcopy(mbd_unpack(K, mdb_key_ref)),MDB_NEXT
else
k = copy(k)
resize!(k,nextsep)
kout = arcopy(convert(K, Ref(MDBValue(k))))
kout = arcopy(mbd_unpack(K, Ref(MDBValue(k))))
k[end] = k[end]+1
mdb_key_ref[] = MDBValue(k)
return kout, MDB_SET_RANGE
Expand Down Expand Up @@ -165,7 +165,7 @@ function get(cur::Cursor, key, ::Type{T}, op::MDB_cursor_op=MDB_SET_KEY) where T
mdb_cursor_get(cur.handle, mdb_key_ref, mdb_val_ref, op)

# Convert to proper type
return convert(T, mdb_val_ref)
return mbd_unpack(T, mdb_val_ref)
end

"""Store by cursor.
Expand Down
2 changes: 1 addition & 1 deletion src/dbi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,5 @@ function get(txn::Transaction, dbi::DBI, key, ::Type{T}) where T
mdb_get(txn.handle, dbi.handle, mdb_key_ref, mdb_val_ref)

# Convert to proper type
return convert(T, mdb_val_ref)
return mbd_unpack(T, mdb_val_ref)
end
6 changes: 3 additions & 3 deletions src/dicts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mutable struct LMDBDict{K,V}
x
end
end
function LMDBDict{K,V}(path::String; readonly = false, rdahead=false) where {K,V}
function LMDBDict{K,V}(path::String; readonly = false, rdahead=false) where {K,V}
flags = readonly ? MDB_RDONLY : zero(Cuint)
if !rdahead
flags = flags | MDB_NORDAHEAD
Expand Down Expand Up @@ -110,7 +110,7 @@ function Base.get(d::LMDBDict{K,V}, key, default) where {K,V}
if ret == MDB_NOTFOUND
return default
elseif ret == Cint(0)
return convert(V, mdb_val_ref)
return mbd_unpack(V, mdb_val_ref)
else
throw(LMDB.LMDBError(ret))
end
Expand All @@ -129,4 +129,4 @@ function Base.delete!(d::LMDBDict{K},k) where K
LMDB.delete!(txn, dbi, convert(K,k))
end
d
end
end
10 changes: 5 additions & 5 deletions test/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ using Test
mdb_val_ref = Ref(MDBValue(val));
mdb_val = mdb_val_ref[]
# @test val == unsafe_string(convert(Ptr{UInt8}, mdb_val.data), mdb_val.size)
@test val == convert(String, mdb_val_ref)
@test val == LMDB.mbd_unpack(String, mdb_val_ref)

val = [1233] # dense array
T = eltype(val)
Expand All @@ -25,7 +25,7 @@ using Test
nvals = floor(Int, mdb_val.mv_size/sizeof(T))
value = unsafe_wrap(Array, convert(Ptr{T}, mdb_val.mv_data), nvals)
@test val == value
@test val == convert(Vector{Int}, mdb_val_ref)
@test val == LMDB.mbd_unpack(Vector{Int}, mdb_val_ref)

val = [0x0003, 0xff45]
val_size = sizeof(val)
Expand All @@ -36,7 +36,7 @@ using Test
nvals = floor(Int, mdb_val.mv_size/sizeof(T))
value = unsafe_wrap(Array, convert(Ptr{T}, mdb_val.mv_data), nvals)
@test val == value
@test val == convert(Vector{UInt16}, mdb_val_ref)
@test val == LMDB.mbd_unpack(Vector{UInt16}, mdb_val_ref)

struct TestType
i::Int
Expand All @@ -53,5 +53,5 @@ using Test
nvals = floor(Int, mdb_val.mv_size/sizeof(T))
value = unsafe_wrap(Array, convert(Ptr{T}, mdb_val.mv_data), nvals)
@test val == value
@test val == convert(Vector{T}, mdb_val_ref)
@test val[1] == convert(T, mdb_val_ref)
@test val == LMDB.mbd_unpack(Vector{T}, mdb_val_ref)
@test val[1] == LMDB.mbd_unpack(T, mdb_val_ref)

0 comments on commit 94f4542

Please sign in to comment.