From 0ae14d5313bf181c73a6e5eb9a03b30828c0c192 Mon Sep 17 00:00:00 2001 From: Alexander Petrov Date: Tue, 25 Feb 2020 11:37:03 +0000 Subject: [PATCH] Issue37 Wrap errors by calling fmt.Errorf and apply the %w verb to the error argument * go1.13 is required * Removed download target --- .gitignore | 1 - .travis.yml | 5 ++--- Makefile | 10 +++------- cmd/language-model/cmd/build-lm.go | 4 ++-- cmd/language-model/cmd/eval.go | 4 ++-- cmd/language-model/cmd/ngram-count.go | 8 ++++---- cmd/spellchecker/cmd/eval.go | 2 +- cmd/suggest/cmd/eval.go | 2 +- cmd/suggest/cmd/indexer.go | 18 +++++++++--------- go.mod | 2 -- go.sum | 7 ------- internal/spellchecker/api/app.go | 17 +++++++++-------- internal/spellchecker/dep/spellchecker.go | 10 +++++----- internal/suggest/api/app.go | 6 +++--- pkg/dictionary/cdb_dictionary.go | 2 +- pkg/dictionary/helpers.go | 16 ++++++++-------- pkg/index/bitmap_posting_list.go | 2 +- pkg/index/index_reader.go | 10 +++++----- pkg/index/indexer_writer.go | 10 +++++----- pkg/index/searcher.go | 6 +++--- pkg/lm/binary.go | 22 +++++++++++----------- pkg/lm/indexer.go | 4 ++-- pkg/lm/language_model.go | 4 ++-- pkg/lm/ngram_reader.go | 10 +++++----- pkg/lm/ngram_vector_builder.go | 2 +- pkg/lm/ngram_writer.go | 6 +++--- pkg/mph/mph.go | 22 +++++++++++----------- pkg/store/fs_directory.go | 8 ++++---- pkg/store/ram_directory.go | 2 +- pkg/suggest/autocomplete.go | 4 ++-- pkg/suggest/indexer.go | 2 +- pkg/suggest/ngram_index_builder.go | 6 +++--- pkg/suggest/service.go | 10 +++++----- pkg/suggest/suggester.go | 2 +- 34 files changed, 116 insertions(+), 130 deletions(-) diff --git a/.gitignore b/.gitignore index b9c6eaa..a007fea 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ -vendor/* build/* diff --git a/.travis.yml b/.travis.yml index d2144a2..b23b742 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,12 +1,11 @@ language: go go: - - "1.12.x" + - "1.13.x" script: - - env GO111MODULE=on make download - - env GO111MODULE=on make test - make vet + - make test notifications: email: diff --git a/Makefile b/Makefile index 0c654ce..5820672 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ .PHONY: build test vet clean -BUILD_FLAGS = -mod=vendor $(GO_BUILD_FLAGS) +BUILD_FLAGS = $(GO_BUILD_FLAGS) default: build @@ -15,8 +15,8 @@ build-lm: build-spellchecker: go build $(BUILD_FLAGS) -o build/spellchecker ./cmd/spellchecker/ -build: download test vet build-suggest build-lm build-spellchecker -build-bin: download build-suggest build-lm +build: test vet build-suggest build-lm build-spellchecker +build-bin: build-suggest build-lm build-docker: docker build --no-cache -t suggest:latest . @@ -24,10 +24,6 @@ build-docker: test: go test -race -v ./... -download: - go mod download - go mod vendor - vet: go vet ./... diff --git a/cmd/language-model/cmd/build-lm.go b/cmd/language-model/cmd/build-lm.go index d1d1e4f..d74a1b0 100644 --- a/cmd/language-model/cmd/build-lm.go +++ b/cmd/language-model/cmd/build-lm.go @@ -19,13 +19,13 @@ var buildLMCmd = &cobra.Command{ config, err := lm.ReadConfig(configPath) if err != nil { - return fmt.Errorf("couldn't read a config %v", err) + return fmt.Errorf("couldn't read a config %w", err) } directory, err := store.NewFSDirectory(config.GetOutputPath()) if err != nil { - return fmt.Errorf("failed to create a fs directory: %v", err) + return fmt.Errorf("failed to create a fs directory: %w", err) } return lm.StoreBinaryLMFromGoogleFormat(directory, config) diff --git a/cmd/language-model/cmd/eval.go b/cmd/language-model/cmd/eval.go index 3a4fb19..49edb82 100644 --- a/cmd/language-model/cmd/eval.go +++ b/cmd/language-model/cmd/eval.go @@ -23,13 +23,13 @@ var evalCmd = &cobra.Command{ config, err := lm.ReadConfig(configPath) if err != nil { - return fmt.Errorf("failed to read config file: %v", err) + return fmt.Errorf("failed to read config file: %w", err) } directory, err := store.NewFSDirectory(config.GetOutputPath()) if err != nil { - return fmt.Errorf("failed to create a fs directory: %v", err) + return fmt.Errorf("failed to create a fs directory: %w", err) } languageModel, err := lm.RetrieveLMFromBinary(directory, config) diff --git a/cmd/language-model/cmd/ngram-count.go b/cmd/language-model/cmd/ngram-count.go index b3353d2..d9fbb9f 100644 --- a/cmd/language-model/cmd/ngram-count.go +++ b/cmd/language-model/cmd/ngram-count.go @@ -22,7 +22,7 @@ var countNGramsCmd = &cobra.Command{ config, err := lm.ReadConfig(configPath) if err != nil { - return fmt.Errorf("could read config %s", err) + return fmt.Errorf("could read config %w", err) } trie, err := buildNGramsCount(config) @@ -40,7 +40,7 @@ func buildNGramsCount(config *lm.Config) (lm.CountTrie, error) { sourceFile, err := os.Open(config.GetSourcePath()) if err != nil { - return nil, fmt.Errorf("could read source file %s", err) + return nil, fmt.Errorf("could read source file %w", err) } defer sourceFile.Close() @@ -64,13 +64,13 @@ func storeNGramsCount(config *lm.Config, trie lm.CountTrie) error { directory, err := store.NewFSDirectory(config.GetOutputPath()) if err != nil { - return fmt.Errorf("failed to create a fs directory: %v", err) + return fmt.Errorf("failed to create a fs directory: %w", err) } writer := lm.NewGoogleNGramWriter(config.NGramOrder, directory) if err := writer.Write(trie); err != nil { - return fmt.Errorf("could save ngrams %s", err) + return fmt.Errorf("could save ngrams %w", err) } return nil diff --git a/cmd/spellchecker/cmd/eval.go b/cmd/spellchecker/cmd/eval.go index 4333989..4e841f9 100644 --- a/cmd/spellchecker/cmd/eval.go +++ b/cmd/spellchecker/cmd/eval.go @@ -39,7 +39,7 @@ var evalCmd = &cobra.Command{ config, err := lm.ReadConfig(configPath) if err != nil { - return fmt.Errorf("failed to read config file: %v", err) + return fmt.Errorf("failed to read config file: %w", err) } service, err := dep.BuildSpellChecker(config, indexDescription) diff --git a/cmd/suggest/cmd/eval.go b/cmd/suggest/cmd/eval.go index a528bd2..9fa9498 100644 --- a/cmd/suggest/cmd/eval.go +++ b/cmd/suggest/cmd/eval.go @@ -82,7 +82,7 @@ func configureService() (*suggest.Service, error) { description, err := suggest.ReadConfigs(configPath) if err != nil { - return nil, fmt.Errorf("Failed to read configs: %v", err) + return nil, fmt.Errorf("Failed to read configs: %w", err) } suggestService := suggest.NewService() diff --git a/cmd/suggest/cmd/indexer.go b/cmd/suggest/cmd/indexer.go index c235761..b29be7b 100644 --- a/cmd/suggest/cmd/indexer.go +++ b/cmd/suggest/cmd/indexer.go @@ -89,7 +89,7 @@ func readConfigs() ([]suggest.IndexDescription, error) { configs, err := suggest.ReadConfigs(configPath) if err != nil { - return nil, fmt.Errorf("invalid config file format %s", err) + return nil, fmt.Errorf("invalid config file format %w", err) } return configs, nil @@ -111,7 +111,7 @@ func indexJob(description suggest.IndexDescription) error { dict, err := buildDictionaryJob(description) if err != nil { - return fmt.Errorf("failed to build a dictionary: %v", err) + return fmt.Errorf("failed to build a dictionary: %w", err) } log.Printf("Time spent %s", time.Since(start)) @@ -123,7 +123,7 @@ func indexJob(description suggest.IndexDescription) error { directory, err := store.NewFSDirectory(description.GetIndexPath()) if err != nil { - return fmt.Errorf("failed to create a directory: %v", err) + return fmt.Errorf("failed to create a directory: %w", err) } if err = suggest.Index(directory, dict, description.GetWriterConfig(), description.GetIndexTokenizer()); err != nil { @@ -179,7 +179,7 @@ func newDictionaryReader(config suggest.IndexDescription) (dictionary.Iterable, f, err := os.Open(config.GetSourcePath()) if err != nil { - return nil, fmt.Errorf("could not open a source file %s", err) + return nil, fmt.Errorf("could not open a source file %w", err) } scanner := bufio.NewScanner(f) @@ -194,17 +194,17 @@ func tryToSendReindexSignal() error { d, err := ioutil.ReadFile(pidPath) if err != nil { - return fmt.Errorf("error parsing pid from %s: %s", pidPath, err) + return fmt.Errorf("error parsing pid from %s: %w", pidPath, err) } pid, err := strconv.Atoi(string(bytes.TrimSpace(d))) if err != nil { - return fmt.Errorf("error parsing pid from %s: %s", pidPath, err) + return fmt.Errorf("error parsing pid from %s: %w", pidPath, err) } if err := syscall.Kill(pid, syscall.SIGHUP); err != nil { - return fmt.Errorf("fail to send reindex signal to %d, %s", pid, err) + return fmt.Errorf("fail to send reindex signal to %d, %w", pid, err) } return nil @@ -215,14 +215,14 @@ func tryToSendReindexRequest() error { resp, err := http.Post(host, "text/plain", nil) if err != nil { - return fmt.Errorf("fail to send reindex request to %s, %s", host, err) + return fmt.Errorf("fail to send reindex request to %s, %w", host, err) } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { - return fmt.Errorf("fail to read response body %s", err) + return fmt.Errorf("fail to read response body %w", err) } if string(body) != "OK" { diff --git a/go.mod b/go.mod index 5053d95..5f8f807 100644 --- a/go.mod +++ b/go.mod @@ -6,14 +6,12 @@ require ( github.com/RoaringBitmap/roaring v0.4.20 github.com/alldroll/cdb v1.0.2 github.com/alldroll/go-datastructures v0.0.0-20190322060030-1d3a19ff3b29 - github.com/djimenez/iconv-go v0.0.0-20160305225143-8960e66bd3da github.com/edsrzf/mmap-go v0.0.0-20190108065903-904c4ced31cd github.com/gorilla/handlers v1.4.0 github.com/gorilla/mux v1.7.1 github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/spf13/cobra v0.0.3 github.com/spf13/pflag v1.0.3 // indirect - golang.org/x/net v0.0.0-20191119073136-fc4aabc6c914 golang.org/x/sync v0.0.0-20190423024810-112230192c58 golang.org/x/sys v0.0.0-20190426135247-a129542de9ae // indirect ) diff --git a/go.sum b/go.sum index 249407b..4a070e8 100644 --- a/go.sum +++ b/go.sum @@ -6,8 +6,6 @@ github.com/alldroll/go-datastructures v0.0.0-20190322060030-1d3a19ff3b29 h1:gKZg github.com/alldroll/go-datastructures v0.0.0-20190322060030-1d3a19ff3b29/go.mod h1:3IP5cUVnXIyZIsUbKXztqoz1brdOXngjFyYtmwqz9MY= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/djimenez/iconv-go v0.0.0-20160305225143-8960e66bd3da h1:0qwwqQCLOOXPl58ljnq3sTJR7yRuMolM02vjxDh4ZVE= -github.com/djimenez/iconv-go v0.0.0-20160305225143-8960e66bd3da/go.mod h1:ns+zIWBBchgfRdxNgIJWn2x6U95LQchxeqiN5Cgdgts= github.com/edsrzf/mmap-go v0.0.0-20190108065903-904c4ced31cd h1:v8VTjPes659sdlQ3O2AbICsk2XjORhYc76QLCFSTEgA= github.com/edsrzf/mmap-go v0.0.0-20190108065903-904c4ced31cd/go.mod h1:W3m91qexYIu40kcj8TLXNUSTCKprH8UQ3GgH5/Xyfc0= github.com/glycerine/go-unsnap-stream v0.0.0-20181221182339-f9677308dec2 h1:Ujru1hufTHVb++eG6OuNDKMxZnGIvF6o/u8q/8h2+I4= @@ -47,13 +45,8 @@ github.com/tinylib/msgp v1.1.0 h1:9fQd+ICuRIu/ue4vxJZu6/LzxN0HwMds2nq/0cFvxHU= github.com/tinylib/msgp v1.1.0/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE= github.com/willf/bitset v1.1.10 h1:NotGKqX0KwQ72NUzqrjZq5ipPNDQex9lo3WpaS8L2sc= github.com/willf/bitset v1.1.10/go.mod h1:RjeCKbqT1RxIR/KWY6phxZiaY1IyutSBfGjNPySAYV4= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/net v0.0.0-20191119073136-fc4aabc6c914 h1:MlY3mEfbnWGmUi4rtHOtNnnnN4UJRGSyLPx+DXA5Sq4= -golang.org/x/net v0.0.0-20191119073136-fc4aabc6c914/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20181221143128-b4a75ba826a6/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190426135247-a129542de9ae h1:mQLHiymj/JXKnnjc62tb7nD5pZLs940/sXJu+Xp3DBA= golang.org/x/sys v0.0.0-20190426135247-a129542de9ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/internal/spellchecker/api/app.go b/internal/spellchecker/api/app.go index d57ba58..3175ff7 100644 --- a/internal/spellchecker/api/app.go +++ b/internal/spellchecker/api/app.go @@ -3,16 +3,17 @@ package api import ( "context" "fmt" + "log" + "os" + "os/signal" + "syscall" + "github.com/gorilla/handlers" "github.com/gorilla/mux" "github.com/suggest-go/suggest/internal/http" "github.com/suggest-go/suggest/internal/spellchecker/dep" "github.com/suggest-go/suggest/pkg/lm" "github.com/suggest-go/suggest/pkg/suggest" - "log" - "os" - "os/signal" - "syscall" ) // App is our application @@ -22,9 +23,9 @@ type App struct { // AppConfig is an application config type AppConfig struct { - Port string - ConfigPath string - PidPath string + Port string + ConfigPath string + PidPath string IndexDescription suggest.IndexDescription } @@ -41,7 +42,7 @@ func (a App) Run() error { config, err := lm.ReadConfig(a.config.ConfigPath) if err != nil { - return fmt.Errorf("failed to read config file: %v", err) + return fmt.Errorf("failed to read config file: %w", err) } spellchecker, err := dep.BuildSpellChecker(config, a.config.IndexDescription) diff --git a/internal/spellchecker/dep/spellchecker.go b/internal/spellchecker/dep/spellchecker.go index 9294ad1..f226f14 100644 --- a/internal/spellchecker/dep/spellchecker.go +++ b/internal/spellchecker/dep/spellchecker.go @@ -14,32 +14,32 @@ func BuildSpellChecker(config *lm.Config, indexDescription suggest.IndexDescript directory, err := store.NewFSDirectory(config.GetOutputPath()) if err != nil { - return nil, fmt.Errorf("failed to create a fs directory: %v", err) + return nil, fmt.Errorf("failed to create a fs directory: %w", err) } languageModel, err := lm.RetrieveLMFromBinary(directory, config) if err != nil { - return nil, fmt.Errorf("failed to retrieve a lm model from binary format: %v", err) + return nil, fmt.Errorf("failed to retrieve a lm model from binary format: %w", err) } dict, err := dictionary.OpenCDBDictionary(config.GetDictionaryPath()) if err != nil { - return nil, fmt.Errorf("failed to open a cdb dictionary: %v", err) + return nil, fmt.Errorf("failed to open a cdb dictionary: %w", err) } // create runtime search index builder builder, err := suggest.NewRAMBuilder(dict, indexDescription) if err != nil { - return nil, fmt.Errorf("failed to create a ngram index: %v", err) + return nil, fmt.Errorf("failed to create a ngram index: %w", err) } index, err := builder.Build() if err != nil { - return nil, fmt.Errorf("failed to build a ngram index: %v", err) + return nil, fmt.Errorf("failed to build a ngram index: %w", err) } return spellchecker.New( diff --git a/internal/suggest/api/app.go b/internal/suggest/api/app.go index a4a2f57..cd17ed4 100644 --- a/internal/suggest/api/app.go +++ b/internal/suggest/api/app.go @@ -49,7 +49,7 @@ func (a App) Run() error { } if err := reindexJob(); err != nil { - return fmt.Errorf("Fail to configure service: %s", err) + return fmt.Errorf("Fail to configure service: %w", err) } ctx, cancelFn := context.WithCancel(context.Background()) @@ -93,13 +93,13 @@ func (a App) writePIDFile() error { err := os.MkdirAll(filepath.Dir(a.config.PidPath), 0700) if err != nil { - return fmt.Errorf("There is no such file for %s, %s", a.config.PidPath, err) + return fmt.Errorf("There is no such file for %s, %w", a.config.PidPath, err) } // Retrieve the PID and write it. pid := strconv.Itoa(os.Getpid()) if err := ioutil.WriteFile(a.config.PidPath, []byte(pid), 0644); err != nil { - return fmt.Errorf("Fail to write pid file to %s, %s", a.config.PidPath, err) + return fmt.Errorf("Fail to write pid file to %s, %w", a.config.PidPath, err) } return nil diff --git a/pkg/dictionary/cdb_dictionary.go b/pkg/dictionary/cdb_dictionary.go index 9ac04a1..eda8055 100644 --- a/pkg/dictionary/cdb_dictionary.go +++ b/pkg/dictionary/cdb_dictionary.go @@ -19,7 +19,7 @@ func NewCDBDictionary(r io.ReaderAt) (Dictionary, error) { reader, err := handle.GetReader(r) if err != nil { - return nil, fmt.Errorf("fail to create cdb dictionary: %v", err) + return nil, fmt.Errorf("fail to create cdb dictionary: %w", err) } return &cdbDictionary{ diff --git a/pkg/dictionary/helpers.go b/pkg/dictionary/helpers.go index 8f81dd0..203038b 100644 --- a/pkg/dictionary/helpers.go +++ b/pkg/dictionary/helpers.go @@ -15,7 +15,7 @@ func OpenCDBDictionary(path string) (Dictionary, error) { dictionaryFile, err := utils.NewMMapReader(path) if err != nil { - return nil, fmt.Errorf("failed to open cdb dictionary file: %v", err) + return nil, fmt.Errorf("failed to open cdb dictionary file: %w", err) } return NewCDBDictionary(dictionaryFile) @@ -26,7 +26,7 @@ func OpenRAMDictionary(path string) (dict Dictionary, err error) { dictionaryFile, err := os.Open(path) if err != nil { - return nil, fmt.Errorf("failed to open dictionary file: %v", err) + return nil, fmt.Errorf("failed to open dictionary file: %w", err) } defer func() { @@ -57,14 +57,14 @@ func BuildCDBDictionary(iterator Iterable, destinationPath string) (Dictionary, ) if err != nil { - return nil, fmt.Errorf("failed to create dictionary file %v", err) + return nil, fmt.Errorf("failed to create dictionary file %w", err) } cdbHandle := cdb.New() cdbWriter, err := cdbHandle.GetWriter(destinationFile) if err != nil { - return nil, fmt.Errorf("failed to create cdb writer %v", err) + return nil, fmt.Errorf("failed to create cdb writer %w", err) } key := make([]byte, 4) @@ -73,22 +73,22 @@ func BuildCDBDictionary(iterator Iterable, destinationPath string) (Dictionary, binary.LittleEndian.PutUint32(key, docID) if err := cdbWriter.Put(key, []byte(word)); err != nil { - return fmt.Errorf("failed to put record to cdb: %v", err) + return fmt.Errorf("failed to put record to cdb: %w", err) } return nil }) if err != nil { - return nil, fmt.Errorf("failed to iterate through a dictionary: %v", err) + return nil, fmt.Errorf("failed to iterate through a dictionary: %w", err) } if err := cdbWriter.Close(); err != nil { - return nil, fmt.Errorf("failed to save cdb dictionary %v", err) + return nil, fmt.Errorf("failed to save cdb dictionary %w", err) } if err := destinationFile.Close(); err != nil { - return nil, fmt.Errorf("failed to close cdb file %v", err) + return nil, fmt.Errorf("failed to close cdb file %w", err) } return OpenCDBDictionary(destinationPath) diff --git a/pkg/index/bitmap_posting_list.go b/pkg/index/bitmap_posting_list.go index 26c1562..eafb738 100644 --- a/pkg/index/bitmap_posting_list.go +++ b/pkg/index/bitmap_posting_list.go @@ -87,7 +87,7 @@ func (i *bitmapPostingList) Init(context PostingListContext) error { } if err != nil { - return fmt.Errorf("failed to create bitmap: %v", err) + return fmt.Errorf("failed to create bitmap: %w", err) } iterator := i.bitmap.Iterator() diff --git a/pkg/index/index_reader.go b/pkg/index/index_reader.go index 2f643da..368766f 100644 --- a/pkg/index/index_reader.go +++ b/pkg/index/index_reader.go @@ -37,13 +37,13 @@ func (ir *Reader) Read() (InvertedIndexIndices, error) { documentReader, err := ir.directory.OpenInput(ir.config.DocumentListFileName) if err != nil { - return nil, fmt.Errorf("failed to open document list: %v", err) + return nil, fmt.Errorf("failed to open document list: %w", err) } index, err := ir.createInvertedIndexIndices(header, documentReader) if err != nil { - return nil, fmt.Errorf("failed to retrieve inverted index: %v", err) + return nil, fmt.Errorf("failed to retrieve inverted index: %w", err) } runtime.SetFinalizer(index, func(d interface{}) { @@ -58,14 +58,14 @@ func (ir *Reader) readHeader() (*header, error) { headerReader, err := ir.directory.OpenInput(ir.config.HeaderFileName) if err != nil { - return nil, fmt.Errorf("failed to open header: %v", err) + return nil, fmt.Errorf("failed to open header: %w", err) } header := &header{} decoder := gob.NewDecoder(headerReader) if err = decoder.Decode(header); err != nil { - return nil, fmt.Errorf("failed to retrieve header: %v", err) + return nil, fmt.Errorf("failed to retrieve header: %w", err) } if header.Version != IndexVersion { @@ -73,7 +73,7 @@ func (ir *Reader) readHeader() (*header, error) { } if err = headerReader.Close(); err != nil { - return nil, fmt.Errorf("failed to close header file: %v", err) + return nil, fmt.Errorf("failed to close header file: %w", err) } return header, nil diff --git a/pkg/index/indexer_writer.go b/pkg/index/indexer_writer.go index 9b5af9a..4925afb 100644 --- a/pkg/index/indexer_writer.go +++ b/pkg/index/indexer_writer.go @@ -90,7 +90,7 @@ func (iw *Writer) Commit() error { documentWriter, err := iw.directory.CreateOutput(iw.config.DocumentListFileName) if err != nil { - return fmt.Errorf("failed to create document list: %v", err) + return fmt.Errorf("failed to create document list: %w", err) } // mapValueOffset stores current posting list offset @@ -138,7 +138,7 @@ func (iw *Writer) Commit() error { } if err = documentWriter.Close(); err != nil { - return fmt.Errorf("failed to close document list: %v", err) + return fmt.Errorf("failed to close document list: %w", err) } return nil @@ -149,18 +149,18 @@ func (iw *Writer) writeHeader(header header) error { headerWriter, err := iw.directory.CreateOutput(iw.config.HeaderFileName) if err != nil { - return fmt.Errorf("failed to create header: %v", err) + return fmt.Errorf("failed to create header: %w", err) } encoder := gob.NewEncoder(headerWriter) err = encoder.Encode(header) if err != nil { - return fmt.Errorf("failed to encode header: %v", err) + return fmt.Errorf("failed to encode header: %w", err) } if err = headerWriter.Close(); err != nil { - return fmt.Errorf("failed to close header file: %v", err) + return fmt.Errorf("failed to close header file: %w", err) } return nil diff --git a/pkg/index/searcher.go b/pkg/index/searcher.go index 77ef94f..95db400 100644 --- a/pkg/index/searcher.go +++ b/pkg/index/searcher.go @@ -53,7 +53,7 @@ func (s *searcher) Search(invertedIndex InvertedIndex, terms []Term, threshold i postingListContext, err := invertedIndex.Get(term) if err != nil { - return fmt.Errorf("failed to retrieve a posting list context: %v", err) + return fmt.Errorf("failed to retrieve a posting list context: %w", err) } list := resolvePostingList(postingListContext) @@ -65,14 +65,14 @@ func (s *searcher) Search(invertedIndex InvertedIndex, terms []Term, threshold i }(list) if err := list.Init(postingListContext); err != nil { - return fmt.Errorf("failed to initialize a posting list iterator: %v", err) + return fmt.Errorf("failed to initialize a posting list iterator: %w", err) } rid = append(rid, list) } if err := s.merger.Merge(rid, threshold, collector); err != nil { - return fmt.Errorf("failed to merge posting lists: %v", err) + return fmt.Errorf("failed to merge posting lists: %w", err) } return nil diff --git a/pkg/lm/binary.go b/pkg/lm/binary.go index ad54b28..3758873 100644 --- a/pkg/lm/binary.go +++ b/pkg/lm/binary.go @@ -31,27 +31,27 @@ func StoreBinaryLMFromGoogleFormat(directory store.Directory, config *Config) er model, err := reader.Read() if err != nil { - return fmt.Errorf("couldn't read ngrams: %v", err) + return fmt.Errorf("couldn't read ngrams: %w", err) } out, err := directory.CreateOutput(config.GetBinaryPath()) if err != nil { - return fmt.Errorf("failed to create a binary file: %v", err) + return fmt.Errorf("failed to create a binary file: %w", err) } enc := gob.NewEncoder(out) if err := enc.Encode(&model); err != nil { - return fmt.Errorf("failed to encode NGramModel in the binary format: %v", err) + return fmt.Errorf("failed to encode NGramModel in the binary format: %w", err) } if _, err := table.Store(out); err != nil { - return fmt.Errorf("failed to encode MPH in the binary format: %v", err) + return fmt.Errorf("failed to encode MPH in the binary format: %w", err) } if err := out.Close(); err != nil { - return fmt.Errorf("failed to close a binary output: %v", err) + return fmt.Errorf("failed to close a binary output: %w", err) } return nil @@ -68,7 +68,7 @@ func RetrieveLMFromBinary(directory store.Directory, config *Config) (LanguageMo in, err := directory.OpenInput(config.GetBinaryPath()) if err != nil { - return nil, fmt.Errorf("failed to open the lm binary file: %v", err) + return nil, fmt.Errorf("failed to open the lm binary file: %w", err) } var ( @@ -86,7 +86,7 @@ func RetrieveLMFromBinary(directory store.Directory, config *Config) (LanguageMo } if err := in.Close(); err != nil { - return nil, fmt.Errorf("failed to close a binary input: %v", err) + return nil, fmt.Errorf("failed to close a binary input: %w", err) } return NewLanguageModel(model, NewIndexer(dict, table), config) @@ -115,7 +115,7 @@ func newDictionaryReader(directory store.Directory) (dictionary.Iterable, error) in, err := directory.OpenInput(fmt.Sprintf(fileFormat, 1)) if err != nil { - return nil, fmt.Errorf("could not open a source file %s", err) + return nil, fmt.Errorf("could not open a source file %w", err) } return &dictionaryReader{ @@ -182,12 +182,12 @@ func (dr *dictionaryReader) Iterate(iterator dictionary.Iterator) error { item := iter.Get().(*dictItem) if err := iterator(docID, item.word); err != nil { - return fmt.Errorf("failed to iterate through dictionary: %v", err) + return fmt.Errorf("failed to iterate through dictionary: %w", err) } } if err := dr.in.Close(); err != nil { - return fmt.Errorf("failed to close a dictionary input: %v", err) + return fmt.Errorf("failed to close a dictionary input: %w", err) } return nil @@ -198,7 +198,7 @@ func buildMPH(dict dictionary.Dictionary) (mph.MPH, error) { table := mph.New() if err := table.Build(dict); err != nil { - return nil, fmt.Errorf("failed to build a mph table: %v", err) + return nil, fmt.Errorf("failed to build a mph table: %w", err) } return table, nil diff --git a/pkg/lm/indexer.go b/pkg/lm/indexer.go index 358a082..8fd7407 100644 --- a/pkg/lm/indexer.go +++ b/pkg/lm/indexer.go @@ -60,7 +60,7 @@ func (i *indexerImpl) Get(token Token) (WordID, error) { stored, err := i.dictionary.Get(index) if err != nil { - return UnknownWordID, fmt.Errorf("failed to get index from the dictionary: %v", err) + return UnknownWordID, fmt.Errorf("failed to get index from the dictionary: %w", err) } if stored != token { @@ -75,7 +75,7 @@ func (i *indexerImpl) Find(index WordID) (Token, error) { val, err := i.dictionary.Get(index) if err != nil { - return UnknownWordSymbol, fmt.Errorf("failed to get index from the dictionary: %v", err) + return UnknownWordSymbol, fmt.Errorf("failed to get index from the dictionary: %w", err) } if val == dictionary.NilValue { diff --git a/pkg/lm/language_model.go b/pkg/lm/language_model.go index e5f2211..81b5298 100644 --- a/pkg/lm/language_model.go +++ b/pkg/lm/language_model.go @@ -34,13 +34,13 @@ func NewLanguageModel( startSymbol, err := indexer.Get(config.StartSymbol) if err != nil { - return nil, fmt.Errorf("failed to get wordID of startSymbol: %v", err) + return nil, fmt.Errorf("failed to get wordID of startSymbol: %w", err) } endSymbol, err := indexer.Get(config.EndSymbol) if err != nil { - return nil, fmt.Errorf("failed to get wordID of endSymbol: %v", err) + return nil, fmt.Errorf("failed to get wordID of endSymbol: %w", err) } return &languageModel{ diff --git a/pkg/lm/ngram_reader.go b/pkg/lm/ngram_reader.go index 36d3c32..6287e6e 100644 --- a/pkg/lm/ngram_reader.go +++ b/pkg/lm/ngram_reader.go @@ -43,7 +43,7 @@ func (gr *googleNGramFormatReader) Read() (NGramModel, error) { builder := NewNGramVectorBuilder(vectors) if err := gr.readNGramVector(builder, i+1); err != nil { - return nil, fmt.Errorf("failed to read %d ngram vector: %v", i+1, err) + return nil, fmt.Errorf("failed to read %d ngram vector: %w", i+1, err) } vectors = append(vectors, builder.Build()) @@ -57,7 +57,7 @@ func (gr *googleNGramFormatReader) readNGramVector(builder NGramVectorBuilder, o in, err := gr.directory.OpenInput(fmt.Sprintf(fileFormat, order)) if err != nil { - return fmt.Errorf("failed to open a ngram input: %v", err) + return fmt.Errorf("failed to open a ngram input: %w", err) } nGrams := make([]WordID, 0, order) @@ -81,16 +81,16 @@ func (gr *googleNGramFormatReader) readNGramVector(builder NGramVectorBuilder, o count, err := strconv.ParseUint(line[tabIndex+1:], 10, 32) if err != nil { - return fmt.Errorf("ngram file is corrupted, expected number: %v", err) + return fmt.Errorf("ngram file is corrupted, expected number: %w", err) } if err := builder.Put(nGrams, WordCount(count)); err != nil { - return fmt.Errorf("failed to add nGrams to a builder: %v", err) + return fmt.Errorf("failed to add nGrams to a builder: %w", err) } } if err := scanner.Err(); err != nil { - return fmt.Errorf("failed to scan ngram file format: %v", err) + return fmt.Errorf("failed to scan ngram file format: %w", err) } return nil diff --git a/pkg/lm/ngram_vector_builder.go b/pkg/lm/ngram_vector_builder.go index b3d4734..075200a 100644 --- a/pkg/lm/ngram_vector_builder.go +++ b/pkg/lm/ngram_vector_builder.go @@ -53,7 +53,7 @@ func (m *nGramVectorBuilder) Put(nGrams []WordID, count WordCount) error { (prev.(*nGramNode)).value += count } else { if _, err := m.tree.Insert(node); err != nil { - return fmt.Errorf("failed to insert the node: %v", err) + return fmt.Errorf("failed to insert the node: %w", err) } } } else { diff --git a/pkg/lm/ngram_writer.go b/pkg/lm/ngram_writer.go index a66d929..57a001d 100644 --- a/pkg/lm/ngram_writer.go +++ b/pkg/lm/ngram_writer.go @@ -41,7 +41,7 @@ func (gw *googleNGramFormatWriter) Write(trie CountTrie) error { out, err := gw.directory.CreateOutput(fmt.Sprintf(fileFormat, i+1)) if err != nil { - return fmt.Errorf("failed to create an output: %v", err) + return fmt.Errorf("failed to create an output: %w", err) } outs[i] = out @@ -55,7 +55,7 @@ func (gw *googleNGramFormatWriter) Write(trie CountTrie) error { joined := strings.Join(nGrams, " ") if _, err := fmt.Fprintf(outs[len(nGrams)-1], nGramFormat, joined, count); err != nil { - return fmt.Errorf("failed to print nGrams: %v", err) + return fmt.Errorf("failed to print nGrams: %w", err) } return nil @@ -67,7 +67,7 @@ func (gw *googleNGramFormatWriter) Write(trie CountTrie) error { for _, out := range outs { if err := out.Close(); err != nil { - return fmt.Errorf("failed to close an output: %v", err) + return fmt.Errorf("failed to close an output: %w", err) } } diff --git a/pkg/mph/mph.go b/pkg/mph/mph.go index 06b0293..f959af1 100644 --- a/pkg/mph/mph.go +++ b/pkg/mph/mph.go @@ -79,7 +79,7 @@ func (m *mph) Build(dict dictionary.Dictionary) error { value, err := dict.Get(bucket[item]) if err != nil { - return fmt.Errorf("Failed to get bucket's key from the dictionary: %v", err) + return fmt.Errorf("Failed to get bucket's key from the dictionary: %w", err) } slot := hash(d, value) % size @@ -97,7 +97,7 @@ func (m *mph) Build(dict dictionary.Dictionary) error { val, err := dict.Get(bucket[0]) if err != nil { - return fmt.Errorf("Failed to get bucket's key from the dictionary: %v", err) + return fmt.Errorf("Failed to get bucket's key from the dictionary: %w", err) } auxiliary[hash(0, val)%size] = int32(d) @@ -130,7 +130,7 @@ func (m *mph) Build(dict dictionary.Dictionary) error { val, err := dict.Get(bucket[0]) if err != nil { - return fmt.Errorf("Failed to get bucket's key from the dictionary: %v", err) + return fmt.Errorf("Failed to get bucket's key from the dictionary: %w", err) } // We subtract one to ensure it's negative even if the zeroeth slot was @@ -161,7 +161,7 @@ func (m *mph) Store(out store.Output) (int, error) { n, err := out.WriteUInt32(uint32(len(m.values))) if err != nil { - return n, fmt.Errorf("failed to write the length of values: %v", err) + return n, fmt.Errorf("failed to write the length of values: %w", err) } for _, v := range m.values { @@ -169,7 +169,7 @@ func (m *mph) Store(out store.Output) (int, error) { n += s if err != nil { - return n, fmt.Errorf("failed to write a value: %v", err) + return n, fmt.Errorf("failed to write a value: %w", err) } } @@ -177,7 +177,7 @@ func (m *mph) Store(out store.Output) (int, error) { n += s if err != nil { - return n, fmt.Errorf("failed to write the length of auxiliary: %v", err) + return n, fmt.Errorf("failed to write the length of auxiliary: %w", err) } for _, v := range m.auxiliary { @@ -185,7 +185,7 @@ func (m *mph) Store(out store.Output) (int, error) { n += s if err != nil { - return n, fmt.Errorf("failed to writer a value: %v", err) + return n, fmt.Errorf("failed to writer a value: %w", err) } } @@ -197,7 +197,7 @@ func (m *mph) Load(in store.Input) (int, error) { n, err := in.ReadUInt32() if err != nil { - return 0, fmt.Errorf("failed to read the length of values: %v", err) + return 0, fmt.Errorf("failed to read the length of values: %w", err) } m.values = make([]dictionary.Key, n) @@ -206,7 +206,7 @@ func (m *mph) Load(in store.Input) (int, error) { v, err := in.ReadUInt32() if err != nil { - return 0, fmt.Errorf("failed to read a value: %v", err) + return 0, fmt.Errorf("failed to read a value: %w", err) } m.values[i] = v @@ -215,7 +215,7 @@ func (m *mph) Load(in store.Input) (int, error) { s, err := in.ReadUInt32() if err != nil { - return 0, fmt.Errorf("failed to read the length of auxiliary: %v", err) + return 0, fmt.Errorf("failed to read the length of auxiliary: %w", err) } m.auxiliary = make([]int32, s) @@ -224,7 +224,7 @@ func (m *mph) Load(in store.Input) (int, error) { v, err := in.ReadUInt32() if err != nil { - return 0, fmt.Errorf("failed to read a value: %v", err) + return 0, fmt.Errorf("failed to read a value: %w", err) } m.auxiliary[i] = int32(v) diff --git a/pkg/store/fs_directory.go b/pkg/store/fs_directory.go index 92f1321..60eb4d9 100644 --- a/pkg/store/fs_directory.go +++ b/pkg/store/fs_directory.go @@ -24,7 +24,7 @@ func NewFSDirectory(path string) (Directory, error) { } if err != nil { - return nil, fmt.Errorf("Failed to receive stat for the path %v", err) + return nil, fmt.Errorf("Failed to receive stat for the path %w", err) } if !stat.IsDir() { @@ -45,7 +45,7 @@ func (fs *fsDirectory) CreateOutput(name string) (Output, error) { ) if err != nil { - return nil, fmt.Errorf("Failed to create output: %v", err) + return nil, fmt.Errorf("Failed to create output: %w", err) } return NewBytesOutput(bufio.NewWriter(file)), nil @@ -56,13 +56,13 @@ func (fs *fsDirectory) OpenInput(name string) (Input, error) { file, err := utils.NewMMapReader(fs.path + "/" + name) if err != nil { - return nil, fmt.Errorf("Failed to open input: %v", err) + return nil, fmt.Errorf("Failed to open input: %w", err) } data, err := file.Bytes() if err != nil { - return nil, fmt.Errorf("Failed to fetch content: %v", err) + return nil, fmt.Errorf("Failed to fetch content: %w", err) } input := NewBytesInput(data) diff --git a/pkg/store/ram_directory.go b/pkg/store/ram_directory.go index e3fb152..695c1cd 100644 --- a/pkg/store/ram_directory.go +++ b/pkg/store/ram_directory.go @@ -30,7 +30,7 @@ func (rd *ramDirectory) CreateOutput(name string) (Output, error) { // OpenInput returns a reader for the given name func (rd *ramDirectory) OpenInput(name string) (Input, error) { if _, ok := rd.files[name]; !ok { - return nil, fmt.Errorf("Failed to open input reader: there is no such input with the name %v", name) + return nil, fmt.Errorf("Failed to open input reader: there is no such input with the name %w", name) } data := rd.files[name].Bytes() diff --git a/pkg/suggest/autocomplete.go b/pkg/suggest/autocomplete.go index 70f414b..33f7adc 100644 --- a/pkg/suggest/autocomplete.go +++ b/pkg/suggest/autocomplete.go @@ -52,12 +52,12 @@ func (n *nGramAutocomplete) Autocomplete(query string, collectorManager Collecto collector, err := collectorManager.Create() if err != nil { - return nil, fmt.Errorf("failed to create a collector: %v", err) + return nil, fmt.Errorf("failed to create a collector: %w", err) } workerPool.Go(func() error { if err = n.searcher.Search(invertedIndex, set, lenSet, collector); err != nil { - return fmt.Errorf("failed to search posting lists: %v", err) + return fmt.Errorf("failed to search posting lists: %w", err) } return nil diff --git a/pkg/suggest/indexer.go b/pkg/suggest/indexer.go index 321cc8c..2bd32a9 100644 --- a/pkg/suggest/indexer.go +++ b/pkg/suggest/indexer.go @@ -20,7 +20,7 @@ func Index( encoder, err := index.NewEncoder() if err != nil { - return fmt.Errorf("failed to create Encoder: %v", err) + return fmt.Errorf("failed to create Encoder: %w", err) } indexWriter := index.NewIndexWriter( diff --git a/pkg/suggest/ngram_index_builder.go b/pkg/suggest/ngram_index_builder.go index 6f84687..fbafd69 100644 --- a/pkg/suggest/ngram_index_builder.go +++ b/pkg/suggest/ngram_index_builder.go @@ -28,7 +28,7 @@ func NewRAMBuilder(dict dictionary.Dictionary, description IndexDescription) (Bu directory := store.NewRAMDirectory() if err := Index(directory, dict, description.GetWriterConfig(), description.GetIndexTokenizer()); err != nil { - return nil, fmt.Errorf("failed to create a ram search index: %v", err) + return nil, fmt.Errorf("failed to create a ram search index: %w", err) } return NewBuilder(directory, description) @@ -39,7 +39,7 @@ func NewFSBuilder(description IndexDescription) (Builder, error) { directory, err := store.NewFSDirectory(description.GetIndexPath()) if err != nil { - return nil, fmt.Errorf("failed to create a fs directory: %v", err) + return nil, fmt.Errorf("failed to create a fs directory: %w", err) } return NewBuilder(directory, description) @@ -61,7 +61,7 @@ func (b *builderImpl) Build() (NGramIndex, error) { invertedIndices, err := b.indexReader.Read() if err != nil { - return nil, fmt.Errorf("failed to build NGramIndex: %v", err) + return nil, fmt.Errorf("failed to build NGramIndex: %w", err) } suggester := NewSuggester( diff --git a/pkg/suggest/service.go b/pkg/suggest/service.go index d755dc4..656923f 100644 --- a/pkg/suggest/service.go +++ b/pkg/suggest/service.go @@ -45,13 +45,13 @@ func (s *Service) AddRunTimeIndex(description IndexDescription) error { dict, err := dictionary.OpenRAMDictionary(description.GetSourcePath()) if err != nil { - return fmt.Errorf("failed to create RAMDriver builder: %v", err) + return fmt.Errorf("failed to create RAMDriver builder: %w", err) } builder, err := NewRAMBuilder(dict, description) if err != nil { - return fmt.Errorf("failed to create RAMDriver builder: %v", err) + return fmt.Errorf("failed to create RAMDriver builder: %w", err) } return s.AddIndex(description.Name, dict, builder) @@ -62,13 +62,13 @@ func (s *Service) AddOnDiscIndex(description IndexDescription) error { dict, err := dictionary.OpenCDBDictionary(description.GetDictionaryFile()) if err != nil { - return fmt.Errorf("failed to create CDB dictionary: %v", err) + return fmt.Errorf("failed to create CDB dictionary: %w", err) } builder, err := NewFSBuilder(description) if err != nil { - return fmt.Errorf("failed to open FS inverted index: %v", err) + return fmt.Errorf("failed to open FS inverted index: %w", err) } return s.AddIndex(description.Name, dict, builder) @@ -79,7 +79,7 @@ func (s *Service) AddIndex(name string, dict dictionary.Dictionary, builder Buil nGramIndex, err := builder.Build() if err != nil { - return fmt.Errorf("failed to build NGramIndex: %v", err) + return fmt.Errorf("failed to build NGramIndex: %w", err) } s.Lock() diff --git a/pkg/suggest/suggester.go b/pkg/suggest/suggester.go index 64a5e1e..c0b3846 100644 --- a/pkg/suggest/suggester.go +++ b/pkg/suggest/suggester.go @@ -97,7 +97,7 @@ func (n *nGramSuggester) Suggest(config SearchConfig) ([]Candidate, error) { } if err := n.searcher.Search(invertedIndex, set, threshold, collector); err != nil { - return fmt.Errorf("failed to search posting lists: %v", err) + return fmt.Errorf("failed to search posting lists: %w", err) } lock.Lock()