Skip to content

Commit

Permalink
Replacing xrootdgo by XRootD.jl (#340)
Browse files Browse the repository at this point in the history
* Replace xrootdgo_jll by XRootD.jl
  • Loading branch information
peremato authored Jun 3, 2024
1 parent cc3ea9b commit a1a17de
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 53 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
StructArrays = "09ab397b-f2b6-538f-b94a-2f83cf4a842a"
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
XRootD = "164e3b87-dc46-4cbc-97a6-5b310108fce0"
XXHashNative = "e5d8e439-e7fa-4681-9c12-1c64bda517be"
xrootdgo_jll = "9d84c17e-11f2-50ef-8cc9-e9701362097f"

[compat]
AbstractTrees = "^0.4"
Expand Down Expand Up @@ -61,9 +61,9 @@ StructArrays = "0.6"
TOML = "^1.0"
Tables = "^1.9"
Test = "^1.0"
XRootD = "^0.1"
XXHashNative = "^1.0.1"
julia = "^1.7"
xrootdgo_jll = "=0.32.1, =0.34.1"

[extras]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
Expand Down
6 changes: 1 addition & 5 deletions src/root.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,7 @@ function ROOTFile(filename::AbstractString; customstructs = Dict("TLorentzVector
fobj = if startswith(filename, r"https?://")
HTTPStream(filename)
elseif startswith(filename, "root://")
length(findall("//", filename)) < 2 && error("The xrootd URL is illegal: missing the '//' separator between the server and the path (e.g. 'root://server:1234//path/to/file.root')")
sep_idx = findall("//", filename)[2]
baseurl = filename[8:first(sep_idx)-1]
filepath = filename[last(sep_idx):end]
XRDStream(baseurl, filepath, "go")
XRDStream(filename)
else
!isfile(filename) && throw(SystemError("opening file $filename", 2))
MmapStream(filename)
Expand Down
42 changes: 16 additions & 26 deletions src/streamsource.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using xrootdgo_jll
using XRootD.XrdCl
import HTTP

mutable struct XRDStream
gofile_id::Cstring # used as key to a global `map` on the Go side
file::File # encapsulates a XRootD.XrdCl!File object
seekloc::Int
size::Int
end
Expand Down Expand Up @@ -130,50 +130,40 @@ function Base.read(fobj::SourceStream)
read(fobj, fobj.size - fobj.seekloc + 1)
end

function XRDStream(urlbase::AbstractString, filepath::AbstractString, username::AbstractString)
file_id = @ccall xrootdgo.Open(urlbase::Cstring, filepath::Cstring, username::Cstring)::Cstring
if unsafe_string(file_id) == "error"
error("xrootd Go library errored.")
end
# file_id = @threadcall((:Open, xrootdgo), Cstring, (Cstring, Cstring, Cstring), urlbase, filepath, username)
size = @ccall xrootdgo.Size(file_id::Cstring)::Int
XRDStream(file_id, 0, size)
function XRDStream(url::AbstractString)
file = File()
st, _ = open(file, url, OpenFlags.Read)
isError(st) && error("XRootD file open error: $st")
st, statinfo = stat(file)
isError(st) && error("XRootD file stat error: $st")
XRDStream(file, 0, statinfo.size)
end

function Base.close(fobj::XRDStream)
xrootdgo.Close(fobj.gofile_id)
close(fobj.file)
end

function read_seek_nb(fobj::XRDStream, seek, nb)
buffer = Vector{UInt8}(undef, nb)
# @threadcall((:ReadAt, xrootdgo), Cvoid, (Ptr{UInt8}, Cstring, Clong, Clong), buffer, fobj.gofile_id, nb, seek)
@ccall xrootdgo.ReadAt(buffer::Ptr{UInt8}, fobj.gofile_id::Cstring, nb::Clong, seek::Clong)::Cvoid
st, buffer = read(fobj.file, nb, seek)
isError(st) && error("XRootD file read error: $st")
return buffer
end
function _read!(ptr, fobj, nb, seekloc)
@ccall xrootdgo.ReadAt(ptr::Ptr{UInt8},
fobj.gofile_id::Cstring, nb::Clong, seekloc::Clong)::Cvoid
end

function _read!(ptr, fobj, nb)
_read!(ptr, fobj, nb, fobj.seekloc)
end

function Base.read(fobj::XRDStream, ::Type{T}) where T
@debug @show T, sizeof(T)
nb = sizeof(T)
output = Ref{T}()
tko = Base.@_gc_preserve_begin output
po = Ptr{UInt8}(pointer_from_objref(output))
_read!(po, fobj, nb, fobj.seekloc)
po = pointer_from_objref(output)
unsafe_read(fobj.file, po, nb, fobj.seekloc)
Base.@_gc_preserve_end tko
fobj.seekloc += nb
return output[]
end

function Base.read(fobj::XRDStream, nb::Integer)
buffer = Vector{UInt8}(undef, nb)
GC.@preserve buffer _read!(buffer, fobj, nb, fobj.seekloc)
st, buffer = read(fobj.file, nb, fobj.seekloc)
isError(st) && error("XRootD file read error: $st")
fobj.seekloc += nb
return buffer
end
42 changes: 22 additions & 20 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,29 @@ using UnROOT
nthreads = UnROOT._maxthreadid()
nthreads == 1 && @warn "Running on a single thread. Please re-run the test suite with at least two threads (`julia --threads 2 ...`)"

include("Aqua.jl")
include("bootstrapping.jl")
include("compressions.jl")
include("jagged.jl")
include("lazy.jl")
include("histograms.jl")
include("views.jl")
include("multithreading.jl")
include("remote.jl")
include("displays.jl")
include("type_stability.jl")
include("utils.jl")
include("misc.jl")
@testset "UnROOT tests" verbose = true begin
include("Aqua.jl")
include("bootstrapping.jl")
include("compressions.jl")
include("jagged.jl")
include("lazy.jl")
include("histograms.jl")
include("views.jl")
include("multithreading.jl")
include("remote.jl")
include("displays.jl")
include("type_stability.jl")
include("utils.jl")
include("misc.jl")

include("type_support.jl")
include("custom_bootstrapping.jl")
include("lorentzvectors.jl")
include("NanoAOD.jl")
include("type_support.jl")
include("custom_bootstrapping.jl")
include("lorentzvectors.jl")
include("NanoAOD.jl")

include("issues.jl")
include("issues.jl")

if VERSION >= v"1.9"
include("rntuple.jl")
if VERSION >= v"1.9"
include("rntuple.jl")
end
end

0 comments on commit a1a17de

Please sign in to comment.