-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsealer.go
More file actions
146 lines (123 loc) · 3.36 KB
/
Copy pathsealer.go
File metadata and controls
146 lines (123 loc) · 3.36 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
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
143
144
145
146
package sealing
import (
"errors"
"os"
"path/filepath"
"github.com/ozontech/seq-db/consts"
"github.com/ozontech/seq-db/frac/common"
"github.com/ozontech/seq-db/frac/sealed"
"github.com/ozontech/seq-db/indexwriter"
"github.com/ozontech/seq-db/util"
)
// Source defines the contract for data sources that can be sealed.
// Provides access to all necessary data components for index creation.
type Source = indexwriter.Source
// Seal writes five index files (.info, .token, .offsets, .id, .lid) for the fraction
// and returns PreloadedData for fast initialization of the sealed fraction.
func Seal(src Source, params common.SealParams) (*sealed.PreloadedData, error) {
info := src.Info()
info.ConstRegularBlockSize = params.TokenBlockSize
if info.ConstRegularBlockSize == 0 {
info.ConstRegularBlockSize = consts.RegularBlockSize
}
if info.To == 0 {
return nil, errors.New("sealing of an empty active fraction is not supported")
}
w := indexwriter.New(params)
if err := createAndWrite(
info.Path+consts.OffsetsTmpFileSuffix,
info.Path+consts.OffsetsFileSuffix,
func(f *os.File) error { return w.WriteOffsetsFile(f, src) },
); err != nil {
return nil, err
}
if err := createAndWrite(
info.Path+consts.IDTmpFileSuffix,
info.Path+consts.IDFileSuffix,
func(f *os.File) error { return w.WriteIDFile(f, src) },
); err != nil {
return nil, err
}
if err := createAndWriteBoth(
info.Path+consts.TokenTmpFileSuffix, info.Path+consts.TokenFileSuffix,
info.Path+consts.LIDTmpFileSuffix, info.Path+consts.LIDFileSuffix,
func(tokenF, lidF *os.File) error { return w.WriteTokenTriplet(tokenF, lidF, src) },
); err != nil {
return nil, err
}
if err := createAndWrite(
info.Path+consts.InfoTmpFileSuffix,
info.Path+consts.InfoFileSuffix,
func(f *os.File) error { return w.WriteInfoFile(f, src) },
); err != nil {
return nil, err
}
util.MustSyncPath(filepath.Dir(info.Path))
// Compute total index size as sum of all 5 files.
var totalSize uint64
for _, suffix := range []string{
consts.InfoFileSuffix,
consts.TokenFileSuffix,
consts.OffsetsFileSuffix,
consts.IDFileSuffix,
consts.LIDFileSuffix,
} {
st, err := os.Stat(info.Path + suffix)
if err != nil {
return nil, err
}
totalSize += uint64(st.Size())
}
info.IndexOnDisk = totalSize
lidsTable := w.LIDsTable()
preloaded := &sealed.PreloadedData{
Info: info,
TokenTable: w.TokenTable(),
BlocksData: sealed.BlocksData{
IDsTable: w.IDsTable(),
LIDsTable: &lidsTable,
BlocksOffsets: src.BlockOffsets(),
},
}
return preloaded, nil
}
func syncAndClose(f *os.File) error {
if err := f.Sync(); err != nil {
f.Close()
return err
}
return f.Close()
}
func createAndWrite(tmp, final string, write func(*os.File) error) error {
f, err := os.Create(tmp)
if err != nil {
return err
}
if err := errors.Join(write(f), syncAndClose(f)); err != nil {
return err
}
return os.Rename(tmp, final)
}
func createAndWriteBoth(
atmp, afinal,
btmp, bfinal string,
write func(*os.File, *os.File) error,
) error {
a, err := os.Create(atmp)
if err != nil {
return err
}
b, err := os.Create(btmp)
if err != nil {
a.Close()
return err
}
writeErr := write(a, b)
if err := errors.Join(writeErr, syncAndClose(a), syncAndClose(b)); err != nil {
return err
}
if err := os.Rename(atmp, afinal); err != nil {
return err
}
return os.Rename(btmp, bfinal)
}