Skip to content

Commit

Permalink
add thread lock when insert
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyvadra committed Dec 8, 2023
1 parent ad7a5c0 commit 3115aa6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
12 changes: 9 additions & 3 deletions src/MmapDB.jl
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,10 @@ function GenerateCode(T::DataType)::Module
# InsertRow
s = "
function InsertRow(v)::Nothing
lock(idLock)
i = Config[\"lastNewID\"] + 1
Config[\"lastNewID\"] += 1"
Config[\"lastNewID\"] += 1
unlock(idLock)"
for i in 1:length(tmpNames)
s *= "
$(tName)Dict[:$(tmpNames[i])][i] = v.$(tmpNames[i])"
Expand All @@ -132,8 +134,10 @@ function GenerateCode(T::DataType)::Module
function InsertRow("
s = s * join(tmpNamesL, ", ")
s = s * ")::Nothing
lock(idLock)
i = Config[\"lastNewID\"] + 1
Config[\"lastNewID\"] += 1"
Config[\"lastNewID\"] += 1
unlock(idLock)"
for i in 1:length(tmpNames)
s *= "
$(tName)Dict[:$(tmpNames[i])][i] = $(tmpNamesL[i])"
Expand All @@ -145,9 +149,11 @@ function GenerateCode(T::DataType)::Module
# BatchInsert
s = "
function BatchInsert(v::Vector)::Nothing
lock(idLock)
i = Config[\"lastNewID\"] + 1 : Config[\"lastNewID\"] + length(v)
ids = collect(i)
Config[\"lastNewID\"] += length(v)"
Config[\"lastNewID\"] += length(v)
unlock(idLock)"
for i in 1:length(tmpNames)
s *= "
$(tName)Dict[:$(tmpNames[i])][ids] = map(x->x.$(tmpNames[i]), v)"
Expand Down
2 changes: 1 addition & 1 deletion src/tpl.body.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ dataFolder = "__ConfigDataFolder__"
alignAI()
return nothing
end
function alignAI()
function alignAI()::Int
assumeI = findlast(x->!iszero(x), __tName__Dict[_syms[1]])
if all(iszero.(
map(s->__tName__Dict[s][assumeI+1], _syms)
Expand Down
1 change: 1 addition & 0 deletions src/tpl.header.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ Config = Dict{String,Any}(
)
# openedFiles = Dict{Symbol, IOStream}()
openedFiles = IOStream[]
idLock = Threads.SpinLock()
26 changes: 19 additions & 7 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,35 @@ using Test
timestamp::Int64
end
t = MmapDB.GenerateCode(Something)
# mem test
@test isnothing(t.CreateMem(10))
# file test
@test isnothing(t.Create!(10))
@test isnothing(t.SetFieldLongitude(1,1.23))
@test t.GetFieldLongitude(1) == 1.23
@test iszero(t.GetFieldLongitude(2))
@test isnothing(t.Close())
# normal test
@test isnothing(t.Create!(10))
for i in 1:10
# mem test
@test isnothing(t.CreateMem(20))
for i in 1:20
t.SetFieldRegion_id(i,i)
t.SetFieldLongitude(i,i/10)
t.SetFieldLatitude(i,10i)
end
@test typeof(t.GetFieldRegion_id(1)) == UInt8
# search
@test t.Findfirst(x->x>4, :region_id) == 5
@test t.SearchSortedFirst(5, :region_id) == 5
# insert
t.Close(); t.CreateMem(20)
for i in 1:5
t.SetFieldRegion_id(i,i)
t.SetFieldLongitude(i,i/10)
t.SetFieldLatitude(i,10i)
t.SetFieldTimestamp(i,i)
end
@test t.alignAI() == 5
v = Something(11,1.2,3.4,5678)
t.InsertRow(v); t.InsertRow(v)
t.BatchInsert([v,v]); t.BatchInsert([v,v])
@test t.Config["lastNewID"] == 11
@test isequal(t.GetFieldRegion_id(11),11)
t.Close()
rm(tmpDir;force=true,recursive=true)
end

0 comments on commit 3115aa6

Please sign in to comment.