1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.eclipse.jgit.lib;
45
46 import java.io.File;
47 import java.io.IOException;
48 import java.util.ArrayList;
49 import java.util.Collection;
50 import java.util.concurrent.ConcurrentHashMap;
51 import java.util.concurrent.ScheduledFuture;
52 import java.util.concurrent.ScheduledThreadPoolExecutor;
53 import java.util.concurrent.TimeUnit;
54
55 import org.eclipse.jgit.annotations.NonNull;
56 import org.eclipse.jgit.errors.RepositoryNotFoundException;
57 import org.eclipse.jgit.internal.storage.file.FileRepository;
58 import org.eclipse.jgit.lib.internal.WorkQueue;
59 import org.eclipse.jgit.util.FS;
60 import org.eclipse.jgit.util.IO;
61 import org.eclipse.jgit.util.RawParseUtils;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
64
65
66
67
68 public class RepositoryCache {
69 private final static Logger LOG = LoggerFactory
70 .getLogger(RepositoryCache.class);
71
72 private static final RepositoryCache.html#RepositoryCache">RepositoryCache cache = new RepositoryCache();
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91 public static Repository open(Key location) throws IOException,
92 RepositoryNotFoundException {
93 return open(location, true);
94 }
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118 public static Repository open(Key location, boolean mustExist)
119 throws IOException {
120 return cache.openRepository(location, mustExist);
121 }
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138 public static void register(Repository db) {
139 if (db.getDirectory() != null) {
140 FileKey key = FileKey.exact(db.getDirectory(), db.getFS());
141 cache.registerRepository(key, db);
142 }
143 }
144
145
146
147
148
149
150
151
152
153
154 public static void close(@NonNull Repository db) {
155 if (db.getDirectory() != null) {
156 FileKey key = FileKey.exact(db.getDirectory(), db.getFS());
157 cache.unregisterAndCloseRepository(key);
158 }
159 }
160
161
162
163
164
165
166
167
168
169
170
171
172
173 public static void unregister(Repository db) {
174 if (db.getDirectory() != null) {
175 unregister(FileKey.exact(db.getDirectory(), db.getFS()));
176 }
177 }
178
179
180
181
182
183
184
185
186
187
188
189
190
191 public static void unregister(Key location) {
192 cache.unregisterRepository(location);
193 }
194
195
196
197
198
199
200
201 public static Collection<Key> getRegisteredKeys() {
202 return cache.getKeys();
203 }
204
205 static boolean isCached(@NonNull Repository repo) {
206 File gitDir = repo.getDirectory();
207 if (gitDir == null) {
208 return false;
209 }
210 FileKey key = new FileKey(gitDir, repo.getFS());
211 return cache.cacheMap.get(key) == repo;
212 }
213
214
215
216
217 public static void clear() {
218 cache.clearAll();
219 }
220
221 static void clearExpired() {
222 cache.clearAllExpired();
223 }
224
225 static void reconfigure(RepositoryCacheConfig repositoryCacheConfig) {
226 cache.configureEviction(repositoryCacheConfig);
227 }
228
229 private final ConcurrentHashMap<Key, Repository> cacheMap;
230
231 private final Lock[] openLocks;
232
233 private ScheduledFuture<?> cleanupTask;
234
235 private volatile long expireAfter;
236
237 private RepositoryCache() {
238 cacheMap = new ConcurrentHashMap<>();
239 openLocks = new Lock[4];
240 for (int i = 0; i < openLocks.length; i++) {
241 openLocks[i] = new Lock();
242 }
243 configureEviction(new RepositoryCacheConfig());
244 }
245
246 private void configureEviction(
247 RepositoryCacheConfig repositoryCacheConfig) {
248 expireAfter = repositoryCacheConfig.getExpireAfter();
249 ScheduledThreadPoolExecutor scheduler = WorkQueue.getExecutor();
250 synchronized (scheduler) {
251 if (cleanupTask != null) {
252 cleanupTask.cancel(false);
253 }
254 long delay = repositoryCacheConfig.getCleanupDelay();
255 if (delay == RepositoryCacheConfig.NO_CLEANUP) {
256 return;
257 }
258 cleanupTask = scheduler.scheduleWithFixedDelay(() -> {
259 try {
260 cache.clearAllExpired();
261 } catch (Throwable e) {
262 LOG.error(e.getMessage(), e);
263 }
264 }, delay, delay, TimeUnit.MILLISECONDS);
265 }
266 }
267
268 private Repository openRepository(final Key location,
269 final boolean mustExist) throws IOException {
270 Repository db = cacheMap.get(location);
271 if (db == null) {
272 synchronized (lockFor(location)) {
273 db = cacheMap.get(location);
274 if (db == null) {
275 db = location.open(mustExist);
276 cacheMap.put(location, db);
277 } else {
278 db.incrementOpen();
279 }
280 }
281 } else {
282 db.incrementOpen();
283 }
284 return db;
285 }
286
287 private void registerRepository(Key location, Repository db) {
288 try (Repository oldDb = cacheMap.put(location, db)) {
289
290 }
291 }
292
293 private Repository unregisterRepository(Key location) {
294 return cacheMap.remove(location);
295 }
296
297 private boolean isExpired(Repository db) {
298 return db != null && db.useCnt.get() <= 0
299 && (System.currentTimeMillis() - db.closedAt.get() > expireAfter);
300 }
301
302 private void unregisterAndCloseRepository(Key location) {
303 synchronized (lockFor(location)) {
304 Repository oldDb = unregisterRepository(location);
305 if (oldDb != null) {
306 oldDb.doClose();
307 }
308 }
309 }
310
311 private Collection<Key> getKeys() {
312 return new ArrayList<>(cacheMap.keySet());
313 }
314
315 private void clearAllExpired() {
316 for (Repository db : cacheMap.values()) {
317 if (isExpired(db)) {
318 RepositoryCache.close(db);
319 }
320 }
321 }
322
323 private void clearAll() {
324 for (Key k : cacheMap.keySet()) {
325 unregisterAndCloseRepository(k);
326 }
327 }
328
329 private Lock lockFor(Key location) {
330 return openLocks[(location.hashCode() >>> 1) % openLocks.length];
331 }
332
333 private static class Lock {
334
335 }
336
337
338
339
340
341
342
343
344 public static interface Key {
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363 Repository open(boolean mustExist) throws IOException,
364 RepositoryNotFoundException;
365 }
366
367
368 public static class FileKey implements Key {
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383 public static FileKey exact(File directory, FS fs) {
384 return new FileKey(directory, fs);
385 }
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406 public static FileKey lenient(File directory, FS fs) {
407 final File gitdir = resolve(directory, fs);
408 return new FileKey(gitdir != null ? gitdir : directory, fs);
409 }
410
411 private final File path;
412 private final FS fs;
413
414
415
416
417
418
419
420
421 protected FileKey(File directory, FS fs) {
422 path = canonical(directory);
423 this.fs = fs;
424 }
425
426 private static File canonical(File path) {
427 try {
428 return path.getCanonicalFile();
429 } catch (IOException e) {
430 return path.getAbsoluteFile();
431 }
432 }
433
434
435 public final File getFile() {
436 return path;
437 }
438
439 @Override
440 public Repository open(boolean mustExist) throws IOException {
441 if (mustExist && !isGitRepository(path, fs))
442 throw new RepositoryNotFoundException(path);
443 return new FileRepository(path);
444 }
445
446 @Override
447 public int hashCode() {
448 return path.hashCode();
449 }
450
451 @Override
452 public boolean equals(Object o) {
453 return o instanceof FileKey && path.equals(((FileKey) o).path);
454 }
455
456 @Override
457 public String toString() {
458 return path.toString();
459 }
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476 public static boolean isGitRepository(File dir, FS fs) {
477 return fs.resolve(dir, Constants.OBJECTS).exists()
478 && fs.resolve(dir, "refs").exists()
479 && isValidHead(new File(dir, Constants.HEAD));
480 }
481
482 private static boolean isValidHead(File head) {
483 final String ref = readFirstLine(head);
484 return ref != null
485 && (ref.startsWith("ref: refs/") || ObjectId.isId(ref));
486 }
487
488 private static String readFirstLine(File head) {
489 try {
490 final byte[] buf = IO.readFully(head, 4096);
491 int n = buf.length;
492 if (n == 0)
493 return null;
494 if (buf[n - 1] == '\n')
495 n--;
496 return RawParseUtils.decode(buf, 0, n);
497 } catch (IOException e) {
498 return null;
499 }
500 }
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521 public static File resolve(File directory, FS fs) {
522 if (isGitRepository(directory, fs))
523 return directory;
524 if (isGitRepository(new File(directory, Constants.DOT_GIT), fs))
525 return new File(directory, Constants.DOT_GIT);
526
527 final String name = directory.getName();
528 final File parent = directory.getParentFile();
529 if (isGitRepository(new File(parent, name + Constants.DOT_GIT_EXT), fs))
530 return new File(parent, name + Constants.DOT_GIT_EXT);
531 return null;
532 }
533 }
534 }