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

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