From a40d6ad7cc7caaed317e88127784b21d7a7f8c39 Mon Sep 17 00:00:00 2001 From: Corneliu Cofaru Date: Tue, 11 Sep 2018 18:32:39 +0200 Subject: [PATCH] Minor spacing improvements --- src/diskcache.jl | 68 +++++++++++++++++++-------------------- src/memcache.jl | 83 ++++++++++++++++++++++++------------------------ src/utils.jl | 10 +++--- 3 files changed, 80 insertions(+), 81 deletions(-) diff --git a/src/diskcache.jl b/src/diskcache.jl index 8f5ef36..8eb6538 100644 --- a/src/diskcache.jl +++ b/src/diskcache.jl @@ -1,7 +1,7 @@ # Disk caching mutable struct DiskCache{T<:Function, I<:Unsigned, O} <: AbstractCache - filename::String # file name + filename::String # file name memcache::MemoryCache{T, I, O} # MemoryCache structure (active) offsets::Dict{I, Tuple{Int, Int}} # existing hash - to - file positions end @@ -9,8 +9,8 @@ end # Function that generates a name based on the name of the cached function _generate_cache_filename(fname::String) = begin - _filename = "_" * fname * "_cache_" * string(hash(fname), base=16) * "_.bin" - return abspath(_filename) + _filename = "_" * fname * "_cache_" * string(hash(fname), base=16) * "_.bin" + return abspath(_filename) end @@ -30,10 +30,10 @@ end (dc::T where T<:DiskCache)(args...; kwargs...) = begin _hash = hash([map(hash, args)..., map(hash, collect(kwargs))...]) if _hash in keys(dc.memcache.cache) - out = dc.memcache.cache[_hash] + out = dc.memcache.cache[_hash] else - @info "Hash miss, caching hash=$_hash..." - out = dc.memcache.func(args...; kwargs...) + @info "Hash miss, caching hash=$_hash..." + out = dc.memcache.func(args...; kwargs...) dc.memcache.cache[_hash] = out end return out @@ -54,28 +54,28 @@ end # Macro supporting construnctions of the form: # julia> foo(x) = x+1 # julia> fooc = @diskcache foo # now `fooc` is the cached version of `foo` -macro diskcache(symb::Symbol, - filename::String=_generate_cache_filename(string(symb))) - @assert isdefined(Main, symb) - _name = String(symb) - ex = quote - try - DiskCache($symb, - name=$_name, - filename=$filename) - catch excep - @error "Could not create DiskCache. $excep" - end - end - return esc(ex) +macro diskcache(symb::Symbol, filename::String= + _generate_cache_filename(string(symb))) + @assert isdefined(Main, symb) + _name = String(symb) + ex = quote + try + DiskCache($symb, + name=$_name, + filename=$filename) + catch excep + @error "Could not create DiskCache. $excep" + end + end + return esc(ex) end # macro # Macro supporting constructions of the form: # julia> foo(x) = x+1 # julia> fooc = @diskcache foo::Int # expects output to be `Int` -macro diskcache(expr::Expr, - filename::String=_generate_cache_filename(string(expr.args[1]))) +macro diskcache(expr::Expr, filename::String= + _generate_cache_filename(string(expr.args[1]))) @assert expr.head == :(::) @assert length(expr.args) == 2 # two arguments _symb = expr.args[1] @@ -85,20 +85,20 @@ macro diskcache(expr::Expr, @assert isdefined(Main, _symb) # check that symbol exists try - @assert _type isa Type + @assert _type isa Type catch # it may be a variable containing a type - @error "The right-hand argument of `::` is not a type." + @error "The right-hand argument of `::` is not a type." end ex = quote - try - DiskCache($_symb, - name=$_name, - filename=$filename, - output_type=$_type) - catch excep - @error "Could not create DiskCache. $excep" - end - end - return esc(ex) + try + DiskCache($_symb, + name=$_name, + filename=$filename, + output_type=$_type) + catch excep + @error "Could not create DiskCache. $excep" + end + end + return esc(ex) end # macro diff --git a/src/memcache.jl b/src/memcache.jl index b3e5ccb..3b93474 100644 --- a/src/memcache.jl +++ b/src/memcache.jl @@ -1,9 +1,9 @@ # Memory caching (dictionary based) mutable struct MemoryCache{T<:Function, I<:Unsigned, O} <: AbstractCache - name::String - func::T - cache::Dict{I, O} + name::String + func::T + cache::Dict{I, O} end @@ -17,12 +17,12 @@ MemoryCache(f::T where T<:Function; # Call method (mc::T where T<:MemoryCache)(args...; kwargs...) = begin - _hash = hash([map(hash, args)..., map(hash, collect(kwargs))...]) - if _hash in keys(mc.cache) - out = mc.cache[_hash] - else - @info "Hash miss, caching hash=$_hash..." - out = mc.func(args...; kwargs...) + _hash = hash([map(hash, args)..., map(hash, collect(kwargs))...]) + if _hash in keys(mc.cache) + out = mc.cache[_hash] + else + @info "Hash miss, caching hash=$_hash..." + out = mc.func(args...; kwargs...) mc.cache[_hash] = out end return out @@ -31,8 +31,8 @@ end # Show method show(io::IO, c::MemoryCache) = begin - println(io, "Memory cache for \"$(c.name)\" " * - "with $(length(c.cache)) entries.") + println(io, "Memory cache for \"$(c.name)\" " * + "with $(length(c.cache)) entries.") end @@ -42,16 +42,16 @@ end # julia> foo(x) = x+1 # julia> fooc = @memcache foo # now `fooc` is the cached version of `foo` macro memcache(symb::Symbol) - @assert isdefined(Main, symb) - _name = String(symb) - ex = quote - try - MemoryCache($symb, name=$_name) - catch excep - @error "Could not create MemoryCache. $excep" - end - end - return esc(ex) + @assert isdefined(Main, symb) + _name = String(symb) + ex = quote + try + MemoryCache($symb, name=$_name) + catch excep + @error "Could not create MemoryCache. $excep" + end + end + return esc(ex) end # macro @@ -59,26 +59,25 @@ end # macro # julia> foo(x) = x+1 # julia> fooc = @memcache foo::Int # expects output to be `Int` macro memcache(expr::Expr) - @assert expr.head == :(::) - @assert length(expr.args) == 2 # two arguments - _symb = expr.args[1] - _typesymbol = expr.args[2] - _type = eval(_typesymbol) - _name = String(_symb) - - @assert isdefined(Main, _symb) # check that symbol exists - try - @assert _type isa Type - catch # it may be a variable containing a type - @error "The right-hand argument of `::` is not a type." - end - + @assert expr.head == :(::) + @assert length(expr.args) == 2 # two arguments + _symb = expr.args[1] + _typesymbol = expr.args[2] + _type = eval(_typesymbol) + _name = String(_symb) + + @assert isdefined(Main, _symb) # check that symbol exists + try + @assert _type isa Type + catch # it may be a variable containing a type + @error "The right-hand argument of `::` is not a type." + end ex = quote - try - MemoryCache($_symb, name=$_name, output_type=$_type) - catch excep - @error "Could not create MemoryCache. $excep" - end - end - return esc(ex) + try + MemoryCache($_symb, name=$_name, output_type=$_type) + catch excep + @error "Could not create MemoryCache. $excep" + end + end + return esc(ex) end # macro diff --git a/src/utils.jl b/src/utils.jl index 72134a3..bce8cc1 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -14,8 +14,8 @@ function cachesync!(dc::T) where T<:DiskCache # dump # else (if file/path does exists) # - # create directory - # create file, + # create directory + # create file, end @@ -31,14 +31,14 @@ function persist!(mc::T; filename::String= _generate_cache_filename(mc.name)) where T<:MemoryCache # Initialize structures _data = mc.cache - I, O = typeof(_data).parameters # works for `Dict` + I, O = typeof(_data).parameters # works for `Dict` offsets = Dict{I, Tuple{Int,Int}}() _dir = join(split(filename, "/")[1:end-1], "/") !isempty(_dir) && !isdir(_dir) && mkdir(_dir) # Write header fid = open(filename, "w") # overwrite/create file - serialize(fid, I) - serialize(fid, O) + serialize(fid, I) + serialize(fid, O) # Write data pairs for (hash, datum) in _data prevpos = position(fid)