summaryrefslogtreecommitdiff
path: root/files/operations.go
diff options
context:
space:
mode:
authorjakka <jakkadoujin@gmail.com>2025-04-13 13:59:04 +0300
committerjakka <jakkadoujin@gmail.com>2025-04-13 13:59:04 +0300
commitb1a2ab25856ec14b042da9fec49c453c6dad95e5 (patch)
tree723023a0be1bfc4bb20b5b9a9becfc6a14f6bf6b /files/operations.go
parent04f0578da42d11cc6e5b98450c1018dde8c49737 (diff)
switched to rosedb, it actually works! stopped using goroutines for file indexing since its slower
Diffstat (limited to 'files/operations.go')
-rw-r--r--files/operations.go97
1 files changed, 68 insertions, 29 deletions
diff --git a/files/operations.go b/files/operations.go
index 885e31f..6f44900 100644
--- a/files/operations.go
+++ b/files/operations.go
@@ -6,6 +6,7 @@ import (
"fmt"
"io"
"io/fs"
+ "log"
"os"
"os/exec"
"path/filepath"
@@ -14,18 +15,38 @@ import (
"time"
"github.com/briandowns/spinner"
- "golang.org/x/sync/errgroup"
+ "github.com/rosedblabs/rosedb/v2"
)
func (file *FileInfo) reencodeFile(ctx context.Context) error {
- out, err := exec.Command("flac", "-8fs", "-j4", file.AbsPath).Output()
- if err != nil {
- fmt.Printf("%v\n", out)
- return err
+ select {
+ case <-ctx.Done():
+ return nil
+ default:
+ args := ctx.Value("flac").([]string)
+ if args != nil {
+ args = append(args, file.AbsPath)
+ cmd := exec.Command("flac", args...)
+ if err := cmd.Run(); err != nil {
+ if !strings.Contains(err.Error(), "interrupt") {
+ log.Println(err.Error())
+ return err
+ }
+ }
+ } else {
+ cmd := exec.Command("flac", "-8f", "-j4", file.AbsPath)
+ if err := cmd.Run(); err != nil {
+ if !strings.Contains(err.Error(), "interrupt") {
+ log.Println(err.Error())
+ return err
+ }
+ }
+ }
+
+ file.Encoder = ctx.Value("encoder").(string)
+ file.Process = false
+ return nil
}
- file.Encoder = ctx.Value("encoder").(string)
- file.Process = false
- return nil
}
func getEncoderVer(path string) (string, error) {
@@ -44,18 +65,11 @@ func getEncoderVer(path string) (string, error) {
}
}
-func getInfoFromFile(path string, info fs.DirEntry) (*FileInfo, error) {
+func getInfoFromFile(path string) (*FileInfo, error) {
var filedata FileInfo
filedata.Process = true
- tmp, err := info.Info()
- if err != nil {
- return nil, err
- }
-
- filedata.Modtime = tmp.ModTime()
-
abspath, err := filepath.Abs(path)
if err != nil {
return nil, err
@@ -87,14 +101,25 @@ func getSha256(path string) ([]byte, error) {
return hash.Sum(nil), nil
}
+func updateSpinner(spin *spinner.Spinner, counter int64) {
+ line := fmt.Sprintf(" Indexing flacs...\t %v", counter)
+ spin.Suffix = line
+}
+
func IndexFlacs(ctx context.Context) error {
+ counter := ctx.Value("counter").(*int64)
+ database := ctx.Value("database").(*rosedb.DB)
+
+ batch := database.NewBatch(rosedb.DefaultBatchOptions)
+
spin := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
- spin.Suffix = " Indexing flacs..."
+
+ updateSpinner(spin, *counter)
spin.Start()
- wg := new(errgroup.Group)
- wg.SetLimit(100)
+ /* wg := new(errgroup.Group)
+ wg.SetLimit(100) */
if err := filepath.WalkDir(ctx.Value("path").(string), func(path string, info fs.DirEntry, err error) error {
select {
@@ -103,17 +128,14 @@ func IndexFlacs(ctx context.Context) error {
spin.Stop()
return filepath.SkipAll
default:
- if err != nil {
- return err
- }
if !info.IsDir() {
if filepath.Ext(path) == ".flac" {
- wg.Go(func() error {
+ /* wg.Go(func() error {
select {
case <-ctx.Done():
return nil
default:
- data, err := getInfoFromFile(path, info)
+ data, err := getInfoFromFile(path)
if err != nil {
return err
}
@@ -122,10 +144,22 @@ func IndexFlacs(ctx context.Context) error {
if err != nil {
return err
}
-
- return data.IndexFile(ctx, hashsum)
+ updateSpinner(spin, *counter)
+ return data.IndexFile(ctx, hashsum, batch)
}
- })
+ }) */
+
+ data, err := getInfoFromFile(path)
+ if err != nil {
+ return err
+ }
+
+ hashsum, err := getSha256(path)
+ if err != nil {
+ return err
+ }
+ updateSpinner(spin, *counter)
+ return data.IndexFile(ctx, hashsum, batch)
}
}
@@ -136,8 +170,13 @@ func IndexFlacs(ctx context.Context) error {
return err
}
- wg.Wait()
- spin.FinalMSG = "Done indexing flacs\n"
+ /* wg.Wait() */
+
+ if err := batch.Commit(); err != nil {
+ return err
+ }
+ spin.FinalMSG = fmt.Sprintf("Done indexing flacs: \t%v to process\n", *counter)
spin.Stop()
+
return nil
}