store index's version stamp inside index file, instead of file's lastModified

This commit is contained in:
Maxim.Mossienko
2017-03-13 18:35:23 +01:00
parent b46d4a8061
commit b830ec36b5
2 changed files with 39 additions and 33 deletions
@@ -50,7 +50,7 @@ import java.util.*;
public class StubUpdatingIndex extends CustomImplementationFileBasedIndexExtension<Integer, SerializedStubTree, FileContent>
implements PsiDependentIndex, CustomInputsIndexFileBasedIndexExtension<Integer> {
static final Logger LOG = Logger.getInstance("#com.intellij.psi.stubs.StubUpdatingIndex");
private static final int VERSION = 31 + (PersistentHashMapValueStorage.COMPRESSION_ENABLED ? 1 : 0);
private static final int VERSION = 32 + (PersistentHashMapValueStorage.COMPRESSION_ENABLED ? 1 : 0);
// todo remove once we don't need this for stub-ast mismatch debug info
private static final FileAttribute INDEXED_STAMP = new FileAttribute("stubIndexStamp", 2, true);
@@ -73,47 +73,53 @@ class StubVersionMap {
List<FileType> addedFileTypes = new ArrayList<>();
long lastUsedCounter = currentStubIndexStamp;
if (allIndexedFiles.lastModified() == currentStubIndexStamp) {
FileTypeRegistry fileTypeRegistry = FileTypeRegistry.getInstance();
boolean canUsePreviousMappings = allIndexedFiles.exists();
FileTypeRegistry fileTypeRegistry = FileTypeRegistry.getInstance();
Set<FileType> loadedFileTypes = new THashSet<>();
Set<FileType> loadedFileTypes = new THashSet<>();
if (canUsePreviousMappings) {
List<String> stringList = StringUtil.split(FileUtil.loadFile(allIndexedFiles, ourEncoding), LINE_SEPARATOR);
long allIndexedFilesVersion = Long.parseLong(stringList.get(0));
if (allIndexedFilesVersion == currentStubIndexStamp) {
for (int i = 1, size = stringList.size(); i < size; ++i) {
List<String> strings = StringUtil.split(stringList.get(i), RECORD_SEPARATOR);
String fileTypeName = strings.get(0);
long usedTimeStamp = Long.parseLong(strings.get(2));
lastUsedCounter = Math.min(lastUsedCounter, usedTimeStamp);
for(String fileTypeInfo: StringUtil.split(FileUtil.loadFile(allIndexedFiles, ourEncoding), LINE_SEPARATOR)) {
List<String> strings = StringUtil.split(fileTypeInfo, RECORD_SEPARATOR);
String fileTypeName = strings.get(0);
long usedTimeStamp = Long.parseLong(strings.get(2));
lastUsedCounter = Math.min(lastUsedCounter, usedTimeStamp);
FileType fileType = fileTypeRegistry.findFileTypeByName(fileTypeName);
if (fileType == null) removedFileTypes.add(fileTypeName);
else {
loadedFileTypes.add(fileType);
Object owner = getVersionOwner(fileType);
if (owner == null) removedFileTypes.add(fileTypeName);
FileType fileType = fileTypeRegistry.findFileTypeByName(fileTypeName);
if (fileType == null) removedFileTypes.add(fileTypeName);
else {
if (!Comparing.equal(strings.get(1), typeAndVersion(owner))) {
updatedFileTypes.add(fileType);
} else {
registerStamp(fileType, usedTimeStamp);
loadedFileTypes.add(fileType);
Object owner = getVersionOwner(fileType);
if (owner == null) removedFileTypes.add(fileTypeName);
else {
if (!Comparing.equal(strings.get(1), typeAndVersion(owner))) {
updatedFileTypes.add(fileType);
}
else {
registerStamp(fileType, usedTimeStamp);
}
}
}
}
} else {
canUsePreviousMappings = false;
}
}
for(FileType fileType:fileTypeToVersionOwner.keySet()) {
if (!loadedFileTypes.contains(fileType)) {
addedFileTypes.add(fileType);
}
for(FileType fileType:fileTypeToVersionOwner.keySet()) {
if (!loadedFileTypes.contains(fileType)) {
addedFileTypes.add(fileType);
}
}
if (!addedFileTypes.isEmpty() || !removedFileTypes.isEmpty()) {
StubUpdatingIndex.LOG.info("requesting complete stub index rebuild due to changes: " +
(addedFileTypes.isEmpty() ? "" : "added file types:" + StringUtil.join(addedFileTypes, FileType::getName, ",") + ";") +
(removedFileTypes.isEmpty() ? "":"removed file types:" + StringUtil.join(removedFileTypes, ",")));
throw new IOException(); // StubVersionMap will be recreated
}
} else {
addedFileTypes.addAll(fileTypeToVersionOwner.keySet());
if (canUsePreviousMappings && (!addedFileTypes.isEmpty() || !removedFileTypes.isEmpty())) {
StubUpdatingIndex.LOG.info("requesting complete stub index rebuild due to changes: " +
(addedFileTypes.isEmpty() ? "" : "added file types:" + StringUtil.join(addedFileTypes, FileType::getName, ",") + ";") +
(removedFileTypes.isEmpty() ? "":"removed file types:" + StringUtil.join(removedFileTypes, ",")));
throw new IOException(); // StubVersionMap will be recreated
}
long counter = lastUsedCounter - 1; // important to start with value smaller and progress downwards
@@ -136,6 +142,7 @@ class StubVersionMap {
}
StringBuilder allFileTypes = new StringBuilder();
allFileTypes.append(currentStubIndexStamp).append(LINE_SEPARATOR);
for (FileType fileType : fileTypeToVersionOwner.keySet()) {
Object owner = fileTypeToVersionOwner.get(fileType);
@@ -144,7 +151,6 @@ class StubVersionMap {
.append(timestamp).append(LINE_SEPARATOR);
}
FileUtil.writeToFile(allIndexedFiles, allFileTypes.toString().getBytes(ourEncoding));
FileUtil.setLastModified(allIndexedFiles, currentStubIndexStamp);
}
myStubIndexStamp = currentStubIndexStamp;