-
Notifications
You must be signed in to change notification settings - Fork 8
/
utils.go
62 lines (51 loc) · 1.16 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
package barrel
import (
"fmt"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
)
// Exists returns true if the given path exists on the filesystem.
func exists(path string) bool {
if _, err := os.Stat(path); err != nil {
return false
}
return true
}
// getDataFiles returns the list of db files in a given directory.
func getDataFiles(dir string) ([]string, error) {
files, err := filepath.Glob(fmt.Sprintf("%s/*.db", dir))
if err != nil {
return nil, err
}
return files, nil
}
// getIDs return the sorted list of IDs extracted from the list of filenames.
func getIDs(files []string) ([]int, error) {
ids := make([]int, 0)
for _, f := range files {
id, err := strconv.ParseInt((strings.TrimPrefix(strings.TrimSuffix(filepath.Base(f), ".db"), "barrel_")), 10, 32)
if err != nil {
return nil, err
}
ids = append(ids, int(id))
}
// Sort in increasing order.
sort.Ints(ids)
return ids, nil
}
// validateKV validates key/value before inserting.
func validateKV(k string, val []byte) error {
if len(k) == 0 {
return ErrEmptyKey
}
if len(k) > MaxKeySize {
return ErrLargeKey
}
if len(val) > MaxValueSize {
return ErrLargeValue
}
return nil
}