dbcrossbarlib 0.5.3

Library for copying data between databases (pre-release)
Documentation
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
//! Driver for working with CSV files.

use std::{
    ffi::{OsStr, OsString},
    fmt,
    path::PathBuf,
    str::FromStr,
};
use tokio::{
    fs,
    io::{self, BufReader},
};
use tracing::{field, Span};
use walkdir::WalkDir;

use crate::tokio_glue::{copy_reader_to_stream, copy_stream_to_writer};
use crate::{common::*, locator::PathLikeLocator};
use crate::{concat::concatenate_csv_streams, data_streams::DataStream};
use crate::{csv_stream::csv_stream_name, DataFormat};

/// (Incomplete.) A CSV file containing data, or a directory containing CSV
/// files.
///
/// TODO: Right now, we take a file path as input and a directory path as
/// output, because we're lazy and haven't finished building this.
#[derive(Clone, Debug)]
pub(crate) struct FileLocator {
    path: PathOrStdio,
}

impl FileLocator {
    /// Construt a `FileLocator` from a path.
    pub(crate) fn from_path<P: Into<PathBuf>>(path: P) -> Self {
        Self {
            path: PathOrStdio::Path(path.into()),
        }
    }

    /// Construct a `FileLocator` using stdin/stdout.
    pub(crate) fn from_stdio() -> Self {
        Self {
            path: PathOrStdio::Stdio,
        }
    }
}

impl fmt::Display for FileLocator {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.path.fmt_locator_helper(Self::scheme(), f)
    }
}

impl FromStr for FileLocator {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self> {
        let path = PathOrStdio::from_str_locator_helper(Self::scheme(), s)?;
        Ok(FileLocator { path })
    }
}

impl Locator for FileLocator {
    fn as_any(&self) -> &dyn Any {
        self
    }

    fn schema(&self, ctx: Context) -> BoxFuture<Option<Schema>> {
        schema_helper(ctx, self.clone()).boxed()
    }

    fn local_data(
        &self,
        ctx: Context,
        shared_args: SharedArguments<Unverified>,
        source_args: SourceArguments<Unverified>,
    ) -> BoxFuture<Option<BoxStream<CsvStream>>> {
        local_data_helper(ctx, self.path.clone(), shared_args, source_args).boxed()
    }

    fn display_output_locators(&self) -> DisplayOutputLocators {
        match &self.path {
            // If we write our data to standard output, we don't also want to
            // print out "csv:-" to the same standard output.
            PathOrStdio::Stdio => DisplayOutputLocators::Never,
            _ => DisplayOutputLocators::IfRequested,
        }
    }

    fn write_local_data(
        &self,
        ctx: Context,
        data: BoxStream<CsvStream>,
        shared_args: SharedArguments<Unverified>,
        dest_args: DestinationArguments<Unverified>,
    ) -> BoxFuture<BoxStream<BoxFuture<BoxLocator>>> {
        write_local_data_helper(ctx, self.path.clone(), data, shared_args, dest_args)
            .boxed()
    }
}

#[instrument(level = "trace", name = "file::schema")]
async fn schema_helper(ctx: Context, locator: FileLocator) -> Result<Option<Schema>> {
    match &locator.path {
        PathOrStdio::Stdio => {
            // This is actually fairly tricky, because we may need to first
            // read the columns from stdin, _then_ start re-reading from the
            // beginning to read the data when `local_data` is called.
            Err(format_err!("cannot yet read schema from stdin"))
        }
        PathOrStdio::Path(_) if locator.is_directory_like() => {
            Err(format_err!("cannot read schema from directory {}", locator))
        }
        PathOrStdio::Path(path) => {
            let data_stream = path_to_data_stream(
                ctx.clone(),
                path.parent().unwrap().to_owned(),
                path.to_owned(),
                DataFormat::Csv,
            )
            .await?;
            data_stream.schema(&ctx).await
        }
    }
}

#[instrument(
    level = "trace",
    name = "file::local_data",
    skip_all,
    fields(path = %path)
)]
async fn local_data_helper(
    ctx: Context,
    path: PathOrStdio,
    shared_args: SharedArguments<Unverified>,
    source_args: SourceArguments<Unverified>,
) -> Result<Option<BoxStream<CsvStream>>> {
    let shared_args = shared_args.verify(FileLocator::features())?;
    let schema = shared_args.schema().to_owned();

    let source_args = source_args.verify(FileLocator::features())?;
    let from_format = source_args.format().cloned();

    match path {
        PathOrStdio::Stdio => {
            let data = BufReader::with_capacity(BUFFER_SIZE, io::stdin());
            let stream = copy_reader_to_stream(data)?;
            let data_stream = DataStream {
                name: "data".to_owned(),
                format: from_format.unwrap_or_default(),
                data: stream
                    .map_err(move |e| format_err!("cannot read stdin: {}", e))
                    .boxed(),
            };
            let csv_stream = data_stream.into_csv_stream(&ctx, &schema).await?;
            Ok(Some(box_stream_once(Ok(csv_stream))))
        }
        PathOrStdio::Path(base_path) => {
            // Recursively look at our paths, picking out the ones that look
            // like CSVs. We do this synchronously because it's reasonably
            // fast and we'd like to catch errors up front.
            let mut paths = vec![];
            debug!("walking {}", base_path.display());
            let walker = WalkDir::new(&base_path).follow_links(true);
            let mut common_ext: Option<Option<OsString>> = None;
            for dirent in walker.into_iter() {
                let dirent = dirent.with_context(|| {
                    format!("error listing files in {}", base_path.display())
                })?;
                let p = dirent.path();
                trace!("found dirent {}", p.display());
                if dirent.file_type().is_dir() {
                    continue;
                } else if !dirent.file_type().is_file() {
                    return Err(format_err!("not a file: {}", p.display()));
                }

                let ext = p.extension().map(OsStr::to_ascii_lowercase);
                if let Some(common_ext) = &common_ext {
                    if ext != *common_ext {
                        return Err(format_err!(
                            "all files in {} must have the same extension",
                            base_path.display()
                        ));
                    }
                } else {
                    common_ext = Some(ext);
                }
                paths.push(p.to_owned());
            }
            let common_ext = common_ext.ok_or_else(|| {
                format_err!("no files found in {}", base_path.display())
            })?;
            let format = from_format
                .or(common_ext.map(|ext| DataFormat::from_extension(&ext)))
                .unwrap_or_default();

            let csv_streams = stream::iter(paths).map(Ok).and_then(move |file_path| {
                let ctx = ctx.clone();
                let schema = schema.clone();
                let base_path = base_path.clone();
                let file_path_copy = file_path.clone();
                let format = format.clone();
                async move {
                    let data_stream = path_to_data_stream(
                        ctx.clone(),
                        base_path.clone(),
                        file_path,
                        format,
                    ).await?;
                    data_stream.into_csv_stream(&ctx, &schema).await
                }
                .instrument(debug_span!("stream_from_file", file_path = %file_path_copy.display(), stream.name = field::Empty))
                .boxed()
            });

            Ok(Some(csv_streams.boxed()))
        }
    }
}

#[instrument(
    level = "debug",
    name = "file::path_to_data_stream",
    skip_all,
    fields(file_path = %file_path.display(), stream.name = field::Empty),
)]
async fn path_to_data_stream(
    _ctx: Context,
    base_path: PathBuf,
    file_path: PathBuf,
    format: DataFormat,
) -> Result<DataStream> {
    // Get the name of our stream.
    let name =
        csv_stream_name(&base_path.to_string_lossy(), &file_path.to_string_lossy())?
            .to_owned();
    Span::current().record("stream.name", &field::display(&name));

    // Open our file.
    let f = fs::File::open(file_path.clone())
        .await
        .with_context(|| format!("cannot open {}", file_path.display()))?;
    let rdr = BufReader::with_capacity(BUFFER_SIZE, f);
    let stream = copy_reader_to_stream(rdr)?;
    let data = stream
        .map_err(move |e| format_err!("cannot read {}: {}", file_path.display(), e))
        .boxed();
    let data_stream = DataStream { name, format, data };
    Ok(data_stream)
}

#[instrument(
    level = "debug",
    name = "file::write_local_data",
    skip_all,
    fields(path = %path)
)]
async fn write_local_data_helper(
    ctx: Context,
    path: PathOrStdio,
    data: BoxStream<CsvStream>,
    shared_args: SharedArguments<Unverified>,
    dest_args: DestinationArguments<Unverified>,
) -> Result<BoxStream<BoxFuture<BoxLocator>>> {
    let shared_args = shared_args.verify(FileLocator::features())?;
    let schema = shared_args.schema().to_owned();
    let dest_args = dest_args.verify(FileLocator::features())?;
    let if_exists = dest_args.if_exists().to_owned();
    match path {
        PathOrStdio::Stdio => {
            let format = dest_args.format().cloned().unwrap_or_default();
            if_exists.warn_if_not_default_for_stdout();
            let csv_stream = concatenate_csv_streams(ctx.clone(), data)?;
            let data_stream =
                DataStream::from_csv_stream(&ctx, format, &schema, csv_stream).await?;
            let fut = async move {
                copy_stream_to_writer(data_stream.data, io::stdout())
                    .await
                    .context("error writing to stdout")?;
                Ok(FileLocator {
                    path: PathOrStdio::Stdio,
                }
                .boxed())
            };
            Ok(box_stream_once(Ok(fut.boxed())))
        }
        PathOrStdio::Path(path) => {
            if path.to_string_lossy().ends_with('/') {
                // Write streams to our directory as multiple files.
                let format = dest_args.format().cloned().unwrap_or_default();
                let result_stream = data.map_ok(move |stream| {
                    let ctx = ctx.clone();
                    let path = path.clone();
                    let schema = schema.clone();
                    let format = format.clone();
                    let if_exists = if_exists.clone();
                    let stream_name = stream.name.clone();

                    async move {
                        // TODO: This join does not handle `..` or nested `/` in
                        // a particularly safe fashion.
                        let csv_path = path.join(format!("{}.csv", stream.name));
                        Span::current().record("path", &field::display(csv_path.display()));
                        let data_stream =
                            DataStream::from_csv_stream(&ctx, format, &schema, stream).await?;
                        write_stream_to_file(
                            data_stream.data,
                            csv_path.clone(),
                            if_exists,
                        )
                        .await?;
                        Ok(FileLocator::from_path(csv_path).boxed())
                    }.instrument(trace_span!("stream_to_file", stream.name = %stream_name, path = field::Empty))
                    .boxed()
                });
                Ok(result_stream.boxed())
            } else {
                // Write all our streams as a single file.
                let format_for_ext = path.extension().map(DataFormat::from_extension);
                let format = dest_args
                    .format()
                    .cloned()
                    .or(format_for_ext)
                    .unwrap_or_default();

                let stream = concatenate_csv_streams(ctx.clone(), data)?;
                let stream_name = stream.name.clone();
                let path_copy = path.clone();
                let data_stream =
                    DataStream::from_csv_stream(&ctx, format, &schema, stream).await?;
                let fut = async move {
                    write_stream_to_file(data_stream.data, path.clone(), if_exists)
                        .await?;
                    Ok(FileLocator::from_path(path).boxed())
                }.instrument(trace_span!("stream_to_file", stream.name = %stream_name, path = %path_copy.display()));
                Ok(box_stream_once(Ok(fut.boxed())))
            }
        }
    }
}

/// Write `data` to `dest`, honoring `if_exists`.
async fn write_stream_to_file(
    data: BoxStream<BytesMut>,
    dest: PathBuf,
    if_exists: IfExists,
) -> Result<()> {
    // Make sure our destination directory exists.
    let dir = dest
        .parent()
        .ok_or_else(|| format_err!("cannot find parent dir for {}", dest.display()))?;
    fs::create_dir_all(dir)
        .await
        .with_context(|| format!("unable to create directory {}", dir.display()))?;

    // Write our our CSV stream.
    debug!("writing stream to file {}", dest.display());
    let wtr = if_exists
        .to_async_open_options_no_append()?
        .open(dest.clone())
        .await
        .with_context(|| format!("cannot open {}", dest.display()))?;
    copy_stream_to_writer(data, wtr)
        .await
        .with_context(|| format!("error writing {}", dest.display()))?;
    Ok(())
}

impl LocatorStatic for FileLocator {
    fn scheme() -> &'static str {
        "file:"
    }

    fn features() -> Features {
        Features {
            locator: LocatorFeatures::LocalData | LocatorFeatures::WriteLocalData,
            write_schema_if_exists: EnumSet::empty(),
            source_args: SourceArgumentsFeatures::Format.into(),
            dest_args: DestinationArgumentsFeatures::Format.into(),
            dest_if_exists: IfExistsFeatures::no_append(),
            _placeholder: (),
        }
    }
}

impl PathLikeLocator for FileLocator {
    fn path(&self) -> Option<&OsStr> {
        match &self.path {
            PathOrStdio::Path(path) => Some(path.as_os_str()),
            PathOrStdio::Stdio => None,
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::data_streams::DataFormat;

    use super::*;

    #[test]
    fn test_directory_locator_has_correct_path_like_properties() {
        let locator = FileLocator::from_str("file:/path/").unwrap();
        assert_eq!(locator.path().unwrap(), "/path/");
        assert!(locator.is_directory_like());
        assert!(locator.extension().is_none());
        assert!(locator.data_format().is_none());
    }

    #[test]
    fn test_csv_file_locator_has_correct_path_like_properties() {
        let locator = FileLocator::from_str("file:/path/file.csv").unwrap();
        assert_eq!(locator.path().unwrap(), "/path/file.csv");
        assert!(!locator.is_directory_like());
        assert_eq!(locator.extension().unwrap(), "csv");
        assert_eq!(locator.data_format(), Some(DataFormat::Csv));
    }

    #[test]
    fn test_jsonl_file_locator_has_correct_path_like_properties() {
        let locator = FileLocator::from_str("file:/path/file.jsonl").unwrap();
        assert_eq!(locator.path().unwrap(), "/path/file.jsonl");
        assert!(!locator.is_directory_like());
        assert_eq!(locator.extension().unwrap(), "jsonl");
        assert_eq!(locator.data_format(), Some(DataFormat::JsonLines));
    }
}