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.internal.storage.file;
45
46 import java.io.File;
47 import java.io.FileInputStream;
48 import java.io.IOException;
49 import java.io.InputStream;
50 import java.text.MessageFormat;
51
52 import org.eclipse.jgit.errors.CorruptObjectException;
53 import org.eclipse.jgit.internal.JGitText;
54 import org.eclipse.jgit.lib.AnyObjectId;
55 import org.eclipse.jgit.lib.ObjectId;
56
57 import com.googlecode.javaewah.EWAHCompressedBitmap;
58
59 /**
60 * Logical representation of the bitmap data stored in the pack index.
61 * {@link org.eclipse.jgit.lib.ObjectId}s are encoded as a single integer in the
62 * range [0, {@link #getObjectCount()}). Compressed bitmaps are available at
63 * certain {@code ObjectId}s, which represent all of the objects reachable from
64 * that {@code ObjectId} (include the {@code ObjectId} itself). The meaning of
65 * the positions in the bitmaps can be decoded using {@link #getObject(int)} and
66 * {@link #ofObjectType(EWAHCompressedBitmap, int)}. Furthermore,
67 * {@link #findPosition(AnyObjectId)} can be used to build other bitmaps that a
68 * compatible with the encoded bitmaps available from the index.
69 */
70 public abstract class PackBitmapIndex {
71 /** Flag bit denoting the bitmap should be reused during index creation. */
72 public static final int FLAG_REUSE = 1;
73
74 /**
75 * Read an existing pack bitmap index file from a buffered stream.
76 * <p>
77 * The format of the file will be automatically detected and a proper access
78 * implementation for that format will be constructed and returned to the
79 * caller. The file may or may not be held open by the returned instance.
80 *
81 * @param idxFile
82 * existing pack .bitmap to read.
83 * @param packIndex
84 * the pack index for the corresponding pack file.
85 * @param reverseIndex
86 * the pack reverse index for the corresponding pack file.
87 * @return a copy of the index in-memory.
88 * @throws java.io.IOException
89 * the stream cannot be read.
90 * @throws CorruptObjectException
91 * the stream does not contain a valid pack bitmap index.
92 */
93 public static PackBitmapIndex open(
94 File idxFile, PackIndex packIndex, PackReverseIndex reverseIndex)
95 throws IOException {
96 final FileInputStream fd = new FileInputStream(idxFile);
97 try {
98 return read(fd, packIndex, reverseIndex);
99 } catch (IOException ioe) {
100 throw new IOException(MessageFormat
101 .format(JGitText.get().unreadablePackIndex,
102 idxFile.getAbsolutePath()),
103 ioe);
104 } finally {
105 try {
106 fd.close();
107 } catch (IOException err2) {
108 // ignore
109 }
110 }
111 }
112
113 /**
114 * Read an existing pack bitmap index file from a buffered stream.
115 * <p>
116 * The format of the file will be automatically detected and a proper access
117 * implementation for that format will be constructed and returned to the
118 * caller. The file may or may not be held open by the returned instance.
119 *
120 * @param fd
121 * stream to read the bitmap index file from. The stream must be
122 * buffered as some small IOs are performed against the stream.
123 * The caller is responsible for closing the stream.
124 * @param packIndex
125 * the pack index for the corresponding pack file.
126 * @param reverseIndex
127 * the pack reverse index for the corresponding pack file.
128 * @return a copy of the index in-memory.
129 * @throws java.io.IOException
130 * the stream cannot be read.
131 * @throws CorruptObjectException
132 * the stream does not contain a valid pack bitmap index.
133 */
134 public static PackBitmapIndex read(
135 InputStream fd, PackIndex packIndex, PackReverseIndex reverseIndex)
136 throws IOException {
137 return new PackBitmapIndexV1(fd, packIndex, reverseIndex);
138 }
139
140 /** Footer checksum applied on the bottom of the pack file. */
141 byte[] packChecksum;
142
143 /**
144 * Finds the position in the bitmap of the object.
145 *
146 * @param objectId
147 * the id for which the bitmap position will be found.
148 * @return the bitmap id or -1 if the object was not found.
149 */
150 public abstract int findPosition(AnyObjectId objectId);
151
152 /**
153 * Get the object at the bitmap position.
154 *
155 * @param position
156 * the id for which the object will be found.
157 * @return the ObjectId.
158 * @throws java.lang.IllegalArgumentException
159 * when the item is not found.
160 */
161 public abstract ObjectId getObject(int position) throws IllegalArgumentException;
162
163 /**
164 * Returns a bitmap containing positions for objects that have the given Git
165 * type.
166 *
167 * @param bitmap
168 * the object bitmap.
169 * @param type
170 * the Git type.
171 * @return the object bitmap with only objects of the Git type.
172 */
173 public abstract EWAHCompressedBitmap ofObjectType(
174 EWAHCompressedBitmap bitmap, int type);
175
176 /**
177 * Returns the previously constructed bitmap for the object.
178 *
179 * @param objectId
180 * the id for which the bitmap will be found.
181 * @return the bitmap or null if the object was not found.
182 */
183 public abstract EWAHCompressedBitmap getBitmap(AnyObjectId objectId);
184
185 /**
186 * Obtain the total number of objects described by this index.
187 * {@code getObjectCount() - 1} is the largest bit that will be set in a
188 * bitmap.
189 *
190 * @return number of objects in this index, and likewise in the associated
191 * pack that this index was generated from.
192 */
193 public abstract int getObjectCount();
194
195 /**
196 * Returns the number of bitmaps in this bitmap index.
197 *
198 * @return the number of bitmaps in this bitmap index.
199 */
200 public abstract int getBitmapCount();
201 }