Skip to content

Commit

Permalink
add anonymous function for read + filter suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
redwrasse committed Mar 9, 2024
1 parent 64ce60c commit 7c60024
Showing 1 changed file with 17 additions and 45 deletions.
62 changes: 17 additions & 45 deletions client/pkg/fileutil/purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,47 +52,32 @@ func purgeFile(lg *zap.Logger, dirname string, suffix string, max uint, interval
zap.Uint("max", max),
zap.Duration("interval", interval))

readDirWithSuffix := func(dirname string) ([]string, error) {
fnames, err := ReadDir(dirname)
if err != nil {
return nil, err
}
// filter in place (ref. https://go.dev/wiki/SliceTricks#filtering-without-allocating)
fnamesWithSuffix := fnames[:0]
for _, fname := range fnames {
if strings.HasSuffix(fname, suffix) {
fnamesWithSuffix = append(fnamesWithSuffix, fname)
}
}

return fnamesWithSuffix, nil
}

go func() {
if donec != nil {
defer close(donec)
}
for {
// get all filenames in directory
// fnames is a slice of filename strings
// note ReadDir reads files already in sorted order,
// so could filter for suffixes and not have to sort again (maybe?)
// also ReadDir already has helper WithExt method could use?
// update: WithExt is slow
fnames, err := ReadDir(dirname)
fnamesWithSuffix, err := readDirWithSuffix(dirname)
if err != nil {
errC <- err
return
}

// filter in place (ref. https://go.dev/wiki/SliceTricks#filtering-without-allocating)
fnamesWithSuffix := fnames[:0]
for _, fname := range fnames {
if strings.HasSuffix(fname, suffix) {
fnamesWithSuffix = append(fnamesWithSuffix, fname)
}
}
//// filter for filenames that have given suffix?
//newfnames := make([]string, 0)
//for _, fname := range fnames {
// if strings.HasSuffix(fname, suffix) {
// newfnames = append(newfnames, fname)
// }
//}
// sort filenames with suffix
// why have to sort again? Aren't ReadDir results above already sorted, and sort preserved by suffix filter?
//sort.Strings(newfnames)
// assign fnames variable to the sorted filenames with suffix (why assign?)
//fnames = newfnames
// iterate over newfnames (why over newfnames if reassigned?)
// looks like fnames is read below to write to channel, while newfnames is modified
// over course of for loop
// todo: investigate if maybe less confusing say to do this than filtering, assigning to new
// variable, then reassigning to original variable, and using one read-only while modifying the other.
nPurged := 0
for nPurged < len(fnamesWithSuffix)-int(max) {
f := filepath.Join(dirname, fnamesWithSuffix[nPurged])
Expand All @@ -118,21 +103,8 @@ func purgeFile(lg *zap.Logger, dirname string, suffix string, max uint, interval
}
lg.Info("purged", zap.String("path", f))
nPurged++
//fnamesWithSuffix = fnamesWithSuffix[1:]
}

// at this point fnames is the newfnames slice as given before the for loop, and newfnames is newfnames advanced as many times as above for loop occurred (= max if completed all iterations, or less if one of the TryLockFile operations resulted in error).

// so we have:
// n := number of original filenames
// m := number of filenames with suffix
// k := number of above for loop iterations that completed without error
// mx := int(max), so for loop can run for at most m - mx iterations
// after for loop completes len(newfnames) is m - k.
// so len(fnames) - len(newfnames) = m - (m -k)
// so below operation writes m - (m - k) of filenames with suffix to channel = k filenames with suffix to channel
// so easier just to calculate k in above for loop, and not have separate fnames and newfnames variables?

if purgec != nil {
for i := 0; i < nPurged; i++ {
purgec <- fnamesWithSuffix[i]
Expand Down

0 comments on commit 7c60024

Please sign in to comment.