Skip to content

Commit

Permalink
IO: Ensure parent directory exists before writing file (#34)
Browse files Browse the repository at this point in the history
* [FileSystemByteSink] Ensure the parent directory exists

* Dumper: Hide prepare() call in FileSystemByteSink.

Co-authored-by: Bart Wiegmans <[email protected]>
  • Loading branch information
shevek-google and bdw-g authored Jun 14, 2022
1 parent a1b0911 commit 436c527
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ public void addTasksTo(List<? super Task<?>> out, ConnectorArguments arguments)
// Not documented.
out.add(new JdbcSelectTask("nz.v_objects.csv", "SELECT * FROM system.._v_objects"));

// TODO; these might be placed in a ParallelTaskGroup?`
// these neeed to be filtered on WHERE = dbname, or else which DB md table will contain SYSTEM data too
for (String db : dbs) {
// The benefit of having this reduces the amount of data in the zip file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class FileSystemOutputHandle implements OutputHandle {
private final Path targetPath;

public FileSystemOutputHandle(@Nonnull Path rootPath, @Nonnull String targetPath) {
// Due to the semantics of prepare(), both of these must be within the same subdirectory.
this.targetPath = rootPath.resolve(targetPath);
this.temporaryPath = rootPath.resolve(targetPath + ".tmp");
// LOG.debug("Created " + this);
Expand All @@ -50,17 +51,29 @@ public boolean exists() throws IOException {
}

@Override
public ByteSink asByteSink() {
public ByteSink asByteSink() throws IOException {
// LOG.debug("As ByteSink: " + this + " = " + targetPath);
prepare();
return new FileSystemByteSink(targetPath);
}

@Override
public ByteSink asTemporaryByteSink() {
public ByteSink asTemporaryByteSink() throws IOException {
// LOG.debug("As Temporary ByteSink: " + this + " = " + temporaryPath);
prepare();
return new FileSystemByteSink(temporaryPath);
}

/**
* Ensures that the target file can be written.
*
* Must be called before calling openStream() on a ByteStream acquired from this object.
*/
private void prepare() throws IOException {
// Ensures that the directory to which we want to write exists
Files.createDirectories(targetPath.getParent());
}

@Override
public void commit() throws IOException {
if (!Files.exists(temporaryPath))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ public interface OutputHandle {

/** Returns a ByteSink on the target file. */
@Nonnull
public ByteSink asByteSink();
public ByteSink asByteSink() throws IOException;

/** Returns a CharSink on the target file. */
@Nonnull
default public CharSink asCharSink(@Nonnull Charset charset) {
default public CharSink asCharSink(@Nonnull Charset charset) throws IOException {
return asByteSink().asCharSink(charset);
}

/** Returns a ByteSink on the temporary file. */
@Nonnull
public ByteSink asTemporaryByteSink();
public ByteSink asTemporaryByteSink() throws IOException;

/**
* Renames the temporary file to the final file.
Expand Down

0 comments on commit 436c527

Please sign in to comment.