-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
sst_writer.go
593 lines (530 loc) · 21.6 KB
/
sst_writer.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
// Copyright 2019 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package storage
import (
"bytes"
"context"
"io"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/util/metamorphic"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/pebble"
"github.com/cockroachdb/pebble/objstorage"
"github.com/cockroachdb/pebble/rangekey"
"github.com/cockroachdb/pebble/sstable"
)
// IngestionValueBlocksEnabled controls whether older versions of MVCC keys in
// the same ingested sstable will have their values written to value blocks.
// This configuration ability was motivated by a case of > 130GB sstables,
// caused by snapshot ingestion. Writing value blocks requires in-memory
// buffering of compressed value blocks, which caused OOMs in the above case.
var IngestionValueBlocksEnabled = settings.RegisterBoolSetting(
settings.ApplicationLevel,
"storage.ingestion.value_blocks.enabled",
"set to true to enable writing of value blocks in ingestion sstables",
metamorphic.ConstantWithTestBool(
"storage.ingestion.value_blocks.enabled", true),
settings.WithPublic)
// SSTWriter writes SSTables.
type SSTWriter struct {
fw *sstable.Writer
// DataSize tracks the total key and value bytes added so far.
DataSize int64
scratch []byte
Meta *sstable.WriterMetadata
supportsRangeKeys bool // TODO(erikgrinaker): remove after 22.2
}
var _ Writer = &SSTWriter{}
var _ ExportWriter = &SSTWriter{}
var _ InternalWriter = &SSTWriter{}
// NoopFinishAbortWritable wraps an io.Writer to make a objstorage.Writable that
// will ignore Finish and Abort calls.
func NoopFinishAbortWritable(w io.Writer) objstorage.Writable {
return &noopFinishAbort{Writer: w}
}
// noopFinishAbort is used to wrap io.Writers for sstable.Writer.
type noopFinishAbort struct {
io.Writer
}
var _ objstorage.Writable = (*noopFinishAbort)(nil)
// Write is part of the objstorage.Writable interface.
func (n *noopFinishAbort) Write(p []byte) error {
// An io.Writer always returns an error if it can't write the entire slice.
_, err := n.Writer.Write(p)
return err
}
// Finish is part of the objstorage.Writable interface.
func (*noopFinishAbort) Finish() error {
return nil
}
// Abort is part of the objstorage.Writable interface.
func (*noopFinishAbort) Abort() {}
// MakeIngestionWriterOptions returns writer options suitable for writing SSTs
// that will subsequently be ingested (e.g. with AddSSTable).
func MakeIngestionWriterOptions(ctx context.Context, cs *cluster.Settings) sstable.WriterOptions {
// By default, take a conservative approach and assume we don't have newer
// table features available. Upgrade to an appropriate version only if the
// cluster supports it. Currently, all supported versions understand
// TableFormatPebblev4.
format := sstable.TableFormatPebblev4
opts := DefaultPebbleOptions().MakeWriterOptions(0, format)
// By default, compress with the algorithm used for storage in a Pebble store.
// There are other, more specific, use cases that may call for a different
// algorithm, which can be set by overriding the default (see
// MakeIngestionSSTWriterWithOverrides).
opts.Compression = getCompressionAlgorithm(ctx, cs, CompressionAlgorithmStorage)
opts.MergerName = "nullptr"
if !IngestionValueBlocksEnabled.Get(&cs.SV) {
opts.DisableValueBlocks = true
}
return opts
}
// makeSSTRewriteOptions should be used instead of MakeIngestionWriterOptions
// when we are going to rewrite ssts. It additionally returns the minimum
// table format that we accept, since sst rewriting will often preserve the
// input table format.
func makeSSTRewriteOptions(
ctx context.Context, cs *cluster.Settings,
) (opts sstable.WriterOptions, minTableFormat sstable.TableFormat) {
// v22.2 clusters use sstable.TableFormatPebblev2.
return MakeIngestionWriterOptions(ctx, cs), sstable.TableFormatPebblev2
}
// MakeBackupSSTWriter creates a new SSTWriter tailored for backup SSTs which
// are typically only ever iterated in their entirety.
func MakeBackupSSTWriter(ctx context.Context, cs *cluster.Settings, f io.Writer) SSTWriter {
// By default, take a conservative approach and assume we don't have newer
// table features available. Upgrade to an appropriate version only if the
// cluster supports it.
format := sstable.TableFormatPebblev2
// TODO(sumeer): add code to use TableFormatPebblev3 after confirming that
// we won't run afoul of any stale tooling that reads backup ssts.
opts := DefaultPebbleOptions().MakeWriterOptions(0, format)
// Don't need BlockPropertyCollectors for backups.
opts.BlockPropertyCollectors = nil
// Disable bloom filters since we only ever iterate backups.
opts.FilterPolicy = nil
// Bump up block size, since we almost never seek or do point lookups, so more
// block checksums and more index entries are just overhead and smaller blocks
// reduce compression ratio.
opts.BlockSize = 128 << 10
opts.Compression = getCompressionAlgorithm(ctx, cs, CompressionAlgorithmBackupTransport)
opts.MergerName = "nullptr"
return SSTWriter{
fw: sstable.NewWriter(&noopFinishAbort{f}, opts),
supportsRangeKeys: opts.TableFormat >= sstable.TableFormatPebblev2,
}
}
// MakeIngestionSSTWriter creates a new SSTWriter tailored for ingestion SSTs.
// These SSTs have bloom filters enabled (as set in DefaultPebbleOptions). If
// the cluster settings permit value blocks, the SST may contain value blocks.
func MakeIngestionSSTWriter(
ctx context.Context, cs *cluster.Settings, w objstorage.Writable,
) SSTWriter {
return MakeIngestionSSTWriterWithOverrides(ctx, cs, w)
}
// SSTWriterOption augments one or more sstable.WriterOptions.
type SSTWriterOption func(opts *sstable.WriterOptions)
// WithValueBlocksDisabled disables the use of value blocks in an SSTable.
var WithValueBlocksDisabled SSTWriterOption = func(opts *sstable.WriterOptions) {
opts.DisableValueBlocks = true
}
// WithCompressionFromClusterSetting sets the compression algorithm for an
// SSTable based on the value of the given cluster setting.
func WithCompressionFromClusterSetting(
ctx context.Context, cs *cluster.Settings, setting *settings.EnumSetting[compressionAlgorithm],
) SSTWriterOption {
return func(opts *sstable.WriterOptions) {
opts.Compression = getCompressionAlgorithm(ctx, cs, setting)
}
}
// MakeIngestionSSTWriterWithOverrides creates a new SSTWriter tailored for
// ingestion SSTs. These SSTs have bloom filters enabled (as set in
// DefaultPebbleOptions) and format set to the highest permissible by the
// cluster settings. Callers that expect to write huge SSTs, say 200+MB, which
// could contain multiple versions for the same key, should pass in a
// WithValueBlocksDisabled option. This is because value blocks are buffered
// in-memory while writing the SST (see
// https://github.com/cockroachdb/cockroach/issues/117113).
func MakeIngestionSSTWriterWithOverrides(
ctx context.Context, cs *cluster.Settings, w objstorage.Writable, overrides ...SSTWriterOption,
) SSTWriter {
opts := MakeIngestionWriterOptions(ctx, cs)
for _, o := range overrides {
o(&opts)
}
return SSTWriter{
fw: sstable.NewWriter(w, opts),
supportsRangeKeys: opts.TableFormat >= sstable.TableFormatPebblev2,
}
}
// Finish finalizes the writer and returns the constructed file's contents,
// since the last call to Truncate (if any). At least one kv entry must have been added.
func (fw *SSTWriter) Finish() error {
if fw.fw == nil {
return errors.New("cannot call Finish on a closed writer")
}
if err := fw.fw.Close(); err != nil {
return err
}
var err error
fw.Meta, err = fw.fw.Raw().Metadata()
fw.fw = nil
return err
}
// ClearRawRange implements the Engine interface.
func (fw *SSTWriter) ClearRawRange(start, end roachpb.Key, pointKeys, rangeKeys bool) error {
fw.scratch = EngineKey{Key: start}.EncodeToBuf(fw.scratch[:0])
endRaw := EngineKey{Key: end}.Encode()
if pointKeys {
fw.DataSize += int64(len(start)) + int64(len(end))
if err := fw.fw.DeleteRange(fw.scratch, endRaw); err != nil {
return err
}
}
if rangeKeys && fw.supportsRangeKeys {
fw.DataSize += int64(len(start)) + int64(len(end))
if err := fw.fw.RangeKeyDelete(fw.scratch, endRaw); err != nil {
return err
}
}
return nil
}
// ClearMVCCRange implements the Writer interface.
func (fw *SSTWriter) ClearMVCCRange(start, end roachpb.Key, pointKeys, rangeKeys bool) error {
panic("not implemented")
}
// ClearMVCCVersions implements the Writer interface.
func (fw *SSTWriter) ClearMVCCVersions(start, end MVCCKey) error {
return fw.clearRange(start, end)
}
// PutMVCCRangeKey implements the Writer interface.
func (fw *SSTWriter) PutMVCCRangeKey(rangeKey MVCCRangeKey, value MVCCValue) error {
// NB: all MVCC APIs currently assume all range keys are range tombstones.
if !value.IsTombstone() {
return errors.New("range keys can only be MVCC range tombstones")
}
valueRaw, err := EncodeMVCCValue(value)
if err != nil {
return errors.Wrapf(err, "failed to encode MVCC value for range key %s", rangeKey)
}
return fw.PutRawMVCCRangeKey(rangeKey, valueRaw)
}
// PutRawMVCCRangeKey implements the Writer interface.
func (fw *SSTWriter) PutRawMVCCRangeKey(rangeKey MVCCRangeKey, value []byte) error {
if err := rangeKey.Validate(); err != nil {
return err
}
return fw.PutEngineRangeKey(
rangeKey.StartKey, rangeKey.EndKey, EncodeMVCCTimestampSuffix(rangeKey.Timestamp), value)
}
// ClearMVCCRangeKey implements the Writer interface.
func (fw *SSTWriter) ClearMVCCRangeKey(rangeKey MVCCRangeKey) error {
if !fw.supportsRangeKeys {
return nil // noop
}
if err := rangeKey.Validate(); err != nil {
return err
}
// If the range key holds an encoded timestamp as it was read from storage,
// write the tombstone to clear it using the same encoding of the timestamp.
// See #129592.
if len(rangeKey.EncodedTimestampSuffix) > 0 {
return fw.ClearEngineRangeKey(rangeKey.StartKey, rangeKey.EndKey,
rangeKey.EncodedTimestampSuffix)
}
return fw.ClearEngineRangeKey(rangeKey.StartKey, rangeKey.EndKey,
EncodeMVCCTimestampSuffix(rangeKey.Timestamp))
}
// PutEngineRangeKey implements the Writer interface.
func (fw *SSTWriter) PutEngineRangeKey(start, end roachpb.Key, suffix, value []byte) error {
if !fw.supportsRangeKeys {
return errors.New("range keys not supported by SST writer")
}
// MVCC values don't account for the timestamp, so we don't account
// for the suffix here.
fw.DataSize += int64(len(start)) + int64(len(end)) + int64(len(value))
return fw.fw.RangeKeySet(
EngineKey{Key: start}.Encode(), EngineKey{Key: end}.Encode(), suffix, value)
}
// ClearEngineRangeKey implements the Writer interface.
func (fw *SSTWriter) ClearEngineRangeKey(start, end roachpb.Key, suffix []byte) error {
if !fw.supportsRangeKeys {
return nil // noop
}
// MVCC values don't account for the timestamp, so we don't account for the
// suffix here.
fw.DataSize += int64(len(start)) + int64(len(end))
return fw.fw.RangeKeyUnset(EngineKey{Key: start}.Encode(), EngineKey{Key: end}.Encode(), suffix)
}
// ClearEngineRange clears point keys in the specified EngineKey range.
func (fw *SSTWriter) ClearEngineRange(start, end EngineKey) error {
fw.scratch = start.EncodeToBuf(fw.scratch[:0])
endRaw := end.Encode()
fw.DataSize += int64(len(start.Key)) + int64(len(end.Key))
if err := fw.fw.DeleteRange(fw.scratch, endRaw); err != nil {
return err
}
return nil
}
// ClearRawEncodedRange implements the InternalWriter interface.
func (fw *SSTWriter) ClearRawEncodedRange(start, end []byte) error {
startEngine, ok := DecodeEngineKey(start)
if !ok {
return errors.New("cannot decode start engine key")
}
endEngine, ok := DecodeEngineKey(end)
if !ok {
return errors.New("cannot decode end engine key")
}
fw.DataSize += int64(len(startEngine.Key)) + int64(len(endEngine.Key))
return fw.fw.DeleteRange(start, end)
}
// PutInternalRangeKey implements the InternalWriter interface.
func (fw *SSTWriter) PutInternalRangeKey(start, end []byte, key rangekey.Key) error {
if !fw.supportsRangeKeys {
return errors.New("range keys not supported by SST writer")
}
startEngine, ok := DecodeEngineKey(start)
if !ok {
return errors.New("cannot decode engine key")
}
endEngine, ok := DecodeEngineKey(end)
if !ok {
return errors.New("cannot decode engine key")
}
fw.DataSize += int64(len(startEngine.Key)) + int64(len(endEngine.Key)) + int64(len(key.Value))
switch key.Kind() {
case pebble.InternalKeyKindRangeKeyUnset:
return fw.fw.RangeKeyUnset(start, end, key.Suffix)
case pebble.InternalKeyKindRangeKeySet:
return fw.fw.RangeKeySet(start, end, key.Suffix, key.Value)
case pebble.InternalKeyKindRangeKeyDelete:
return fw.fw.RangeKeyDelete(start, end)
default:
panic("unexpected range key kind")
}
}
// PutInternalPointKey implements the InternalWriter interface.
func (fw *SSTWriter) PutInternalPointKey(key *pebble.InternalKey, value []byte) error {
ek, ok := DecodeEngineKey(key.UserKey)
if !ok {
return errors.New("cannot decode engine key")
}
fw.DataSize += int64(len(ek.Key)) + int64(len(value))
return fw.fw.Raw().AddWithForceObsolete(*key, value, false /* forceObsolete */)
}
// clearRange clears all point keys in the given range by dropping a Pebble
// range tombstone.
//
// NB: Does not clear range keys.
func (fw *SSTWriter) clearRange(start, end MVCCKey) error {
if fw.fw == nil {
return errors.New("cannot call ClearRange on a closed writer")
}
fw.DataSize += int64(len(start.Key)) + int64(len(end.Key))
fw.scratch = EncodeMVCCKeyToBuf(fw.scratch[:0], start)
return fw.fw.DeleteRange(fw.scratch, EncodeMVCCKey(end))
}
// Put puts a kv entry into the sstable being built. An error is returned if it
// is not greater than any previously added entry (according to the comparator
// configured during writer creation). `Close` cannot have been called.
//
// TODO(sumeer): Put has been removed from the Writer interface, but there
// are many callers of this SSTWriter method. Fix those callers and remove.
func (fw *SSTWriter) Put(key MVCCKey, value []byte) error {
if fw.fw == nil {
return errors.New("cannot call Put on a closed writer")
}
fw.DataSize += int64(len(key.Key)) + int64(len(value))
fw.scratch = EncodeMVCCKeyToBuf(fw.scratch[:0], key)
return fw.fw.Set(fw.scratch, value)
}
// PutMVCC implements the Writer interface.
// An error is returned if it is not greater than any previously added entry
// (according to the comparator configured during writer creation). `Close`
// cannot have been called.
func (fw *SSTWriter) PutMVCC(key MVCCKey, value MVCCValue) error {
if key.Timestamp.IsEmpty() {
panic("PutMVCC timestamp is empty")
}
encValue, err := EncodeMVCCValue(value)
if err != nil {
return err
}
return fw.put(key, encValue)
}
// PutRawMVCC implements the Writer interface.
// An error is returned if it is not greater than any previously added entry
// (according to the comparator configured during writer creation). `Close`
// cannot have been called.
func (fw *SSTWriter) PutRawMVCC(key MVCCKey, value []byte) error {
if key.Timestamp.IsEmpty() {
panic("PutRawMVCC timestamp is empty")
}
return fw.put(key, value)
}
// PutUnversioned implements the Writer interface.
// An error is returned if it is not greater than any previously added entry
// (according to the comparator configured during writer creation). `Close`
// cannot have been called.
func (fw *SSTWriter) PutUnversioned(key roachpb.Key, value []byte) error {
return fw.put(MVCCKey{Key: key}, value)
}
// PutEngineKey implements the Writer interface.
// An error is returned if it is not greater than any previously added entry
// (according to the comparator configured during writer creation). `Close`
// cannot have been called.
func (fw *SSTWriter) PutEngineKey(key EngineKey, value []byte) error {
if fw.fw == nil {
return errors.New("cannot call Put on a closed writer")
}
fw.DataSize += int64(len(key.Key)) + int64(len(value))
fw.scratch = key.EncodeToBuf(fw.scratch[:0])
return fw.fw.Set(fw.scratch, value)
}
// put puts a kv entry into the sstable being built. An error is returned if it
// is not greater than any previously added entry (according to the comparator
// configured during writer creation). `Close` cannot have been called.
func (fw *SSTWriter) put(key MVCCKey, value []byte) error {
if fw.fw == nil {
return errors.New("cannot call Put on a closed writer")
}
fw.DataSize += int64(len(key.Key)) + int64(len(value))
fw.scratch = EncodeMVCCKeyToBuf(fw.scratch[:0], key)
return fw.fw.Set(fw.scratch, value)
}
// ApplyBatchRepr implements the Writer interface.
func (fw *SSTWriter) ApplyBatchRepr(repr []byte, sync bool) error {
panic("unimplemented")
}
// ClearMVCC implements the Writer interface. An error is returned if it is
// not greater than any previous point key passed to this Writer (according to
// the comparator configured during writer creation). `Close` cannot have been
// called.
func (fw *SSTWriter) ClearMVCC(key MVCCKey, opts ClearOptions) error {
if key.Timestamp.IsEmpty() {
panic("ClearMVCC timestamp is empty")
}
return fw.clear(key, opts)
}
// ClearUnversioned implements the Writer interface. An error is returned if
// it is not greater than any previous point key passed to this Writer
// (according to the comparator configured during writer creation). `Close`
// cannot have been called.
func (fw *SSTWriter) ClearUnversioned(key roachpb.Key, opts ClearOptions) error {
return fw.clear(MVCCKey{Key: key}, opts)
}
// ClearEngineKey implements the Writer interface. An error is returned if it is
// not greater than any previous point key passed to this Writer (according to
// the comparator configured during writer creation). `Close` cannot have been
// called.
func (fw *SSTWriter) ClearEngineKey(key EngineKey, opts ClearOptions) error {
if fw.fw == nil {
return errors.New("cannot call Clear on a closed writer")
}
fw.scratch = key.EncodeToBuf(fw.scratch[:0])
fw.DataSize += int64(len(key.Key))
// TODO(jackson): We could use opts.ValueSize if known, but it would require
// additional logic around ensuring the cluster version is at least
// V23_2_UseSizedPebblePointTombstones. It's probably not worth it until we
// can unconditionally use it; I don't believe we ever write point
// tombstones to sstables constructed within Cockroach.
return fw.fw.Delete(fw.scratch)
}
// An error is returned if it is not greater than any previous point key
// passed to this Writer (according to the comparator configured during writer
// creation). `Close` cannot have been called.
func (fw *SSTWriter) clear(key MVCCKey, opts ClearOptions) error {
if fw.fw == nil {
return errors.New("cannot call Clear on a closed writer")
}
fw.scratch = EncodeMVCCKeyToBuf(fw.scratch[:0], key)
fw.DataSize += int64(len(key.Key))
// TODO(jackson): We could use opts.ValueSize if known, but it would require
// additional logic around ensuring the cluster version is at least
// V23_2_UseSizedPebblePointTombstones. It's probably not worth it until we
// can unconditionally use it; I don't believe we ever write point
// tombstones to sstables constructed within Cockroach.
return fw.fw.Delete(fw.scratch)
}
// SingleClearEngineKey implements the Writer interface.
func (fw *SSTWriter) SingleClearEngineKey(key EngineKey) error {
panic("unimplemented")
}
// ClearMVCCIteratorRange implements the Writer interface.
func (fw *SSTWriter) ClearMVCCIteratorRange(_, _ roachpb.Key, _, _ bool) error {
panic("not implemented")
}
// Merge implements the Writer interface.
func (fw *SSTWriter) Merge(key MVCCKey, value []byte) error {
if fw.fw == nil {
return errors.New("cannot call Merge on a closed writer")
}
fw.DataSize += int64(len(key.Key)) + int64(len(value))
fw.scratch = EncodeMVCCKeyToBuf(fw.scratch[:0], key)
return fw.fw.Merge(fw.scratch, value)
}
// LogData implements the Writer interface.
func (fw *SSTWriter) LogData(data []byte) error {
// No-op.
return nil
}
// LogLogicalOp implements the Writer interface.
func (fw *SSTWriter) LogLogicalOp(op MVCCLogicalOpType, details MVCCLogicalOpDetails) {
// No-op.
}
// Close finishes and frees memory and other resources. Close is idempotent.
func (fw *SSTWriter) Close() {
if fw.fw == nil {
return
}
// pebble.Writer *does* return interesting errors from Close... but normally
// we already called its Close() in Finish() and we no-op here. Thus the only
// time we expect to be here is in a deferred Close(), in which case the caller
// probably is already returning some other error, so returning one from this
// method just makes for messy defers.
_ = fw.fw.Close()
fw.fw = nil
}
// ShouldWriteLocalTimestamps implements the Writer interface.
func (fw *SSTWriter) ShouldWriteLocalTimestamps(context.Context) bool {
return false
}
// BufferedSize implements the Writer interface.
func (fw *SSTWriter) BufferedSize() int {
return 0
}
// MemObject is an in-memory implementation of objstorage.Writable, intended
// use with SSTWriter.
type MemObject struct {
bytes.Buffer
}
var _ objstorage.Writable = (*MemObject)(nil)
// Write is part of the objstorage.Writable interface.
func (f *MemObject) Write(p []byte) error {
_, err := f.Buffer.Write(p)
return err
}
// Finish is part of the objstorage.Writable interface.
func (*MemObject) Finish() error {
return nil
}
// Abort is part of the objstorage.Writable interface.
func (*MemObject) Abort() {}
// Close implements the writeCloseSyncer interface.
func (*MemObject) Close() error {
return nil
}
// Data returns the in-memory buffer behind this MemObject.
func (f *MemObject) Data() []byte {
return f.Bytes()
}