|
60 | 60 | import java.util.HashMap; |
61 | 61 | import java.util.List; |
62 | 62 | import java.util.Map; |
63 | | -import java.util.Objects; |
64 | 63 | import java.util.SortedSet; |
65 | 64 | import java.util.TreeSet; |
66 | 65 | import java.util.concurrent.atomic.AtomicBoolean; |
@@ -148,14 +147,36 @@ void loadCacheFiles(CacheService cacheService) { |
148 | 147 |
|
149 | 148 | if (Files.isDirectory(shardCachePath)) { |
150 | 149 | logger.trace("found snapshot cache dir at [{}], loading cache files from disk and index", shardCachePath); |
151 | | - Files.walkFileTree(shardCachePath, new CacheFileVisitor(cacheService, writer, documents)); |
| 150 | + Files.walkFileTree(shardCachePath, new SimpleFileVisitor<>() { |
| 151 | + @Override |
| 152 | + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { |
| 153 | + try { |
| 154 | + final String id = buildId(file); |
| 155 | + final Document cacheDocument = documents.get(id); |
| 156 | + if (cacheDocument != null) { |
| 157 | + logger.trace("indexing cache file with id [{}] in persistent cache index", id); |
| 158 | + writer.updateCacheFile(id, cacheDocument); |
| 159 | + |
| 160 | + final CacheKey cacheKey = buildCacheKey(cacheDocument); |
| 161 | + final long fileLength = getFileLength(cacheDocument); |
| 162 | + final SortedSet<Tuple<Long, Long>> ranges = buildCacheFileRanges(cacheDocument); |
| 163 | + |
| 164 | + logger.trace("adding cache file with [id={}, cache key={}, ranges={}]", id, cacheKey, ranges); |
| 165 | + cacheService.put(cacheKey, fileLength, file.getParent(), id, ranges); |
| 166 | + } else { |
| 167 | + logger.trace("deleting cache file [{}] (does not exist in persistent cache index)", file); |
| 168 | + Files.delete(file); |
| 169 | + } |
| 170 | + } catch (Exception e) { |
| 171 | + throw ExceptionsHelper.convertToRuntime(e); |
| 172 | + } |
| 173 | + return FileVisitResult.CONTINUE; |
| 174 | + } |
| 175 | + }); |
152 | 176 | } |
153 | 177 | } |
154 | 178 | } |
155 | 179 | } |
156 | | - for (CacheIndexWriter writer : writers) { |
157 | | - writer.prepareCommit(); |
158 | | - } |
159 | 180 | for (CacheIndexWriter writer : writers) { |
160 | 181 | writer.commit(); |
161 | 182 | } |
@@ -228,8 +249,11 @@ public long getNumDocs() { |
228 | 249 | @Override |
229 | 250 | public void close() throws IOException { |
230 | 251 | if (closed.compareAndSet(false, true)) { |
231 | | - IOUtils.close(writers); |
232 | | - documents.clear(); |
| 252 | + try { |
| 253 | + IOUtils.close(writers); |
| 254 | + } finally { |
| 255 | + documents.clear(); |
| 256 | + } |
233 | 257 | } |
234 | 258 | } |
235 | 259 |
|
@@ -446,48 +470,6 @@ public String toString() { |
446 | 470 | } |
447 | 471 | } |
448 | 472 |
|
449 | | - /** |
450 | | - * {@link CacheFileVisitor} is used to visit cache files on disk and find information about them using the Lucene documents loaded |
451 | | - * at startup from the persistent cache index. If there are no corresponding document for a cache file, the cache file is deleted |
452 | | - * from disk. If a corresponding document is found, the cache file is added to the current persistent cache index and inserted in |
453 | | - * the searchable snapshots cache. |
454 | | - */ |
455 | | - private static class CacheFileVisitor extends SimpleFileVisitor<Path> { |
456 | | - |
457 | | - private final CacheService cacheService; |
458 | | - private final Map<String, Document> documents; |
459 | | - private final CacheIndexWriter writer; |
460 | | - |
461 | | - private CacheFileVisitor(CacheService cacheService, CacheIndexWriter writer, Map<String, Document> documents) { |
462 | | - this.cacheService = Objects.requireNonNull(cacheService); |
463 | | - this.documents = Objects.requireNonNull(documents); |
464 | | - this.writer = Objects.requireNonNull(writer); |
465 | | - } |
466 | | - |
467 | | - @Override |
468 | | - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { |
469 | | - try { |
470 | | - final String id = buildId(file); |
471 | | - final Document cacheDocument = documents.get(id); |
472 | | - if (cacheDocument != null) { |
473 | | - logger.trace("indexing cache file with id [{}] in persistent cache index", id); |
474 | | - writer.updateCacheFile(id, cacheDocument); |
475 | | - |
476 | | - final CacheKey cacheKey = buildCacheKey(cacheDocument); |
477 | | - logger.trace("adding cache file with [id={}, cache key={}]", id, cacheKey); |
478 | | - final long fileLength = getFileLength(cacheDocument); |
479 | | - cacheService.put(cacheKey, fileLength, file.getParent(), id, buildCacheFileRanges(cacheDocument)); |
480 | | - } else { |
481 | | - logger.trace("deleting cache file [{}] (does not exist in persistent cache index)", file); |
482 | | - Files.delete(file); |
483 | | - } |
484 | | - } catch (Exception e) { |
485 | | - throw ExceptionsHelper.convertToRuntime(e); |
486 | | - } |
487 | | - return FileVisitResult.CONTINUE; |
488 | | - } |
489 | | - } |
490 | | - |
491 | 473 | private static final String CACHE_ID_FIELD = "cache_id"; |
492 | 474 | private static final String CACHE_PATH_FIELD = "cache_path"; |
493 | 475 | private static final String CACHE_RANGES_FIELD = "cache_ranges"; |
|
0 commit comments