-
Notifications
You must be signed in to change notification settings - Fork 2
/
warcfile.go
804 lines (723 loc) · 24.3 KB
/
warcfile.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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
/*
* Copyright 2021 National Library of Norway.
*
* 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 gowarc
import (
"bufio"
"errors"
"fmt"
"io"
"log"
"os"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/klauspost/compress/gzip"
"github.com/nlnwa/gowarc/v2/internal"
"github.com/nlnwa/gowarc/v2/internal/countingreader"
"github.com/nlnwa/gowarc/v2/internal/timestamp"
"github.com/prometheus/prometheus/tsdb/fileutil"
)
// WarcFileNameGenerator is the interface that wraps the NewWarcfileName function.
type WarcFileNameGenerator interface {
// NewWarcfileName returns a directory (might be the empty string for current directory) and a file name
NewWarcfileName() (string, string)
}
// PatternNameGenerator implements the WarcFileNameGenerator.
//
// New filenames are generated based on a pattern which defaults to the recommendation in the WARC 1.1 standard
// (https://iipc.github.io/warc-specifications/specifications/warc-format/warc-1.1/#annex-c-informative-warc-file-size-and-name-recommendations).
// The pattern is like golangs fmt package (https://pkg.go.dev/fmt), but allows for named fields in curly braces.
// The available predefined names are:
// - prefix - content of the Prefix field
// - ext - content of the Extension field
// - ts - current time as 14-digit GMT Time-stamp
// - serial - atomically increased serial number for every generated file name. Initial value is 0 if Serial field is not set
// - ip - primary IP address of the node
// - host - host name of the node
// - hostOrIp - host name of the node, falling back to IP address if host name could not be resolved
type PatternNameGenerator struct {
Directory string // Directory to store warcfiles. Defaults to the empty string
Prefix string // Prefix available to be used in pattern. Defaults to the empty string
Serial int32 // Serial number available for use in pattern. It is atomically increased with every generated file name.
Pattern string // Pattern for generated file name. Defaults to: "%{prefix}s%{ts}s-%04{serial}d-%{hostOrIp}s.%{ext}s"
Extension string // Extension for file name. Defaults to: "warc"
params map[string]interface{}
}
const (
defaultPattern = "%{prefix}s%{ts}s-%04{serial}d-%{hostOrIp}s.%{ext}s"
defaultExtension = "warc"
)
// Allow overriding of time.Now for tests
var now = time.Now
var ip = internal.GetOutboundIP
var host = internal.GetHostName
var hostOrIp = internal.GetHostNameOrIP
// NewWarcfileName returns a directory (might be the empty string for current directory) and a file name
func (g *PatternNameGenerator) NewWarcfileName() (string, string) {
if g.Pattern == "" {
g.Pattern = defaultPattern
}
if g.Extension == "" {
g.Extension = defaultExtension
}
if g.params == nil {
g.params = map[string]interface{}{
"prefix": g.Prefix,
"ext": g.Extension,
"ip": ip(),
"host": host(),
"hostOrIp": hostOrIp(),
}
}
p := map[string]interface{}{
"ts": timestamp.UTC14(now()),
"serial": atomic.AddInt32(&g.Serial, 1),
}
for k, v := range g.params {
p[k] = v
}
name := internal.Sprintt(g.Pattern, p)
return g.Directory, name
}
// WarcFileWriter is used to write WARC files.
// Use [NewWarcFileWriter] to create a new instance.
//
// The WarcFileWriter writes to one or more files simultaneously. The number of files is controlled by the [WithMaxConcurrentWriters] option.
// The WarcFileWriter will create a new file when the current file size exceeds the value set by the [WithMaxFileSize] option.
// File names are generated by the [WarcFileNameGenerator] set by the [WithFileNameGenerator] option.
// The WarcFileWriter will add a Warcinfo record to each file if the [WithWarcInfoFunc] option is set.
type WarcFileWriter struct {
opts *warcFileWriterOptions
writers []*singleWarcFileWriter
shutWriters *sync.WaitGroup
jobs chan *job
middleCh chan *job
closing chan struct{} // signal channel
closed chan struct{}
}
func (w *WarcFileWriter) String() string {
return fmt.Sprintf("WarcFileWriter (%s)", w.opts)
}
// NewWarcFileWriter creates a new WarcFileWriter with the supplied options.
func NewWarcFileWriter(opts ...WarcFileWriterOption) *WarcFileWriter {
o := defaultwarcFileWriterOptions()
for _, opt := range opts {
opt.apply(&o)
}
w := &WarcFileWriter{opts: &o,
closing: make(chan struct{}), // signal channel
closed: make(chan struct{}),
middleCh: make(chan *job),
jobs: make(chan *job),
shutWriters: &sync.WaitGroup{},
}
w.shutWriters.Add(o.maxConcurrentWriters)
// the middle layer
go func() {
exit := func(v *job, needSend bool) {
close(w.closed)
if needSend {
w.jobs <- v
}
close(w.jobs)
}
for {
select {
case <-w.closing:
exit(nil, false)
return
case v := <-w.middleCh:
select {
case <-w.closing:
exit(v, true)
return
case w.jobs <- v:
}
}
}
}()
for i := 0; i < o.maxConcurrentWriters; i++ {
writer := &singleWarcFileWriter{opts: &o, shutWriters: w.shutWriters}
if o.compress {
writer.gz, _ = gzip.NewWriterLevel(nil, o.gzipLevel)
}
w.writers = append(w.writers, writer)
go worker(writer, w.jobs)
}
return w
}
func worker(w *singleWarcFileWriter, jobs <-chan *job) {
defer func() {
if err := w.Close(); err != nil {
log.Println(err)
}
w.shutWriters.Done()
}()
for j := range jobs {
res := make([]WriteResponse, len(j.records))
for i, r := range j.records {
res[i] = w.Write(r)
}
j.responses <- res
}
}
type job struct {
records []WarcRecord
responses chan<- []WriteResponse
}
type WriteResponse struct {
FileName string // filename
FileOffset int64 // the offset in file
BytesWritten int64 // number of uncompressed bytes written
Err error // eventual error
}
// Write marshals one or more WarcRecords to file.
//
// If more than one is written, then those will be written sequentially to the same file if size permits.
// If the writer was created with the WithAddWarcConcurrentToHeader option, each record will have cross-reference headers.
//
// Returns a slice with one WriteResponse for each record written.
func (w *WarcFileWriter) Write(record ...WarcRecord) []WriteResponse {
select {
case <-w.closed:
return nil
default:
}
job, result := w.createWriteJob(record...)
select {
case <-w.closed:
return nil
case w.middleCh <- job:
return <-result
}
}
func (w *WarcFileWriter) createWriteJob(record ...WarcRecord) (*job, <-chan []WriteResponse) {
if w.opts.addConcurrentHeader {
for k, wr := range record {
for k2, wr2 := range record {
if k == k2 {
continue
}
wr.WarcHeader().AddId(WarcConcurrentTo, wr2.WarcHeader().GetId(WarcRecordID))
}
}
}
result := make(chan []WriteResponse)
job := &job{
records: record,
responses: result,
}
return job, result
}
// Rotate closes the current files beeing written to.
//
// A call to Write after Rotate creates new files.
func (w *WarcFileWriter) Rotate() error {
var err multiErr
for _, writer := range w.writers {
if e := writer.Close(); e != nil {
err = append(err, e)
}
}
if err != nil {
return fmt.Errorf("closing error: %w", err)
}
return nil
}
// Close closes the current file(s) being written to and then releases all resources used by the WarcFileWriter.
//
// Calling Write after Close will panic.
func (w *WarcFileWriter) Close() error {
select {
case w.closing <- struct{}{}:
<-w.closed
case <-w.closed:
}
w.shutWriters.Wait()
return nil
}
type singleWarcFileWriter struct {
opts *warcFileWriterOptions
currentFileName string
currentFile *os.File
currentFileSize int64
currentWarcInfoId string
writeLock sync.Mutex
shutWriters *sync.WaitGroup
gz *gzip.Writer // Holds gzip writer, enabling reuse
}
func (w *singleWarcFileWriter) Write(record WarcRecord) (response WriteResponse) {
w.writeLock.Lock()
defer w.writeLock.Unlock()
// Calculate max record size when segmentation is enabled
var maxRecordSize int64
if w.opts.useSegmentation {
if w.opts.compress {
maxRecordSize = int64(float64(w.opts.maxFileSize) / w.opts.expectedCompressionRatio)
} else {
maxRecordSize = w.opts.maxFileSize
}
}
// Check if the current file has space for the new record
if w.currentFile != nil && w.opts.maxFileSize > 0 {
s := record.WarcHeader().Get(ContentLength)
if s != "" {
size, err := strconv.ParseInt(s, 10, 64)
if w.opts.compress {
// Take compression in account when evaluating if record will fit file
size = int64(float64(size) * w.opts.expectedCompressionRatio)
}
if err != nil {
response.Err = err
return
}
if w.currentFileSize > 0 && (w.currentFileSize+size) > w.opts.maxFileSize {
// Not enough space in file, close it so a new will be created
err = w.close()
if err != nil {
response.Err = err
return
}
}
}
}
// Create new file if necessary
if w.currentFile == nil {
if err := w.createFile(); err != nil {
response.Err = err
return
}
}
response.FileOffset = w.currentFileSize
response.FileName = w.currentFileName
response.BytesWritten, response.Err = w.writeRecord(w.currentFile, record, maxRecordSize)
if response.Err != nil {
return
}
if w.opts.flush {
// sync file to reduce possibility of half written records in case of crash
if response.Err = w.currentFile.Sync(); response.Err != nil {
return
}
}
fi, err := w.currentFile.Stat()
if err != nil {
response.Err = err
return
}
w.currentFileSize = fi.Size()
return
}
func (w *singleWarcFileWriter) createFile() error {
var suffix string
if w.opts.compress {
suffix = w.opts.compressSuffix
}
dir, fileName := w.opts.nameGenerator.NewWarcfileName()
fileName += suffix
path := dir
if path != "" && !strings.HasSuffix(path, "/") {
path += "/"
}
if w.opts.beforeFileCreationHook != nil {
_ = w.opts.beforeFileCreationHook(path + fileName)
}
path += fileName + w.opts.openFileSuffix
file, err := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666)
if err != nil {
return err
}
w.currentFileName = fileName
w.currentFile = file
if w.opts.warcInfoFunc != nil {
if _, err := w.createWarcInfoRecord(fileName); err != nil {
return err
}
}
return nil
}
func (w *singleWarcFileWriter) writeRecord(writer io.Writer, record WarcRecord, maxRecordSize int64) (int64, error) {
if w.opts.compress {
w.gz.Reset(writer)
defer func() { _ = w.gz.Close() }()
writer = w.gz
}
if w.currentWarcInfoId != "" {
record.WarcHeader().SetId(WarcWarcinfoID, w.currentWarcInfoId)
}
nextRec, size, err := w.opts.marshaler.Marshal(writer, record, maxRecordSize)
if err != nil {
return size, err
}
if nextRec != nil {
res := w.Write(nextRec)
res.BytesWritten += size
return res.BytesWritten, res.Err
}
return size, nil
}
func (w *singleWarcFileWriter) createWarcInfoRecord(fileName string) (int64, error) {
r := NewRecordBuilder(Warcinfo, w.opts.recordOptions...)
r.AddWarcHeader(WarcDate, timestamp.UTCW3cIso8601(now()))
r.AddWarcHeader(WarcFilename, fileName)
r.AddWarcHeader(ContentType, ApplicationWarcFields)
if err := w.opts.warcInfoFunc(r); err != nil {
return 0, err
}
warcinfo, _, err := r.Build()
if err != nil {
return 0, err
}
w.currentWarcInfoId = ""
n, err := w.writeRecord(w.currentFile, warcinfo, 0)
if err != nil {
return 0, err
}
w.currentWarcInfoId = warcinfo.WarcHeader().GetId(WarcRecordID)
if w.opts.flush {
// sync file to reduce possibility of half written records in case of crash
if err := w.currentFile.Sync(); err != nil {
return 0, err
}
}
fi, err := w.currentFile.Stat()
if err != nil {
return 0, err
}
w.currentFileSize = fi.Size()
return n, err
}
// Close closes the current file being written to.
//
// It is legal to call Write after close, but then a new file will be opened.
func (w *singleWarcFileWriter) Close() error {
w.writeLock.Lock()
defer w.writeLock.Unlock()
return w.close()
}
// Close closes the current file being written to.
//
// It is legal to call Write after close, but then a new file will be opened.
func (w *singleWarcFileWriter) close() error {
if w.currentFile != nil {
f := w.currentFile
w.currentFile = nil
w.currentFileName = ""
if err := f.Close(); err != nil {
return fmt.Errorf("failed to close file: %s: %w", f.Name(), err)
}
finalFileName := strings.TrimSuffix(f.Name(), w.opts.openFileSuffix)
if err := fileutil.Rename(f.Name(), finalFileName); err != nil {
return fmt.Errorf("failed to rename file: %s: %w", f.Name(), err)
}
if w.opts.afterFileCreationHook != nil {
_ = w.opts.afterFileCreationHook(finalFileName, w.currentFileSize, w.currentWarcInfoId)
}
}
return nil
}
// WarcFileReader is used to read WARC files.
// Use [NewWarcFileReader] to create a new instance.
type WarcFileReader struct {
file io.Reader
initialOffset int64
warcReader Unmarshaler
countingReader *countingreader.Reader
bufferedReader *bufio.Reader
}
var inputBufPool = sync.Pool{
New: func() interface{} {
return bufio.NewReaderSize(nil, 1024*1024)
},
}
// NewWarcFileReader creates a new [WarcFileReader] from the supplied filename.
// If offset is > 0, the reader will start reading from that offset.
// The WarcFileReader can be configured with options. See [WarcRecordOption].
func NewWarcFileReader(filename string, offset int64, opts ...WarcRecordOption) (*WarcFileReader, error) {
info, err := os.Stat(filename)
if err != nil {
return nil, err
}
if info.IsDir() {
return nil, errors.New("is directory")
}
file, err := os.Open(filename) // For read access.
if err != nil {
return nil, err
}
return NewWarcFileReaderFromStream(file, offset, opts...)
}
// NewWarcFileReaderFromStream creates a new [WarcFileReader] from the supplied io.Reader.
// The WarcFileReader can be configured with options. See [WarcRecordOption].
//
// It is the responsibility of the caller to close the io.Reader.
func NewWarcFileReaderFromStream(r io.Reader, offset int64, opts ...WarcRecordOption) (*WarcFileReader, error) {
if s, ok := r.(io.Seeker); ok {
_, err := s.Seek(offset, 0)
if err != nil {
return nil, err
}
}
wf := &WarcFileReader{
file: r,
initialOffset: offset,
warcReader: NewUnmarshaler(opts...),
countingReader: countingreader.New(r),
}
buf := inputBufPool.Get().(*bufio.Reader)
buf.Reset(wf.countingReader)
wf.bufferedReader = buf
return wf, nil
}
// Next reads the next WarcRecord from the WarcFileReader.
// The method also provides the offset at which the record is found within the file.
//
// The validation and error values that Next produces depend on the errorPolicy options that have been set on the WarcFileReader:
//
// - [ErrIgnore]: This setting ignores all errors. A WarcRecord and its offset are returned without any validation.
// An error is only returned if the file is so badly formatted that nothing meaningful can be parsed.
//
// - [ErrWarn]: Similar to ErrIgnore, this setting returns a WarcRecord and its offset.
// However, the record is validated and all validation errors are collected in a Validation object which can then be examined.
//
// - [ErrFail]: If this is set, the method will return an error in the case of a validation error, and WarcRecord might be nil.
//
// - Mixed Policies: It's possible to set different error policies for different types of errors with the following options:
// [WithSyntaxErrorPolicy], [WithSpecViolationPolicy] and [WithUnknownRecordTypePolicy].
// The return values of Next would be a mix of the aforementioned scenarios based on the policies set.
//
// When at end of file, returned offset is equal to length of file, WarcRecord is nil and err is [io.EOF].
func (wf *WarcFileReader) Next() (WarcRecord, int64, *Validation, error) {
offset := wf.initialOffset + wf.countingReader.N() - int64(wf.bufferedReader.Buffered())
record, recordOffset, validation, err := wf.warcReader.Unmarshal(wf.bufferedReader)
return record, offset + recordOffset, validation, err
}
// Close closes the WarcFileReader.
func (wf *WarcFileReader) Close() error {
inputBufPool.Put(wf.bufferedReader)
if wf.file != nil {
if c, ok := wf.file.(io.Closer); ok {
return c.Close()
}
}
return nil
}
// Options for Warc file writer
type warcFileWriterOptions struct {
maxFileSize int64
compress bool
gzipLevel int
expectedCompressionRatio float64
useSegmentation bool
compressSuffix string
openFileSuffix string
nameGenerator WarcFileNameGenerator
marshaler Marshaler
maxConcurrentWriters int
warcInfoFunc func(recordBuilder WarcRecordBuilder) error
addConcurrentHeader bool
flush bool
beforeFileCreationHook func(fileName string) error
afterFileCreationHook func(fileName string, size int64, warcInfoId string) error
recordOptions []WarcRecordOption
}
func (w *warcFileWriterOptions) String() string {
return fmt.Sprintf("File size: %d, Compressed: %v, Num writers: %d", w.maxFileSize, w.compress, w.maxConcurrentWriters)
}
// WarcFileWriterOption configures how to write WARC files.
type WarcFileWriterOption interface {
apply(*warcFileWriterOptions)
}
// funcWarcFileWriterOption wraps a function that modifies warcFileWriterOptions into an
// implementation of the WarcFileWriterOption interface.
type funcWarcFileWriterOption struct {
f func(*warcFileWriterOptions)
}
func (fo *funcWarcFileWriterOption) apply(po *warcFileWriterOptions) {
fo.f(po)
}
func newFuncWarcFileOption(f func(*warcFileWriterOptions)) *funcWarcFileWriterOption {
return &funcWarcFileWriterOption{
f: f,
}
}
func defaultwarcFileWriterOptions() warcFileWriterOptions {
return warcFileWriterOptions{
maxFileSize: 1024 * 1024 * 1024, // 1 GiB
compress: true,
gzipLevel: gzip.DefaultCompression,
expectedCompressionRatio: .5,
useSegmentation: false,
compressSuffix: ".gz",
openFileSuffix: ".open",
nameGenerator: &PatternNameGenerator{},
marshaler: &defaultMarshaler{},
maxConcurrentWriters: 1,
addConcurrentHeader: false,
recordOptions: []WarcRecordOption{},
}
}
// WithMaxFileSize sets the max size of the Warc file before creating a new one.
//
// defaults to 1 GiB
func WithMaxFileSize(size int64) WarcFileWriterOption {
return newFuncWarcFileOption(func(o *warcFileWriterOptions) {
o.maxFileSize = size
})
}
// WithCompression sets if writer should write gzip compressed WARC files.
//
// defaults to true
func WithCompression(compress bool) WarcFileWriterOption {
return newFuncWarcFileOption(func(o *warcFileWriterOptions) {
o.compress = compress
})
}
// WithCompressionLevel sets the gzip level (1-9) to use for compression.
//
// defaults to 5
func WithCompressionLevel(gzipLevel int) WarcFileWriterOption {
return newFuncWarcFileOption(func(o *warcFileWriterOptions) {
if gzipLevel == gzip.DefaultCompression {
gzipLevel = 5
}
if gzipLevel < gzip.BestSpeed || gzipLevel > gzip.BestCompression {
panic("illegal compression level " + strconv.Itoa(gzipLevel) + ", must be between 1 and 9")
}
o.gzipLevel = gzipLevel
})
}
// WithFlush sets if writer should commit each record to stable storage.
//
// defaults to false
func WithFlush(flush bool) WarcFileWriterOption {
return newFuncWarcFileOption(func(o *warcFileWriterOptions) {
o.flush = flush
})
}
// WithSegmentation sets if writer should use segmentation for large WARC records.
//
// defaults to false
func WithSegmentation() WarcFileWriterOption {
return newFuncWarcFileOption(func(o *warcFileWriterOptions) {
o.useSegmentation = true
})
}
// WithCompressedFileSuffix sets a suffix to be added after the name generated by the WarcFileNameGenerator id compression is on.
//
// defaults to ".gz"
func WithCompressedFileSuffix(suffix string) WarcFileWriterOption {
return newFuncWarcFileOption(func(o *warcFileWriterOptions) {
o.compressSuffix = suffix
})
}
// WithOpenFileSuffix sets a suffix to be added to the file name while the file is open for writing.
//
// The suffix is automatically removed when the file is closed.
//
// defaults to ".open"
func WithOpenFileSuffix(suffix string) WarcFileWriterOption {
return newFuncWarcFileOption(func(o *warcFileWriterOptions) {
o.openFileSuffix = suffix
})
}
// WithFileNameGenerator sets the WarcFileNameGenerator to use for generating new Warc file names.
//
// Default is to use a [PatternNameGenerator] with the default pattern.
func WithFileNameGenerator(generator WarcFileNameGenerator) WarcFileWriterOption {
return newFuncWarcFileOption(func(o *warcFileWriterOptions) {
o.nameGenerator = generator
})
}
// WithMarshaler sets the Warc record marshaler to use.
//
// defaults to defaultMarshaler
func WithMarshaler(marshaler Marshaler) WarcFileWriterOption {
return newFuncWarcFileOption(func(o *warcFileWriterOptions) {
o.marshaler = marshaler
})
}
// WithMaxConcurrentWriters sets the maximum number of Warc files that can be written simultaneously.
//
// defaults to one
func WithMaxConcurrentWriters(count int) WarcFileWriterOption {
return newFuncWarcFileOption(func(o *warcFileWriterOptions) {
o.maxConcurrentWriters = count
})
}
// WithExpectedCompressionRatio sets the expectd reduction in size when using compression.
//
// This value is used to decide if a record will fit into a Warcfile's MaxFileSize when using compression
// since it's not possible to know this before the record is written. If the value is far from the actual size reduction,
// an under- or overfilled file might be the result.
//
// defaults to .5 (half the uncompressed size)
func WithExpectedCompressionRatio(ratio float64) WarcFileWriterOption {
return newFuncWarcFileOption(func(o *warcFileWriterOptions) {
o.expectedCompressionRatio = ratio
})
}
// WithWarcInfoFunc sets a warcinfo-record generator function to be called for every new WARC-file created.
//
// The function receives a [WarcRecordBuilder] which is prepopulated with WARC-Record-ID, WARC-Type, WARC-Date and Content-Type.
// After the submitted function returns, Content-Length and WARC-Block-Digest fields are calculated.
//
// When this option is set, records written to the warcfile will have the WARC-Warcinfo-ID automatically set to point
// to the generated warcinfo record.
//
// Use [WithRecordOptions] to modify the options used to create the WarcInfo record.
//
// defaults nil (no generation of warcinfo record)
func WithWarcInfoFunc(f func(recordBuilder WarcRecordBuilder) error) WarcFileWriterOption {
return newFuncWarcFileOption(func(o *warcFileWriterOptions) {
o.warcInfoFunc = f
})
}
// WithAddWarcConcurrentToHeader configures if records written in the same call to Write should have WARC-Concurrent-To
// headers added for cross-reference.
//
// default false
func WithAddWarcConcurrentToHeader(addConcurrentHeader bool) WarcFileWriterOption {
return newFuncWarcFileOption(func(o *warcFileWriterOptions) {
o.addConcurrentHeader = addConcurrentHeader
})
}
// WithRecordOptions sets the options to use for creating WarcInfo records.
//
// See WithWarcInfoFunc
func WithRecordOptions(opts ...WarcRecordOption) WarcFileWriterOption {
return newFuncWarcFileOption(func(o *warcFileWriterOptions) {
o.recordOptions = opts
})
}
// WithBeforeFileCreationHook sets a function to be called before a new file is created.
//
// The function receives the file name of the new file.
func WithBeforeFileCreationHook(f func(fileName string) error) WarcFileWriterOption {
return newFuncWarcFileOption(func(o *warcFileWriterOptions) {
o.beforeFileCreationHook = f
})
}
// WithAfterFileCreationHook sets a function to be called after a new file is created.
//
// The function receives the file name of the new file, the size of the file and the WARC-Warcinfo-ID.
func WithAfterFileCreationHook(f func(fileName string, size int64, warcInfoId string) error) WarcFileWriterOption {
return newFuncWarcFileOption(func(o *warcFileWriterOptions) {
o.afterFileCreationHook = f
})
}