-
Notifications
You must be signed in to change notification settings - Fork 39
/
sfw.go
707 lines (643 loc) · 17.7 KB
/
sfw.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
// Copyright 2023 Sneller, 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 vm
import (
"bytes"
"fmt"
"io"
"sync"
"slices"
"github.com/SnellerInc/sneller/ion"
"github.com/SnellerInc/sneller/ion/zion/zll"
)
// QuerySink represents a sink for query outputs.
// Every query writes into a QuerySink.
type QuerySink interface {
// Open opens a new stream for output.
// Each stream is only safe to use from
// a single goroutine. Multiple streams
// may be opened for concurrent output.
Open() (io.WriteCloser, error)
io.Closer
}
// auxbindings is passed along with
// symtab to rowConsumer.symbolize to indicate
// that some variable bindings should be assumed
// to arrive via rowParams rather than via the
// primary delimiters provided in writeRows
type auxbindings struct {
// bound[i] corresponds to the variable that
// is bound to future rowParams.auxbound[i] values
bound []string
}
func (a *auxbindings) set(from *auxbindings) {
a.bound = append(a.bound[:0], from.bound...)
}
func (a *auxbindings) reset() {
a.bound = a.bound[:0]
}
// push creates a new binding and returns its index
// (to be used in rowParams.auxbound)
func (a *auxbindings) push(x string) int {
n := len(a.bound)
a.bound = append(a.bound, x)
return n
}
// id returns the id associated with a binding in a,
// or (-1, false) if no such binding is present
func (a *auxbindings) id(x string) (int, bool) {
for i := len(a.bound) - 1; i >= 0; i-- {
if a.bound[i] == x {
return i, true
}
}
return -1, false
}
// dummy auxbindings for most rowConsumers
// rowParams is auxilliary data passed
// to rowConsumer.writeRows that indicates
// things like extra variable bindings, etc.
type rowParams struct {
// auxbound contains a list of auxilliary bindings
// corresponding to the aux binding list provided
// in symbolize(); the length of each vmref slice
// will be the same as the number of rows passed to writeRows()
auxbound [][]vmref
}
// RowConsumer represents part of a QuerySink
// that consumes vectors of rows.
// (It is often the case that the io.WriteCloser
// returned from QuerySink.Open can be cast into
// a RowConsumer, in which case the caller may choose
// to use this interface instead of re-materializing
// the data.)
type rowConsumer interface {
// symbolize is called every time
// the current symbol table changes
//
// aux provides a list of bindings that supersedes
// the bindings provided by delims in writeRows;
// see auxbindings
symbolize(st *symtab, aux *auxbindings) error
// writeRows writes a slice of vmrefs
// (pointing to the inside of each row)
// into the next sub-query
//
// the implementation of writeRows *may*
// re-use the delims slice, but it *must not*
// write to the memory pointed to by delims;
// it must allocate new memory for new output
//
// the rules for modification of delims also
// apply to all of params.auxbound[*]; the slices
// themselves may be modified, but the values pointed
// to by the vmrefs should not be modified
writeRows(delims []vmref, params *rowParams) error
// next returns the next io.WriteCloser
// in the chain of query operators;
// this may be nil
next() rowConsumer
// Closer indicates that the caller has
// finished writing row data.
io.Closer
}
// asRowConsumer converts and arbitrary stream
// into a RowConsumer. If the destination implements
// RowConsumer, that implementation will be returned
// directly. Otherwise, the returned RowConsumer will
// serialize the row data passed to it before writing
// to the destination.
//
// Use this function when you've been given the
// return value of QuerySink.Open() and you want
// to write row data to it.
func asRowConsumer(dst io.WriteCloser) rowConsumer {
if s, ok := dst.(*rowSplitter); ok {
s.drop()
ret := s.rowConsumer
// most common case
return ret
}
if rc, ok := dst.(rowConsumer); ok {
return rc
}
return Rematerialize(dst)
}
// rowSplitter is a QuerySink that implements io.WriteCloser
// so that materialized data can be fed to a RowConsumer
type rowSplitter struct {
rowConsumer // automatically adopts writeRows() and Close()
shared symtab // current symbol table
delims []vmref // buffer of delimiters; allocated lazily
delimhint int
symbolized bool // seen any symbol tables
params rowParams
aux auxbindings
vmcache []byte
pos *int64
// zstate is non-nil and configured if ConfigureZion has been called
zstate *zionState
zout zionConsumer // cast from rowConsumer
}
// default number of rows to process per batch
const defaultDelims = 512
// splitter takes a rowConsumer and a default batch size
// and produces a rowSplitter that splits materialized row data
// into individual rows for consumption by a RowConsumer
func splitter(q rowConsumer) *rowSplitter {
s := &rowSplitter{rowConsumer: q, delimhint: defaultDelims}
if tee, ok := q.(*teeSplitter); ok {
s.pos = &tee.pos
}
return s
}
// write vmm-allocated bytes w/o copying
func (q *rowSplitter) writeVM(src []byte, delims []vmref) error {
for len(src) > 0 {
n, nb := scanvmm(src, delims)
if nb == 0 {
panic("no progress")
} else if int(nb) > len(src) {
panic("scanned past end of src")
}
if n > 0 {
err := q.writeRows(delims[:n], &q.params)
if err != nil {
return err
}
}
src = src[nb:]
}
return nil
}
// write non-vmm bytes by copying immediately after scanning
func (q *rowSplitter) writeVMCopy(src []byte, delims []vmref) error {
if q.vmcache == nil {
q.vmcache = Malloc()
}
const (
// startGranule is the desired size
// of copies into the vmm region
startGranule = 32 * 1024
// minDelims is the desired minimum
// number of delimiters passed to the core
minDelims = 32
)
granule := startGranule
for len(src) > 0 {
// copy data until we reach minDelims
// or the input data is exhausted
nd := 0
mem := q.vmcache[:0]
scancopy:
for nd < minDelims && len(mem)+granule < PageSize && len(src) > 0 {
off := len(mem)
copied := copy(mem[off:off+granule], src)
nnd, bytes := scanvmm(mem[off:off+copied], delims[nd:])
if nnd == 0 {
// just a nop pad:
if bytes > 0 {
src = src[bytes:]
continue scancopy
}
if nd > 0 {
break scancopy // just take what we have
}
// granule not large enough
// to fit a single object,
// so let's grow it and try again
granule *= 2
if granule > PageSize {
return fmt.Errorf("object > PageSize(%d)", PageSize)
}
continue scancopy
}
if bytes == 0 {
// should never be zero if nnd != 0
panic("zero added bytes")
}
nd += nnd // added delims
mem = mem[:off+int(bytes)] // only keep good data
src = src[bytes:] // chomp off input
}
err := q.writeRows(delims[:nd], &q.params)
if err != nil {
return err
}
}
return nil
}
// EndSegmentWriter is implemented by
// some io.WriteClosers returned by
// QuerySink.Open.
//
// See also: HintEndSegment.
type EndSegmentWriter interface {
EndSegment()
}
// HintEndSegment calls EndSegment() on w
// if it can be cast to an EndSegmentWriter.
//
// Callers that partition data into logical
// segments that begin with a fresh symbol table
// can use HintEndSegment as a hint to release temporary
// resources (like vm memory) that are specific to
// the most-recently-processed segment.
func HintEndSegment(w io.Writer) {
if esw, ok := w.(EndSegmentWriter); ok {
esw.EndSegment()
}
}
// EndSegment implements blockfmt.SegmentHintWriter.EndSegment
func (q *rowSplitter) EndSegment() {
// since we know we will have to re-build the symbol table
// anyway, we can free the symbol table memory so that
// interleaved queries can use the same vm buffers
q.symbolized = false
q.shared.Reset()
// similarly, we can free the vmcache:
if q.vmcache != nil {
Free(q.vmcache)
q.vmcache = nil
if q.zstate != nil {
q.zstate.buckets.Decompressed = nil
}
}
for rc := q.rowConsumer; rc != nil; rc = rc.next() {
if esw, ok := rc.(EndSegmentWriter); ok {
esw.EndSegment()
}
}
}
func (q *rowSplitter) drop() {
noLeakCheck(q)
q.shared.Reset()
if q.vmcache != nil {
Free(q.vmcache)
q.vmcache = nil
if q.zstate != nil {
q.zstate.buckets.Decompressed = nil
}
}
}
func (q *rowSplitter) Close() error {
err := q.rowConsumer.Close()
q.drop()
return err
}
func (q *rowSplitter) ConfigureZion(blocksize int64, fields []string) bool {
if blocksize > PageSize {
return false
}
if q.zout != nil {
q.zstate.blocksize = blocksize
return q.zout.zionOk(fields)
}
out, ok := q.rowConsumer.(zionConsumer)
if !ok {
out = &zionFlattener{rowConsumer: q.rowConsumer}
}
if !out.zionOk(fields) {
return false
}
q.zout = out
q.zstate = new(zionState)
q.zstate.blocksize = blocksize
q.zstate.shape.Symtab = &zionSymtab{parent: q}
return true
}
// zionSymtab implements zll.Symtab
type zionSymtab struct {
parent *rowSplitter
}
func (z *zionSymtab) Symbolize(x string) (ion.Symbol, bool) {
return z.parent.shared.Symbolize(x)
}
// this is straight out of z.parent.Write
func (z *zionSymtab) Unmarshal(src []byte) ([]byte, error) {
q := z.parent
if !q.shared.resident() {
leakCheck(q)
}
q.shared.rewind()
rest, err := q.shared.Unmarshal(src)
if err != nil {
return nil, err
}
q.shared.snapshot() // restore on next Unmarshal
q.shared.flags.set(sfZion)
q.aux.reset()
err = q.zout.symbolize(&q.shared, &q.aux)
if err != nil {
return rest, err
}
return rest, nil
}
func (q *rowSplitter) writeZion(src []byte) (int, error) {
rest, err := q.zstate.shape.Decode(src)
if err != nil {
return 0, err
}
q.zstate.buckets.Reset(&q.zstate.shape, rest)
if q.vmcache == nil {
q.vmcache = Malloc()
}
// make sure decompression writes into vmm
q.zstate.buckets.Decompressed = q.vmcache[:0]
// we already make sure page+1 can be read safely;
// don't risk trying to append to the page from Malloc
q.zstate.buckets.SkipPadding = true
if q.pos != nil {
*q.pos += q.zstate.blocksize
}
err = q.zout.writeZion(q.zstate)
if err != nil {
return 0, err
}
return len(src), nil
}
// Write implements io.Writer
//
// NOTE: each call to Write must contain
// zero or more complete ion objects.
// The data passed to Write may contain a symbol table,
// but if it does, it must come first.
func (q *rowSplitter) Write(buf []byte) (int, error) {
if q.zstate != nil && zll.IsMagic(buf) {
return q.writeZion(buf)
}
if !q.symbolized && (len(buf) < 4 || !ion.IsBVM(buf)) {
return 0, fmt.Errorf("first rowSplitter.Write does not have a new symbol table")
}
boff := int32(0)
// if we have a symbol table, then parse it
// (ion.Symtab.Unmarshal takes care of the BVM resetting the table)
if len(buf) >= 4 && ion.IsBVM(buf) || ion.TypeOf(buf) == ion.AnnotationType {
if !q.shared.resident() {
leakCheck(q)
}
q.shared.rewind() // revert to previous Unmarshal state
rest, err := q.shared.Unmarshal(buf)
if err != nil {
return 0, fmt.Errorf("rowSplitter.Write: %w", err)
}
q.shared.snapshot() // mark this point for the next rewind()
q.shared.flags.clear(sfZion)
q.symbolized = true
boff = int32(len(buf) - len(rest))
q.aux.reset()
err = q.symbolize(&q.shared, &q.aux)
if err != nil {
return 0, err
}
}
// we round up rather than down for each
// call to Write so that a LIMIT that is
// satisfied on the first block yields a
// non-zero number of bytes scanned
if q.pos != nil {
*q.pos += int64(len(buf))
}
// allocate q.delims lazily
if len(q.delims) < q.delimhint {
q.delims = make([]vmref, q.delimhint)
}
var err error
if Allocated(buf) {
err = q.writeVM(buf[boff:], q.delims)
} else {
err = q.writeVMCopy(buf[boff:], q.delims)
}
return len(buf), err
}
// QueryBuffer is an in-memory implementation
// of QuerySink that can be trivially converted
// to a Table. It can be used to force a sub-query
// to be fully materialized before being consumed
// by another query. It also guarantees that the
// input chunks are padded to a fixed alignment.
type QueryBuffer struct {
lock sync.Mutex
buf bytes.Buffer
chunksize int
tail []byte // used to make nop pads
}
// Size returns the number of bytes in the table.
func (q *QueryBuffer) Size() int64 {
return int64(q.buf.Len())
}
// Alignment returns the alignment of the table.
func (q *QueryBuffer) Alignment() int {
return q.chunksize
}
// SetAlignment sets the alignment to which
// subsequent calls to Write will be padded.
func (q *QueryBuffer) SetAlignment(align int) {
q.chunksize = align
}
// Reset resets the buffer so that it contains no data.
func (q *QueryBuffer) Reset() {
q.buf.Reset()
}
// Bytes returns all of the bytes written to the buffer.
func (q *QueryBuffer) Bytes() []byte {
return q.buf.Bytes()
}
// Open implements QueryConsumer.Open
func (q *QueryBuffer) Open() (io.WriteCloser, error) {
return q, nil
}
// Write implements io.Writer
func (q *QueryBuffer) Write(buf []byte) (int, error) {
q.lock.Lock()
defer q.lock.Unlock()
if q.chunksize == 0 {
q.chunksize = defaultAlign
}
if len(buf) > q.chunksize {
return 0, fmt.Errorf("chunk of %d bytes too big for QueryBuffer", len(buf))
}
q.buf.Grow(q.chunksize)
q.buf.Write(buf)
nopsize := q.chunksize - len(buf)
for nopsize > 0 {
if cap(q.tail) < nopsize {
q.tail = make([]byte, nopsize)
}
q.tail = q.tail[:nopsize]
wrote, padded := ion.NopPadding(q.tail, len(q.tail))
q.buf.Write(q.tail[:wrote+padded])
nopsize -= (wrote + padded)
}
return len(buf), nil
}
// Close implements io.Closer
func (q *QueryBuffer) Close() error { return nil }
// Table produces a view of the data in the QueryBuffer
func (q *QueryBuffer) Table() *BufferedTable {
return &BufferedTable{buf: q.Bytes(), align: q.chunksize}
}
// Rematerializer is a RowConsumer that
// rematerializes row data into contiguous
// blocks of ion data.
type Rematerializer struct {
buf ion.Buffer
out io.WriteCloser
stsize int
aux []ion.Symbol
auxpos []int
empty bool
}
// Rematerialize returns a RowConsumer that guarantees
// that the row data is fully rematerialized before being
// written to 'dst'
func Rematerialize(dst io.WriteCloser) *Rematerializer {
r := &Rematerializer{empty: true, out: dst}
return r
}
func (m *Rematerializer) next() rowConsumer { return nil }
func (m *Rematerializer) flush() error {
if m.empty {
return nil
}
buf := m.buf.Bytes()
_, err := m.out.Write(buf)
m.buf.Set(buf[:m.stsize])
m.empty = true
return err
}
// symbolize implements RowConsumer.symbolize
func (m *Rematerializer) symbolize(st *symtab, aux *auxbindings) error {
err := m.flush()
if err != nil {
return err
}
m.buf.Reset()
m.aux = shrink(m.aux, len(aux.bound))
m.auxpos = shrink(m.auxpos, len(aux.bound))
for i := range aux.bound {
m.aux[i] = st.Intern(aux.bound[i])
m.auxpos[i] = i
}
// produce aux symbols in sorted order
slices.SortFunc(m.auxpos, func(i, j int) int {
return int(m.aux[i]) - int(m.aux[j])
})
st.Marshal(&m.buf, true)
m.stsize = m.buf.Size()
return nil
}
func (m *Rematerializer) writeRows(delims []vmref, rp *rowParams) error {
if m.stsize == 0 {
panic("WriteRows() called before Symbolize()")
}
if len(m.aux) == 0 {
return m.writeRowsFast(delims)
}
for i := range delims {
mem := delims[i].mem()
var sym ion.Symbol
var err error
m.buf.BeginStruct(-1)
// let BeginField handle the sorting
for _, pos := range m.auxpos {
mem := rp.auxbound[pos][i].mem()
if len(mem) == 0 {
continue
}
m.buf.BeginField(m.aux[pos])
m.buf.UnsafeAppend(mem)
}
for len(mem) > 0 {
before := mem
sym, mem, err = ion.ReadLabel(mem)
if err != nil {
return fmt.Errorf("vm.Rematerializer: writeRows: %x %w", before, err)
}
size := ion.SizeOf(mem)
m.buf.BeginField(sym)
m.buf.UnsafeAppend(mem[:size])
mem = mem[size:]
}
m.buf.EndStruct()
m.empty = false
}
return nil
}
// writeRows implements RowConsumer.writeRows
func (m *Rematerializer) writeRowsFast(delims []vmref) error {
for i := range delims {
if delims[i][1] == 0 {
continue
}
size := int(delims[i][1]) + 8 // generous slack
if defaultAlign-m.buf.Size() < size {
err := m.flush()
if err != nil {
return err
}
}
m.buf.UnsafeAppendFields(delims[i].mem())
m.empty = false
}
return nil
}
// Close implements io.Closer
func (m *Rematerializer) Close() error {
err := m.flush()
err2 := m.out.Close()
if err == nil {
err = err2
}
return err
}
// Locked turns an io.Writer into a goroutine-safe
// io.Writer where each write is serialized against
// other writes. Locked takes into account whether
// dst is the result of another call to Locked or
// LockedSink and optimizes accordingly.
func Locked(dst io.Writer) io.Writer {
if _, ok := dst.(*sink); ok {
return dst
}
return &sink{dst: dst}
}
// LockedSink returns a QuerySink for which
// all calls to Open return a wrapper of dst
// that serializes calls to io.Writer.Write.
// (See also Locked.)
func LockedSink(dst io.Writer) QuerySink {
if s, ok := dst.(*sink); ok {
return s
}
return &sink{dst: dst}
}
// trivial vm.QuerySink for
// producing an output stream
type sink struct {
lock sync.Mutex
dst io.Writer
}
func (s *sink) Write(p []byte) (int, error) {
s.lock.Lock()
defer s.lock.Unlock()
return s.dst.Write(p)
}
func (s *sink) EndSegment() {
s.lock.Lock()
defer s.lock.Unlock()
HintEndSegment(s.dst)
}
func (s *sink) Open() (io.WriteCloser, error) { return s, nil }
func (s *sink) Close() error { return nil }