View Javadoc
1   /*
2    * Copyright (C) 2013, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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   * A PackBitmapIndex that remaps the bitmaps in the previous index to the
61   * positions in the new pack index. Note, unlike typical PackBitmapIndex
62   * implementations this implementation is not thread safe, as it is intended to
63   * be used with a PackBitmapIndexBuilder, which is also not thread safe.
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  	 * A PackBitmapIndex that maps the positions in the prevBitmapIndex to the
76  	 * ones in the newIndex.
77  	 *
78  	 * @param prevBitmapIndex
79  	 *            the bitmap index with the old mapping.
80  	 * @param newIndex
81  	 *            the bitmap index with the new mapping.
82  	 * @return a bitmap index that attempts to do the mapping between the two.
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 	/** {@inheritDoc} */
120 	@Override
121 	public int findPosition(AnyObjectId objectId) {
122 		return newPackIndex.findPosition(objectId);
123 	}
124 
125 	/** {@inheritDoc} */
126 	@Override
127 	public ObjectId getObject(int position) throws IllegalArgumentException {
128 		return newPackIndex.getObject(position);
129 	}
130 
131 	/** {@inheritDoc} */
132 	@Override
133 	public int getObjectCount() {
134 		return newPackIndex.getObjectCount();
135 	}
136 
137 	/** {@inheritDoc} */
138 	@Override
139 	public EWAHCompressedBitmap ofObjectType(
140 			EWAHCompressedBitmap bitmap, int type) {
141 		return newPackIndex.ofObjectType(bitmap, type);
142 	}
143 
144 	/** {@inheritDoc} */
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 	/** {@inheritDoc} */
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 		convertedBitmaps.add(
204 				new StoredBitmap(objectId, bitmap, null, oldBitmap.getFlags()));
205 		return bitmap;
206 	}
207 
208 	/** An entry in the old PackBitmapIndex. */
209 	public final class Entry extends ObjectId {
210 		private final int flags;
211 
212 		Entry(AnyObjectId src, int flags) {
213 			super(src);
214 			this.flags = flags;
215 		}
216 
217 		/** @return the flags associated with the bitmap. */
218 		public int getFlags() {
219 			return flags;
220 		}
221 	}
222 
223 	/** {@inheritDoc} */
224 	@Override
225 	public int getBitmapCount() {
226 		// The count is only useful for the end index, not the remapper.
227 		return 0;
228 	}
229 }