go-hep.org/x/hep@v0.38.1/groot/rtree/copy.go (about) 1 // Copyright ©2019 The go-hep Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package rtree 6 7 import ( 8 "fmt" 9 ) 10 11 // Copy copies from src to dst until either the reader is depleted or 12 // an error occurs. It returns the number of bytes copied and the first error 13 // encountered while copying, if any. 14 func Copy(dst Writer, src *Reader) (int64, error) { 15 // FIXME(sbinet): optimize for the case(s) where: 16 // - all branches are being copied 17 // - compression is the same 18 // => it might not be needed to uncompress-recompress the baskets. 19 20 var ( 21 tot int64 22 err error 23 ) 24 25 wvars := dst.(*wtree).wvars 26 rvars := make([]ReadVar, len(wvars)) 27 for i, wvar := range wvars { 28 rvars[i] = ReadVar{ 29 Name: wvar.Name, 30 Value: wvar.Value, 31 } 32 } 33 34 orig := src.rvars 35 defer func() { 36 src.rvars = orig 37 src.dirty = true 38 }() 39 src.rvars = rvars 40 src.dirty = true 41 42 err = src.Read(func(ctx RCtx) error { 43 written, err := dst.Write() 44 if err != nil { 45 return fmt.Errorf("rtree: could not write entry %d to tree: %w", ctx.Entry, err) 46 } 47 tot += int64(written) 48 return nil 49 }) 50 if err != nil { 51 return tot, fmt.Errorf("rtree: could not read through tree: %w", err) 52 } 53 54 return tot, err 55 }