ObjectDirectory.java

  1. /*
  2.  * Copyright (C) 2009, Google Inc. and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */

  10. package org.eclipse.jgit.internal.storage.file;

  11. import static java.nio.charset.StandardCharsets.UTF_8;
  12. import static org.eclipse.jgit.internal.storage.pack.PackExt.PACK;
  13. import static org.eclipse.jgit.internal.storage.pack.PackExt.BITMAP_INDEX;
  14. import static org.eclipse.jgit.internal.storage.pack.PackExt.INDEX;

  15. import java.io.BufferedReader;
  16. import java.io.File;
  17. import java.io.FileNotFoundException;
  18. import java.io.IOException;
  19. import java.nio.file.Files;
  20. import java.text.MessageFormat;
  21. import java.util.ArrayList;
  22. import java.util.Collection;
  23. import java.util.Collections;
  24. import java.util.HashSet;
  25. import java.util.List;
  26. import java.util.Objects;
  27. import java.util.Set;
  28. import java.util.concurrent.atomic.AtomicReference;

  29. import org.eclipse.jgit.internal.JGitText;
  30. import org.eclipse.jgit.internal.storage.pack.ObjectToPack;
  31. import org.eclipse.jgit.internal.storage.pack.PackExt;
  32. import org.eclipse.jgit.internal.storage.pack.PackWriter;
  33. import org.eclipse.jgit.lib.AbbreviatedObjectId;
  34. import org.eclipse.jgit.lib.AnyObjectId;
  35. import org.eclipse.jgit.lib.Config;
  36. import org.eclipse.jgit.lib.Constants;
  37. import org.eclipse.jgit.lib.ObjectDatabase;
  38. import org.eclipse.jgit.lib.ObjectId;
  39. import org.eclipse.jgit.lib.ObjectLoader;
  40. import org.eclipse.jgit.lib.RepositoryCache;
  41. import org.eclipse.jgit.lib.RepositoryCache.FileKey;
  42. import org.eclipse.jgit.util.FS;
  43. import org.eclipse.jgit.util.FileUtils;

  44. /**
  45.  * Traditional file system based {@link org.eclipse.jgit.lib.ObjectDatabase}.
  46.  * <p>
  47.  * This is the classical object database representation for a Git repository,
  48.  * where objects are stored loose by hashing them into directories by their
  49.  * {@link org.eclipse.jgit.lib.ObjectId}, or are stored in compressed containers
  50.  * known as {@link org.eclipse.jgit.internal.storage.file.Pack}s.
  51.  * <p>
  52.  * Optionally an object database can reference one or more alternates; other
  53.  * ObjectDatabase instances that are searched in addition to the current
  54.  * database.
  55.  * <p>
  56.  * Databases are divided into two halves: a half that is considered to be fast
  57.  * to search (the {@code PackFile}s), and a half that is considered to be slow
  58.  * to search (loose objects). When alternates are present the fast half is fully
  59.  * searched (recursively through all alternates) before the slow half is
  60.  * considered.
  61.  */
  62. public class ObjectDirectory extends FileObjectDatabase {
  63.     /** Maximum number of candidates offered as resolutions of abbreviation. */
  64.     private static final int RESOLVE_ABBREV_LIMIT = 256;

  65.     private final AlternateHandle handle = new AlternateHandle(this);

  66.     private final Config config;

  67.     private final File objects;

  68.     private final File infoDirectory;

  69.     private final LooseObjects loose;

  70.     private final PackDirectory packed;

  71.     private final PackDirectory preserved;

  72.     private final File alternatesFile;

  73.     private final FS fs;

  74.     private final AtomicReference<AlternateHandle[]> alternates;

  75.     private final File shallowFile;

  76.     private FileSnapshot shallowFileSnapshot = FileSnapshot.DIRTY;

  77.     private Set<ObjectId> shallowCommitsIds;

  78.     /**
  79.      * Initialize a reference to an on-disk object directory.
  80.      *
  81.      * @param cfg
  82.      *            configuration this directory consults for write settings.
  83.      * @param dir
  84.      *            the location of the <code>objects</code> directory.
  85.      * @param alternatePaths
  86.      *            a list of alternate object directories
  87.      * @param fs
  88.      *            the file system abstraction which will be necessary to perform
  89.      *            certain file system operations.
  90.      * @param shallowFile
  91.      *            file which contains IDs of shallow commits, null if shallow
  92.      *            commits handling should be turned off
  93.      * @throws java.io.IOException
  94.      *             an alternate object cannot be opened.
  95.      */
  96.     public ObjectDirectory(final Config cfg, final File dir,
  97.             File[] alternatePaths, FS fs, File shallowFile) throws IOException {
  98.         config = cfg;
  99.         objects = dir;
  100.         infoDirectory = new File(objects, "info"); //$NON-NLS-1$
  101.         File packDirectory = new File(objects, "pack"); //$NON-NLS-1$
  102.         File preservedDirectory = new File(packDirectory, "preserved"); //$NON-NLS-1$
  103.         alternatesFile = new File(objects, Constants.INFO_ALTERNATES);
  104.         loose = new LooseObjects(objects);
  105.         packed = new PackDirectory(config, packDirectory);
  106.         preserved = new PackDirectory(config, preservedDirectory);
  107.         this.fs = fs;
  108.         this.shallowFile = shallowFile;

  109.         alternates = new AtomicReference<>();
  110.         if (alternatePaths != null) {
  111.             AlternateHandle[] alt;

  112.             alt = new AlternateHandle[alternatePaths.length];
  113.             for (int i = 0; i < alternatePaths.length; i++)
  114.                 alt[i] = openAlternate(alternatePaths[i]);
  115.             alternates.set(alt);
  116.         }
  117.     }

  118.     /** {@inheritDoc} */
  119.     @Override
  120.     public final File getDirectory() {
  121.         return loose.getDirectory();
  122.     }

  123.     /**
  124.      * <p>Getter for the field <code>packDirectory</code>.</p>
  125.      *
  126.      * @return the location of the <code>pack</code> directory.
  127.      */
  128.     public final File getPackDirectory() {
  129.         return packed.getDirectory();
  130.     }

  131.     /**
  132.      * <p>Getter for the field <code>preservedDirectory</code>.</p>
  133.      *
  134.      * @return the location of the <code>preserved</code> directory.
  135.      */
  136.     public final File getPreservedDirectory() {
  137.         return preserved.getDirectory();
  138.     }

  139.     /** {@inheritDoc} */
  140.     @Override
  141.     public boolean exists() {
  142.         return fs.exists(objects);
  143.     }

  144.     /** {@inheritDoc} */
  145.     @Override
  146.     public void create() throws IOException {
  147.         loose.create();
  148.         FileUtils.mkdir(infoDirectory);
  149.         packed.create();
  150.     }

  151.     /** {@inheritDoc} */
  152.     @Override
  153.     public ObjectDirectoryInserter newInserter() {
  154.         return new ObjectDirectoryInserter(this, config);
  155.     }

  156.     /**
  157.      * Create a new inserter that inserts all objects as pack files, not loose
  158.      * objects.
  159.      *
  160.      * @return new inserter.
  161.      */
  162.     public PackInserter newPackInserter() {
  163.         return new PackInserter(this);
  164.     }

  165.     /** {@inheritDoc} */
  166.     @Override
  167.     public void close() {
  168.         loose.close();

  169.         packed.close();

  170.         // Fully close all loaded alternates and clear the alternate list.
  171.         AlternateHandle[] alt = alternates.get();
  172.         if (alt != null && alternates.compareAndSet(alt, null)) {
  173.             for(AlternateHandle od : alt)
  174.                 od.close();
  175.         }
  176.     }

  177.     /** {@inheritDoc} */
  178.     @Override
  179.     public Collection<Pack> getPacks() {
  180.         return packed.getPacks();
  181.     }

  182.     /** {@inheritDoc} */
  183.     @Override
  184.     public long getApproximateObjectCount() {
  185.         long count = 0;
  186.         for (Pack p : getPacks()) {
  187.             try {
  188.                 count += p.getIndex().getObjectCount();
  189.             } catch (IOException e) {
  190.                 return -1;
  191.             }
  192.         }
  193.         return count;
  194.     }

  195.     /**
  196.      * {@inheritDoc}
  197.      * <p>
  198.      * Add a single existing pack to the list of available pack files.
  199.      */
  200.     @Override
  201.     public Pack openPack(File pack) throws IOException {
  202.         PackFile pf;
  203.         try {
  204.             pf = new PackFile(pack);
  205.         } catch (IllegalArgumentException e) {
  206.             throw new IOException(
  207.                     MessageFormat.format(JGitText.get().notAValidPack, pack),
  208.                     e);
  209.         }

  210.         String p = pf.getName();
  211.         // TODO(nasserg): See if PackFile can do these checks instead
  212.         if (p.length() != 50 || !p.startsWith("pack-") //$NON-NLS-1$
  213.                 || !pf.getPackExt().equals(PACK)) {
  214.             throw new IOException(
  215.                     MessageFormat.format(JGitText.get().notAValidPack, pack));
  216.         }

  217.         PackFile bitmapIdx = pf.create(BITMAP_INDEX);
  218.         Pack res = new Pack(pack, bitmapIdx.exists() ? bitmapIdx : null);
  219.         packed.insert(res);
  220.         return res;
  221.     }

  222.     /** {@inheritDoc} */
  223.     @Override
  224.     public String toString() {
  225.         return "ObjectDirectory[" + getDirectory() + "]"; //$NON-NLS-1$ //$NON-NLS-2$
  226.     }

  227.     /** {@inheritDoc} */
  228.     @Override
  229.     public boolean has(AnyObjectId objectId) {
  230.         return loose.hasCached(objectId)
  231.                 || hasPackedOrLooseInSelfOrAlternate(objectId)
  232.                 || (restoreFromSelfOrAlternate(objectId, null)
  233.                         && hasPackedOrLooseInSelfOrAlternate(objectId));
  234.     }

  235.     private boolean hasPackedOrLooseInSelfOrAlternate(AnyObjectId objectId) {
  236.         return hasPackedInSelfOrAlternate(objectId, null)
  237.                 || hasLooseInSelfOrAlternate(objectId, null);
  238.     }

  239.     private boolean hasPackedInSelfOrAlternate(AnyObjectId objectId,
  240.             Set<AlternateHandle.Id> skips) {
  241.         if (hasPackedObject(objectId)) {
  242.             return true;
  243.         }
  244.         skips = addMe(skips);
  245.         for (AlternateHandle alt : myAlternates()) {
  246.             if (!skips.contains(alt.getId())) {
  247.                 if (alt.db.hasPackedInSelfOrAlternate(objectId, skips)) {
  248.                     return true;
  249.                 }
  250.             }
  251.         }
  252.         return false;
  253.     }

  254.     private boolean hasLooseInSelfOrAlternate(AnyObjectId objectId,
  255.             Set<AlternateHandle.Id> skips) {
  256.         if (loose.has(objectId)) {
  257.             return true;
  258.         }
  259.         skips = addMe(skips);
  260.         for (AlternateHandle alt : myAlternates()) {
  261.             if (!skips.contains(alt.getId())) {
  262.                 if (alt.db.hasLooseInSelfOrAlternate(objectId, skips)) {
  263.                     return true;
  264.                 }
  265.             }
  266.         }
  267.         return false;
  268.     }

  269.     boolean hasPackedObject(AnyObjectId objectId) {
  270.         return packed.has(objectId);
  271.     }

  272.     @Override
  273.     void resolve(Set<ObjectId> matches, AbbreviatedObjectId id)
  274.             throws IOException {
  275.         resolve(matches, id, null);
  276.     }

  277.     private void resolve(Set<ObjectId> matches, AbbreviatedObjectId id,
  278.             Set<AlternateHandle.Id> skips)
  279.             throws IOException {
  280.         if (!packed.resolve(matches, id, RESOLVE_ABBREV_LIMIT))
  281.             return;

  282.         if (!loose.resolve(matches, id, RESOLVE_ABBREV_LIMIT))
  283.             return;

  284.         skips = addMe(skips);
  285.         for (AlternateHandle alt : myAlternates()) {
  286.             if (!skips.contains(alt.getId())) {
  287.                 alt.db.resolve(matches, id, skips);
  288.                 if (matches.size() > RESOLVE_ABBREV_LIMIT) {
  289.                     return;
  290.                 }
  291.             }
  292.         }
  293.     }

  294.     @Override
  295.     ObjectLoader openObject(WindowCursor curs, AnyObjectId objectId)
  296.             throws IOException {
  297.         ObjectLoader ldr = openObjectWithoutRestoring(curs, objectId);
  298.         if (ldr == null && restoreFromSelfOrAlternate(objectId, null)) {
  299.             ldr = openObjectWithoutRestoring(curs, objectId);
  300.         }
  301.         return ldr;
  302.     }

  303.     private ObjectLoader openObjectWithoutRestoring(WindowCursor curs, AnyObjectId objectId)
  304.             throws IOException {
  305.         if (loose.hasCached(objectId)) {
  306.             ObjectLoader ldr = openLooseObject(curs, objectId);
  307.             if (ldr != null) {
  308.                 return ldr;
  309.             }
  310.         }
  311.         ObjectLoader ldr = openPackedFromSelfOrAlternate(curs, objectId, null);
  312.         if (ldr != null) {
  313.             return ldr;
  314.         }
  315.         return openLooseFromSelfOrAlternate(curs, objectId, null);
  316.     }

  317.     private ObjectLoader openPackedFromSelfOrAlternate(WindowCursor curs,
  318.             AnyObjectId objectId, Set<AlternateHandle.Id> skips) {
  319.         ObjectLoader ldr = openPackedObject(curs, objectId);
  320.         if (ldr != null) {
  321.             return ldr;
  322.         }
  323.         skips = addMe(skips);
  324.         for (AlternateHandle alt : myAlternates()) {
  325.             if (!skips.contains(alt.getId())) {
  326.                 ldr = alt.db.openPackedFromSelfOrAlternate(curs, objectId, skips);
  327.                 if (ldr != null) {
  328.                     return ldr;
  329.                 }
  330.             }
  331.         }
  332.         return null;
  333.     }

  334.     private ObjectLoader openLooseFromSelfOrAlternate(WindowCursor curs,
  335.             AnyObjectId objectId, Set<AlternateHandle.Id> skips)
  336.                     throws IOException {
  337.         ObjectLoader ldr = openLooseObject(curs, objectId);
  338.         if (ldr != null) {
  339.             return ldr;
  340.         }
  341.         skips = addMe(skips);
  342.         for (AlternateHandle alt : myAlternates()) {
  343.             if (!skips.contains(alt.getId())) {
  344.                 ldr = alt.db.openLooseFromSelfOrAlternate(curs, objectId, skips);
  345.                 if (ldr != null) {
  346.                     return ldr;
  347.                 }
  348.             }
  349.         }
  350.         return null;
  351.     }

  352.     ObjectLoader openPackedObject(WindowCursor curs, AnyObjectId objectId) {
  353.         return packed.open(curs, objectId);
  354.     }

  355.     @Override
  356.     ObjectLoader openLooseObject(WindowCursor curs, AnyObjectId id)
  357.             throws IOException {
  358.         return loose.open(curs, id);
  359.     }

  360.     @Override
  361.     long getObjectSize(WindowCursor curs, AnyObjectId id) throws IOException {
  362.         long sz = getObjectSizeWithoutRestoring(curs, id);
  363.         if (0 > sz && restoreFromSelfOrAlternate(id, null)) {
  364.             sz = getObjectSizeWithoutRestoring(curs, id);
  365.         }
  366.         return sz;
  367.     }

  368.     private long getObjectSizeWithoutRestoring(WindowCursor curs,
  369.             AnyObjectId id) throws IOException {
  370.         if (loose.hasCached(id)) {
  371.             long len = loose.getSize(curs, id);
  372.             if (0 <= len) {
  373.                 return len;
  374.             }
  375.         }
  376.         long len = getPackedSizeFromSelfOrAlternate(curs, id, null);
  377.         if (0 <= len) {
  378.             return len;
  379.         }
  380.         return getLooseSizeFromSelfOrAlternate(curs, id, null);
  381.     }

  382.     private long getPackedSizeFromSelfOrAlternate(WindowCursor curs,
  383.             AnyObjectId id, Set<AlternateHandle.Id> skips) {
  384.         long len = packed.getSize(curs, id);
  385.         if (0 <= len) {
  386.             return len;
  387.         }
  388.         skips = addMe(skips);
  389.         for (AlternateHandle alt : myAlternates()) {
  390.             if (!skips.contains(alt.getId())) {
  391.                 len = alt.db.getPackedSizeFromSelfOrAlternate(curs, id, skips);
  392.                 if (0 <= len) {
  393.                     return len;
  394.                 }
  395.             }
  396.         }
  397.         return -1;
  398.     }

  399.     private long getLooseSizeFromSelfOrAlternate(WindowCursor curs,
  400.             AnyObjectId id, Set<AlternateHandle.Id> skips) throws IOException {
  401.         long len = loose.getSize(curs, id);
  402.         if (0 <= len) {
  403.             return len;
  404.         }
  405.         skips = addMe(skips);
  406.         for (AlternateHandle alt : myAlternates()) {
  407.             if (!skips.contains(alt.getId())) {
  408.                 len = alt.db.getLooseSizeFromSelfOrAlternate(curs, id, skips);
  409.                 if (0 <= len) {
  410.                     return len;
  411.                 }
  412.             }
  413.         }
  414.         return -1;
  415.     }

  416.     @Override
  417.     void selectObjectRepresentation(PackWriter packer, ObjectToPack otp,
  418.             WindowCursor curs) throws IOException {
  419.         selectObjectRepresentation(packer, otp, curs, null);
  420.     }

  421.     private void selectObjectRepresentation(PackWriter packer, ObjectToPack otp,
  422.             WindowCursor curs, Set<AlternateHandle.Id> skips) throws IOException {
  423.         packed.selectRepresentation(packer, otp, curs);

  424.         skips = addMe(skips);
  425.         for (AlternateHandle h : myAlternates()) {
  426.             if (!skips.contains(h.getId())) {
  427.                 h.db.selectObjectRepresentation(packer, otp, curs, skips);
  428.             }
  429.         }
  430.     }

  431.     private boolean restoreFromSelfOrAlternate(AnyObjectId objectId,
  432.             Set<AlternateHandle.Id> skips) {
  433.         if (restoreFromSelf(objectId)) {
  434.             return true;
  435.         }

  436.         skips = addMe(skips);
  437.         for (AlternateHandle alt : myAlternates()) {
  438.             if (!skips.contains(alt.getId())) {
  439.                 if (alt.db.restoreFromSelfOrAlternate(objectId, skips)) {
  440.                     return true;
  441.                 }
  442.             }
  443.         }
  444.         return false;
  445.     }

  446.     private boolean restoreFromSelf(AnyObjectId objectId) {
  447.         Pack preservedPack = preserved.getPack(objectId);
  448.         if (preservedPack == null) {
  449.             return false;
  450.         }
  451.         PackFile preservedFile = new PackFile(preservedPack.getPackFile());
  452.         // Restore the index last since the set will be considered for use once
  453.         // the index appears.
  454.         for (PackExt ext : PackExt.values()) {
  455.             if (!INDEX.equals(ext)) {
  456.                 restore(preservedFile.create(ext));
  457.             }
  458.         }
  459.         restore(preservedFile.create(INDEX));
  460.         return true;
  461.     }

  462.     private boolean restore(PackFile preservedPack) {
  463.         PackFile restored = preservedPack
  464.                 .createForDirectory(packed.getDirectory());
  465.         try {
  466.             Files.createLink(restored.toPath(), preservedPack.toPath());
  467.         } catch (IOException e) {
  468.             return false;
  469.         }
  470.         return true;
  471.     }

  472.     @Override
  473.     InsertLooseObjectResult insertUnpackedObject(File tmp, ObjectId id,
  474.             boolean createDuplicate) throws IOException {
  475.         // If the object is already in the repository, remove temporary file.
  476.         //
  477.         if (loose.hasCached(id)) {
  478.             FileUtils.delete(tmp, FileUtils.RETRY);
  479.             return InsertLooseObjectResult.EXISTS_LOOSE;
  480.         }
  481.         if (!createDuplicate && has(id)) {
  482.             FileUtils.delete(tmp, FileUtils.RETRY);
  483.             return InsertLooseObjectResult.EXISTS_PACKED;
  484.         }
  485.         return loose.insert(tmp, id);
  486.     }

  487.     @Override
  488.     Config getConfig() {
  489.         return config;
  490.     }

  491.     @Override
  492.     FS getFS() {
  493.         return fs;
  494.     }

  495.     @Override
  496.     Set<ObjectId> getShallowCommits() throws IOException {
  497.         if (shallowFile == null || !shallowFile.isFile())
  498.             return Collections.emptySet();

  499.         if (shallowFileSnapshot == null
  500.                 || shallowFileSnapshot.isModified(shallowFile)) {
  501.             shallowCommitsIds = new HashSet<>();

  502.             try (BufferedReader reader = open(shallowFile)) {
  503.                 String line;
  504.                 while ((line = reader.readLine()) != null) {
  505.                     try {
  506.                         shallowCommitsIds.add(ObjectId.fromString(line));
  507.                     } catch (IllegalArgumentException ex) {
  508.                         throw new IOException(MessageFormat
  509.                                 .format(JGitText.get().badShallowLine, line),
  510.                                 ex);
  511.                     }
  512.                 }
  513.             }

  514.             shallowFileSnapshot = FileSnapshot.save(shallowFile);
  515.         }

  516.         return shallowCommitsIds;
  517.     }

  518.     void closeAllPackHandles(File packFile) {
  519.         // if the packfile already exists (because we are rewriting a
  520.         // packfile for the same set of objects maybe with different
  521.         // PackConfig) then make sure we get rid of all handles on the file.
  522.         // Windows will not allow for rename otherwise.
  523.         if (packFile.exists()) {
  524.             for (Pack p : packed.getPacks()) {
  525.                 if (packFile.getPath().equals(p.getPackFile().getPath())) {
  526.                     p.close();
  527.                     break;
  528.                 }
  529.             }
  530.         }
  531.     }

  532.     AlternateHandle[] myAlternates() {
  533.         AlternateHandle[] alt = alternates.get();
  534.         if (alt == null) {
  535.             synchronized (alternates) {
  536.                 alt = alternates.get();
  537.                 if (alt == null) {
  538.                     try {
  539.                         alt = loadAlternates();
  540.                     } catch (IOException e) {
  541.                         alt = new AlternateHandle[0];
  542.                     }
  543.                     alternates.set(alt);
  544.                 }
  545.             }
  546.         }
  547.         return alt;
  548.     }

  549.     Set<AlternateHandle.Id> addMe(Set<AlternateHandle.Id> skips) {
  550.         if (skips == null) {
  551.             skips = new HashSet<>();
  552.         }
  553.         skips.add(handle.getId());
  554.         return skips;
  555.     }

  556.     private AlternateHandle[] loadAlternates() throws IOException {
  557.         final List<AlternateHandle> l = new ArrayList<>(4);
  558.         try (BufferedReader br = open(alternatesFile)) {
  559.             String line;
  560.             while ((line = br.readLine()) != null) {
  561.                 l.add(openAlternate(line));
  562.             }
  563.         }
  564.         return l.toArray(new AlternateHandle[0]);
  565.     }

  566.     private static BufferedReader open(File f)
  567.             throws IOException, FileNotFoundException {
  568.         return Files.newBufferedReader(f.toPath(), UTF_8);
  569.     }

  570.     private AlternateHandle openAlternate(String location)
  571.             throws IOException {
  572.         final File objdir = fs.resolve(objects, location);
  573.         return openAlternate(objdir);
  574.     }

  575.     private AlternateHandle openAlternate(File objdir) throws IOException {
  576.         final File parent = objdir.getParentFile();
  577.         if (FileKey.isGitRepository(parent, fs)) {
  578.             FileKey key = FileKey.exact(parent, fs);
  579.             FileRepository db = (FileRepository) RepositoryCache.open(key);
  580.             return new AlternateRepository(db);
  581.         }

  582.         ObjectDirectory db = new ObjectDirectory(config, objdir, null, fs, null);
  583.         return new AlternateHandle(db);
  584.     }

  585.     /**
  586.      * Compute the location of a loose object file.
  587.      */
  588.     @Override
  589.     public File fileFor(AnyObjectId objectId) {
  590.         return loose.fileFor(objectId);
  591.     }

  592.     static class AlternateHandle {
  593.         static class Id {
  594.             String alternateId;

  595.             public Id(File object) {
  596.                 try {
  597.                     this.alternateId = object.getCanonicalPath();
  598.                 } catch (Exception e) {
  599.                     alternateId = null;
  600.                 }
  601.             }

  602.             @Override
  603.             public boolean equals(Object o) {
  604.                 if (o == this) {
  605.                     return true;
  606.                 }
  607.                 if (o == null || !(o instanceof Id)) {
  608.                     return false;
  609.                 }
  610.                 Id aId = (Id) o;
  611.                 return Objects.equals(alternateId, aId.alternateId);
  612.             }

  613.             @Override
  614.             public int hashCode() {
  615.                 if (alternateId == null) {
  616.                     return 1;
  617.                 }
  618.                 return alternateId.hashCode();
  619.             }
  620.         }

  621.         final ObjectDirectory db;

  622.         AlternateHandle(ObjectDirectory db) {
  623.             this.db = db;
  624.         }

  625.         void close() {
  626.             db.close();
  627.         }

  628.         public Id getId(){
  629.             return db.getAlternateId();
  630.         }
  631.     }

  632.     static class AlternateRepository extends AlternateHandle {
  633.         final FileRepository repository;

  634.         AlternateRepository(FileRepository r) {
  635.             super(r.getObjectDatabase());
  636.             repository = r;
  637.         }

  638.         @Override
  639.         void close() {
  640.             repository.close();
  641.         }
  642.     }

  643.     /** {@inheritDoc} */
  644.     @Override
  645.     public ObjectDatabase newCachedDatabase() {
  646.         return newCachedFileObjectDatabase();
  647.     }

  648.     CachedObjectDirectory newCachedFileObjectDatabase() {
  649.         return new CachedObjectDirectory(this);
  650.     }

  651.     AlternateHandle.Id getAlternateId() {
  652.         return new AlternateHandle.Id(objects);
  653.     }
  654. }