implement scan and index queue
This commit is contained in:
parent
118cfd91de
commit
e0314dcf56
4 changed files with 81 additions and 33 deletions
71
pkg/server/queue.go
Normal file
71
pkg/server/queue.go
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
// Copyright (C) 2022 Marius Schellenberger
|
||||
|
||||
package server
|
||||
|
||||
import "context"
|
||||
|
||||
type ScanQueue struct {
|
||||
ctx context.Context
|
||||
srv *HTTPServer
|
||||
pchan chan string
|
||||
}
|
||||
|
||||
func NewScanQueue(ctx context.Context, srv *HTTPServer, buffer int) *ScanQueue {
|
||||
return &ScanQueue{
|
||||
ctx: ctx,
|
||||
srv: srv,
|
||||
pchan: make(chan string, buffer),
|
||||
}
|
||||
}
|
||||
|
||||
func (sq *ScanQueue) Add(path string) (err error) {
|
||||
err = sq.srv.FS.AddScan(path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
select {
|
||||
case <-sq.ctx.Done():
|
||||
return
|
||||
case sq.pchan <- path:
|
||||
}
|
||||
}()
|
||||
return
|
||||
}
|
||||
|
||||
func (sq *ScanQueue) Scan() {
|
||||
for {
|
||||
select {
|
||||
case <-sq.ctx.Done():
|
||||
//close(sq.pchan)
|
||||
return
|
||||
case p := <-sq.pchan:
|
||||
sq.scanFile(p)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (sq *ScanQueue) scanFile(path string) {
|
||||
srv := sq.srv
|
||||
defer srv.FS.RemoveScan(path)
|
||||
log := srv.Log.WithValues("file", path)
|
||||
file, txt, err := srv.Scanner.Scan(path)
|
||||
if err != nil {
|
||||
log.Error(err, "error scanning file")
|
||||
return
|
||||
}
|
||||
tags, err := srv.DB.GetAllRTags()
|
||||
if err != nil {
|
||||
log.Error(err, "error getting tags")
|
||||
}
|
||||
found := tags.Match(txt)
|
||||
id, err := srv.DB.Index.Add(txt, found)
|
||||
if err != nil {
|
||||
log.Error(err, "error adding file to index")
|
||||
return
|
||||
}
|
||||
err = srv.DB.NewFile(id, file, found)
|
||||
if err != nil {
|
||||
log.Error(err, "error adding file to DB")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue