Skip to content

Commit

Permalink
Added forceIndexing parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
svera committed Sep 4, 2024
1 parent cc38f3a commit c443408
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 15 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ A personal documents server, Coreander indexes the documents (EPUBs and PDFs wit
* Read indexed epubs and PDFs from Coreander's interface thanks to [foliate-js](https://github.com/johnfactotum/foliate-js).
* Restrictable access only to registered users.
* Upload documents through the web interface.
* Download as kepub (epub for Kobo devices) converted on the fly thanks to [Kepubify](https://github.com/pgaskin/kepubify).

## Installation

Expand Down Expand Up @@ -97,7 +98,7 @@ On first run, Coreander creates an admin user with the following credentials:
* `PORT`: Port number in which the webserver listens for requests. Defaults to 3000.
* `BATCH_SIZE`: Number of documents persisted by the indexer in one write operation. Defaults to 100.
* `COVER_MAX_WIDTH`: Maximum horizontal size for documents cover thumbnails in pixels. Defaults to 600.
* `SKIP_INDEXING`: Whether to bypass the indexing process or not.
* `FORCE_INDEXING`: Whether to force indexing already indexed documents or not. Defaults to false.
* `SMTP_SERVER`: Address of the send mail server.
* `SMTP_PORT`: Port number of the send mail server. Defaults to 587.
* `SMTP_USER`: User to authenticate against the SMTP server.
Expand Down
4 changes: 2 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ type Config struct {
BatchSize int `env:"BATCH_SIZE" env-default:"100"`
// CoverMaxWidth sets the maximum horizontal size for documents cover thumbnails in pixels
CoverMaxWidth int `env:"COVER_MAX_WIDTH" env-default:"600"`
// SkipIndexing signals whether to bypass the indexing process or not
SkipIndexing bool `env:"SKIP_INDEXING" env-default:"false"`
// ForceIndexing signals whether to force indexing already indexed documents or not
ForceIndexing bool `env:"FORCE_INDEXING" env-default:"false"`
// SmtpServer points to the address of the send mail server
SmtpServer string `env:"SMTP_SERVER"`
// SmtpPort defines the port in which the mail server listens for requests
Expand Down
2 changes: 1 addition & 1 deletion internal/index/bleve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestIndexAndSearch(t *testing.T) {
appFS.MkdirAll("lib", 0755)
afero.WriteFile(appFS, tcase.filename, []byte(""), 0644)

err = idx.AddLibrary(1)
err = idx.AddLibrary(1, true)
if err != nil {
t.Errorf("Error indexing: %s", err.Error())
}
Expand Down
4 changes: 2 additions & 2 deletions internal/index/bleve_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ func (b *BleveIndexer) RemoveFile(file string) error {

// AddLibrary scans <libraryPath> for documents and adds them to the index in batches of <bathSize> if they
// haven't been previously indexed
func (b *BleveIndexer) AddLibrary(batchSize int) error {
func (b *BleveIndexer) AddLibrary(batchSize int, forceIndexing bool) error {
batch := b.idx.NewBatch()
batchSlugs := make(map[string]struct{}, batchSize)
languages := []string{}
b.indexStartTime = float64(time.Now().UnixNano())
e := afero.Walk(b.fs, b.libraryPath, func(fullPath string, f os.FileInfo, err error) error {
if indexed, lang := b.isAlreadyIndexed(fullPath); indexed {
if indexed, lang := b.isAlreadyIndexed(fullPath); indexed && !forceIndexing {
b.indexedDocuments += 1
languages = addLanguage(lang, languages)
return nil
Expand Down
2 changes: 1 addition & 1 deletion internal/webserver/webserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func bootstrapApp(db *gorm.DB, sender webserver.Sender, appFs afero.Fs, webserve
idx = index.NewBleve(indexFile, appFs, webserverConfig.LibraryPath, metadataReaders)
}

err = idx.AddLibrary(100)
err = idx.AddLibrary(100, true)
if err != nil {
log.Fatal(err)
}
Expand Down
12 changes: 4 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,7 @@ func migrateDir() {
func main() {
defer idx.Close()

if !cfg.SkipIndexing {
go startIndex(idx, appFs, cfg.BatchSize, cfg.LibPath)
} else {
go fileWatcher(idx, cfg.LibPath)
}
go startIndex(idx, appFs, cfg.BatchSize, cfg.LibPath)

sender = &infrastructure.NoEmail{}
if cfg.SmtpServer != "" && cfg.SmtpUser != "" && cfg.SmtpPassword != "" {
Expand Down Expand Up @@ -133,7 +129,7 @@ func main() {
func startIndex(idx *index.BleveIndexer, appFs afero.Fs, batchSize int, libPath string) {
start := time.Now().Unix()
log.Printf("Indexing documents at %s, this can take a while depending on the size of your library.", libPath)
err := idx.AddLibrary(batchSize)
err := idx.AddLibrary(batchSize, cfg.ForceIndexing)
if err != nil {
log.Fatal(err)
}
Expand All @@ -147,7 +143,7 @@ func getIndexFile(fs afero.Fs) bleve.Index {
indexFile, err := bleve.Open(homeDir + indexPath)
if err == bleve.ErrorIndexPathDoesNotExist {
log.Println("No index found, creating a new one.")
cfg.SkipIndexing = false
cfg.ForceIndexing = false
indexFile = index.Create(homeDir + indexPath)
}
version, err := indexFile.GetInternal([]byte("version"))
Expand All @@ -159,7 +155,7 @@ func getIndexFile(fs afero.Fs) bleve.Index {
if err = fs.RemoveAll(homeDir + indexPath); err != nil {
log.Fatal(err)
}
cfg.SkipIndexing = false
cfg.ForceIndexing = false
indexFile = index.Create(homeDir + indexPath)
}
return indexFile
Expand Down

0 comments on commit c443408

Please sign in to comment.