Skip to content

Commit

Permalink
Differentiate between already indexed errors and other errors
Browse files Browse the repository at this point in the history
  • Loading branch information
maeb committed Nov 6, 2022
1 parent 0d4d845 commit 99885a2
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 14 deletions.
3 changes: 2 additions & 1 deletion cmd/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func NewCommand() *cobra.Command {
format := "cdxj"
indexDepth := 4
indexWorkers := 8
badgerDir := "."
badgerDir := "./warcdb"
badgerBatchMaxSize := 1000
badgerBatchMaxWait := 5 * time.Second
badgerCompression := "snappy"
Expand Down Expand Up @@ -159,6 +159,7 @@ func indexCmd(_ *cobra.Command, args []string) error {
}
}

log.Info().Msg("Starting auto indexer")
indexer, err := index.NewAutoIndexer(indexWorker,
index.WithMaxDepth(viper.GetInt("max-depth")),
index.WithIncludes(includes...),
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,5 @@ func initConfig() {
}
logger.InitLog(viper.GetString("log-level"), viper.GetString("log-formatter"), viper.GetBool("log-method"))

log.Debug().Msgf("Using config file: %s", viper.ConfigFileUsed())
log.Info().Msgf("Using config file: %s", viper.ConfigFileUsed())
}
2 changes: 1 addition & 1 deletion cmd/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func NewCommand() *cobra.Command {
autoIndex := true
indexDepth := 4
indexWorkers := 8
badgerDir := "."
badgerDir := "./warcdb"
badgerCompression := "snappy"
badgerDatabase := ""
badgerBatchMaxSize := 1000
Expand Down
8 changes: 8 additions & 0 deletions index/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ import (
"github.com/nlnwa/gowarcserver/schema"
)

type indexError string

const AlreadyIndexedError indexError = "already indexed"

func (a indexError) Error() string {
return string(a)
}

type DateRange interface {
ContainsTime(time.Time) (bool, error)
ContainsStr(string) (bool, error)
Expand Down
7 changes: 6 additions & 1 deletion index/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package index

import (
"errors"
"sync"
"time"

Expand Down Expand Up @@ -49,7 +50,11 @@ func NewWorker(a Indexer, nrOfWorkers int) *indexWorker {
select {
case job := <-iw.jobs:
if err := a.Index(job); err != nil {
log.Warn().Err(err).Msgf("Index: %s", job)
if errors.Is(err, AlreadyIndexedError) {
log.Debug().Err(err).Msg(job)
} else {
log.Warn().Err(err).Msgf(job)
}
}
iw.wg.Done()
iw.mx.Lock()
Expand Down
4 changes: 2 additions & 2 deletions internal/badgeridx/badger.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ func (l logger) Warningf(fmt string, args ...interface{}) {
}

func (l logger) Infof(fmt string, args ...interface{}) {
log.Debug().Msgf(l.prefix+fmt, args...)
log.Trace().Msgf(l.prefix+fmt, args...)
}

func (l logger) Debugf(fmt string, args ...interface{}) {
log.Debug().Msgf(l.prefix+fmt, args...)
log.Trace().Msgf(l.prefix+fmt, args...)
}

func newBadgerDB(dir string, compression options.CompressionType, readOnly bool) (*badger.DB, error) {
Expand Down
10 changes: 4 additions & 6 deletions internal/badgeridx/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package badgeridx

import (
"context"
"errors"
"fmt"
"os"
"path"
Expand Down Expand Up @@ -63,17 +62,16 @@ func NewDB(options ...DbOption) (db *DB, err error) {
var fileIndex *badger.DB
var cdxIndex *badger.DB

dir := path.Join(opts.Path, "warcdb")
batch := make(chan index.Record, opts.BatchMaxSize)
done := make(chan struct{})

if idIndex, err = newBadgerDB(path.Join(dir, opts.Database, "id-index"), opts.Compression, opts.ReadOnly); err != nil {
if idIndex, err = newBadgerDB(path.Join(opts.Path, opts.Database, "id-index"), opts.Compression, opts.ReadOnly); err != nil {
return
}
if fileIndex, err = newBadgerDB(path.Join(dir, opts.Database, "file-index"), opts.Compression, opts.ReadOnly); err != nil {
if fileIndex, err = newBadgerDB(path.Join(opts.Path, opts.Database, "file-index"), opts.Compression, opts.ReadOnly); err != nil {
return
}
if cdxIndex, err = newBadgerDB(path.Join(dir, opts.Database, "cdx-index"), opts.Compression, opts.ReadOnly); err != nil {
if cdxIndex, err = newBadgerDB(path.Join(opts.Path, opts.Database, "cdx-index"), opts.Compression, opts.ReadOnly); err != nil {
return
}

Expand Down Expand Up @@ -171,7 +169,7 @@ func (db *DB) addFile(filePath string) error {
}
fileInfoLastModified := fileInfo.LastModified.AsTime()
if fileInfo.Size == fileSize && fileInfoLastModified.Equal(fileLastModified) {
return errors.New("already indexed")
return index.AlreadyIndexedError
}
}

Expand Down
3 changes: 1 addition & 2 deletions internal/tikvidx/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package tikvidx
import (
"bytes"
"context"
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -152,7 +151,7 @@ func (db *DB) addFile(filePath string) error {
}
fileInfoLastModified := fileInfo.LastModified.AsTime()
if fileInfo.Size == fileSize && fileInfoLastModified.Equal(fileLastModified) {
return errors.New("already indexed")
return index.AlreadyIndexedError
}
}

Expand Down

0 comments on commit 99885a2

Please sign in to comment.