1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-12-25 09:18:51 -05:00

* better account for large history files

This commit is contained in:
Reinhard Pointner 2013-11-17 19:05:45 +00:00
parent 1c89f8c3ef
commit f4070a4c07

View File

@ -1,7 +1,5 @@
package net.sourceforge.filebot; package net.sourceforge.filebot;
import static net.sourceforge.filebot.Settings.*; import static net.sourceforge.filebot.Settings.*;
import java.io.File; import java.io.File;
@ -19,35 +17,32 @@ import net.sourceforge.filebot.History.Element;
import net.sourceforge.tuned.ByteBufferInputStream; import net.sourceforge.tuned.ByteBufferInputStream;
import net.sourceforge.tuned.ByteBufferOutputStream; import net.sourceforge.tuned.ByteBufferOutputStream;
public final class HistorySpooler { public final class HistorySpooler {
private static final HistorySpooler instance = new HistorySpooler(); private static final HistorySpooler instance = new HistorySpooler();
public static HistorySpooler getInstance() { public static HistorySpooler getInstance() {
return instance; return instance;
} }
private File persistentHistoryFile = new File(getApplicationFolder(), "history.xml"); private File persistentHistoryFile = new File(getApplicationFolder(), "history.xml");
private int persistentHistoryTotalSize = -1; private int persistentHistoryTotalSize = -1;
private boolean persistentHistoryEnabled = true; private boolean persistentHistoryEnabled = true;
private History sessionHistory = new History(); private History sessionHistory = new History();
public synchronized History getCompleteHistory() throws IOException { public synchronized History getCompleteHistory() throws IOException {
if (!persistentHistoryEnabled || persistentHistoryFile.length() <= 0) { if (!persistentHistoryEnabled || persistentHistoryFile.length() <= 0) {
return new History(); return new History();
} }
RandomAccessFile f = new RandomAccessFile(persistentHistoryFile, "rw"); RandomAccessFile f = new RandomAccessFile(persistentHistoryFile, "rw");
FileChannel channel = f.getChannel(); FileChannel channel = f.getChannel();
FileLock lock = channel.lock(); FileLock lock = channel.lock();
try { try {
ByteBufferOutputStream data = new ByteBufferOutputStream(f.length()); ByteBufferOutputStream data = new ByteBufferOutputStream(f.length());
data.transferFully(channel); data.transferFully(channel);
History history = History.importHistory(new ByteBufferInputStream(data.getByteBuffer())); History history = History.importHistory(new ByteBufferInputStream(data.getByteBuffer()));
history.addAll(sessionHistory.sequences()); history.addAll(sessionHistory.sequences());
return history; return history;
@ -57,13 +52,12 @@ public final class HistorySpooler {
f.close(); f.close();
} }
} }
public synchronized void commit() { public synchronized void commit() {
if (!persistentHistoryEnabled || sessionHistory.sequences().isEmpty()) { if (!persistentHistoryEnabled || sessionHistory.sequences().isEmpty()) {
return; return;
} }
try { try {
if (persistentHistoryFile.length() <= 0) { if (persistentHistoryFile.length() <= 0) {
persistentHistoryFile.createNewFile(); persistentHistoryFile.createNewFile();
@ -72,18 +66,18 @@ public final class HistorySpooler {
FileChannel channel = f.getChannel(); FileChannel channel = f.getChannel();
FileLock lock = channel.lock(); FileLock lock = channel.lock();
try { try {
ByteBufferOutputStream data = new ByteBufferOutputStream(f.length()); ByteBufferOutputStream data = new ByteBufferOutputStream((int) (f.length() > 0 ? f.length() : 1024), 0.2f);
int read = data.transferFully(channel); int read = data.transferFully(channel);
History history = read > 0 ? History.importHistory(new ByteBufferInputStream(data.getByteBuffer())) : new History(); History history = read > 0 ? History.importHistory(new ByteBufferInputStream(data.getByteBuffer())) : new History();
history.addAll(sessionHistory.sequences()); history.addAll(sessionHistory.sequences());
data.rewind(); data.rewind();
History.exportHistory(history, data); History.exportHistory(history, data);
channel.position(0); channel.position(0);
channel.write(data.getByteBuffer()); channel.write(data.getByteBuffer());
sessionHistory.clear(); sessionHistory.clear();
persistentHistoryTotalSize = history.totalSize(); persistentHistoryTotalSize = history.totalSize();
} catch (Exception e) { } catch (Exception e) {
@ -97,34 +91,30 @@ public final class HistorySpooler {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, "Failed to write rename history.", e); Logger.getLogger(getClass().getName()).log(Level.SEVERE, "Failed to write rename history.", e);
} }
} }
public synchronized void append(Iterable<Entry<File, File>> elements) { public synchronized void append(Iterable<Entry<File, File>> elements) {
List<Element> sequence = new ArrayList<Element>(); List<Element> sequence = new ArrayList<Element>();
for (Entry<File, File> element : elements) { for (Entry<File, File> element : elements) {
sequence.add(new Element(element.getKey().getName(), element.getValue().getPath(), element.getKey().getParentFile())); sequence.add(new Element(element.getKey().getName(), element.getValue().getPath(), element.getKey().getParentFile()));
} }
// append to session history // append to session history
if (sequence.size() > 0) { if (sequence.size() > 0) {
sessionHistory.add(sequence); sessionHistory.add(sequence);
} }
} }
public History getSessionHistory() { public History getSessionHistory() {
return sessionHistory; return sessionHistory;
} }
public int getPersistentHistoryTotalSize() { public int getPersistentHistoryTotalSize() {
return persistentHistoryTotalSize; return persistentHistoryTotalSize;
} }
public void setPersistentHistoryEnabled(boolean persistentHistoryEnabled) { public void setPersistentHistoryEnabled(boolean persistentHistoryEnabled) {
this.persistentHistoryEnabled = persistentHistoryEnabled; this.persistentHistoryEnabled = persistentHistoryEnabled;
} }
} }