/
message.go
225 lines (190 loc) · 5.18 KB
/
message.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
215
216
217
218
219
220
221
222
223
224
225
package topicwriterinternal
import (
"bytes"
"errors"
"fmt"
"io"
"time"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/grpcwrapper/rawtopic/rawtopiccommon"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/grpcwrapper/rawtopic/rawtopicwriter"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
)
var errNoRawContent = xerrors.Wrap(errors.New("ydb: internal state error - no raw message content"))
type PublicMessage struct {
SeqNo int64
CreatedAt time.Time
Data io.Reader
Metadata map[string][]byte
// partitioning at level message available by protocol, but doesn't available by current server implementation
// the field hidden from public access for prevent runtime errors.
// it will be published after implementation on server side.
futurePartitioning PublicFuturePartitioning
}
// PublicFuturePartitioning will be published in feature, after server implementation completed.
type PublicFuturePartitioning struct {
messageGroupID string
partitionID int64
hasPartitionID bool
}
func (p PublicFuturePartitioning) ToRaw() rawtopicwriter.Partitioning {
if p.hasPartitionID {
return rawtopicwriter.NewPartitioningPartitionID(p.partitionID)
}
return rawtopicwriter.NewPartitioningMessageGroup(p.messageGroupID)
}
func NewPartitioningWithMessageGroupID(id string) PublicFuturePartitioning {
return PublicFuturePartitioning{
messageGroupID: id,
}
}
func NewPartitioningWithPartitionID(id int64) PublicFuturePartitioning {
return PublicFuturePartitioning{
partitionID: id,
hasPartitionID: true,
}
}
type messageWithDataContent struct {
PublicMessage
dataWasRead bool
hasRawContent bool
hasEncodedContent bool
metadataCached bool
bufCodec rawtopiccommon.Codec
bufEncoded bytes.Buffer
rawBuf bytes.Buffer
encoders *EncoderMap
BufUncompressedSize int
}
func (m *messageWithDataContent) GetEncodedBytes(codec rawtopiccommon.Codec) ([]byte, error) {
if codec == rawtopiccommon.CodecRaw {
return m.getRawBytes()
}
return m.getEncodedBytes(codec)
}
func (m *messageWithDataContent) cacheMetadata() {
if m.metadataCached {
return
}
// ensure message metadata can't be changed by external code
if len(m.Metadata) > 0 {
ownCopy := make(map[string][]byte, len(m.Metadata))
for key, val := range m.Metadata {
ownCopy[key] = bytes.Clone(val)
}
m.Metadata = ownCopy
} else {
m.Metadata = nil
}
m.metadataCached = true
}
func (m *messageWithDataContent) CacheMessageData(codec rawtopiccommon.Codec) error {
m.cacheMetadata()
_, err := m.GetEncodedBytes(codec)
return err
}
func (m *messageWithDataContent) encodeRawContent(codec rawtopiccommon.Codec) ([]byte, error) {
if !m.hasRawContent {
return nil, xerrors.WithStackTrace(errNoRawContent)
}
m.bufEncoded.Reset()
writer, err := m.encoders.CreateLazyEncodeWriter(codec, &m.bufEncoded)
if err != nil {
return nil, xerrors.WithStackTrace(xerrors.Wrap(fmt.Errorf(
"ydb: failed create encoder for message, codec '%v': %w",
codec,
err,
)))
}
_, err = writer.Write(m.rawBuf.Bytes())
if err == nil {
err = writer.Close()
}
if err != nil {
return nil, xerrors.WithStackTrace(xerrors.Wrap(fmt.Errorf(
"ydb: failed to compress message, codec '%v': %w",
codec,
err,
)))
}
m.bufCodec = codec
return m.bufEncoded.Bytes(), nil
}
func (m *messageWithDataContent) readDataToRawBuf() error {
m.rawBuf.Reset()
m.hasRawContent = true
if m.Data != nil {
writtenBytes, err := io.Copy(&m.rawBuf, m.Data)
if err != nil {
return xerrors.WithStackTrace(err)
}
m.BufUncompressedSize = int(writtenBytes)
m.Data = nil
}
return nil
}
func (m *messageWithDataContent) readDataToTargetCodec(codec rawtopiccommon.Codec) error {
m.dataWasRead = true
m.hasEncodedContent = true
m.bufCodec = codec
m.bufEncoded.Reset()
encoder, err := m.encoders.CreateLazyEncodeWriter(codec, &m.bufEncoded)
if err != nil {
return err
}
reader := m.Data
if reader == nil {
reader = &bytes.Reader{}
}
bytesCount, err := io.Copy(encoder, reader)
if err == nil {
err = encoder.Close()
}
if err != nil {
return xerrors.WithStackTrace(xerrors.Wrap(fmt.Errorf(
"ydb: failed compress message with codec '%v': %w",
codec,
err,
)))
}
m.BufUncompressedSize = int(bytesCount)
m.Data = nil
return nil
}
func (m *messageWithDataContent) getRawBytes() ([]byte, error) {
if m.hasRawContent {
return m.rawBuf.Bytes(), nil
}
if m.dataWasRead {
return nil, xerrors.WithStackTrace(errNoRawContent)
}
err := m.readDataToRawBuf()
if err != nil {
return nil, err
}
return m.rawBuf.Bytes(), nil
}
func (m *messageWithDataContent) getEncodedBytes(codec rawtopiccommon.Codec) ([]byte, error) {
switch {
case m.hasEncodedContent && m.bufCodec == codec:
return m.bufEncoded.Bytes(), nil
case m.hasRawContent:
return m.encodeRawContent(codec)
case m.dataWasRead:
return nil, errNoRawContent
default:
err := m.readDataToTargetCodec(codec)
if err != nil {
return nil, err
}
return m.bufEncoded.Bytes(), nil
}
}
func newMessageDataWithContent(
message PublicMessage, //nolint:gocritic
encoders *EncoderMap,
) messageWithDataContent {
return messageWithDataContent{
PublicMessage: message,
encoders: encoders,
}
}