Skip to main content

faucet_source_gcs/
config.rs

1//! GCS source configuration.
2
3use faucet_common_gcs::GcsCredentials;
4use faucet_core::DEFAULT_BATCH_SIZE;
5use schemars::JsonSchema;
6use serde::{Deserialize, Serialize};
7
8/// Format of files stored in GCS.
9#[derive(Debug, Clone, Default, Serialize, Deserialize, JsonSchema)]
10#[serde(rename_all = "snake_case")]
11pub enum GcsFileFormat {
12    /// Each line in the file is a separate JSON record.
13    #[default]
14    JsonLines,
15    /// The entire file is a JSON array of records.
16    JsonArray,
17    /// Each file becomes a single record with `"key"` and `"content"` fields.
18    RawText,
19    /// Apache Parquet objects. Decoded via the Arrow Parquet reader; the
20    /// resulting `RecordBatch`es feed both the row path (converted to JSON)
21    /// and the **columnar** fast path
22    /// ([`Source::stream_batches`](faucet_core::Source::stream_batches)) so a
23    /// `gcs(parquet) → parquet`/`delta` chain never materializes
24    /// `serde_json::Value`. Requires the crate-local `arrow` feature
25    /// (RFC 0002 / #375).
26    #[cfg(feature = "arrow")]
27    Parquet,
28}
29
30/// Configuration for the GCS source connector.
31#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
32pub struct GcsSourceConfig {
33    /// GCS bucket name.
34    pub bucket: String,
35    /// Object name prefix filter. Ignored when `object_keys` is set.
36    pub prefix: Option<String>,
37    /// Explicit object names. When set, listing is skipped and `prefix`
38    /// is ignored.
39    pub object_keys: Option<Vec<String>>,
40    /// Credential source.
41    #[serde(default)]
42    pub auth: GcsCredentials,
43    /// File format.
44    #[serde(default)]
45    pub file_format: GcsFileFormat,
46    /// Hard cap on the number of objects read (after listing).
47    pub max_objects: Option<usize>,
48    /// Maximum concurrent object reads (default: 10).
49    #[serde(default = "default_concurrency")]
50    pub concurrency: usize,
51    /// Records per emitted `StreamPage`. See "Streaming and batching"
52    /// in the README. `batch_size = 0` is the "no batching" sentinel and
53    /// emits one page per object.
54    #[serde(default = "default_batch_size")]
55    pub batch_size: usize,
56    /// Verify each object's byte length against the `size` GCS reports for it,
57    /// failing the read with [`FaucetError::Source`](faucet_core::FaucetError::Source)
58    /// on a short (truncated) or over-long transfer (#161). Cheap (a byte
59    /// counter over the body that is read anyway) and defaults to `true`.
60    /// The check is automatically skipped for an object served with a
61    /// non-empty `Content-Encoding` (GCS may decompressively transcode it on
62    /// read, so the received byte count would not match the stored `size`).
63    #[serde(default = "default_true")]
64    pub verify_length: bool,
65    /// Verify each object's body against the CRC32C (or MD5) checksum GCS
66    /// reports for it (#161). Stronger than the length check but costs a hash
67    /// over the full body, so it defaults to `false`. Skipped for an object
68    /// with no usable checksum or one served with a non-empty
69    /// `Content-Encoding` (the stored checksum covers the stored bytes, which
70    /// transcoding would not return).
71    #[serde(default)]
72    pub verify_checksum: bool,
73    /// Optional storage-host override (e.g. `http://localhost:4443` for
74    /// fake-gcs-server). Production users should leave this unset.
75    pub storage_host: Option<String>,
76    /// Compression codec applied to each downloaded object. Defaults to
77    /// [`CompressionConfig::Auto`](faucet_core::CompressionConfig::Auto) —
78    /// the codec is resolved per-object-key, so a single source can read a
79    /// mix of compressed and uncompressed objects. Requires the
80    /// crate-local `compression` feature.
81    #[cfg(feature = "compression")]
82    #[serde(default)]
83    pub compression: faucet_core::CompressionConfig,
84}
85
86fn default_batch_size() -> usize {
87    DEFAULT_BATCH_SIZE
88}
89fn default_concurrency() -> usize {
90    10
91}
92fn default_true() -> bool {
93    true
94}
95
96impl GcsSourceConfig {
97    /// Create a new config with the required bucket name and sensible defaults.
98    pub fn new(bucket: impl Into<String>) -> Self {
99        Self {
100            bucket: bucket.into(),
101            prefix: None,
102            object_keys: None,
103            auth: GcsCredentials::default(),
104            file_format: GcsFileFormat::default(),
105            max_objects: None,
106            concurrency: default_concurrency(),
107            batch_size: default_batch_size(),
108            verify_length: true,
109            verify_checksum: false,
110            storage_host: None,
111            #[cfg(feature = "compression")]
112            compression: faucet_core::CompressionConfig::default(),
113        }
114    }
115
116    pub fn prefix(mut self, prefix: impl Into<String>) -> Self {
117        self.prefix = Some(prefix.into());
118        self
119    }
120
121    pub fn object_keys(mut self, keys: Vec<String>) -> Self {
122        self.object_keys = Some(keys);
123        self
124    }
125
126    pub fn auth(mut self, creds: GcsCredentials) -> Self {
127        self.auth = creds;
128        self
129    }
130
131    pub fn file_format(mut self, format: GcsFileFormat) -> Self {
132        self.file_format = format;
133        self
134    }
135
136    pub fn max_objects(mut self, max: usize) -> Self {
137        self.max_objects = Some(max);
138        self
139    }
140
141    pub fn concurrency(mut self, concurrency: usize) -> Self {
142        self.concurrency = concurrency;
143        self
144    }
145
146    pub fn with_batch_size(mut self, batch_size: usize) -> Self {
147        self.batch_size = batch_size;
148        self
149    }
150
151    pub fn storage_host(mut self, host: impl Into<String>) -> Self {
152        self.storage_host = Some(host.into());
153        self
154    }
155
156    /// Enable or disable the per-object length verification (default `true`).
157    /// See [`verify_length`](Self::verify_length).
158    pub fn verify_length(mut self, verify: bool) -> Self {
159        self.verify_length = verify;
160        self
161    }
162
163    /// Enable or disable per-object checksum verification (default `false`).
164    /// See [`verify_checksum`](Self::verify_checksum).
165    pub fn verify_checksum(mut self, verify: bool) -> Self {
166        self.verify_checksum = verify;
167        self
168    }
169
170    /// Set the compression codec. Available only with the `compression` feature.
171    #[cfg(feature = "compression")]
172    pub fn compression(mut self, c: faucet_core::CompressionConfig) -> Self {
173        self.compression = c;
174        self
175    }
176}
177
178#[cfg(test)]
179mod tests {
180    use super::*;
181
182    #[test]
183    fn default_config() {
184        let config = GcsSourceConfig::new("my-bucket");
185        assert_eq!(config.bucket, "my-bucket");
186        assert!(config.prefix.is_none());
187        assert!(config.object_keys.is_none());
188        assert!(matches!(config.auth, GcsCredentials::ApplicationDefault));
189        assert!(matches!(config.file_format, GcsFileFormat::JsonLines));
190        assert!(config.max_objects.is_none());
191        assert_eq!(config.concurrency, 10);
192        assert_eq!(config.batch_size, faucet_core::DEFAULT_BATCH_SIZE);
193        assert!(config.storage_host.is_none());
194    }
195
196    #[test]
197    fn builder_methods() {
198        let config = GcsSourceConfig::new("my-bucket")
199            .prefix("data/")
200            .file_format(GcsFileFormat::JsonArray)
201            .max_objects(5)
202            .concurrency(20)
203            .with_batch_size(250)
204            .storage_host("http://localhost:4443");
205
206        assert_eq!(config.bucket, "my-bucket");
207        assert_eq!(config.prefix.as_deref(), Some("data/"));
208        assert!(matches!(config.file_format, GcsFileFormat::JsonArray));
209        assert_eq!(config.max_objects, Some(5));
210        assert_eq!(config.concurrency, 20);
211        assert_eq!(config.batch_size, 250);
212        assert_eq!(
213            config.storage_host.as_deref(),
214            Some("http://localhost:4443")
215        );
216    }
217
218    #[test]
219    fn file_format_default_is_json_lines() {
220        assert!(matches!(GcsFileFormat::default(), GcsFileFormat::JsonLines));
221    }
222
223    #[test]
224    fn batch_size_zero_is_accepted_as_no_batching_sentinel() {
225        let config = GcsSourceConfig::new("b").with_batch_size(0);
226        assert_eq!(config.batch_size, 0);
227        assert!(faucet_core::validate_batch_size(config.batch_size).is_ok());
228    }
229
230    #[test]
231    fn batch_size_above_max_is_rejected() {
232        let config = GcsSourceConfig::new("b").with_batch_size(faucet_core::MAX_BATCH_SIZE + 1);
233        assert!(faucet_core::validate_batch_size(config.batch_size).is_err());
234    }
235
236    #[cfg(feature = "compression")]
237    #[test]
238    fn compression_default_is_auto() {
239        let cfg = GcsSourceConfig::new("bucket");
240        assert_eq!(cfg.compression, faucet_core::CompressionConfig::Auto);
241    }
242
243    #[test]
244    fn verify_defaults_length_on_checksum_off() {
245        let cfg = GcsSourceConfig::new("b");
246        assert!(cfg.verify_length);
247        assert!(!cfg.verify_checksum);
248    }
249
250    #[test]
251    fn verify_builders_override() {
252        let cfg = GcsSourceConfig::new("b")
253            .verify_length(false)
254            .verify_checksum(true);
255        assert!(!cfg.verify_length);
256        assert!(cfg.verify_checksum);
257    }
258
259    #[test]
260    fn verify_fields_default_when_absent_from_json() {
261        let json = r#"{
262            "bucket": "my-bucket",
263            "prefix": null,
264            "object_keys": null,
265            "file_format": "json_lines",
266            "max_objects": null,
267            "concurrency": 10,
268            "storage_host": null
269        }"#;
270        let config: GcsSourceConfig = serde_json::from_str(json).unwrap();
271        assert!(config.verify_length);
272        assert!(!config.verify_checksum);
273    }
274
275    #[test]
276    fn batch_size_defaults_when_omitted_from_json() {
277        let json = r#"{
278            "bucket": "my-bucket",
279            "prefix": null,
280            "object_keys": null,
281            "file_format": "json_lines",
282            "max_objects": null,
283            "concurrency": 10,
284            "storage_host": null
285        }"#;
286        let config: GcsSourceConfig = serde_json::from_str(json).unwrap();
287        assert_eq!(config.batch_size, faucet_core::DEFAULT_BATCH_SIZE);
288    }
289}