sync test storage

This commit is contained in:
Dmitry Avdeev
2015-09-03 15:55:14 +03:00
parent 166b36176b
commit 4e054962eb
@@ -21,10 +21,11 @@ import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.ThrowableComputable;
import com.intellij.openapi.util.io.FileUtilRt;
import com.intellij.openapi.vfs.newvfs.persistent.FlushingDaemon;
import com.intellij.util.io.*;
import com.intellij.util.io.DataExternalizer;
import com.intellij.util.io.EnumeratorStringDescriptor;
import com.intellij.util.io.PersistentEnumeratorBase;
import com.intellij.util.io.PersistentHashMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -33,7 +34,6 @@ import java.io.DataOutput;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.concurrent.ScheduledFuture;
/**
* @author Dmitry Avdeev
@@ -57,7 +57,6 @@ public class TestStateStorage implements Disposable {
private static final Logger LOG = Logger.getInstance(TestStateStorage.class);
private PersistentHashMap<String, Record> myMap;
private volatile ScheduledFuture<?> myMapFlusher;
public static TestStateStorage getInstance(@NotNull Project project) {
return ServiceManager.getService(project, TestStateStorage.class);
@@ -69,43 +68,41 @@ public class TestStateStorage implements Disposable {
FileUtilRt.createParentDirs(file);
try {
myMap = create(file);
} catch (IOException e) {
}
catch (PersistentEnumeratorBase.CorruptedException e) {
PersistentHashMap.deleteFilesStartingWith(file);
try {
myMap = create(file);
}
catch (IOException e1) {
LOG.error(e1);
}
}
catch (IOException e) {
LOG.error(e);
}
myMapFlusher = FlushingDaemon.everyFiveSeconds(new Runnable() {
@Override
public void run() {
if (myMapFlusher == null) return; // disposed
if (myMap != null && myMap.isDirty()) myMap.force();
}
});
Disposer.register(project, this);
}
@NotNull
protected PersistentHashMap<String, Record> create(final File file) throws IOException {
return IOUtil.openCleanOrResetBroken(new ThrowableComputable<PersistentHashMap<String, Record>, IOException>() {
private static PersistentHashMap<String, Record> create(File file) throws IOException {
return new PersistentHashMap<String, Record>(file, new EnumeratorStringDescriptor(), new DataExternalizer<Record>() {
@Override
public PersistentHashMap<String, Record> compute() throws IOException {
return new PersistentHashMap<String, Record>(file, new EnumeratorStringDescriptor(), new DataExternalizer<Record>() {
@Override
public void save(@NotNull DataOutput out, Record value) throws IOException {
out.writeInt(value.magnitude);
out.writeLong(value.date.getTime());
}
@Override
public Record read(@NotNull DataInput in) throws IOException {
return new Record(in.readInt(), new Date(in.readLong()));
}
});
public void save(@NotNull DataOutput out, Record value) throws IOException {
out.writeInt(value.magnitude);
out.writeLong(value.date.getTime());
}
}, file);
@Override
public Record read(@NotNull DataInput in) throws IOException {
return new Record(in.readInt(), new Date(in.readLong()));
}
});
}
@Nullable
public Record getState(String testUrl) {
public synchronized Record getState(String testUrl) {
try {
return myMap == null ? null : myMap.get(testUrl);
}
@@ -115,7 +112,7 @@ public class TestStateStorage implements Disposable {
}
}
public void writeState(@NotNull String testUrl, Record record) {
public synchronized void writeState(@NotNull String testUrl, Record record) {
if (myMap == null) return;
try {
myMap.put(testUrl, record);
@@ -126,9 +123,7 @@ public class TestStateStorage implements Disposable {
}
@Override
public void dispose() {
myMapFlusher.cancel(false);
myMapFlusher = null;
public synchronized void dispose() {
try {
myMap.close();
}