Skip to content

Commit

Permalink
Use java.nio.file.Files::createDirectories to create the dumpfile parent
Browse files Browse the repository at this point in the history
  • Loading branch information
SebChmie committed Mar 11, 2024
1 parent a2f286a commit 01cb649
Showing 1 changed file with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.nio.file.Files;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collection;
Expand Down Expand Up @@ -276,7 +277,7 @@ private VALUE readValue(ByteIterable value) {
}

/**
* Dumps the content of an unreadable value to a file as a json (it tries to parse it as an object and than tries to dump it as a json).
* Dumps the content of an unreadable value to a file as a json (it tries to parse it as an object and then tries to dump it as a json).
*
* @param gzippedObj The object to dump.
* @param keyOfDump The key under which the unreadable value is accessible. It is used for the file name.
Expand All @@ -294,9 +295,23 @@ private static void dumpToFile(byte[] gzippedObj, @NonNull String keyOfDump, Exc
return;
}

if (!dumpfile.getParentFile().exists() && !dumpfile.getParentFile().mkdirs()) {
//TODO this seems to occur sometimes, is it maybe just a race condition?
throw new IllegalStateException("Could not create `%s`.".formatted(dumpfile.getParentFile()));
try {
/*
Files::createDirectories creates a directory by creating all nonexistent parent directories first.
Each directory creation attempt uses Files::createDirectory, through Files::createAndCheckIsDirectory.
Files::createDirectory is atomic and Files::createAndCheckIsDirectory catches and ignores FileAlreadyExistsException for directories.
Thus, after leaving Files::createDirectories the parent of dumpfile exists.
*/
Files.createDirectories(dumpfile.toPath().getParent());

// Should be a redundant check, due to the above reasoning
if (!dumpfile.getParentFile().exists()) {
throw new IllegalStateException("Could not create `%s`.".formatted(dumpfile.getParentFile()));
}
}
catch (IOException e) {
// Files::createDirectories throws java.io.IOException
throw new IllegalStateException("Could not create `%s` due to an IOException:%s".formatted(dumpfile.getParentFile(), e.getMessage()));
}

// Write json
Expand Down Expand Up @@ -351,7 +366,7 @@ private static byte[] debugUnGzip(byte[] bytes) throws IOException {

/**
* Iterates a given consumer over the entries of this store.
* Depending on the {@link XodusStoreFactory} corrupt entries may be dump to a file and/or removed from the store.
* Depending on the {@link XodusStoreFactory} corrupt entries may be dumped to a file and/or removed from the store.
* These entries are not submitted to the consumer.
*
* @implNote This method is concurrent!
Expand Down Expand Up @@ -420,7 +435,8 @@ private ByteIterable handle(StoreEntryConsumer<KEY, VALUE> consumer, IterationSt
result.incrTotalProcessed();

// Try to read the key first
key = getDeserializedAndDumpFailed(keyRaw, SerializingStore.this::readKey, () -> new String(keyRaw.getBytesUnsafe()), valueRaw, "Could not parse key [{}]");
key =
getDeserializedAndDumpFailed(keyRaw, SerializingStore.this::readKey, () -> new String(keyRaw.getBytesUnsafe()), valueRaw, "Could not parse key [{}]");
if (key == null) {
result.incrFailedKeys();
return keyRaw;
Expand All @@ -433,7 +449,8 @@ private ByteIterable handle(StoreEntryConsumer<KEY, VALUE> consumer, IterationSt
result.incrFailedValues();
return keyRaw;
}
}catch(Exception e){
}
catch (Exception e) {
log.error("Failed processing key/value", e);
return keyRaw;
}
Expand Down

0 comments on commit 01cb649

Please sign in to comment.