Skip to content

Commit

Permalink
change deleter to path length sorted heap
Browse files Browse the repository at this point in the history
  • Loading branch information
rbtr committed Apr 5, 2020
1 parent e8203c9 commit 11efbb4
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions internal/plugin/output/deleter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ file, You can obtain one at https://mozilla.org/MPL/2.0/.
package output

import (
"container/list"
"container/heap"
"context"
"os"

Expand All @@ -29,21 +29,21 @@ func (*Deleter) Init(context.Context) error {
// Receive implements the Plugin interface on the Deleter
func (d *Deleter) Receive(c <-chan types.Item) {
log.Trace("started deleter output")
q := list.New()
h := &stringHeap{}
for m := range c {
log.Infof("deleter_output: received_input %#v", m)
if m.Delete {
log.Infof("deleter_output: queueing %s", m.SourcePath)
q.PushBack(m)
heap.Push(h, m.SourcePath)
}
}
for e := q.Front(); e != nil; e = e.Next() {
i := (e.Value).(types.Item)
log.Infof("deleter_output: deleting %s", i.SourcePath)
for h.Len() > 0 {
path := heap.Pop(h).(string)
log.Infof("deleter_output: deleting %s", path)
if d.DryRun {
continue
}
if err := os.Remove(i.SourcePath); err != nil {
if err := os.Remove(path); err != nil {
log.Error(err)
}
}
Expand All @@ -52,3 +52,21 @@ func (d *Deleter) Receive(c <-chan types.Item) {
func NewDeleter(dryRun bool) output.Output {
return &Deleter{DryRun: dryRun}
}

type stringHeap []string

func (h stringHeap) Len() int { return len(h) }
func (h stringHeap) Less(i, j int) bool { return len(h[i]) > len(h[j]) }
func (h stringHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }

func (h *stringHeap) Push(x interface{}) {
*h = append(*h, x.(string))
}

func (h *stringHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}

0 comments on commit 11efbb4

Please sign in to comment.