From 2459e976426451c7f9224ac38d89dcd5b19fadda Mon Sep 17 00:00:00 2001 From: Shuhei Kadowaki Date: Thu, 5 Mar 2020 21:16:24 +0900 Subject: [PATCH 1/2] refresh db after creating it --- src/static.jl | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/src/static.jl b/src/static.jl index adb7223..fc6611e 100644 --- a/src/static.jl +++ b/src/static.jl @@ -5,7 +5,14 @@ using Serialization: serialize, deserialize include("utils.jl") const PROGRESS_ID = "docseeker_progress" -DOCDBCACHE = DocObj[] +const DB_DIR = joinpath(@__DIR__, "..", "db") +""" + DOC_DB_CACHE::Vector{DocObj} + +Database of docstrings that is supposed to be created by [`createdocsdb`](@ref). +""" +const DOC_DB_CACHE = DocObj[] +const DB_TASK = Ref{Union{Nothing,Task}}(nothing) function _createdocsdb() @info "Docs" progress=0 _id=PROGRESS_ID @@ -22,6 +29,8 @@ function _createdocsdb() catch err @error err finally + @info "Docs: Refreshing database" progress=1-(1/1_000_000) _id=PROGRESS_ID + refreshdb!() # refresh database @info "" progress=1 _id=PROGRESS_ID end end @@ -55,35 +64,39 @@ function createdocsdb() rm(joinpath(dbdir, file)) end end - @async _createdocsdb() + DB_TASK[] = @async _createdocsdb() nothing end """ loaddocsdb() -> Vector{DocObj} -Retrieve the docstrings from the "database" created by [`createdocsdb()`](@ref). -Will return an empty vector if the database is locked by [`createdocsdb()`](@ref). +Retrieve the docstrings from [`DOC_DB_CACHE`](@ref). """ function loaddocsdb() - global DOCDBCACHE - isempty(DOCDBCACHE) && (DOCDBCACHE = _loaddocsdb()) - length(DOCDBCACHE) == 0 && + if DB_TASK[] !== nothing && !istaskdone(DB_TASK[]) + @warn "Doc cache is being created by `DocSeeker.createdocsdb()`." + end + isempty(DOC_DB_CACHE) && refreshdb!() + isempty(DOC_DB_CACHE) && throw(ErrorException("Please regenerate the doc cache by calling `DocSeeker.createdocsdb()`.")) - DOCDBCACHE + return DOC_DB_CACHE end -function _loaddocsdb() - dbdir = joinpath(@__DIR__, "..", "db") - docs = DocObj[] - for file in readdir(dbdir) +""" + refreshdb!() + +Refresh [`DOC_DB_CACHE`](@ref). +""" +function refreshdb!() + empty!(DOC_DB_CACHE) + for file in readdir(DB_DIR) endswith(file, ".db") || continue try - append!(docs, deserialize(joinpath(dbdir, file))) + append!(DOC_DB_CACHE, deserialize(joinpath(DB_DIR, file))) catch err # @error err, file end end - unique!(docs) - return docs + unique!(DOC_DB_CACHE) end From 91980d71ea98f4274b5891118766f680b6c0dbdb Mon Sep 17 00:00:00 2001 From: Shuhei Kadowaki Date: Thu, 5 Mar 2020 21:16:58 +0900 Subject: [PATCH 2/2] more const --- src/introspective.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/introspective.jl b/src/introspective.jl index 3de0818..aa201c0 100644 --- a/src/introspective.jl +++ b/src/introspective.jl @@ -2,11 +2,11 @@ mutable struct GlobalCache time::Float64 cache::Vector{DocObj} end -const CACHE = GlobalCache(0.0, DocObj[]) +const CACHE = GlobalCache(0., DocObj[]) -CACHETIMEOUT = 30 # s +const CACHETIMEOUT = Ref(30.) # s -MAX_RETURN_SIZE = 20 # how many results to return at most +const MAX_RETURN_SIZE = 20 # how many results to return at most function searchdocs(needle::AbstractString; loaded::Bool = true, mod::Module = Main, maxreturns::Int = MAX_RETURN_SIZE, exportedonly::Bool = false, @@ -95,7 +95,7 @@ end Find all docstrings in all currently loaded Modules. """ function alldocs(topmod = Main)::Vector{DocObj} - time() - CACHE.time < CACHETIMEOUT && return CACHE.cache + time() - CACHE.time < CACHETIMEOUT[] && return CACHE.cache results = DocObj[] # all bindings