package org.eclipse.jdt.internal.core.search.indexing;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.internal.core.index.DiskIndex;
import org.eclipse.jdt.internal.core.search.processing.JobManager;
import org.eclipse.jdt.internal.core.util.Util;
public class IndexNamesRegistry {
private final File savedIndexNamesFile;
private final Job writeJob;
private final IPath javaPluginWorkingLocation;
private final Object queueMutex = new Object();
private char[][] pendingWrite;
public IndexNamesRegistry(File savedIndexNamesFile, IPath javaPluginWorkingLocation) {
super();
this.savedIndexNamesFile = savedIndexNamesFile;
this.writeJob = Job.create("Updating index names", this::save);
this.writeJob.setSystem(true);
this.javaPluginWorkingLocation = javaPluginWorkingLocation;
}
public void write(char[][] newContents) {
synchronized (this.queueMutex) {
this.pendingWrite = newContents;
}
this.writeJob.schedule();
}
public char[][] read(IProgressMonitor monitor) {
char[][] newContents;
synchronized (this.queueMutex) {
newContents = this.pendingWrite;
}
if (newContents != null) {
return newContents;
}
try {
char[] savedIndexNames = org.eclipse.jdt.internal.compiler.util.Util
.getFileCharContent(this.savedIndexNamesFile, null);
if (savedIndexNames.length > 0) {
char[][] names = CharOperation.splitOn('\n', savedIndexNames);
if (names.length > 1) {
String savedSignature = DiskIndex.SIGNATURE + "+" + this.javaPluginWorkingLocation.toOSString();
if (savedSignature.equals(new String(names[0])))
return names;
}
}
} catch (IOException ignored) {
if (JobManager.VERBOSE)
Util.verbose("Failed to read saved index file names");
}
return null;
}
private void save(IProgressMonitor monitor) {
SubMonitor subMonitor = SubMonitor.convert(monitor);
char[][] newContents;
synchronized (this.queueMutex) {
newContents = this.pendingWrite;
}
if (newContents == null) {
return;
}
subMonitor.setWorkRemaining(newContents.length);
try (BufferedWriter writer = new BufferedWriter(new FileWriter(this.savedIndexNamesFile))) {
writer.write(DiskIndex.SIGNATURE);
writer.write('+');
writer.write(this.javaPluginWorkingLocation.toOSString());
writer.write('\n');
for (char[] next : newContents) {
subMonitor.split(1);
writer.write(next);
writer.write('\n');
}
} catch (IOException ignored) {
if (JobManager.VERBOSE)
Util.verbose("Failed to write saved index file names", System.err);
}
synchronized (this.queueMutex) {
if (this.pendingWrite == newContents) {
this.pendingWrite = null;
}
}
}
public void delete() {
synchronized (this.queueMutex) {
this.pendingWrite = null;
}
this.writeJob.cancel();
try {
this.writeJob.join();
} catch (InterruptedException e) {
}
synchronized (this.queueMutex) {
this.pendingWrite = null;
}
this.savedIndexNamesFile.delete();
}
}