-
Notifications
You must be signed in to change notification settings - Fork 111
/
appender.go
109 lines (89 loc) · 2.32 KB
/
appender.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
// Copyright 2022 Linkall Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package wal
import (
// standard libraries.
"context"
"io"
// this project.
"github.com/vanus-labs/vanus/internal/store/wal/record"
)
func (w *WAL) newAppender(ctx context.Context, entries [][]byte, direct bool, callback AppendCallback) *appender {
return &appender{
w: w,
entries: entries,
ranges: make([]Range, len(entries)),
ctx: ctx,
direct: direct,
callback: callback,
}
}
type appender struct {
w *WAL
entries [][]byte
records []record.Record
padding int
i, j int
ranges []Range
ctx context.Context
direct bool
callback AppendCallback
}
// Make sure Data implements io.Reader.
var _ io.Reader = (*appender)(nil)
func (a *appender) invoke() {
a.w.appendWg.Add(1)
a.w.s.Append(a, a.onAppended)
if a.direct {
a.w.s.Sync()
}
// metrics.WALEntryWriteCounter.Add(float64(len(entries)))
// metrics.WALEntryWriteSizeCounter.Add(float64(entrySize))
// metrics.WALRecordWriteCounter.Add(float64(recordCount))
// metrics.WALRecordWriteSizeCounter.Add(float64(recordSize))
}
func (a *appender) Read(b []byte) (int, error) {
if a.j < len(a.records) {
n, err := a.records[a.j].MarshalTo(b)
a.j++
return n, err
}
if a.padding != 0 {
n := a.padding
a.padding = 0
return n, nil
}
if a.i != 0 {
a.ranges[a.i-1].EO += a.w.s.WriteOffset()
}
if a.i < len(a.entries) {
a.ranges[a.i].SO = a.w.s.WriteOffset()
a.records, a.padding = record.Pack(a.entries[a.i], len(b), a.w.blockSize)
a.ranges[a.i].EO = -int64(a.padding)
a.i++
a.j = 1
return a.records[0].MarshalTo(b)
}
// Release memory.
a.records = nil
a.entries = nil
return 0, io.EOF
}
func (a *appender) onAppended(_ int, err error) {
if err != nil {
panic(err)
}
a.callback(a.ranges, nil)
a.w.appendWg.Done()
}