[NebulousLabs/Sia] Add clear download history methods and endpoints (#3084)

ChrisSchinnerl commented on this pull request.

> + r.downloadHistoryMu.Lock()
+ defer r.downloadHistoryMu.Unlock()
+ r.downloadHistory = r.downloadHistory[:0]
+ return nil
+// ClearDownloadHistoryAfter clears the download history after
+// the given timestamp
+func (r *Renter) ClearDownloadHistoryAfter(timestamp int64) error {
+ if err := r.tg.Add(); err != nil {
+ return err
+ }
+ defer r.tg.Done()
+ r.downloadHistoryMu.Lock()
+ defer r.downloadHistoryMu.Unlock()
+ i := sort.Search(len(r.downloadHistory), func(i int) bool { return r.downloadHistory[i].staticStartTime.UnixNano() == timestamp })

Because the binary search looks at an element in the middle of the slice and uses the return value of `f` to determine if the element that we search for is in the left or right subslice. Then it looks at an element in the middle of the subslice and repeats the previous step until it finds the element with the smallest index i for which `f(i) == true`.
In our case no 2 elements should have the same timestamp and therefore it will always find the correct entry.

Добавить комментарий