1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.eclipse.jgit.internal.storage.file;
45
46 import java.util.Collections;
47 import java.util.Iterator;
48 import java.util.NoSuchElementException;
49
50 import org.eclipse.jgit.internal.storage.file.BasePackBitmapIndex.StoredBitmap;
51 import org.eclipse.jgit.lib.AnyObjectId;
52 import org.eclipse.jgit.lib.BitmapIndex;
53 import org.eclipse.jgit.lib.ObjectId;
54 import org.eclipse.jgit.lib.ObjectIdOwnerMap;
55
56 import com.googlecode.javaewah.EWAHCompressedBitmap;
57 import com.googlecode.javaewah.IntIterator;
58
59
60
61
62
63
64
65 public class PackBitmapIndexRemapper extends PackBitmapIndex
66 implements Iterable<PackBitmapIndexRemapper.Entry> {
67
68 private final BasePackBitmapIndex oldPackIndex;
69 final PackBitmapIndex newPackIndex;
70 private final ObjectIdOwnerMap<StoredBitmap> convertedBitmaps;
71 private final BitSet inflated;
72 private final int[] prevToNewMapping;
73
74
75
76
77
78
79
80
81
82
83
84 public static PackBitmapIndexRemapper newPackBitmapIndex(
85 BitmapIndex prevBitmapIndex, PackBitmapIndex newIndex) {
86 if (!(prevBitmapIndex instanceof BitmapIndexImpl))
87 return new PackBitmapIndexRemapper(newIndex);
88
89 PackBitmapIndex prevIndex = ((BitmapIndexImpl) prevBitmapIndex)
90 .getPackBitmapIndex();
91 if (!(prevIndex instanceof BasePackBitmapIndex))
92 return new PackBitmapIndexRemapper(newIndex);
93
94 return new PackBitmapIndexRemapper(
95 (BasePackBitmapIndex) prevIndex, newIndex);
96 }
97
98 private PackBitmapIndexRemapper(PackBitmapIndex newPackIndex) {
99 this.oldPackIndex = null;
100 this.newPackIndex = newPackIndex;
101 this.convertedBitmaps = null;
102 this.inflated = null;
103 this.prevToNewMapping = null;
104 }
105
106 private PackBitmapIndexRemapper(
107 BasePackBitmapIndex oldPackIndex, PackBitmapIndex newPackIndex) {
108 this.oldPackIndex = oldPackIndex;
109 this.newPackIndex = newPackIndex;
110 convertedBitmaps = new ObjectIdOwnerMap<>();
111 inflated = new BitSet(newPackIndex.getObjectCount());
112
113 prevToNewMapping = new int[oldPackIndex.getObjectCount()];
114 for (int pos = 0; pos < prevToNewMapping.length; pos++)
115 prevToNewMapping[pos] = newPackIndex.findPosition(
116 oldPackIndex.getObject(pos));
117 }
118
119
120 @Override
121 public int findPosition(AnyObjectId objectId) {
122 return newPackIndex.findPosition(objectId);
123 }
124
125
126 @Override
127 public ObjectId getObject(int position) throws IllegalArgumentException {
128 return newPackIndex.getObject(position);
129 }
130
131
132 @Override
133 public int getObjectCount() {
134 return newPackIndex.getObjectCount();
135 }
136
137
138 @Override
139 public EWAHCompressedBitmap ofObjectType(
140 EWAHCompressedBitmap bitmap, int type) {
141 return newPackIndex.ofObjectType(bitmap, type);
142 }
143
144
145 @Override
146 public Iterator<Entry> iterator() {
147 if (oldPackIndex == null)
148 return Collections.<Entry> emptyList().iterator();
149
150 final Iterator<StoredBitmap> it = oldPackIndex.getBitmaps().iterator();
151 return new Iterator<Entry>() {
152 private Entry entry;
153
154 @Override
155 public boolean hasNext() {
156 while (entry == null && it.hasNext()) {
157 StoredBitmap sb = it.next();
158 if (newPackIndex.findPosition(sb) != -1)
159 entry = new Entry(sb, sb.getFlags());
160 }
161 return entry != null;
162 }
163
164 @Override
165 public Entry next() {
166 if (!hasNext())
167 throw new NoSuchElementException();
168
169 Entry res = entry;
170 entry = null;
171 return res;
172 }
173
174 @Override
175 public void remove() {
176 throw new UnsupportedOperationException();
177 }
178 };
179 }
180
181
182 @Override
183 public EWAHCompressedBitmap getBitmap(AnyObjectId objectId) {
184 EWAHCompressedBitmap bitmap = newPackIndex.getBitmap(objectId);
185 if (bitmap != null || oldPackIndex == null)
186 return bitmap;
187
188 StoredBitmap stored = convertedBitmaps.get(objectId);
189 if (stored != null)
190 return stored.getBitmap();
191
192 StoredBitmap oldBitmap = oldPackIndex.getBitmaps().get(objectId);
193 if (oldBitmap == null)
194 return null;
195
196 if (newPackIndex.findPosition(objectId) == -1)
197 return null;
198
199 inflated.clear();
200 for (IntIterator i = oldBitmap.getBitmap().intIterator(); i.hasNext();)
201 inflated.set(prevToNewMapping[i.next()]);
202 bitmap = inflated.toEWAHCompressedBitmap();
203 bitmap.trim();
204 convertedBitmaps.add(
205 new StoredBitmap(objectId, bitmap, null, oldBitmap.getFlags()));
206 return bitmap;
207 }
208
209
210 public static final class Entry extends ObjectId {
211 private final int flags;
212
213 Entry(AnyObjectId src, int flags) {
214 super(src);
215 this.flags = flags;
216 }
217
218
219 public int getFlags() {
220 return flags;
221 }
222 }
223
224
225 @Override
226 public int getBitmapCount() {
227
228 return 0;
229 }
230 }