Skip to content

Commit

Permalink
Merge pull request #252 from wagoodman/fix-log-and-tar-paths
Browse files Browse the repository at this point in the history
Fix log and relative paths
  • Loading branch information
wagoodman authored Nov 20, 2019
2 parents fe158e4 + 6b659d9 commit 35db1ec
Show file tree
Hide file tree
Showing 14 changed files with 346 additions and 145 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ func initLogging() {

if viper.GetBool("log.enabled") {
logFileObj, err = os.OpenFile(viper.GetString("log.path"), os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
log.SetOutput(logFileObj)
} else {
log.SetOutput(ioutil.Discard)
}
Expand All @@ -139,7 +140,6 @@ func initLogging() {
}

log.SetLevel(level)
log.SetOutput(logFileObj)
log.Debug("Starting Dive...")
}

Expand Down
93 changes: 0 additions & 93 deletions dive/filetree/cache.go

This file was deleted.

175 changes: 175 additions & 0 deletions dive/filetree/comparer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package filetree

import (
"fmt"
"github.com/sirupsen/logrus"
)

type TreeIndexKey struct {
bottomTreeStart, bottomTreeStop, topTreeStart, topTreeStop int
}

func NewTreeIndexKey(bottomTreeStart, bottomTreeStop, topTreeStart, topTreeStop int) TreeIndexKey {
return TreeIndexKey{
bottomTreeStart: bottomTreeStart,
bottomTreeStop: bottomTreeStop,
topTreeStart: topTreeStart,
topTreeStop: topTreeStop,
}
}

func (index TreeIndexKey) String() string {
if index.bottomTreeStart == index.bottomTreeStop && index.topTreeStart == index.topTreeStop {
return fmt.Sprintf("Index(%d:%d)", index.bottomTreeStart, index.topTreeStart)
} else if index.bottomTreeStart == index.bottomTreeStop {
return fmt.Sprintf("Index(%d:%d-%d)", index.bottomTreeStart, index.topTreeStart, index.topTreeStop)
} else if index.topTreeStart == index.topTreeStop {
return fmt.Sprintf("Index(%d-%d:%d)", index.bottomTreeStart, index.bottomTreeStop, index.topTreeStart)
}
return fmt.Sprintf("Index(%d-%d:%d-%d)", index.bottomTreeStart, index.bottomTreeStop, index.topTreeStart, index.topTreeStop)
}

type Comparer struct {
refTrees []*FileTree
trees map[TreeIndexKey]*FileTree
pathErrors map[TreeIndexKey][]PathError
}

func NewComparer(refTrees []*FileTree) Comparer {
return Comparer{
refTrees: refTrees,
trees: make(map[TreeIndexKey]*FileTree),
pathErrors: make(map[TreeIndexKey][]PathError),
}
}

func (cmp *Comparer) GetPathErrors(key TreeIndexKey) ([]PathError, error) {
_, pathErrors, err := cmp.get(key)
if err != nil {
return nil, err
}
return pathErrors, nil
}

func (cmp *Comparer) GetTree(key TreeIndexKey) (*FileTree, error) {
//func (cmp *Comparer) GetTree(bottomTreeStart, bottomTreeStop, topTreeStart, topTreeStop int) (*FileTree, []PathError, error) {
//key := TreeIndexKey{bottomTreeStart, bottomTreeStop, topTreeStart, topTreeStop}

if value, exists := cmp.trees[key]; exists {
return value, nil
}

value, pathErrors, err := cmp.get(key)
if err != nil {
return nil, err
}
cmp.trees[key] = value
cmp.pathErrors[key] = pathErrors
return value, nil
}

func (cmp *Comparer) get(key TreeIndexKey) (*FileTree, []PathError, error) {
newTree, pathErrors, err := StackTreeRange(cmp.refTrees, key.bottomTreeStart, key.bottomTreeStop)
if err != nil {
return nil, nil, err
}
for idx := key.topTreeStart; idx <= key.topTreeStop; idx++ {
markPathErrors, err := newTree.CompareAndMark(cmp.refTrees[idx])
pathErrors = append(pathErrors, markPathErrors...)
if err != nil {
logrus.Errorf("error while building tree: %+v", err)
return nil, nil, err
}
}
return newTree, pathErrors, nil
}

// case 1: layer compare (top tree SIZE is fixed (BUT floats forward), Bottom tree SIZE changes)
func (cmp *Comparer) NaturalIndexes() <-chan TreeIndexKey {
indexes := make(chan TreeIndexKey)

go func() {
defer close(indexes)

var bottomTreeStart, bottomTreeStop, topTreeStart, topTreeStop int

for selectIdx := 0; selectIdx < len(cmp.refTrees); selectIdx++ {
bottomTreeStart = 0
topTreeStop = selectIdx

if selectIdx == 0 {
bottomTreeStop = selectIdx
topTreeStart = selectIdx
} else {
bottomTreeStop = selectIdx - 1
topTreeStart = selectIdx
}

indexes <- TreeIndexKey{
bottomTreeStart: bottomTreeStart,
bottomTreeStop: bottomTreeStop,
topTreeStart: topTreeStart,
topTreeStop: topTreeStop,
}
}
}()
return indexes

}

// case 2: aggregated compare (bottom tree is ENTIRELY fixed, top tree SIZE changes)
func (cmp *Comparer) AggregatedIndexes() <-chan TreeIndexKey {
indexes := make(chan TreeIndexKey)

go func() {
defer close(indexes)

var bottomTreeStart, bottomTreeStop, topTreeStart, topTreeStop int

for selectIdx := 0; selectIdx < len(cmp.refTrees); selectIdx++ {
bottomTreeStart = 0
topTreeStop = selectIdx
if selectIdx == 0 {
bottomTreeStop = selectIdx
topTreeStart = selectIdx
} else {
bottomTreeStop = 0
topTreeStart = 1
}

indexes <- TreeIndexKey{
bottomTreeStart: bottomTreeStart,
bottomTreeStop: bottomTreeStop,
topTreeStart: topTreeStart,
topTreeStop: topTreeStop,
}
}
}()
return indexes

}

func (cmp *Comparer) BuildCache() (errors []error) {
for index := range cmp.NaturalIndexes() {
pathError, _ := cmp.GetPathErrors(index)
if len(pathError) > 0 {
for _, path := range pathError {
errors = append(errors, fmt.Errorf("path error at layer index %s: %s", index, path))
}
}
_, err := cmp.GetTree(index)
if err != nil {
errors = append(errors, err)
return errors
}
}

for index := range cmp.AggregatedIndexes() {
_, err := cmp.GetTree(index)
if err != nil {
errors = append(errors, err)
return errors
}
}
return errors
}
7 changes: 6 additions & 1 deletion dive/filetree/efficiency.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ func Efficiency(trees []*FileTree) (float64, EfficiencySlice) {
sizeBytes += curNode.Data.FileInfo.Size
return nil
}
stackedTree, err := StackTreeRange(trees, 0, currentTree-1)
stackedTree, failedPaths, err := StackTreeRange(trees, 0, currentTree-1)
if len(failedPaths) > 0 {
for _, path := range failedPaths {
logrus.Errorf(path.String())
}
}
if err != nil {
logrus.Errorf("unable to stack tree range: %+v", err)
return err
Expand Down
Loading

0 comments on commit 35db1ec

Please sign in to comment.