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.

LocalCachedPack.java 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (C) 2011, 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 java.io.File;
  12. import java.io.FileNotFoundException;
  13. import java.io.IOException;
  14. import java.util.List;
  15. import org.eclipse.jgit.internal.storage.pack.CachedPack;
  16. import org.eclipse.jgit.internal.storage.pack.ObjectToPack;
  17. import org.eclipse.jgit.internal.storage.pack.PackExt;
  18. import org.eclipse.jgit.internal.storage.pack.PackOutputStream;
  19. import org.eclipse.jgit.internal.storage.pack.StoredObjectRepresentation;
  20. class LocalCachedPack extends CachedPack {
  21. private final ObjectDirectory odb;
  22. private final String[] packNames;
  23. private Pack[] packs;
  24. LocalCachedPack(ObjectDirectory odb, List<String> packNames) {
  25. this.odb = odb;
  26. this.packNames = packNames.toArray(new String[0]);
  27. }
  28. LocalCachedPack(List<Pack> packs) {
  29. odb = null;
  30. packNames = null;
  31. this.packs = packs.toArray(new Pack[0]);
  32. }
  33. /** {@inheritDoc} */
  34. @Override
  35. public long getObjectCount() throws IOException {
  36. long cnt = 0;
  37. for (Pack pack : getPacks())
  38. cnt += pack.getObjectCount();
  39. return cnt;
  40. }
  41. void copyAsIs(PackOutputStream out, WindowCursor wc)
  42. throws IOException {
  43. for (Pack pack : getPacks())
  44. pack.copyPackAsIs(out, wc);
  45. }
  46. /** {@inheritDoc} */
  47. @Override
  48. public boolean hasObject(ObjectToPack obj, StoredObjectRepresentation rep) {
  49. try {
  50. LocalObjectRepresentation local = (LocalObjectRepresentation) rep;
  51. for (Pack pack : getPacks()) {
  52. if (local.pack == pack)
  53. return true;
  54. }
  55. return false;
  56. } catch (FileNotFoundException packGone) {
  57. return false;
  58. }
  59. }
  60. private Pack[] getPacks() throws FileNotFoundException {
  61. if (packs == null) {
  62. Pack[] p = new Pack[packNames.length];
  63. for (int i = 0; i < packNames.length; i++)
  64. p[i] = getPackFile(packNames[i]);
  65. packs = p;
  66. }
  67. return packs;
  68. }
  69. private Pack getPackFile(String packName) throws FileNotFoundException {
  70. for (Pack pack : odb.getPacks()) {
  71. if (packName.equals(pack.getPackName()))
  72. return pack;
  73. }
  74. throw new FileNotFoundException(getPackFilePath(packName));
  75. }
  76. private String getPackFilePath(String packName) {
  77. final File packDir = odb.getPackDirectory();
  78. return new PackFile(packDir, packName, PackExt.PACK).getPath();
  79. }
  80. }