/
atomic.go
139 lines (106 loc) · 3.12 KB
/
atomic.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
// Copyright 2021 Dataptive SAS.
//
// 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 recio
import (
"encoding/binary"
"hash/crc32"
)
var castagnoliTable = crc32.MakeTable(crc32.Castagnoli)
// AtomicReader provides atomicity guarantees on records read from unreliable
// channels and storage media. It does so by wrapping a Reader and checking
// on each Read that the record's checksum matches its data.
type AtomicReader struct {
reader Reader
wrapper atomicWrapper
}
// NewAtomicReader creates a new AtomicReader wrapping the provided Reader.
func NewAtomicReader(r Reader) (ar *AtomicReader) {
ar = &AtomicReader{
reader: r,
wrapper: atomicWrapper{},
}
return ar
}
// Read reads a record from the wrapped Reader and checks its trailing CRC32-C.
// If the check fails, Read returns with err == ErrCorrupt.
func (ar *AtomicReader) Read(v Decoder) (n int, err error) {
ar.wrapper.decoder = v
n, err = ar.reader.Read(&ar.wrapper)
if err != nil {
return n, err
}
return n, nil
}
// AtomicWriter provides atomicity guarantees on records written to unreliable
// channels and storage media. It does so by wrapping a Writer and appending
// checksums to records on write.
type AtomicWriter struct {
writer Writer
wrapper atomicWrapper
}
// NewAtomicWriter returns a new AtomicWriter wrapping the provided Writer.
func NewAtomicWriter(w Writer) (aw *AtomicWriter) {
aw = &AtomicWriter{
writer: w,
wrapper: atomicWrapper{},
}
return aw
}
// Write writes a record to the wrapped writer and appends a CRC32-C as a
// trailer.
func (aw *AtomicWriter) Write(v Encoder) (n int, err error) {
aw.wrapper.encoder = v
n, err = aw.writer.Write(&aw.wrapper)
if err != nil {
return n, err
}
return n, nil
}
// atomicWrapper wraps an encoder or a decoder and implements checksum
// appending and checking logic.
type atomicWrapper struct {
encoder Encoder
decoder Decoder
}
// Encode encodes a record and appends a CRC32-C at its end.
func (w *atomicWrapper) Encode(p []byte) (n int, err error) {
n, err = w.encoder.Encode(p)
if err != nil {
return n, err
}
if len(p) < n+4 {
return 0, ErrShortBuffer
}
crc := crc32.Checksum(p[:n], castagnoliTable)
binary.BigEndian.PutUint32(p[n:n+4], crc)
n += 4
return n, nil
}
// Decode decodes a record and checks the CRC32-C at its end.
func (w *atomicWrapper) Decode(p []byte) (n int, err error) {
n, err = w.decoder.Decode(p)
if err != nil {
return n, err
}
if len(p) < n+4 {
return 0, ErrShortBuffer
}
expected := crc32.Checksum(p[:n], castagnoliTable)
crc := binary.BigEndian.Uint32(p[n : n+4])
n += 4
if expected != crc {
return n, ErrCorrupt
}
return n, nil
}