import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
-import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
try {
Files.move(temp.toPath(), actual.toPath(), StandardCopyOption.ATOMIC_MOVE);
} catch (IOException e) {
- LOG.warn("Failed to move %s to %s. Deleting %s..", temp, actual, temp, e);
+ LOG.warn("Failed to move {} to {}. Deleting {}..", temp, actual, temp, e);
if (!temp.delete()) {
LOG.error("Failed to successfully delete file {}", temp);
}
LOG.debug("Deleting files: {}", files);
- files.forEach(file -> file.delete());
+ files.forEach(File::delete);
return null;
}
LOG.debug("Deleting files: {}", files);
- files.forEach(file -> file.delete());
+ files.forEach(File::delete);
return null;
}
private Collection<File> getSnapshotFiles(final String persistenceId) {
String encodedPersistenceId = encode(persistenceId);
- File[] files = snapshotDir.listFiles((FilenameFilter) (dir, name) -> {
+ File[] files = snapshotDir.listFiles((dir, name) -> {
int persistenceIdEndIndex = name.lastIndexOf('-', name.lastIndexOf('-') - 1);
return PERSISTENCE_ID_START_INDEX + encodedPersistenceId.length() == persistenceIdEndIndex
&& name.startsWith(encodedPersistenceId, PERSISTENCE_ID_START_INDEX) && !name.endsWith(".tmp");
private Collection<SnapshotMetadata> getSnapshotMetadatas(final String persistenceId,
final SnapshotSelectionCriteria criteria) {
return getSnapshotFiles(persistenceId).stream().flatMap(file -> toStream(extractMetadata(file)))
- .filter(md -> criteria.matches(md)).collect(Collectors.toList());
+ .filter(criteria::matches).collect(Collectors.toList());
}
private static Stream<SnapshotMetadata> toStream(@Nullable final SnapshotMetadata md) {
}
try {
- String persistenceId = name.substring(PERSISTENCE_ID_START_INDEX, persistenceIdEndIndex);
+ // Since the persistenceId is url encoded in the filename, we need
+ // to decode relevant filename's part to obtain persistenceId back
+ String persistenceId = decode(name.substring(PERSISTENCE_ID_START_INDEX, persistenceIdEndIndex));
long sequenceNumber = Long.parseLong(name.substring(persistenceIdEndIndex + 1, sequenceNumberEndIndex));
long timestamp = Long.parseLong(name.substring(sequenceNumberEndIndex + 1));
return new SnapshotMetadata(persistenceId, sequenceNumber, timestamp);
}
}
+ private static String decode(final String str) {
+ try {
+ return URLDecoder.decode(str, StandardCharsets.UTF_8.name());
+ } catch (final UnsupportedEncodingException e) {
+ // Shouldn't happen
+ LOG.warn("Error decoding {}", str, e);
+ return str;
+ }
+ }
+
@VisibleForTesting
static int compare(final SnapshotMetadata m1, final SnapshotMetadata m2) {
return (int) (!m1.persistenceId().equals(m2.persistenceId())