You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ObjectDirectory.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. /*
  2. * Copyright (C) 2009, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.storage.file;
  44. import java.io.BufferedReader;
  45. import java.io.File;
  46. import java.io.FileInputStream;
  47. import java.io.FileNotFoundException;
  48. import java.io.FileReader;
  49. import java.io.IOException;
  50. import java.text.MessageFormat;
  51. import java.util.ArrayList;
  52. import java.util.Arrays;
  53. import java.util.Collection;
  54. import java.util.Collections;
  55. import java.util.HashMap;
  56. import java.util.HashSet;
  57. import java.util.List;
  58. import java.util.Map;
  59. import java.util.Set;
  60. import java.util.concurrent.atomic.AtomicReference;
  61. import org.eclipse.jgit.JGitText;
  62. import org.eclipse.jgit.errors.PackMismatchException;
  63. import org.eclipse.jgit.events.ConfigChangedEvent;
  64. import org.eclipse.jgit.events.ConfigChangedListener;
  65. import org.eclipse.jgit.lib.AbbreviatedObjectId;
  66. import org.eclipse.jgit.lib.AnyObjectId;
  67. import org.eclipse.jgit.lib.Config;
  68. import org.eclipse.jgit.lib.Constants;
  69. import org.eclipse.jgit.lib.CoreConfig;
  70. import org.eclipse.jgit.lib.ObjectDatabase;
  71. import org.eclipse.jgit.lib.ObjectId;
  72. import org.eclipse.jgit.lib.ObjectInserter;
  73. import org.eclipse.jgit.lib.ObjectLoader;
  74. import org.eclipse.jgit.lib.RepositoryCache;
  75. import org.eclipse.jgit.lib.RepositoryCache.FileKey;
  76. import org.eclipse.jgit.storage.pack.ObjectToPack;
  77. import org.eclipse.jgit.storage.pack.PackWriter;
  78. import org.eclipse.jgit.util.FS;
  79. /**
  80. * Traditional file system based {@link ObjectDatabase}.
  81. * <p>
  82. * This is the classical object database representation for a Git repository,
  83. * where objects are stored loose by hashing them into directories by their
  84. * {@link ObjectId}, or are stored in compressed containers known as
  85. * {@link PackFile}s.
  86. * <p>
  87. * Optionally an object database can reference one or more alternates; other
  88. * ObjectDatabase instances that are searched in addition to the current
  89. * database.
  90. * <p>
  91. * Databases are divided into two halves: a half that is considered to be fast
  92. * to search (the {@code PackFile}s), and a half that is considered to be slow
  93. * to search (loose objects). When alternates are present the fast half is fully
  94. * searched (recursively through all alternates) before the slow half is
  95. * considered.
  96. */
  97. public class ObjectDirectory extends FileObjectDatabase implements
  98. ConfigChangedListener {
  99. private static final PackList NO_PACKS = new PackList(-1, -1, new PackFile[0]);
  100. /** Maximum number of candidates offered as resolutions of abbreviation. */
  101. private static final int RESOLVE_ABBREV_LIMIT = 256;
  102. private final Config config;
  103. private final File objects;
  104. private final File infoDirectory;
  105. private final File packDirectory;
  106. private final File alternatesFile;
  107. private final AtomicReference<PackList> packList;
  108. private final FS fs;
  109. private final AtomicReference<AlternateHandle[]> alternates;
  110. private int streamFileThreshold;
  111. /**
  112. * Initialize a reference to an on-disk object directory.
  113. *
  114. * @param cfg
  115. * configuration this directory consults for write settings.
  116. * @param dir
  117. * the location of the <code>objects</code> directory.
  118. * @param alternatePaths
  119. * a list of alternate object directories
  120. * @param fs
  121. * the file system abstraction which will be necessary to perform
  122. * certain file system operations.
  123. * @throws IOException
  124. * an alternate object cannot be opened.
  125. */
  126. public ObjectDirectory(final Config cfg, final File dir,
  127. File[] alternatePaths, FS fs) throws IOException {
  128. config = cfg;
  129. objects = dir;
  130. infoDirectory = new File(objects, "info");
  131. packDirectory = new File(objects, "pack");
  132. alternatesFile = new File(infoDirectory, "alternates");
  133. packList = new AtomicReference<PackList>(NO_PACKS);
  134. this.fs = fs;
  135. alternates = new AtomicReference<AlternateHandle[]>();
  136. if (alternatePaths != null) {
  137. AlternateHandle[] alt;
  138. alt = new AlternateHandle[alternatePaths.length];
  139. for (int i = 0; i < alternatePaths.length; i++)
  140. alt[i] = openAlternate(alternatePaths[i]);
  141. alternates.set(alt);
  142. }
  143. onConfigChanged(new ConfigChangedEvent());
  144. }
  145. public void onConfigChanged(ConfigChangedEvent event) {
  146. CoreConfig core = config.get(CoreConfig.KEY);
  147. streamFileThreshold = core.getStreamFileThreshold();
  148. }
  149. /**
  150. * @return the location of the <code>objects</code> directory.
  151. */
  152. public final File getDirectory() {
  153. return objects;
  154. }
  155. @Override
  156. public boolean exists() {
  157. return objects.exists();
  158. }
  159. @Override
  160. public void create() throws IOException {
  161. objects.mkdirs();
  162. infoDirectory.mkdir();
  163. packDirectory.mkdir();
  164. }
  165. @Override
  166. public ObjectInserter newInserter() {
  167. return new ObjectDirectoryInserter(this, config);
  168. }
  169. @Override
  170. public void close() {
  171. final PackList packs = packList.get();
  172. packList.set(NO_PACKS);
  173. for (final PackFile p : packs.packs)
  174. p.close();
  175. // Fully close all loaded alternates and clear the alternate list.
  176. AlternateHandle[] alt = alternates.get();
  177. if (alt != null) {
  178. alternates.set(null);
  179. for(final AlternateHandle od : alt)
  180. od.close();
  181. }
  182. }
  183. /**
  184. * Compute the location of a loose object file.
  185. *
  186. * @param objectId
  187. * identity of the loose object to map to the directory.
  188. * @return location of the object, if it were to exist as a loose object.
  189. */
  190. public File fileFor(final AnyObjectId objectId) {
  191. return fileFor(objectId.name());
  192. }
  193. private File fileFor(final String objectName) {
  194. final String d = objectName.substring(0, 2);
  195. final String f = objectName.substring(2);
  196. return new File(new File(objects, d), f);
  197. }
  198. /**
  199. * @return unmodifiable collection of all known pack files local to this
  200. * directory. Most recent packs are presented first. Packs most
  201. * likely to contain more recent objects appear before packs
  202. * containing objects referenced by commits further back in the
  203. * history of the repository.
  204. */
  205. public Collection<PackFile> getPacks() {
  206. final PackFile[] packs = packList.get().packs;
  207. return Collections.unmodifiableCollection(Arrays.asList(packs));
  208. }
  209. /**
  210. * Add a single existing pack to the list of available pack files.
  211. *
  212. * @param pack
  213. * path of the pack file to open.
  214. * @param idx
  215. * path of the corresponding index file.
  216. * @throws IOException
  217. * index file could not be opened, read, or is not recognized as
  218. * a Git pack file index.
  219. */
  220. public void openPack(final File pack, final File idx) throws IOException {
  221. final String p = pack.getName();
  222. final String i = idx.getName();
  223. if (p.length() != 50 || !p.startsWith("pack-") || !p.endsWith(".pack"))
  224. throw new IOException(MessageFormat.format(JGitText.get().notAValidPack, pack));
  225. if (i.length() != 49 || !i.startsWith("pack-") || !i.endsWith(".idx"))
  226. throw new IOException(MessageFormat.format(JGitText.get().notAValidPack, idx));
  227. if (!p.substring(0, 45).equals(i.substring(0, 45)))
  228. throw new IOException(MessageFormat.format(JGitText.get().packDoesNotMatchIndex, pack));
  229. insertPack(new PackFile(idx, pack));
  230. }
  231. @Override
  232. public String toString() {
  233. return "ObjectDirectory[" + getDirectory() + "]";
  234. }
  235. boolean hasObject1(final AnyObjectId objectId) {
  236. for (final PackFile p : packList.get().packs) {
  237. try {
  238. if (p.hasObject(objectId)) {
  239. return true;
  240. }
  241. } catch (IOException e) {
  242. // The hasObject call should have only touched the index,
  243. // so any failure here indicates the index is unreadable
  244. // by this process, and the pack is likewise not readable.
  245. //
  246. removePack(p);
  247. continue;
  248. }
  249. }
  250. return false;
  251. }
  252. void resolve(Set<ObjectId> matches, AbbreviatedObjectId id)
  253. throws IOException {
  254. PackList pList = packList.get();
  255. if (pList == null)
  256. pList = scanPacks(pList);
  257. for (PackFile p : pList.packs) {
  258. try {
  259. p.resolve(matches, id, RESOLVE_ABBREV_LIMIT);
  260. } catch (IOException e) {
  261. // Assume the pack is corrupted.
  262. //
  263. removePack(p);
  264. }
  265. if (matches.size() > RESOLVE_ABBREV_LIMIT)
  266. return;
  267. }
  268. String fanOut = id.name().substring(0, 2);
  269. String[] entries = new File(getDirectory(), fanOut).list();
  270. if (entries != null) {
  271. for (String e : entries) {
  272. if (e.length() != Constants.OBJECT_ID_STRING_LENGTH - 2)
  273. continue;
  274. try {
  275. ObjectId entId = ObjectId.fromString(fanOut + e);
  276. if (id.prefixCompare(entId) == 0)
  277. matches.add(entId);
  278. } catch (IllegalArgumentException notId) {
  279. continue;
  280. }
  281. if (matches.size() > RESOLVE_ABBREV_LIMIT)
  282. return;
  283. }
  284. }
  285. for (AlternateHandle alt : myAlternates()) {
  286. alt.db.resolve(matches, id);
  287. if (matches.size() > RESOLVE_ABBREV_LIMIT)
  288. return;
  289. }
  290. }
  291. ObjectLoader openObject1(final WindowCursor curs,
  292. final AnyObjectId objectId) throws IOException {
  293. PackList pList = packList.get();
  294. SEARCH: for (;;) {
  295. for (final PackFile p : pList.packs) {
  296. try {
  297. final ObjectLoader ldr = p.get(curs, objectId);
  298. if (ldr != null)
  299. return ldr;
  300. } catch (PackMismatchException e) {
  301. // Pack was modified; refresh the entire pack list.
  302. //
  303. pList = scanPacks(pList);
  304. continue SEARCH;
  305. } catch (IOException e) {
  306. // Assume the pack is corrupted.
  307. //
  308. removePack(p);
  309. }
  310. }
  311. return null;
  312. }
  313. }
  314. long getObjectSize1(final WindowCursor curs, final AnyObjectId objectId)
  315. throws IOException {
  316. PackList pList = packList.get();
  317. SEARCH: for (;;) {
  318. for (final PackFile p : pList.packs) {
  319. try {
  320. long sz = p.getObjectSize(curs, objectId);
  321. if (0 <= sz)
  322. return sz;
  323. } catch (PackMismatchException e) {
  324. // Pack was modified; refresh the entire pack list.
  325. //
  326. pList = scanPacks(pList);
  327. continue SEARCH;
  328. } catch (IOException e) {
  329. // Assume the pack is corrupted.
  330. //
  331. removePack(p);
  332. }
  333. }
  334. return -1;
  335. }
  336. }
  337. @Override
  338. long getObjectSize2(WindowCursor curs, String objectName,
  339. AnyObjectId objectId) throws IOException {
  340. try {
  341. File path = fileFor(objectName);
  342. FileInputStream in = new FileInputStream(path);
  343. try {
  344. return UnpackedObject.getSize(in, objectId, curs);
  345. } finally {
  346. in.close();
  347. }
  348. } catch (FileNotFoundException noFile) {
  349. return -1;
  350. }
  351. }
  352. @Override
  353. void selectObjectRepresentation(PackWriter packer, ObjectToPack otp,
  354. WindowCursor curs) throws IOException {
  355. PackList pList = packList.get();
  356. SEARCH: for (;;) {
  357. for (final PackFile p : pList.packs) {
  358. try {
  359. LocalObjectRepresentation rep = p.representation(curs, otp);
  360. if (rep != null)
  361. packer.select(otp, rep);
  362. } catch (PackMismatchException e) {
  363. // Pack was modified; refresh the entire pack list.
  364. //
  365. pList = scanPacks(pList);
  366. continue SEARCH;
  367. } catch (IOException e) {
  368. // Assume the pack is corrupted.
  369. //
  370. removePack(p);
  371. }
  372. }
  373. break SEARCH;
  374. }
  375. for (AlternateHandle h : myAlternates())
  376. h.db.selectObjectRepresentation(packer, otp, curs);
  377. }
  378. boolean hasObject2(final String objectName) {
  379. return fileFor(objectName).exists();
  380. }
  381. ObjectLoader openObject2(final WindowCursor curs,
  382. final String objectName, final AnyObjectId objectId)
  383. throws IOException {
  384. try {
  385. File path = fileFor(objectName);
  386. FileInputStream in = new FileInputStream(path);
  387. try {
  388. return UnpackedObject.open(in, path, objectId, curs);
  389. } finally {
  390. in.close();
  391. }
  392. } catch (FileNotFoundException noFile) {
  393. return null;
  394. }
  395. }
  396. boolean tryAgain1() {
  397. final PackList old = packList.get();
  398. if (old.tryAgain(packDirectory.lastModified()))
  399. return old != scanPacks(old);
  400. return false;
  401. }
  402. private void insertPack(final PackFile pf) {
  403. PackList o, n;
  404. do {
  405. o = packList.get();
  406. // If the pack in question is already present in the list
  407. // (picked up by a concurrent thread that did a scan?) we
  408. // do not want to insert it a second time.
  409. //
  410. final PackFile[] oldList = o.packs;
  411. final String name = pf.getPackFile().getName();
  412. for (PackFile p : oldList) {
  413. if (PackFile.SORT.compare(pf, p) < 0)
  414. break;
  415. if (name.equals(p.getPackFile().getName()))
  416. return;
  417. }
  418. final PackFile[] newList = new PackFile[1 + oldList.length];
  419. newList[0] = pf;
  420. System.arraycopy(oldList, 0, newList, 1, oldList.length);
  421. n = new PackList(o.lastRead, o.lastModified, newList);
  422. } while (!packList.compareAndSet(o, n));
  423. }
  424. private void removePack(final PackFile deadPack) {
  425. PackList o, n;
  426. do {
  427. o = packList.get();
  428. final PackFile[] oldList = o.packs;
  429. final int j = indexOf(oldList, deadPack);
  430. if (j < 0)
  431. break;
  432. final PackFile[] newList = new PackFile[oldList.length - 1];
  433. System.arraycopy(oldList, 0, newList, 0, j);
  434. System.arraycopy(oldList, j + 1, newList, j, newList.length - j);
  435. n = new PackList(o.lastRead, o.lastModified, newList);
  436. } while (!packList.compareAndSet(o, n));
  437. deadPack.close();
  438. }
  439. private static int indexOf(final PackFile[] list, final PackFile pack) {
  440. for (int i = 0; i < list.length; i++) {
  441. if (list[i] == pack)
  442. return i;
  443. }
  444. return -1;
  445. }
  446. private PackList scanPacks(final PackList original) {
  447. synchronized (packList) {
  448. PackList o, n;
  449. do {
  450. o = packList.get();
  451. if (o != original) {
  452. // Another thread did the scan for us, while we
  453. // were blocked on the monitor above.
  454. //
  455. return o;
  456. }
  457. n = scanPacksImpl(o);
  458. if (n == o)
  459. return n;
  460. } while (!packList.compareAndSet(o, n));
  461. return n;
  462. }
  463. }
  464. private PackList scanPacksImpl(final PackList old) {
  465. final Map<String, PackFile> forReuse = reuseMap(old);
  466. final long lastRead = System.currentTimeMillis();
  467. final long lastModified = packDirectory.lastModified();
  468. final Set<String> names = listPackDirectory();
  469. final List<PackFile> list = new ArrayList<PackFile>(names.size() >> 2);
  470. boolean foundNew = false;
  471. for (final String indexName : names) {
  472. // Must match "pack-[0-9a-f]{40}.idx" to be an index.
  473. //
  474. if (indexName.length() != 49 || !indexName.endsWith(".idx"))
  475. continue;
  476. final String base = indexName.substring(0, indexName.length() - 4);
  477. final String packName = base + ".pack";
  478. if (!names.contains(packName)) {
  479. // Sometimes C Git's HTTP fetch transport leaves a
  480. // .idx file behind and does not download the .pack.
  481. // We have to skip over such useless indexes.
  482. //
  483. continue;
  484. }
  485. final PackFile oldPack = forReuse.remove(packName);
  486. if (oldPack != null) {
  487. list.add(oldPack);
  488. continue;
  489. }
  490. final File packFile = new File(packDirectory, packName);
  491. final File idxFile = new File(packDirectory, indexName);
  492. list.add(new PackFile(idxFile, packFile));
  493. foundNew = true;
  494. }
  495. // If we did not discover any new files, the modification time was not
  496. // changed, and we did not remove any files, then the set of files is
  497. // the same as the set we were given. Instead of building a new object
  498. // return the same collection.
  499. //
  500. if (!foundNew && lastModified == old.lastModified && forReuse.isEmpty())
  501. return old.updateLastRead(lastRead);
  502. for (final PackFile p : forReuse.values()) {
  503. p.close();
  504. }
  505. if (list.isEmpty())
  506. return new PackList(lastRead, lastModified, NO_PACKS.packs);
  507. final PackFile[] r = list.toArray(new PackFile[list.size()]);
  508. Arrays.sort(r, PackFile.SORT);
  509. return new PackList(lastRead, lastModified, r);
  510. }
  511. private static Map<String, PackFile> reuseMap(final PackList old) {
  512. final Map<String, PackFile> forReuse = new HashMap<String, PackFile>();
  513. for (final PackFile p : old.packs) {
  514. if (p.invalid()) {
  515. // The pack instance is corrupted, and cannot be safely used
  516. // again. Do not include it in our reuse map.
  517. //
  518. p.close();
  519. continue;
  520. }
  521. final PackFile prior = forReuse.put(p.getPackFile().getName(), p);
  522. if (prior != null) {
  523. // This should never occur. It should be impossible for us
  524. // to have two pack files with the same name, as all of them
  525. // came out of the same directory. If it does, we promised to
  526. // close any PackFiles we did not reuse, so close the second,
  527. // readers are likely to be actively using the first.
  528. //
  529. forReuse.put(prior.getPackFile().getName(), prior);
  530. p.close();
  531. }
  532. }
  533. return forReuse;
  534. }
  535. private Set<String> listPackDirectory() {
  536. final String[] nameList = packDirectory.list();
  537. if (nameList == null)
  538. return Collections.emptySet();
  539. final Set<String> nameSet = new HashSet<String>(nameList.length << 1);
  540. for (final String name : nameList) {
  541. if (name.startsWith("pack-"))
  542. nameSet.add(name);
  543. }
  544. return nameSet;
  545. }
  546. AlternateHandle[] myAlternates() {
  547. AlternateHandle[] alt = alternates.get();
  548. if (alt == null) {
  549. synchronized (alternates) {
  550. alt = alternates.get();
  551. if (alt == null) {
  552. try {
  553. alt = loadAlternates();
  554. } catch (IOException e) {
  555. alt = new AlternateHandle[0];
  556. }
  557. alternates.set(alt);
  558. }
  559. }
  560. }
  561. return alt;
  562. }
  563. private AlternateHandle[] loadAlternates() throws IOException {
  564. final List<AlternateHandle> l = new ArrayList<AlternateHandle>(4);
  565. final BufferedReader br = open(alternatesFile);
  566. try {
  567. String line;
  568. while ((line = br.readLine()) != null) {
  569. l.add(openAlternate(line));
  570. }
  571. } finally {
  572. br.close();
  573. }
  574. return l.toArray(new AlternateHandle[l.size()]);
  575. }
  576. private static BufferedReader open(final File f)
  577. throws FileNotFoundException {
  578. return new BufferedReader(new FileReader(f));
  579. }
  580. private AlternateHandle openAlternate(final String location)
  581. throws IOException {
  582. final File objdir = fs.resolve(objects, location);
  583. return openAlternate(objdir);
  584. }
  585. private AlternateHandle openAlternate(File objdir) throws IOException {
  586. final File parent = objdir.getParentFile();
  587. if (FileKey.isGitRepository(parent, fs)) {
  588. FileKey key = FileKey.exact(parent, fs);
  589. FileRepository db = (FileRepository) RepositoryCache.open(key);
  590. return new AlternateRepository(db);
  591. }
  592. ObjectDirectory db = new ObjectDirectory(config, objdir, null, fs);
  593. return new AlternateHandle(db);
  594. }
  595. private static final class PackList {
  596. /** Last wall-clock time the directory was read. */
  597. volatile long lastRead;
  598. /** Last modification time of {@link ObjectDirectory#packDirectory}. */
  599. final long lastModified;
  600. /** All known packs, sorted by {@link PackFile#SORT}. */
  601. final PackFile[] packs;
  602. private boolean cannotBeRacilyClean;
  603. PackList(final long lastRead, final long lastModified,
  604. final PackFile[] packs) {
  605. this.lastRead = lastRead;
  606. this.lastModified = lastModified;
  607. this.packs = packs;
  608. this.cannotBeRacilyClean = notRacyClean(lastRead);
  609. }
  610. private boolean notRacyClean(final long read) {
  611. return read - lastModified > 2 * 60 * 1000L;
  612. }
  613. PackList updateLastRead(final long now) {
  614. if (notRacyClean(now))
  615. cannotBeRacilyClean = true;
  616. lastRead = now;
  617. return this;
  618. }
  619. boolean tryAgain(final long currLastModified) {
  620. // Any difference indicates the directory was modified.
  621. //
  622. if (lastModified != currLastModified)
  623. return true;
  624. // We have already determined the last read was far enough
  625. // after the last modification that any new modifications
  626. // are certain to change the last modified time.
  627. //
  628. if (cannotBeRacilyClean)
  629. return false;
  630. if (notRacyClean(lastRead)) {
  631. // Our last read should have marked cannotBeRacilyClean,
  632. // but this thread may not have seen the change. The read
  633. // of the volatile field lastRead should have fixed that.
  634. //
  635. return false;
  636. }
  637. // We last read this directory too close to its last observed
  638. // modification time. We may have missed a modification. Scan
  639. // the directory again, to ensure we still see the same state.
  640. //
  641. return true;
  642. }
  643. }
  644. @Override
  645. public ObjectDatabase newCachedDatabase() {
  646. return newCachedFileObjectDatabase();
  647. }
  648. FileObjectDatabase newCachedFileObjectDatabase() {
  649. return new CachedObjectDirectory(this);
  650. }
  651. @Override
  652. int getStreamFileThreshold() {
  653. return streamFileThreshold;
  654. }
  655. }