Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refresh db after createdocsdb #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions src/introspective.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
43 changes: 28 additions & 15 deletions src/static.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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