-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
93 lines (79 loc) · 1.74 KB
/
utils.go
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package cove
import (
"errors"
"fmt"
"os"
"path/filepath"
"time"
)
func Vacuum(interval time.Duration, max int) func(cache *Cache) {
return func(cache *Cache) {
vacuumNamespace := func(ns *Cache) int {
start := time.Now()
n, err := ns.Vacuum(max)
elapsed := time.Since(start)
if err != nil {
cache.log.Error("could not vacuum namespace", "err", err)
return 0
}
if cache.log != nil && n > 0 {
cache.log.Debug("[cove] vacuumed", "ns", ns.namespace, "time", elapsed, "n", n)
}
if cache.log != nil && n == 0 {
cache.log.Debug("[cove] vacuumed", "ns", ns.namespace, "time", elapsed, "n", n)
}
return n
}
tic := time.NewTicker(interval)
for {
select {
case <-tic.C:
case <-cache.closed:
cache.log.Info("[cove] vacuum stopping")
return
}
for _, namespace := range cache.namespaces {
start := time.Now()
for i := 0; i < 100; i++ {
n := vacuumNamespace(namespace)
if n == 0 {
break
}
elapsed := time.Since(start)
if elapsed > time.Second {
break
}
select {
case <-cache.closed:
cache.log.Info("[cove] vacuum stopping")
return
default:
}
}
}
}
}
}
func URITemp() string {
d := fmt.Sprintf("%d-cove", time.Now().Unix())
uri := filepath.Join(os.TempDir(), d, "cove.db")
os.MkdirAll(filepath.Dir(uri), 0755)
return fmt.Sprintf("file:%s?tmp=true", uri)
}
func URIFromPath(path string) string {
_ = os.MkdirAll(filepath.Dir(path), 0755)
return fmt.Sprintf("file:%s", path)
}
func Hit(err error) (bool, error) {
if err == nil {
return true, nil
}
if errors.Is(err, NotFound) {
return false, nil
}
return false, err
}
func Miss(err error) (bool, error) {
hit, err := Hit(err)
return !hit, err
}