Skip to content

Commit 689235b

Browse files
committed
add local file timestamp map to quickly check if new delta-based index cache is outdated or not
1 parent 9f42dcc commit 689235b

File tree

1 file changed

+39
-9
lines changed

1 file changed

+39
-9
lines changed

headless-services/spring-boot-language-server/src/main/java/org/springframework/ide/vscode/boot/index/cache/IndexCacheOnDiscDeltaBased.java

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
public class IndexCacheOnDiscDeltaBased implements IndexCache {
6767

6868
private final File cacheDirectory;
69+
private final Map<IndexCacheKey, Map<String, Long>> timestamps;
6970

7071
private static final Logger log = LoggerFactory.getLogger(IndexCacheOnDiscDeltaBased.class);
7172

@@ -79,6 +80,8 @@ public IndexCacheOnDiscDeltaBased(File cacheDirectory) {
7980
if (!this.cacheDirectory.exists()) {
8081
log.warn("symbol cache directory does not exist and cannot be created: " + this.cacheDirectory.toString());
8182
}
83+
84+
this.timestamps = new ConcurrentHashMap<>();
8285
}
8386

8487
@Override
@@ -101,6 +104,10 @@ public <T extends IndexCacheable> void store(IndexCacheKey cacheKey, String[] fi
101104

102105
IndexCacheStore<T> store = new IndexCacheStore<T>(timestampedFiles, elements, dependencies.asMap(), type);
103106
persist(cacheKey, new DeltaSnapshot<T>(store), false);
107+
108+
// update local timestamp cache
109+
ConcurrentHashMap<String, Long> timestampMap = new ConcurrentHashMap<>(timestampedFiles);
110+
this.timestamps.put(cacheKey, timestampMap);
104111
}
105112

106113
@SuppressWarnings("unchecked")
@@ -134,6 +141,9 @@ public <T extends IndexCacheable> Pair<T[], Multimap<String, String>> retrieve(I
134141
dependencies.replaceValues(entry.getKey(), entry.getValue());
135142
}
136143
}
144+
145+
// update local timestamp cache
146+
this.timestamps.put(cacheKey, new ConcurrentHashMap<>(timestampedFiles));
137147

138148
return Pair.of(
139149
(T[]) symbols.toArray((T[]) Array.newInstance(type, symbols.size())),
@@ -150,12 +160,20 @@ public <T extends IndexCacheable> Pair<T[], Multimap<String, String>> retrieve(I
150160

151161
@Override
152162
public <T extends IndexCacheable> void removeFile(IndexCacheKey cacheKey, String file, Class<T> type) {
153-
persist(cacheKey, new DeltaDelete<T>(new String[] {file}), true);
163+
removeFiles(cacheKey, new String[] {file}, type);
154164
}
155165

156166
@Override
157167
public <T extends IndexCacheable> void removeFiles(IndexCacheKey cacheKey, String[] files, Class<T> type) {
158168
persist(cacheKey, new DeltaDelete<T>(files), true);
169+
170+
// update local timestamp cache
171+
Map<String, Long> timestampsMap = this.timestamps.get(cacheKey);
172+
if (timestampsMap != null) {
173+
for (String file : files) {
174+
timestampsMap.remove(file);
175+
}
176+
}
159177
}
160178

161179
@Override
@@ -164,6 +182,9 @@ public void remove(IndexCacheKey cacheKey) {
164182
if (cacheStore.exists()) {
165183
cacheStore.delete();
166184
}
185+
186+
// update local timestamp cache
187+
this.timestamps.remove(cacheKey);
167188
}
168189

169190
@Override
@@ -182,6 +203,10 @@ public <T extends IndexCacheable> void update(IndexCacheKey cacheKey, String fil
182203

183204
IndexCacheStore<T> deltaStore = new IndexCacheStore<T>(timestampsDelta, generatedSymbols, dependenciesDelta, type);
184205
persist(cacheKey, new DeltaUpdate<T>(deltaStore), true);
206+
207+
// update local timestamp cache
208+
Map<String, Long> timestampsMap = this.timestamps.computeIfAbsent(cacheKey, (s) -> new ConcurrentHashMap<>());
209+
timestampsMap.put(file, lastModified);
185210
}
186211

187212
@Override
@@ -202,18 +227,23 @@ public <T extends IndexCacheable> void update(IndexCacheKey cacheKey, String[] f
202227

203228
IndexCacheStore<T> deltaStore = new IndexCacheStore<T>(timestampsDelta, generatedSymbols, dependenciesDelta, type);
204229
persist(cacheKey, new DeltaUpdate<T>(deltaStore), true);
230+
231+
// update local timestamp cache
232+
Map<String, Long> timestampsMap = this.timestamps.computeIfAbsent(cacheKey, (s) -> new ConcurrentHashMap<>());
233+
for (int i = 0; i < files.length; i++) {
234+
timestampsMap.put(files[i], lastModified[i]);
235+
}
205236
}
206237

207238
@Override
208239
public long getModificationTimestamp(IndexCacheKey cacheKey, String file) {
209-
// IndexCacheStore<? extends IndexCacheable> cacheStore = this.stores.get(cacheKey);
210-
//
211-
// if (cacheStore != null) {
212-
// Long result = cacheStore.getTimestampedFiles().get(file);
213-
// if (result != null) {
214-
// return result;
215-
// }
216-
// }
240+
Map<String, Long> timestampsMap = this.timestamps.get(cacheKey);
241+
if (timestampsMap != null) {
242+
Long result = timestampsMap.get(file);
243+
if (result != null) {
244+
return result;
245+
}
246+
}
217247

218248
return 0;
219249
}

0 commit comments

Comments
 (0)