Skip to content

Commit 436c527

Browse files
shevek-googlebdw-g
andauthored
IO: Ensure parent directory exists before writing file (#34)
* [FileSystemByteSink] Ensure the parent directory exists * Dumper: Hide prepare() call in FileSystemByteSink. Co-authored-by: Bart Wiegmans <[email protected]>
1 parent a1b0911 commit 436c527

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

dumper/app/src/main/java/com/google/edwmigration/dumper/application/dumper/connector/netezza/NetezzaMetadataConnector.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ public void addTasksTo(List<? super Task<?>> out, ConnectorArguments arguments)
172172
// Not documented.
173173
out.add(new JdbcSelectTask("nz.v_objects.csv", "SELECT * FROM system.._v_objects"));
174174

175+
// TODO; these might be placed in a ParallelTaskGroup?`
175176
// these neeed to be filtered on WHERE = dbname, or else which DB md table will contain SYSTEM data too
176177
for (String db : dbs) {
177178
// The benefit of having this reduces the amount of data in the zip file

dumper/app/src/main/java/com/google/edwmigration/dumper/application/dumper/io/FileSystemOutputHandle.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class FileSystemOutputHandle implements OutputHandle {
3838
private final Path targetPath;
3939

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

5253
@Override
53-
public ByteSink asByteSink() {
54+
public ByteSink asByteSink() throws IOException {
5455
// LOG.debug("As ByteSink: " + this + " = " + targetPath);
56+
prepare();
5557
return new FileSystemByteSink(targetPath);
5658
}
5759

5860
@Override
59-
public ByteSink asTemporaryByteSink() {
61+
public ByteSink asTemporaryByteSink() throws IOException {
6062
// LOG.debug("As Temporary ByteSink: " + this + " = " + temporaryPath);
63+
prepare();
6164
return new FileSystemByteSink(temporaryPath);
6265
}
6366

67+
/**
68+
* Ensures that the target file can be written.
69+
*
70+
* Must be called before calling openStream() on a ByteStream acquired from this object.
71+
*/
72+
private void prepare() throws IOException {
73+
// Ensures that the directory to which we want to write exists
74+
Files.createDirectories(targetPath.getParent());
75+
}
76+
6477
@Override
6578
public void commit() throws IOException {
6679
if (!Files.exists(temporaryPath))

dumper/app/src/main/java/com/google/edwmigration/dumper/application/dumper/io/OutputHandle.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@ public interface OutputHandle {
3333

3434
/** Returns a ByteSink on the target file. */
3535
@Nonnull
36-
public ByteSink asByteSink();
36+
public ByteSink asByteSink() throws IOException;
3737

3838
/** Returns a CharSink on the target file. */
3939
@Nonnull
40-
default public CharSink asCharSink(@Nonnull Charset charset) {
40+
default public CharSink asCharSink(@Nonnull Charset charset) throws IOException {
4141
return asByteSink().asCharSink(charset);
4242
}
4343

4444
/** Returns a ByteSink on the temporary file. */
4545
@Nonnull
46-
public ByteSink asTemporaryByteSink();
46+
public ByteSink asTemporaryByteSink() throws IOException;
4747

4848
/**
4949
* Renames the temporary file to the final file.

0 commit comments

Comments
 (0)