View Javadoc
1   /*
2    * Copyright (C) 2012, 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.lib;
45  
46  import java.util.Iterator;
47  
48  import org.eclipse.jgit.internal.storage.file.PackBitmapIndex;
49  
50  /**
51   * A compressed bitmap representation of the entire object graph.
52   *
53   * @since 3.0
54   */
55  public interface BitmapIndex {
56  	/**
57  	 * Get the bitmap for the id. The returned bitmap is immutable and the
58  	 * bitwise operations return the result of the operation in a new Bitmap.
59  	 *
60  	 * @param objectId
61  	 *            the object ID
62  	 * @return the Bitmap for the objectId or null, if one does not exist.
63  	 */
64  	Bitmap getBitmap(AnyObjectId objectId);
65  
66  	/** @return a new {@code BitmapBuilder} based on the values in the index. */
67  	BitmapBuilder newBitmapBuilder();
68  
69  	/**
70  	 * A bitmap representation of ObjectIds that can be iterated to return the
71  	 * underlying {@code ObjectId}s or operated on with other {@code Bitmap}s.
72  	 */
73  	public interface Bitmap extends Iterable<BitmapObject> {
74  		/**
75  		 * Bitwise-OR the current bitmap with the value from the other
76  		 * bitmap.
77  		 *
78  		 * @param other
79  		 *            the other bitmap
80  		 * @return a bitmap that is the bitwise-OR.
81  		 */
82  		Bitmap or(Bitmap other);
83  
84  		/**
85  		 * Bitwise-AND-NOT the current bitmap with the value from the other
86  		 * bitmap.
87  		 *
88  		 * @param other
89  		 *            the other bitmap
90  		 * @return a bitmap that is the bitwise-AND-NOT.
91  		 */
92  		Bitmap andNot(Bitmap other);
93  
94  		/**
95  		 * Bitwise-XOR the current bitmap with the value from the other
96  		 * bitmap.
97  		 *
98  		 * @param other
99  		 *            the other bitmap
100 		 * @return a bitmap that is the bitwise-XOR.
101 		 */
102 		Bitmap xor(Bitmap other);
103 
104 		/**
105 		 * Returns an iterator over a set of elements of type BitmapObject. The
106 		 * BitmapObject instance is reused across calls to
107 		 * {@link Iterator#next()} for performance reasons.
108 		 *
109 		 * @return an Iterator.
110 		 */
111 		@Override
112 		Iterator<BitmapObject> iterator();
113 	}
114 
115 	/**
116 	 * A builder for a bitmap. The bitwise operations update the builder and
117 	 * return a reference to the current builder.
118 	 */
119 	public interface BitmapBuilder extends Bitmap {
120 		/**
121 		 * Adds the id and the existing bitmap for the id, if one exists, to the
122 		 * bitmap.
123 		 *
124 		 * @param objectId
125 		 *            the object ID
126 		 * @param type
127 		 *            the Git object type. See {@link Constants}.
128 		 * @return true if the value was not contained or able to be loaded.
129 		 * @deprecated use {@link #or} or {@link #addObject} instead.
130 		 */
131 		@Deprecated
132 		boolean add(AnyObjectId objectId, int type);
133 
134 		/**
135 		 * Whether the bitmap has the id set.
136 		 *
137 		 * @param objectId
138 		 *            the object ID
139 		 * @return whether the bitmap currently contains the object ID
140 		 */
141 		boolean contains(AnyObjectId objectId);
142 
143 		/**
144 		 * Adds the id to the bitmap.
145 		 *
146 		 * @param objectId
147 		 *            the object ID
148 		 * @param type
149 		 *            the Git object type. See {@link Constants}.
150 		 * @return the current builder.
151 		 * @since 4.2
152 		 */
153 		BitmapBuilder addObject(AnyObjectId objectId, int type);
154 
155 		/**
156 		 * Remove the id from the bitmap.
157 		 *
158 		 * @param objectId
159 		 *            the object ID
160 		 */
161 		void remove(AnyObjectId objectId);
162 
163 		/**
164 		 * Bitwise-OR the current bitmap with the value from the other bitmap.
165 		 *
166 		 * @param other
167 		 *            the other bitmap
168 		 * @return the current builder.
169 		 */
170 		@Override
171 		BitmapBuilder or(Bitmap other);
172 
173 		/**
174 		 * Bitwise-AND-NOT the current bitmap with the value from the other
175 		 * bitmap.
176 		 *
177 		 * @param other
178 		 *            the other bitmap
179 		 * @return the current builder.
180 		 */
181 		@Override
182 		BitmapBuilder andNot(Bitmap other);
183 
184 		/**
185 		 * Bitwise-XOR the current bitmap with the value from the other bitmap.
186 		 *
187 		 * @param other
188 		 *            the other bitmap
189 		 * @return the current builder.
190 		 */
191 		@Override
192 		BitmapBuilder xor(Bitmap other);
193 
194 		/** @return the fully built immutable bitmap */
195 		Bitmap build();
196 
197 		/**
198 		 * Determines if the entire bitmap index is contained in the bitmap. If
199 		 * it is, the matching bits are removed from the bitmap and true is
200 		 * returned. If the bitmap index is null, false is returned.
201 		 *
202 		 * @param bitmapIndex
203 		 *            the bitmap index to check if it is completely contained
204 		 *            inside of the current bitmap.
205 		 * @return {@code true} if the bitmap index was a complete match.
206 		 */
207 		boolean removeAllOrNone(PackBitmapIndex bitmapIndex);
208 
209 		/** @return the number of elements in the bitmap. */
210 		int cardinality();
211 
212 		/**
213 		 * Get the BitmapIndex for this BitmapBuilder.
214 		 *
215 		 * @return the BitmapIndex for this BitmapBuilder
216 		 *
217 		 * @since 4.2
218 		 */
219 		BitmapIndex getBitmapIndex();
220 	}
221 }