feat: decode brotli-compressed files
Alan Pearce alan@alanpearce.eu
Fri, 17 May 2024 10:51:42 +0200
7 files changed, 65 insertions(+), 16 deletions(-)
M go.sum → go.sum
@@ -1,5 +1,7 @@ github.com/RoaringBitmap/roaring v1.9.3 h1:t4EbC5qQwnisr5PrP9nt0IRhRTb9gMUgQF4t4S2OByM= github.com/RoaringBitmap/roaring v1.9.3/go.mod h1:6AXUsoIEzDTFFQCe1RbGA6uFONMhvejWj5rqITANK90= +github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M= +github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY= github.com/bcicen/jstream v1.0.1 h1:BXY7Cu4rdmc0rhyTVyT3UkxAiX3bnLpKLas9btbH5ck= github.com/bcicen/jstream v1.0.1/go.mod h1:9ielPxqFry7Y4Tg3j4BfjPocfJ3TbsRtXOAYXYmRuAQ= github.com/bits-and-blooms/bitset v1.12.0/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8=
M gomod2nix.toml → gomod2nix.toml
@@ -4,6 +4,9 @@ [mod] [mod."github.com/RoaringBitmap/roaring"] version = "v1.9.3" hash = "sha256-LZfRufkU4UhuEcgxuCPd6divX2KIdcHp1FOt79mQV7Q=" + [mod."github.com/andybalholm/brotli"] + version = "v1.1.0" + hash = "sha256-njLViV4v++ZdgOWGWzlvkefuFvA/nkugl3Ta/h1nu/0=" [mod."github.com/bcicen/jstream"] version = "v1.0.1" hash = "sha256-mm+/BuIEYYj6XOHCCJLxVMKd1XcBXCiRCWA+aTvr1sE="
M internal/importer/main.go → internal/importer/main.go
@@ -2,6 +2,7 @@ package importer import ( "context" + "io" "log/slog" "os/exec" "path" @@ -59,6 +60,7 @@ if err != nil { logger.Warn("could not set source repo revision", "error", err) } + var file io.ReadCloser var processor Processor switch source.Importer { case config.Options: @@ -69,7 +71,11 @@ files.Options, "revision", source.Repo.Revision, ) - processor, err = NewOptionProcessor(files.Options, source) + file, err = openFileDecoded(files.Options) + if err != nil { + logger.Warn("could not open file", "filename", files.Options, "error", err) + } + processor, err = NewOptionProcessor(file, source) case config.Packages: logger.Debug( "creating processor", @@ -78,7 +84,11 @@ files.Packages, "revision", source.Repo.Revision, ) - processor, err = NewPackageProcessor(files.Packages, source) + file, err = openFileDecoded(files.Packages) + if err != nil { + logger.Warn("could not open file", "filename", files.Packages, "error", err) + } + processor, err = NewPackageProcessor(file, source) } if err != nil { logger.Warn("failed to create processor", "type", source.Importer, "error", err)
M internal/importer/options.go → internal/importer/options.go
@@ -2,8 +2,8 @@ package importer import ( "context" + "io" "log/slog" - "os" "reflect" "searchix/internal/config" "searchix/internal/nix" @@ -58,15 +58,11 @@ type OptionIngester struct { dec *jstream.Decoder ms *mapstructure.Decoder optJSON nixOptionJSON - infile *os.File + infile io.ReadCloser source *config.Source } -func NewOptionProcessor(inpath string, source *config.Source) (*OptionIngester, error) { - infile, err := os.Open(inpath) - if err != nil { - return nil, errors.WithMessagef(err, "failed to open input file %s", inpath) - } +func NewOptionProcessor(infile io.ReadCloser, source *config.Source) (*OptionIngester, error) { i := OptionIngester{ dec: jstream.NewDecoder(infile, 1).EmitKV(), optJSON: nixOptionJSON{},
M internal/importer/package.go → internal/importer/package.go
@@ -3,8 +3,8 @@ import ( "context" "encoding/json" + "io" "log/slog" - "os" "reflect" "searchix/internal/config" "searchix/internal/nix" @@ -41,7 +41,7 @@ type PackageIngester struct { dec *jstream.Decoder ms *mapstructure.Decoder pkg *packageJSON - infile *os.File + infile io.ReadCloser source *config.Source } @@ -60,11 +60,7 @@ return string(s) } -func NewPackageProcessor(inpath string, source *config.Source) (*PackageIngester, error) { - infile, err := os.Open(inpath) - if err != nil { - return nil, errors.WithMessagef(err, "failed to open input file %s", inpath) - } +func NewPackageProcessor(infile io.ReadCloser, source *config.Source) (*PackageIngester, error) { i := &PackageIngester{ dec: jstream.NewDecoder(infile, 2).EmitKV(), pkg: &packageJSON{},
M internal/importer/utils.go → internal/importer/utils.go
@@ -3,11 +3,14 @@ import ( "bytes" "fmt" + "io" "net/url" "os" + "path" "searchix/internal/config" "searchix/internal/nix" + "github.com/andybalholm/brotli" "github.com/bcicen/jstream" "github.com/pkg/errors" ) @@ -80,3 +83,41 @@ } return nil } + +type brotliReadCloser struct { + src io.ReadCloser + *brotli.Reader +} + +func newBrotliReader(src io.ReadCloser) *brotliReadCloser { + return &brotliReadCloser{ + src: src, + Reader: brotli.NewReader(src), + } +} + +func (r *brotliReadCloser) Close() error { + return errors.Wrap(r.src.Close(), "failed to call close on underlying reader") +} + +func openFileDecoded(filename string) (io.ReadCloser, error) { + var reader io.ReadCloser + var err error + ext := path.Ext(filename) + reader, err = os.Open(filename) + if err != nil { + return nil, errors.WithMessagef(err, "failed to open file %s", filename) + } + switch ext { + case ".json": + // nothing to do + case ".br": + reader = newBrotliReader(reader) + default: + reader.Close() + + return nil, errors.Errorf("invalid file extension %s", ext) + } + + return reader, nil +}