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.7KB

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