Skip to content

Commit

Permalink
groot/riofs: drop mmap-ing ROOT files by default, add file+mmap:// co…
Browse files Browse the repository at this point in the history
…nvenience protocol

Fixes #1019.

Signed-off-by: Sebastien Binet <[email protected]>
  • Loading branch information
sbinet committed Nov 15, 2024
1 parent 4130003 commit 5389a1f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
51 changes: 51 additions & 0 deletions groot/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,54 @@ func ExampleOpen_graph() {
// (x,y)[2] = (+3.000000e+00, +6.000000e+00)
// (x,y)[3] = (+4.000000e+00, +8.000000e+00)
}

// ExampleOpen_file shows how users can open a local ROOT file with the 'file://' protocol.
func ExampleOpen_file() {
f, err := groot.Open("file://./testdata/simple.root")
if err != nil {
log.Fatal(err)
}
defer f.Close()

for _, key := range f.Keys() {
fmt.Printf("key: %q cycle=%d title=%q\n", key.Name(), key.Cycle(), key.Title())
}

obj, err := f.Get("tree")
if err != nil {
log.Fatal(err)
}

tree := obj.(rtree.Tree)
fmt.Printf("tree: %q, entries=%d\n", tree.Name(), tree.Entries())

// Output:
// key: "tree" cycle=1 title="fake data"
// tree: "tree", entries=4
}

// ExampleOpen_mmap shows how users can open and mmap a local ROOT file.
// mmap-ing a file may be useful for performances reasons.
func ExampleOpen_mmap() {
f, err := groot.Open("file+mmap://./testdata/simple.root")
if err != nil {
log.Fatal(err)
}
defer f.Close()

for _, key := range f.Keys() {
fmt.Printf("key: %q cycle=%d title=%q\n", key.Name(), key.Cycle(), key.Title())
}

obj, err := f.Get("tree")
if err != nil {
log.Fatal(err)
}

tree := obj.(rtree.Tree)
fmt.Printf("tree: %q, entries=%d\n", tree.Name(), tree.Entries())

// Output:
// key: "tree" cycle=1 title="fake data"
// tree: "tree", entries=4
}
7 changes: 7 additions & 0 deletions groot/riofs/fileplugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package riofs
import (
"fmt"
"net/url"
"os"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -70,9 +71,15 @@ func openFile(path string) (Reader, error) {

func openLocalFile(path string) (Reader, error) {
path = strings.TrimPrefix(path, "file://")
return os.Open(path)
}

func mmapLocalFile(path string) (Reader, error) {
path = strings.TrimPrefix(path, "file+mmap://")
return mmap.Open(path)
}

func init() {
Register("file", openLocalFile)
Register("file+mmap", mmapLocalFile)
}

0 comments on commit 5389a1f

Please sign in to comment.