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 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   * 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  	private 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<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 	/** An entry in the old PackBitmapIndex. */
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 		/** @return the flags associated with the bitmap. */
208 		public int getFlags() {
209 			return flags;
210 		}
211 	}
212 }