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 com.googlecode.javaewah.EWAHCompressedBitmap;
51 import com.googlecode.javaewah.IntIterator;
52
53 import org.eclipse.jgit.internal.storage.file.BasePackBitmapIndex.StoredBitmap;
54 import org.eclipse.jgit.lib.AnyObjectId;
55 import org.eclipse.jgit.lib.BitmapIndex;
56 import org.eclipse.jgit.lib.ObjectId;
57 import org.eclipse.jgit.lib.ObjectIdOwnerMap;
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 private 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<StoredBitmap>();
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 @Override
120 public int findPosition(AnyObjectId objectId) {
121 return newPackIndex.findPosition(objectId);
122 }
123
124 @Override
125 public ObjectId getObject(int position) throws IllegalArgumentException {
126 return newPackIndex.getObject(position);
127 }
128
129 @Override
130 public int getObjectCount() {
131 return newPackIndex.getObjectCount();
132 }
133
134 @Override
135 public EWAHCompressedBitmap ofObjectType(
136 EWAHCompressedBitmap bitmap, int type) {
137 return newPackIndex.ofObjectType(bitmap, type);
138 }
139
140 public Iterator<Entry> iterator() {
141 if (oldPackIndex == null)
142 return Collections.<Entry> emptyList().iterator();
143
144 final Iterator<StoredBitmap> it = oldPackIndex.getBitmaps().iterator();
145 return new Iterator<Entry>() {
146 private Entry entry;
147
148 public boolean hasNext() {
149 while (entry == null && it.hasNext()) {
150 StoredBitmap sb = it.next();
151 if (newPackIndex.findPosition(sb) != -1)
152 entry = new Entry(sb, sb.getFlags());
153 }
154 return entry != null;
155 }
156
157 public Entry next() {
158 if (!hasNext())
159 throw new NoSuchElementException();
160
161 Entry res = entry;
162 entry = null;
163 return res;
164 }
165
166 public void remove() {
167 throw new UnsupportedOperationException();
168 }
169 };
170 }
171
172 @Override
173 public EWAHCompressedBitmap getBitmap(AnyObjectId objectId) {
174 EWAHCompressedBitmap bitmap = newPackIndex.getBitmap(objectId);
175 if (bitmap != null || oldPackIndex == null)
176 return bitmap;
177
178 StoredBitmap stored = convertedBitmaps.get(objectId);
179 if (stored != null)
180 return stored.getBitmap();
181
182 StoredBitmap oldBitmap = oldPackIndex.getBitmaps().get(objectId);
183 if (oldBitmap == null)
184 return null;
185
186 if (newPackIndex.findPosition(objectId) == -1)
187 return null;
188
189 inflated.clear();
190 for (IntIterator i = oldBitmap.getBitmap().intIterator(); i.hasNext();)
191 inflated.set(prevToNewMapping[i.next()]);
192 bitmap = inflated.toEWAHCompressedBitmap();
193 convertedBitmaps.add(
194 new StoredBitmap(objectId, bitmap, null, oldBitmap.getFlags()));
195 return bitmap;
196 }
197
198
199 public final class Entry extends ObjectId {
200 private final int flags;
201
202 private Entry(AnyObjectId src, int flags) {
203 super(src);
204 this.flags = flags;
205 }
206
207
208 public int getFlags() {
209 return flags;
210 }
211 }
212 }