-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathjson_db_index.go
More file actions
91 lines (85 loc) · 1.76 KB
/
json_db_index.go
File metadata and controls
91 lines (85 loc) · 1.76 KB
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
package metadata
import (
"errors"
"os"
"path"
"path/filepath"
"strings"
)
type jsonDBIndex struct {
layers []jsonLayer
}
func NewJSONDBIndex(layers []Layer) DBIndex {
jLayers := make([]jsonLayer, len(layers))
for i, layer := range layers {
jLayers[i] = layer2JsonLayer(layer)
}
return &jsonDBIndex{
layers: jLayers,
}
}
func (j *jsonDBIndex) Databases() ([]string, error) {
res := map[string]bool{}
for _, l := range j.layers {
ents, err := os.ReadDir(path.Join(l.Path))
if errors.Is(err, os.ErrNotExist) {
continue
}
if err != nil {
return nil, err
}
for _, ent := range ents {
if ent.IsDir() {
res[ent.Name()] = true
}
}
}
_res := make([]string, 0, len(res))
for k := range res {
_res = append(_res, k)
}
return _res, nil
}
func (j *jsonDBIndex) Tables(database string) ([]string, error) {
res := map[string]bool{}
for _, l := range j.layers {
ents, err := os.ReadDir(path.Join(l.Path, database))
if errors.Is(err, os.ErrNotExist) {
continue
}
if err != nil {
return nil, err
}
for _, ent := range ents {
if ent.IsDir() {
res[ent.Name()] = true
}
}
}
_res := make([]string, 0, len(res))
for k := range res {
_res = append(_res, k)
}
return _res, nil
}
func (j *jsonDBIndex) Paths(database string, table string) ([]string, error) {
res := map[string]bool{}
for _, l := range j.layers {
root := path.Join(l.Path, database, table)
filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
return nil
}
if strings.HasPrefix(info.Name(), "hour=") {
res[path[len(root)+1:]] = true
return filepath.SkipDir
}
return nil
})
}
_res := make([]string, 0, len(res))
for k := range res {
_res = append(_res, k)
}
return _res, nil
}