Skip to content
This repository has been archived by the owner on Apr 5, 2024. It is now read-only.

Don't upload replays twice #93

Merged
merged 3 commits into from
Dec 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@
*/
@DatabaseTable(tableName = "ReplayFile")
public class ReplayFile implements Serializable {

private static final Logger LOG = LoggerFactory.getLogger(ReplayFile.class);
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 5416365418274150142L;
private final BooleanProperty failedProperty = new SimpleBooleanProperty(null, "failed", false);
@DatabaseField(generatedId = true)
private Long id;
private long id;
private File file;
@DatabaseField(width = 1023, unique = true)
private String fileName;
Expand All @@ -57,10 +56,6 @@ public ReplayFile(final File file) {
this.fileName = file.toString();
}

public static long getSerialVersionUID() {
return serialVersionUID;
}

public static List<ReplayFile> fromDirectory(File file) {
final List<ReplayFile> replayFiles = new ArrayList<>();
final File[] children = file.listFiles((dir, name) -> name.endsWith(".StormReplay"));
Expand All @@ -73,14 +68,13 @@ public static List<ReplayFile> fromDirectory(File file) {
}

return replayFiles;

}

public Long getId() {
public long getId() {
return id;
}

public void setId(final Long id) {
public void setId(final long id) {
this.id = id;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package ninja.eivind.hotsreplayuploader.repositories;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.misc.TransactionManager;
import com.j256.ormlite.spring.DaoFactory;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.PreparedQuery;
Expand All @@ -24,14 +23,12 @@
import ninja.eivind.hotsreplayuploader.di.Initializable;
import ninja.eivind.hotsreplayuploader.files.AccountDirectoryWatcher;
import ninja.eivind.hotsreplayuploader.models.ReplayFile;

import javax.inject.Inject;
import javax.inject.Singleton;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -73,10 +70,8 @@ public void deleteReplay(final ReplayFile replayFile) {
@Override
public void updateReplay(final ReplayFile file) {
try {
TransactionManager.callInTransaction(connectionSource, (Callable<Void>) () -> {
dao.createOrUpdate(file);
return null;
});
dao.createOrUpdate(file);
dao.refresh(file);
} catch (SQLException e) {
throw new RuntimeException(e);
}
Expand All @@ -94,12 +89,8 @@ public List<ReplayFile> getAll() {
if (file.exists()) {
return replayFile;
} else {
try {
dao.delete(replayFile);
return null;
} catch (SQLException e) {
throw new RuntimeException(e);
}
deleteReplay(replayFile);
return null;
}
})
.filter(file -> file != null) //dont list already deleted files
Expand All @@ -108,12 +99,21 @@ public List<ReplayFile> getAll() {

private List<ReplayFile> refreshAll(List<ReplayFile> replayFiles) {
try {
final List<ReplayFile> collect = new ArrayList<>();
final List<ReplayFile> fromDb = dao.queryForAll();
collect.addAll(fromDb);
replayFiles.stream().filter(file -> !fromDb.contains(file)).forEach(collect::add);
return collect;
} catch (SQLException e) {

dao.callBatchTasks(new Callable<Void>() {
public Void call() throws Exception {
//create new db entities for new files
replayFiles.stream()
.filter(file -> !fromDb.contains(file))
.forEach(file -> createReplay(file));
return null;
}
});

//get all entities again, but fresh from the db
return dao.queryForAll();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Expand All @@ -131,6 +131,14 @@ public void deleteByFileName(ReplayFile file) {
}
}

private void createReplay(final ReplayFile replayFile) {
try {
dao.create(replayFile);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

private ReplayFile getByFileName(final ReplayFile replayFile) {
try {
final SelectArg selectArg = new SelectArg("fileName", replayFile.getFileName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public UploaderService() throws IOException {
public void initialize() {
LOG.info("Initializing " + getClass().getSimpleName());
watcher.addFileListener(file -> {
fileRepository.deleteByFileName(file);
fileRepository.updateReplay(file);
files.add(file);
uploadQueue.add(file);
});
Expand Down