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

type stability with JSON.jl #16

Open
wants to merge 1 commit 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
13 changes: 13 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name = "AssetRegistry"
uuid = "bf4720bc-e11a-5d0c-854e-bdca1663c893"
license = "MIT"
version = "0.1.0"

[deps]
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
Pidfile = "fa939f87-e72e-5be4-a000-7fc836dbe307"
SHA = "ea8e919c-243c-51af-8825-aaa63cd721ce"

[compat]
JSON = "0.21, 1.0"
Pidfile = "1.2.0"
77 changes: 32 additions & 45 deletions src/AssetRegistry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ julia> key = AssetRegistry.register("/Users/ranjan/.julia/v0.6/Tachyons/assets/t
```
"""
function register(path; registry_file = joinpath(homedir(), ".jlassetregistry.json"))
target = normpath(abspath(expanduser(path)))
(isfile(target) || isdir(target)) ||
error("Asset not found")
target = gettarget(path)
(isfile(target) || isdir(target)) || error("Asset not found")

key = getkey(target)
if haskey(registry, key)
Expand All @@ -37,71 +36,57 @@ function register(path; registry_file = joinpath(homedir(), ".jlassetregistry.js
end

# update global registry file

# touch(registry_file) -- this doesn't work on Azure fs
if !isfile(registry_file)
# WARN: may need a lock here
open(_->nothing, registry_file, "w")
end

pidlock = joinpath(homedir(), ".jlassetregistry.lock")

withlock(pidlock) do
fkey = filekey(key)
io = open(registry_file, "r+") # open in read-and-write mode
# first get existing entries
prev_registry = filesize(io) > 0 ? JSON.parse(io) : Dict{String,Tuple{String,Int}}()

update_registry_file(registry_file, key) do prev_registry, fkey
if haskey(prev_registry, fkey)
prev_registry[fkey] = (target, prev_registry[fkey][2]+1) # increment ref count
prev_registry[fkey] = [target, prev_registry[fkey][2]+1] # increment ref count
else
prev_registry[fkey] = (target, 1) # init refcount to 1
prev_registry[fkey] = [target, 1] # init refcount to 1
end

# write the updated registry to file
seekstart(io)
JSON.print(io, prev_registry)

# truncate it to current length
truncate(io, position(io))
close(io)
end

registry[key] = target # keep this in current process memory


key
return key
end

function deregister(path; registry_file = joinpath(homedir(), ".jlassetregistry.json"))
target = normpath(abspath(expanduser(path)))
target = gettarget(path)

key = getkey(target)
if !haskey(registry, key)
return
return nothing
end

pop!(registry, key)

touch(registry_file)

pidlock = joinpath(homedir(), ".jlassetregistry.lock")
withlock(pidlock) do
fkey = filekey(key)
io = open(registry_file, "r+")

# get existing information
prev_registry = filesize(io) > 0 ? JSON.parse(io) : Dict{String,Tuple{String, Int}}()

update_registry_file(registry_file, key) do prev_registry, fkey
if haskey(prev_registry, fkey)
val, count = prev_registry[fkey]
if count == 1
pop!(prev_registry, fkey)
else
prev_registry[fkey] = (val, count-1) # increment ref count
prev_registry[fkey] = [val, count-1] # increment ref count
end
end
end

return key
end

function update_registry_file(f, file, key)
pidlock = joinpath(homedir(), ".jlassetregistry.lock")
# touch(registry_file) -- this doesn't work on Azure fs
if !isfile(file)
# WARN: may need a lock here
open(_ -> nothing, file, "w")
end
withlock(pidlock) do
fkey = filekey(key)
io = open(file, "r+")
# get existing information
DT = Dict{String,Vector{Union{String,Int}}}
prev_registry = filesize(io) > 0 ? JSON.parse(io) : DT()
f(prev_registry, fkey)
# write the updated registry to file
seekstart(io)
JSON.print(io, prev_registry)

Expand All @@ -110,9 +95,11 @@ function deregister(path; registry_file = joinpath(homedir(), ".jlassetregistry.
close(io)
end

key
return key
end

gettarget(path) = normpath(abspath(expanduser(path)))

getkey(path) = baseurl[] * "/assetserver/" * bytes2hex(sha1(abspath(path))) * "-" * basename(path)

isregistered(path) = haskey(registry, getkey(path))
Expand Down