From 3115aa6b265c267518398e3b08aa45386e3ec207 Mon Sep 17 00:00:00 2001 From: Cyvadra Date: Sat, 9 Dec 2023 02:26:02 +0800 Subject: [PATCH] add thread lock when insert --- src/MmapDB.jl | 12 +++++++++--- src/tpl.body.jl | 2 +- src/tpl.header.jl | 1 + test/runtests.jl | 26 +++++++++++++++++++------- 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/MmapDB.jl b/src/MmapDB.jl index 3493e41..5057d14 100644 --- a/src/MmapDB.jl +++ b/src/MmapDB.jl @@ -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])" @@ -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])" @@ -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)" diff --git a/src/tpl.body.jl b/src/tpl.body.jl index 8d2dc7f..2ba759e 100644 --- a/src/tpl.body.jl +++ b/src/tpl.body.jl @@ -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) diff --git a/src/tpl.header.jl b/src/tpl.header.jl index d9e5046..9a13842 100644 --- a/src/tpl.header.jl +++ b/src/tpl.header.jl @@ -8,3 +8,4 @@ Config = Dict{String,Any}( ) # openedFiles = Dict{Symbol, IOStream}() openedFiles = IOStream[] +idLock = Threads.SpinLock() diff --git a/test/runtests.jl b/test/runtests.jl index 51fc92c..2be6a61 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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