forked from influxdata/influxdb
/
tombstone.go
142 lines (114 loc) · 2.81 KB
/
tombstone.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package tsm1
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
)
type Tombstoner struct {
mu sync.Mutex
// Path is the location of the file to record tombstone. This should be the
// full path to a TSM file.
Path string
}
func (t *Tombstoner) Add(keys []string) error {
t.mu.Lock()
defer t.mu.Unlock()
// If this TSMFile has not been written (mainly in tests), don't write a
// tombstone because the keys will not be written when it's actually saved.
if t.Path == "" {
return nil
}
tombstones, err := t.readTombstone()
if err != nil {
return nil
}
for _, k := range keys {
tombstones = append(tombstones, k)
}
return t.writeTombstone(tombstones)
}
func (t *Tombstoner) ReadAll() ([]string, error) {
return t.readTombstone()
}
func (t *Tombstoner) Delete() error {
t.mu.Lock()
defer t.mu.Unlock()
if err := os.RemoveAll(t.tombstonePath()); err != nil {
return err
}
return nil
}
// HasTombstones return true if there are any tombstone entries recorded.
func (t *Tombstoner) HasTombstones() bool {
stat, err := os.Stat(t.tombstonePath())
if err != nil {
return false
}
return stat.Size() > 0
}
// TombstoneFiles returns any tombstone files associated with this TSM file.
func (t *Tombstoner) TombstoneFiles() []FileStat {
stat, err := os.Stat(t.tombstonePath())
if err != nil {
return nil
}
if stat.Size() > 0 {
return []FileStat{FileStat{
Path: stat.Name(),
LastModified: stat.ModTime().UnixNano(),
Size: uint32(stat.Size())}}
}
return nil
}
func (t *Tombstoner) writeTombstone(tombstones []string) error {
tmp, err := ioutil.TempFile(filepath.Dir(t.Path), "tombstone")
if err != nil {
return err
}
defer tmp.Close()
if _, err := tmp.Write([]byte(strings.Join(tombstones, "\n"))); err != nil {
return err
}
// fsync the file to flush the write
if err := tmp.Sync(); err != nil {
return err
}
tmpFilename := tmp.Name()
tmp.Close()
if err := os.Rename(tmpFilename, t.tombstonePath()); err != nil {
return err
}
return syncDir(filepath.Dir(t.tombstonePath()))
}
func (t *Tombstoner) readTombstone() ([]string, error) {
var b []byte
tf, err := os.Open(t.tombstonePath())
defer tf.Close()
if !os.IsNotExist(err) {
b, err = ioutil.ReadAll(tf)
if err != nil {
return nil, err
}
}
lines := strings.TrimSpace(string(b))
if lines == "" {
return nil, nil
}
return strings.Split(string(b), "\n"), nil
}
func (t *Tombstoner) tombstonePath() string {
if strings.HasSuffix(t.Path, "tombstone") {
return t.Path
}
// Filename is 0000001.tsm1
filename := filepath.Base(t.Path)
// Strip off the tsm1
ext := filepath.Ext(filename)
if ext != "" {
filename = strings.TrimSuffix(filename, ext)
}
// Append the "tombstone" suffix to create a 0000001.tombstone file
return filepath.Join(filepath.Dir(t.Path), filename+".tombstone")
}