-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
214 lines (194 loc) · 4.29 KB
/
writer.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package cdb
import (
"bytes"
"encoding/binary"
"errors"
"io"
"os"
"strconv"
)
var ErrCdbWriteBiggerThan4GB = errors.New("cdb: file size is limited to 4GB")
const limit4GB = 1<<32 - 1
type CdbWriter struct {
f *os.File // 1
b *bytes.Buffer // 2
htables [entryTableSize][]slot
recordNum uint32
pos uint32
isFreezed bool
}
func NewFileWriter(name string) (m *CdbWriter, err error) {
f, err := os.Create(name)
if err != nil {
return nil, err
}
if _, err = f.Seek(int64(headerSize), 0); err != nil {
_ = f.Close()
return nil, err
}
return &CdbWriter{
f: f,
b: nil,
htables: [entryTableSize][]slot{},
pos: headerSize,
}, nil
}
func NewMemoryWriter() (m *CdbWriter) {
return &CdbWriter{
f: nil,
b: bytes.NewBuffer(make([]byte, headerSize)),
htables: [entryTableSize][]slot{},
pos: headerSize,
}
}
func (this *CdbWriter) WriteKeyValue(key []byte, data []byte) (err error) {
if this.isFreezed {
return this.invalidFreezeState()
}
if uint64(this.GetByteNum())+uint64(len(key))+uint64(len(data))+24 > limit4GB {
return ErrCdbWriteBiggerThan4GB
}
klen, dlen := uint32(len(key)), uint32(len(data))
w := this.getWriter()
err = writeNums(w, klen, dlen)
if err != nil {
return err
}
_, err = w.Write(key)
if err != nil {
return err
}
_, err = w.Write(data)
if err != nil {
return err
}
h := checksum(key)
tableNum := h % entryTableSize
this.htables[tableNum] = append(this.htables[tableNum], slot{h, this.pos})
this.pos += 8 + klen + dlen
this.recordNum++
return nil
}
func (this *CdbWriter) GetByteNum() uint32 {
if this.isFreezed {
return this.pos
}
return this.pos + this.recordNum*16
}
func (this *CdbWriter) getWriter() io.Writer {
if this.f != nil {
return this.f
}
return this.b
}
func (this *CdbWriter) Freeze() (db *Cdb, err error) {
if this.isFreezed {
return nil, this.invalidFreezeState()
}
this.isFreezed = true
maxSlots := 0
for _, slots := range this.htables {
if len(slots) > maxSlots {
maxSlots = len(slots)
}
}
slotTable := make([]slot, maxSlots*2)
header := make([]byte, headerSize)
// Write hashValue tables.
for i, slots := range this.htables {
if slots == nil {
putNum(header[i*8:], this.pos)
continue
}
nslots := uint32(len(slots) * 2)
hashSlotTable := slotTable[:nslots]
// Reset table slots.
for j := 0; j < len(hashSlotTable); j++ {
hashSlotTable[j].h = 0
hashSlotTable[j].pos = 0
}
for _, slot := range slots {
slotPos := (slot.h / entryTableSize) % nslots
for hashSlotTable[slotPos].pos != 0 {
slotPos++
if slotPos == uint32(len(hashSlotTable)) {
slotPos = 0
}
}
hashSlotTable[slotPos] = slot
}
if err = writeSlots(this.getWriter(), hashSlotTable); err != nil {
return nil, err
}
putNum(header[i*8:], this.pos)
putNum(header[i*8+4:], nslots)
this.pos += 8 * nslots
}
if this.f != nil {
_, err = this.f.WriteAt(header, 0)
if err != nil {
return nil, err
}
err = this.f.Sync()
if err != nil {
return nil, err
}
return OpenReaderAt(this.f)
}
copy(this.b.Bytes(), header)
return OpenReaderAt(bytes.NewReader(this.b.Bytes()))
}
func (this *CdbWriter) Close() (err error) {
if !this.isFreezed {
_, err = this.Freeze()
if err != nil {
if this.f != nil {
_ = this.f.Close()
}
return err
}
}
if this.f != nil {
return this.f.Close()
}
return nil
}
func (this *CdbWriter) GetMemoryWriterBytes() (bs []byte, err error) {
if this.b == nil {
return nil, errors.New("cdb: GetMemoryWriterBytes() only support memory writer")
}
if !this.isFreezed {
_, err = this.Freeze()
if err != nil {
return nil, err
}
}
return this.b.Bytes(), nil
}
func putNum(buf []byte, x uint32) {
binary.LittleEndian.PutUint32(buf, x)
}
func writeNums(w io.Writer, x, y uint32) (err error) {
var buf [8]byte
putNum(buf[:], x)
putNum(buf[4:], y)
_, err = w.Write(buf[:])
return err
}
type slot struct {
h, pos uint32
}
func writeSlots(w io.Writer, slots []slot) (err error) {
var buf [8]byte
for _, np := range slots {
putNum(buf[:], np.h)
putNum(buf[4:], np.pos)
if _, err = w.Write(buf[:8]); err != nil {
return err
}
}
return nil
}
func (this *CdbWriter) invalidFreezeState() error {
return errors.New("cdb: invalid freeze state: " + strconv.FormatBool(this.isFreezed))
}