-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmanifest_edit.go
165 lines (139 loc) · 3.41 KB
/
manifest_edit.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package bitcask
import (
"bytes"
"encoding/binary"
"errors"
)
const (
manifestEditDeleteFileTag = 1
manifestEditAddFileTag = 2
manifestEditNextFidTag = 3
manifestEditFreeBytesTag = 4
)
var (
ErrCorruptedManifest = errors.New("corrupted manifest file")
ErrUnknownManifestEditTag = errors.New("unknown manifest tag")
)
type LogFile struct {
wal *Wal
fid uint64
}
// the manifest edit will be persist one by one, and append to
// MANIFEST file
type ManifestEdit struct {
// the delete list of wal file
deleteFiles []LogFile
// the add list of wal file
addFiles []LogFile
// free bytes for each wal file
freeBytes map[uint64]uint64
// the available file number
nextFid uint64
hasNextFid bool
}
func NewManifestEdit() *ManifestEdit {
return &ManifestEdit{
hasNextFid: false,
freeBytes: make(map[uint64]uint64),
}
}
func (edit *ManifestEdit) Merge(other *ManifestEdit) {
if len(other.addFiles) > 0 {
edit.addFiles = append(edit.addFiles, other.addFiles...)
}
if len(other.deleteFiles) > 0 {
edit.deleteFiles = append(edit.deleteFiles, other.deleteFiles...)
}
if other.hasNextFid {
edit.hasNextFid = true
edit.nextFid = max(edit.nextFid, other.nextFid)
}
if len(other.freeBytes) > 0 {
for fid, freeBytes := range other.freeBytes {
edit.freeBytes[fid] += freeBytes
}
}
}
func (edit *ManifestEdit) Encode() []byte {
var buf bytes.Buffer
encodeVarint := func(v uint64) {
var tmp [binary.MaxVarintLen64]byte
n := binary.PutUvarint(tmp[:], v)
buf.Write(tmp[:n])
}
if edit.hasNextFid {
encodeVarint(manifestEditNextFidTag)
encodeVarint(edit.nextFid)
}
for _, file := range edit.addFiles {
encodeVarint(manifestEditAddFileTag)
encodeVarint(file.fid)
}
for _, file := range edit.deleteFiles {
encodeVarint(manifestEditDeleteFileTag)
encodeVarint(file.fid)
}
for fid, freeBytes := range edit.freeBytes {
encodeVarint(manifestEditFreeBytesTag)
encodeVarint(fid)
encodeVarint(freeBytes)
}
return buf.Bytes()
}
func (edit *ManifestEdit) Clear() {
edit.nextFid = 0
edit.hasNextFid = false
edit.addFiles = nil
edit.deleteFiles = nil
edit.freeBytes = make(map[uint64]uint64)
}
func (edit *ManifestEdit) DecodeFrom(data []byte) error {
offset := 0
edit.Clear()
for offset < len(data) {
tag, nbytes := binary.Uvarint(data[offset:])
if nbytes <= 0 {
return ErrCorruptedManifest
}
offset += nbytes
switch tag {
case manifestEditDeleteFileTag:
fid, nbytes := binary.Uvarint(data[offset:])
if nbytes <= 0 {
return ErrCorruptedManifest
}
offset += nbytes
edit.deleteFiles = append(edit.deleteFiles, LogFile{fid: fid})
case manifestEditAddFileTag:
fid, nbytes := binary.Uvarint(data[offset:])
if nbytes <= 0 {
return ErrCorruptedManifest
}
offset += nbytes
edit.addFiles = append(edit.addFiles, LogFile{fid: fid})
case manifestEditNextFidTag:
fid, nbytes := binary.Uvarint(data[offset:])
if nbytes <= 0 {
return ErrCorruptedManifest
}
offset += nbytes
edit.hasNextFid = true
edit.nextFid = max(edit.nextFid, fid)
case manifestEditFreeBytesTag:
fid, nbytes := binary.Uvarint(data[offset:])
if nbytes <= 0 {
return ErrCorruptedManifest
}
offset += nbytes
freeBytes, nbytes := binary.Uvarint(data[offset:])
if nbytes <= 0 {
return ErrCorruptedManifest
}
offset += nbytes
edit.freeBytes[fid] += freeBytes
default:
return ErrUnknownManifestEditTag
}
}
return nil
}