-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathencoder_options.go
More file actions
142 lines (119 loc) · 3.89 KB
/
encoder_options.go
File metadata and controls
142 lines (119 loc) · 3.89 KB
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
package pbf
import (
"errors"
"fmt"
"os"
"path"
"time"
"m4o.io/pbf/v2/internal/encoder"
)
const (
DefaultBlobCompression = encoder.ZLIB
tempFileName = "entities.pbf"
)
var (
// ErrCreateTempDir is returned when the encoder cannot create a temporary directory.
ErrCreateTempDir = errors.New("create temporary directory")
// ErrCreateTempFile is returned when the encoder cannot create a temporary file.
ErrCreateTempFile = errors.New("create temporary file")
)
// encoderOptions provides optional configuration parameters for Encoder construction.
type encoderOptions struct {
compression encoder.BlobCompression
nCPU uint16 // the number of CPUs to use for background processing
store string
wrtr *os.File
requiredFeatures []string
optionalFeatures []string
writingProgram string
source string
osmosisReplicationTimestamp time.Time
osmosisReplicationSequenceNumber int64
osmosisReplicationBaseURL string
}
// EncoderOption configures how we set up the encoder.
type EncoderOption func(*encoderOptions)
// WithCompression specifies the compression algorithm to use when encoding
// PBF blobs. The default is ZLIB.
func WithCompression(compression encoder.BlobCompression) EncoderOption {
return func(o *encoderOptions) {
o.compression = compression
}
}
// WithStorePath lets you specify where to temporarily store entities.
func WithStorePath(path string) EncoderOption {
return func(o *encoderOptions) {
o.store = path
}
}
// WithRequiredFeatures sets the required features of the PBF header.
func WithRequiredFeatures(features ...string) EncoderOption {
return func(o *encoderOptions) {
o.requiredFeatures = append(o.requiredFeatures, features...)
}
}
// WithOptionalFeatures sets the optional features of the PBF header.
func WithOptionalFeatures(features ...string) EncoderOption {
return func(o *encoderOptions) {
o.optionalFeatures = append(o.optionalFeatures, features...)
}
}
// WithWritingProgram sets the writing program of the PBF header.
func WithWritingProgram(program string) EncoderOption {
return func(o *encoderOptions) {
o.writingProgram = program
}
}
// WithSource sets the source of the PBF header.
func WithSource(source string) EncoderOption {
return func(o *encoderOptions) {
o.source = source
}
}
// WithOsmosisReplicationTimestamp sets the Osmosis replication timestamp of
// the PBF header.
func WithOsmosisReplicationTimestamp(timestamp time.Time) EncoderOption {
return func(o *encoderOptions) {
o.osmosisReplicationTimestamp = timestamp
}
}
// WithOsmosisReplicationSequenceNumber sets the Osmosis replication sequence
// number of the PBF header.
func WithOsmosisReplicationSequenceNumber(sequenceNumber int64) EncoderOption {
return func(o *encoderOptions) {
o.osmosisReplicationSequenceNumber = sequenceNumber
}
}
// WithOsmosisReplicationBaseURL sets the Osmosis replication base URL of the
// PBF header.
func WithOsmosisReplicationBaseURL(url string) EncoderOption {
return func(o *encoderOptions) {
o.osmosisReplicationBaseURL = url
}
}
// defaultEncoderConfig provides a default configuration for encoders.
var defaultEncoderConfig = encoderOptions{
compression: DefaultBlobCompression,
requiredFeatures: []string{
"OsmSchema-V0.6",
"DenseNodes",
"HistoricalInformation",
},
}
// initializeTempStore initializes the temporary file that entities are stored
// before being copied, after the header, to the io.Writer passed to the encoder.
func initializeTempStore(o *encoderOptions) error {
if o.store == "" {
tmpdir, err := os.MkdirTemp("", "pbf")
if err != nil {
return fmt.Errorf("%w: %w", ErrCreateTempDir, err)
}
o.store = tmpdir
}
if wrtr, err := os.Create(path.Join(o.store, tempFileName)); err != nil {
return fmt.Errorf("%w %s: %w", ErrCreateTempFile, path.Join(o.store, tempFileName), err)
} else {
o.wrtr = wrtr
}
return nil
}