/
record.go
287 lines (258 loc) · 6.42 KB
/
record.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright 2017 The go-hep Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package sio
import (
"bytes"
"fmt"
"io"
"reflect"
)
// recordHeader describes the on-disk record (header part)
type recordHeader struct {
Len uint32
Typ uint32
}
// recordData describes the on-disk record (payload part)
type recordData struct {
Options uint32
DataLen uint32 // length of compressed record data
UCmpLen uint32 // length of uncompressed record data
NameLen uint32 // length of record name
}
// Record manages blocks of data
type Record struct {
name string // record name
unpack bool // whether to unpack incoming records
options uint32 // options (flag word)
bindex map[string]int // index of connected blocks
bnames []string // connected blocks names
blocks []Block // connected blocks
}
// Name returns the name of this record
func (rec *Record) Name() string {
return rec.name
}
// Unpack returns whether to unpack incoming records
func (rec *Record) Unpack() bool {
return rec.unpack
}
// SetUnpack sets whether to unpack incoming records
func (rec *Record) SetUnpack(unpack bool) {
rec.unpack = unpack
}
// Compress returns the compression flag
func (rec *Record) Compress() bool {
return rec.options&optCompress != 0
}
// SetCompress sets or resets the compression flag
func (rec *Record) SetCompress(compress bool) {
rec.options &= optNotCompress
if compress {
rec.options |= optCompress
}
}
// Options returns the options of this record.
func (rec *Record) Options() uint32 {
return rec.options
}
// Disconnect disconnects all blocks previously connected to this
// Record (for reading or writing.)
func (rec *Record) Disconnect() {
rec.bnames = rec.bnames[:0]
rec.bindex = make(map[string]int)
rec.blocks = rec.blocks[:0]
}
// Connect connects a Block to this Record (for reading or writing)
func (rec *Record) Connect(name string, ptr interface{}) error {
var err error
iblk, ok := rec.bindex[name]
if !ok {
iblk = len(rec.blocks)
rec.bnames = append(rec.bnames, name)
rec.blocks = append(rec.blocks, nil)
rec.bindex[name] = iblk
//return fmt.Errorf("sio.Record: Block name [%s] already connected", name)
//return ErrBlockConnected
}
var block Block
switch ptr := ptr.(type) {
case Block:
block = ptr
case Codec:
rt := reflect.TypeOf(ptr)
var vers uint32
if ptr, ok := ptr.(Versioner); ok {
vers = ptr.VersionSio()
}
block = &userBlock{
blk: ptr,
version: vers,
name: rt.Name(),
}
default:
rt := reflect.TypeOf(ptr)
if rt.Kind() != reflect.Ptr {
return fmt.Errorf("sio: Connect needs a pointer to a block of data")
}
var vers uint32
if ptr, ok := ptr.(Versioner); ok {
vers = ptr.VersionSio()
}
block = &genericBlock{
rt: rt,
rv: reflect.ValueOf(ptr),
version: vers,
name: rt.Name(),
}
}
rec.blocks[iblk] = block
return err
}
// read reads a record
func (rec *Record) read(r *reader) error {
var err error
// fmt.Printf("::: reading record [%s]... [%d]\n", rec.name, r.Len())
type fixlink struct {
link Linker
vers uint32
}
var linkers []fixlink
// loop until data has been depleted
for r.Len() > 0 {
beg := r.Len()
// read block header
var hdr blockHeader
err = bread(r, &hdr)
if err != nil {
return err
}
if hdr.Typ != blkMarker {
// fmt.Printf("*** err record[%s]: noblockmarker\n", rec.name)
return ErrRecordNoBlockMarker
}
var data blockData
err = bread(r, &data)
if err != nil {
return err
}
r.ver = data.Version
var cbuf bytes.Buffer
nlen := align4U32(data.NameLen)
n, err := io.CopyN(&cbuf, r, int64(nlen))
if err != nil {
// fmt.Printf(">>> err:%v\n", err)
return err
}
if n != int64(nlen) {
return ErrBlockShortRead
}
name := string(cbuf.Bytes()[:data.NameLen])
iblk, ok := rec.bindex[name]
if ok {
blk := rec.blocks[iblk]
// fmt.Printf("### %q\n", string(buf.Bytes()))
err = blk.UnmarshalSio(r)
end := r.Len()
if err != nil {
// fmt.Printf("*** error unmarshaling record=%q block=%q: %v\n", rec.name, name, err)
return err
}
if beg-end != int(hdr.Len) {
/*
if true {
var typ interface{}
switch blk := blk.(type) {
case *userBlock:
typ = blk.blk
case *genericBlock:
typ = blk.rv.Interface()
}
log.Printf("record %q block %q (%T) (beg-end=%d-%d=%d != %d)", rec.Name(), name, typ, beg, end, beg-end, int(hdr.Len))
} else {
*/
return ErrBlockShortRead
}
// fmt.Printf(">>> read record=%q block=%q (buf=%d)\n", rec.name, name, buf.Len())
if ublk, ok := blk.(*userBlock); ok {
if link, ok := ublk.blk.(Linker); ok {
linkers = append(linkers, fixlink{link, data.Version})
}
}
}
// check whether there is still something to be read.
// if there is, check whether there is a block-marker
if r.Len() > 0 {
next := bytes.Index(r.Bytes(), blkMarkerBeg)
if next > 0 {
pos := next - 4 // sizeof mark-block
r.Next(pos) // drain the buffer until next block
} else {
// drain the whole buffer
r.Next(r.Len())
}
}
}
r.relocate()
for _, fix := range linkers {
err = fix.link.LinkSio(fix.vers)
if err != nil {
return err
}
}
// fmt.Printf("::: reading record [%s]... [done]\n", rec.name)
return err
}
func (rec *Record) write(w *writer) error {
var (
err error
work = make([]byte, 16*1024*1024)
)
for i, k := range rec.bnames {
blk := rec.blocks[i]
bhdr := blockHeader{
Typ: blkMarker,
}
bdata := blockData{
Version: blk.VersionSio(),
NameLen: uint32(len(k)),
}
wblk := newWriterFrom(w)
wblk.ver = bdata.Version
err = blk.MarshalSio(wblk)
if err != nil {
return err
}
bhdr.Len = uint32(blockHeaderSize) + uint32(blockDataSize) +
align4U32(bdata.NameLen) + uint32(wblk.Len())
// fmt.Printf("blockHeader: %v\n", bhdr)
// fmt.Printf("blockData: %v (%s)\n", bdata, k)
err = bwrite(w, &bhdr)
if err != nil {
return err
}
err = bwrite(w, &bdata)
if err != nil {
return err
}
_, err = w.Write([]byte(k))
if err != nil {
return err
}
padlen := align4U32(bdata.NameLen) - bdata.NameLen
if padlen > 0 {
_, err = w.Write(make([]byte, int(padlen)))
if err != nil {
return err
}
}
_, err := io.CopyBuffer(w, wblk.buf, work)
if err != nil {
return err
}
w.ids = wblk.ids
w.tag = wblk.tag
w.ptr = wblk.ptr
}
return err
}