-
Notifications
You must be signed in to change notification settings - Fork 2
/
walguardedfile.go
58 lines (47 loc) · 1.38 KB
/
walguardedfile.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
package wal
import (
"github.com/function61/eventhorizon/config"
"log"
"os"
"strings"
)
type WalGuardedFile struct {
nextFreePosition uint64
walSize uint64
fd *os.File
// This is original filename (not real filename)
fileNameFictional string // "/tenants/foo/_/0.log"
}
func WalGuardedFileOpen(fileName string) *WalGuardedFile {
fd, err := os.OpenFile(computeInternalPath(fileName), os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
panic(err)
}
stats, err := fd.Stat()
if err != nil {
panic(err)
}
return &WalGuardedFile{
nextFreePosition: uint64(stats.Size()),
fd: fd,
walSize: 0, // filled on WAL scanning
fileNameFictional: fileName,
}
}
// Returns the internal path by which the file actually resides in the filesystem.
//
// TODO: have Writer dictate a safe internal path for WAL usage and not do this
// ourselves.
func (w *WalGuardedFile) GetInternalRealPath() string {
return computeInternalPath(w.fileNameFictional)
}
func (w *WalGuardedFile) Close() error {
log.Printf("WalGuardedFile: closing %s", w.fileNameFictional)
return w.fd.Close()
}
// "/foo/bar.txt" => "/full/path-to-wal/_foo_bar.txt"
func computeInternalPath(fileName string) string {
// "/tenants/foo/_/0.log" => "_tenants_foo___0.log"
fileNameSafe := strings.Replace(fileName, "/", "_", -1)
return config.WalManagerDataDir + "/" + fileNameSafe
}