This repository has been archived by the owner on Mar 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
backend.go
115 lines (93 loc) · 2.74 KB
/
backend.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package backend
import (
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
type Backend interface {
// Lists all valid DBs (the first set of directories under the source root).
ListDBs() ([]string, error)
// ListVersions returns a sorted list of valid versions for the given db. If
// after is possed, only versions greater than it will be returned. If
// checkForSuccess is passed, only versions for which there is a _SUCCESS file
// present will be returned.
ListVersions(db, after string, checkForSuccessFile bool) ([]string, error)
// ListFiles returns a sorted list of all valid-looking data files for a db
// and version. It excludes files that begin with '_' or '.'.
ListFiles(db, version string) ([]string, error)
// Open returns an io.ReadCloser for a given file from a specific version
// of a db.
Open(db, version, file string) (io.ReadCloser, error)
// DisplayPath returns a human-readable path indicating where the backend
// is rooted.
DisplayPath(parts ...string) string
}
// A basic backend for the local filesystem
type LocalBackend struct {
path string
}
func NewLocalBackend(path string) *LocalBackend {
return &LocalBackend{path}
}
func (lb *LocalBackend) ListDBs() ([]string, error) {
files, err := ioutil.ReadDir(lb.path)
if err != nil {
return nil, err
}
var res []string
for _, f := range files {
if !f.IsDir() {
continue
}
res = append(res, f.Name())
}
return res, nil
}
func (lb *LocalBackend) ListVersions(db, after string, checkForSuccess bool) ([]string, error) {
files, err := ioutil.ReadDir(filepath.Join(lb.path, db))
if err != nil {
return nil, err
}
var res []string
for _, f := range files {
if !f.IsDir() {
continue
}
name := f.Name()
fullPath := filepath.Join(lb.path, db, name)
if name > after && (!checkForSuccess || lb.checkForSuccessFile(fullPath)) {
res = append(res, name)
}
}
return res, nil
}
func (lb *LocalBackend) ListFiles(db, version string) ([]string, error) {
infos, err := ioutil.ReadDir(filepath.Join(lb.path, db, version))
if err != nil {
return nil, err
}
var res []string
for _, info := range infos {
name := info.Name()
if !info.IsDir() && !strings.HasPrefix(name, "_") && !strings.HasPrefix(name, ".") {
res = append(res, filepath.Base(info.Name()))
}
}
return res, nil
}
func (lb *LocalBackend) Open(db, version, file string) (io.ReadCloser, error) {
return os.Open(filepath.Join(lb.path, db, version, file))
}
func (lb *LocalBackend) DisplayPath(parts ...string) string {
allParts := append([]string{lb.path}, parts...)
return filepath.Join(allParts...)
}
func (lb *LocalBackend) checkForSuccessFile(path string) bool {
if _, err := os.Stat(filepath.Join(path, "_SUCCESS")); err == nil {
return true
} else {
return false
}
}