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