Skip to content

Commit

Permalink
JLD.delete! removes JldDataset + stuff in _refs
Browse files Browse the repository at this point in the history
  • Loading branch information
ggggggggg committed Aug 4, 2014
1 parent 318554d commit 4d663c7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
9 changes: 9 additions & 0 deletions doc/jld.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ close(file)
This creates a dataset named `"A"` containing the contents of the variable `A`.
There are also the convenient `save("mydata.jld", "A", A)` or `@save "mydata.jld" A` [syntaxes](../README.md).

You may also use an array like syntax
```julia
file = jldopen("mydata.jld", "w")
file["a"] = [1:100]
b = file["a"][20:30]
close(file)
```
Use the `delete!` function to delete `JldDataset`s and their associated references. Directly deleting a `JldDataset` with `o_delete` will leave behind unwanted objects that may cause future errors, especially if you reuse the same path in the JLD file.

JLD files can be opened with the `mmaparrays` option, which if true returns "qualified" array data sets as arrays using [memory-mapping](hdf5.md#memory-mapping):

```julia
Expand Down
8 changes: 7 additions & 1 deletion src/jld.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ using HDF5
# Add methods to...
import HDF5: close, dump, exists, file, getindex, setindex!, g_create, g_open, o_delete, name, names, read, size, write,
HDF5ReferenceObj, HDF5BitsKind, ismmappable, readmmap
import Base: length, endof, show, done, next, start
import Base: length, endof, show, done, next, start, delete!

if !isdefined(:setfield!)
const setfield! = setfield
Expand Down Expand Up @@ -197,6 +197,12 @@ name(p::Union(JldFile, JldGroup, JldDataset)) = name(p.plain)
exists(p::Union(JldFile, JldGroup, JldDataset), path::ByteString) = exists(p.plain, path)
root(p::Union(JldFile, JldGroup, JldDataset)) = g_open(file(p), "/")
o_delete(parent::Union(JldFile, JldGroup), args...) = o_delete(parent.plain, args...)
function delete!(g::Union(JldGroup, JldFile), name)
beginswith(name,'_') && error("$name is internal to the JLD format, use o_delete if you really want to delete it")
o_delete(g, name)
o_delete(g, joinpath("_refs", name))
end
delete!(parent::Union(JldFile, JldGroup), args...) = for a in args delete!(parent,a) end
ismmappable(obj::JldDataset) = ismmappable(obj.plain)
readmmap(obj::JldDataset, args...) = readmmap(obj.plain, args...)
setindex!(parent::Union(JldFile, JldGroup), val, path::ASCIIString) = write(parent, path, val)
Expand Down
12 changes: 11 additions & 1 deletion test/jld.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using HDF5
using JLD
using Base.Test

# Define variables of different types
x = 3.7
Expand Down Expand Up @@ -296,14 +297,23 @@ save(fn, "i106", Mod106.typ(1, Mod106.UnexportedT))
i106 = load(fn, "i106")
@assert i106 == Mod106.typ(1, Mod106.UnexportedT)

# bracket synax for datasets
# bracket synax for datasets + delete!
jldopen(fn, "w") do file
file["a"] = [1:100]
file["b"] = [x*y for x=1:10,y=1:10]
file["ms"] = ms
delete!(file, "ms")
file["ms"] = β
g_create(file,"g")
file["g/ms"] = ms
@test_throws ErrorException delete!(file, "_refs/g/ms")
delete!(file, "g/ms")
end
jldopen(fn, "r") do file
@assert(file["a"][1:50] == [1:50])
@assert(file["b"][5,6][1]==5*6)
@assert(read(file["ms"]) == β)
@assert(!exists(file, "g/ms"))
end

# bracket syntax when created by HDF5
Expand Down

0 comments on commit 4d663c7

Please sign in to comment.