github.com/anishathalye/periscope@v0.3.5/internal/periscope/refresh.go (about)

     1  package periscope
     2  
     3  import (
     4  	"github.com/anishathalye/periscope/internal/db"
     5  	"github.com/anishathalye/periscope/internal/herror"
     6  	"github.com/anishathalye/periscope/internal/par"
     7  
     8  	"fmt"
     9  	"log"
    10  )
    11  
    12  type RefreshOptions struct {
    13  }
    14  
    15  func (ps *Periscope) Refresh(options *RefreshOptions) herror.Interface {
    16  	summary, err := ps.db.Summary()
    17  	if err != nil {
    18  		return err
    19  	}
    20  	infos, err := ps.db.AllInfosC()
    21  	if err != nil {
    22  		return err
    23  	}
    24  
    25  	bar := ps.progressBar(int(summary.Files), `scanning: {{ counters . }} {{ bar . "[" "=" ">" " " "]" }} {{ etime . }} {{ rtime . "ETA %s" "%.0s" " " }} `)
    26  
    27  	var gone []string
    28  	for path := range par.MapN(infos, scanThreads, func(_, v interface{}, emit func(x interface{})) {
    29  		path := v.(db.FileInfo).Path
    30  		_, _, err := ps.checkFile(path, true, false, "", true, false)
    31  		bar.Increment()
    32  		if err != nil {
    33  			log.Printf("removing '%s' from database", path)
    34  			emit(path)
    35  		}
    36  	}) {
    37  		gone = append(gone, path.(string))
    38  	}
    39  	// note: we can't actually delete the files while scanning because
    40  	// we're doing a streaming read from the database
    41  	tx, err := ps.db.Begin()
    42  	if err != nil {
    43  		return err
    44  	}
    45  	for _, path := range gone {
    46  		if err = tx.Remove(path); err != nil {
    47  			return err
    48  		}
    49  	}
    50  	if err = tx.Commit(); err != nil {
    51  		return err
    52  	}
    53  	bar.Finish()
    54  	fmt.Fprintf(ps.outStream, "removed %d files from the database\n", len(gone))
    55  	return nil
    56  }