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