/
remove.go
75 lines (63 loc) · 1.33 KB
/
remove.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
package main
import (
"bufio"
"io/ioutil"
"os"
"path/filepath"
"github.com/segmentio/feature"
)
type removeConfig struct {
commonConfig
}
func remove(config removeConfig, group group, tier tier, collection collection, ids []id) error {
return config.mount(func(path feature.MountPoint) error {
t, err := path.OpenTier(string(group), string(tier))
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
defer t.Close()
c, err := t.OpenCollection(string(collection))
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
defer c.Close()
if len(ids) == 0 {
return nil
}
index := make(map[string]struct{}, len(ids))
for _, id := range ids {
index[string(id)] = struct{}{}
}
list := make([]string, 0, 100)
if err := feature.Scan(c.IDs(), func(id string) error {
if _, rm := index[id]; !rm {
list = append(list, id)
}
return nil
}); err != nil {
return err
}
filePath := c.Path()
f, err := ioutil.TempFile(filepath.Dir(filePath), "."+filepath.Base(filePath))
if err != nil {
return err
}
defer os.Remove(f.Name())
defer f.Close()
w := bufio.NewWriter(f)
for _, id := range list {
w.WriteString(id)
w.WriteString("\n")
}
if err := w.Flush(); err != nil {
return err
}
return os.Rename(f.Name(), filePath)
})
}