-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgc_queue.go
More file actions
38 lines (30 loc) · 939 Bytes
/
gc_queue.go
File metadata and controls
38 lines (30 loc) · 939 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main
import (
"os"
"time"
)
// queue is a list of files that can be deleted.
type queue []os.FileInfo
// deletionPriority determines the priority to be deleted.
// 1KB is considered the same as 1 second in this code.
// Therefore files that are 1MB larger will be deleted 1024 seconds or about 17 minutes earlier.
func (q queue) deletionPriority(i int) int {
const bytesPerSecond = 1024
return int(time.Since(q[i].ModTime()).Seconds()) + int(q[i].Size()/bytesPerSecond)
}
// === CODE BELOW THIS POINT MAKES queue IMPLEMENT container/Heap.Interface. ===
func (q queue) Len() int { return len(q) }
func (q queue) Less(i, j int) bool {
return q.deletionPriority(i) > q.deletionPriority(j)
}
func (q queue) Swap(i, j int) {
q[i], q[j] = q[j], q[i]
}
func (q *queue) Push(x interface{}) {
*q = append(*q, x.(os.FileInfo))
}
func (q *queue) Pop() interface{} {
res := (*q)[len(*q)-1]
*q = (*q)[:len(*q)-1]
return res
}