Skip to content

Commit

Permalink
feat[close #1]: Read withMetadata from storage
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkobrombin committed Feb 27, 2024
1 parent 52655bd commit 030596b
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 26 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,12 @@ import (
)

func main() {
s := storage.NewStorage(storage.StorageOptions{Root: "/path/to/storage"})
s := storage.NewStorage(storage.StorageOptions{
Root: "/path/to/storage",
WithMetadata: true,
})
h := hash.NewSHA256Generator()
p := processor.NewDedupProcessor("/path/to/folder", s, h, 2, true)
p := processor.NewDedupProcessor("/path/to/folder", s, h, 2)

d := dabadee.NewDaBaDee(p)
err := d.Run()
Expand Down
2 changes: 1 addition & 1 deletion cmd/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func cpCommand(cmd *cobra.Command, args []string) {
h := hash.NewSHA256Generator()

// Create processor
processor := processor.NewCpProcessor(source, dest, s, h, withMetadata)
processor := processor.NewCpProcessor(source, dest, s, h)

// Run the processor
log.Printf("Copying %s to %s..", source, dest)
Expand Down
2 changes: 1 addition & 1 deletion cmd/dedup.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func dedupCommand(cmd *cobra.Command, args []string) {
h := hash.NewSHA256Generator()

// Create processor
processor := processor.NewDedupProcessor(source, s, h, workers, withMetadata)
processor := processor.NewDedupProcessor(source, s, h, workers)

// Run the processor
log.Printf("Deduplicating %s..", source)
Expand Down
16 changes: 6 additions & 10 deletions pkg/processor/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,15 @@ type CpProcessor struct {

// HashGen is the hash generator to use
HashGen hash.Generator

// WithMetadata is a flag to enable metadata hashing
WithMetadata bool
}

// NewCpProcessor creates a new CpProcessor
func NewCpProcessor(sourceFile, destFile string, storage *storage.Storage, hashGen hash.Generator, withMetadata bool) *CpProcessor {
func NewCpProcessor(sourceFile, destFile string, storage *storage.Storage, hashGen hash.Generator) *CpProcessor {
return &CpProcessor{
SourceFile: sourceFile,
DestFile: destFile,
Storage: storage,
HashGen: hashGen,
WithMetadata: withMetadata,
SourceFile: sourceFile,
DestFile: destFile,
Storage: storage,
HashGen: hashGen,
}
}

Expand All @@ -45,7 +41,7 @@ func (p *CpProcessor) Process() (err error) {
// Compute file hash
var finalHash string

if p.WithMetadata {
if p.Storage.Opts.WithMetadata {
finalHash, err = p.HashGen.ComputeFullHash(p.SourceFile)
if err != nil {
return fmt.Errorf("computing full hash: %w", err)
Expand Down
16 changes: 6 additions & 10 deletions pkg/processor/dedup.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,15 @@ type DedupProcessor struct {

// Workers is the number of workers to use
Workers int

// WithMetadata is a flag to enable metadata hashing
WithMetadata bool
}

// NewDedupProcessor creates a new DedupProcessor
func NewDedupProcessor(source string, storage *storage.Storage, hashGen hash.Generator, workers int, withMetadata bool) *DedupProcessor {
func NewDedupProcessor(source string, storage *storage.Storage, hashGen hash.Generator, workers int) *DedupProcessor {
return &DedupProcessor{
Source: source,
Storage: storage,
HashGen: hashGen,
Workers: workers,
WithMetadata: withMetadata,
Source: source,
Storage: storage,
HashGen: hashGen,
Workers: workers,
}
}

Expand Down Expand Up @@ -124,7 +120,7 @@ func (p *DedupProcessor) processFile(path string) (err error) {
// Compute file hash
var finalHash string

if p.WithMetadata {
if p.Storage.Opts.WithMetadata {
finalHash, err = p.HashGen.ComputeFullHash(path)
if err != nil {
return fmt.Errorf("computing full hash: %w", err)
Expand Down
1 change: 0 additions & 1 deletion tests/cp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ func TestCpCommand(t *testing.T) {
filepath.Join(testPath, "file-0-link"),
s,
h,
true,
)

d := dabadee.NewDaBaDee(cpProcessor)
Expand Down
2 changes: 1 addition & 1 deletion tests/dedup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestDedupCommand(t *testing.T) {

h := hash.NewSHA256Generator()

processor := processor.NewDedupProcessor(testPath, s, h, 1, true)
processor := processor.NewDedupProcessor(testPath, s, h, 1)

d := dabadee.NewDaBaDee(processor)

Expand Down

0 comments on commit 030596b

Please sign in to comment.