-
Notifications
You must be signed in to change notification settings - Fork 0
/
compact.go
85 lines (67 loc) · 1.48 KB
/
compact.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
package unix_xfs
import (
"errors"
"github.com/yottaStore/golang/utils/block"
"github.com/yottaStore/golang/utils/utils"
"golang.org/x/sys/unix"
)
func compact(record string, d *IODriver) error {
// Read body
rb, err := read_body(record, d)
if err != nil {
return err
}
// Read tails
tails, tsize, err := read_tails(record, d)
if err != nil {
return err
}
ab, err := read_appends(record, tails, d)
if err != nil {
return err
}
rb = append(rb, ab...)
newBodyPath := d.Data + record + "/body." + utils.RandString(8)
fd, err := unix.Open(newBodyPath, CreateOpts, 0766)
defer close_fd(fd)
if err != nil {
return err
}
buff, err := block.Serialize(rb, block.BodyType, 0)
if err != nil {
return err
}
_, err = unix.Write(fd, buff)
if err != nil {
return err
}
// TODO: check for lock
tailsPath := d.Data + record + "/tails"
opts := AppendOpts
tfd, err := unix.Open(tailsPath, opts, 0766)
defer close_fd(tfd)
if err != nil {
return errors.New("error opening tails: " + err.Error())
}
err = unix.Rename(newBodyPath, d.Data+record+"/body")
if err != nil {
return err
}
b, err := block.Serialize([]byte{}, block.SkipType, 0)
if err != nil {
return err
}
_, err = unix.Write(tfd, b)
err = unix.Fallocate(tfd, unix.FALLOC_FL_COLLAPSE_RANGE, 0, tsize)
//err = unix.Ftruncate(tfd, tsize)
if err != nil {
return err
}
for _, tail := range tails {
err = unix.Unlink(string(tail.Pointer))
if err != nil {
return err
}
}
return nil
}