From f188c0e24291401335b90626aad4ba562948b525 Mon Sep 17 00:00:00 2001 From: whyrusleeping Date: Mon, 20 Jan 2020 17:36:34 -0800 Subject: [PATCH] take advantage of batching when loading car, if available --- car.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/car.go b/car.go index 1ad9c15..515fc0a 100644 --- a/car.go +++ b/car.go @@ -149,12 +149,61 @@ func (cr *carReader) Next() (blocks.Block, error) { return blocks.NewBlockWithCid(data, c) } +type batchStore interface { + PutMany([]blocks.Block) error +} + func LoadCar(s Store, r io.Reader) (*CarHeader, error) { cr, err := NewCarReader(r) if err != nil { return nil, err } + if bs, ok := s.(batchStore); ok { + return loadCarFast(bs, cr) + } + + return loadCarSlow(s, cr) +} + +func loadCarFast(s batchStore, cr *carReader) (*CarHeader, error) { + var buf []blocks.Block + for { + blk, err := cr.Next() + switch err { + case io.EOF: + if len(buf) > 0 { + if err := s.PutMany(buf); err != nil { + return nil, err + } + } + return cr.Header, nil + default: + return nil, err + case nil: + } + + buf = append(buf, blk) + + if len(buf) > 1000 { + if err := s.PutMany(buf); err != nil { + return nil, err + } + buf = buf[:0] + } + } + + if len(buf) > 0 { + if err := s.PutMany(buf); err != nil { + return nil, err + } + } + + return cr.Header, nil +} + +func loadCarSlow(s Store, cr *carReader) (*CarHeader, error) { + for { blk, err := cr.Next() switch err { -- GitLab