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.IOException;
48 import java.io.InputStream;
49 import java.text.MessageFormat;
50
51 import org.eclipse.jgit.errors.CorruptObjectException;
52 import org.eclipse.jgit.internal.JGitText;
53 import org.eclipse.jgit.lib.AnyObjectId;
54 import org.eclipse.jgit.lib.ObjectId;
55 import org.eclipse.jgit.util.io.SilentFileInputStream;
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 try (SilentFileInputStream fd = new SilentFileInputStream(
97 idxFile)) {
98 try {
99 return read(fd, packIndex, reverseIndex);
100 } catch (IOException ioe) {
101 throw new IOException(
102 MessageFormat.format(JGitText.get().unreadablePackIndex,
103 idxFile.getAbsolutePath()),
104 ioe);
105 }
106 }
107 }
108
109 /**
110 * Read an existing pack bitmap index file from a buffered stream.
111 * <p>
112 * The format of the file will be automatically detected and a proper access
113 * implementation for that format will be constructed and returned to the
114 * caller. The file may or may not be held open by the returned instance.
115 *
116 * @param fd
117 * stream to read the bitmap index file from. The stream must be
118 * buffered as some small IOs are performed against the stream.
119 * The caller is responsible for closing the stream.
120 * @param packIndex
121 * the pack index for the corresponding pack file.
122 * @param reverseIndex
123 * the pack reverse index for the corresponding pack file.
124 * @return a copy of the index in-memory.
125 * @throws java.io.IOException
126 * the stream cannot be read.
127 * @throws CorruptObjectException
128 * the stream does not contain a valid pack bitmap index.
129 */
130 public static PackBitmapIndex read(
131 InputStream fd, PackIndex packIndex, PackReverseIndex reverseIndex)
132 throws IOException {
133 return new PackBitmapIndexV1(fd, packIndex, reverseIndex);
134 }
135
136 /** Footer checksum applied on the bottom of the pack file. */
137 byte[] packChecksum;
138
139 /**
140 * Finds the position in the bitmap of the object.
141 *
142 * @param objectId
143 * the id for which the bitmap position will be found.
144 * @return the bitmap id or -1 if the object was not found.
145 */
146 public abstract int findPosition(AnyObjectId objectId);
147
148 /**
149 * Get the object at the bitmap position.
150 *
151 * @param position
152 * the id for which the object will be found.
153 * @return the ObjectId.
154 * @throws java.lang.IllegalArgumentException
155 * when the item is not found.
156 */
157 public abstract ObjectId getObject(int position) throws IllegalArgumentException;
158
159 /**
160 * Returns a bitmap containing positions for objects that have the given Git
161 * type.
162 *
163 * @param bitmap
164 * the object bitmap.
165 * @param type
166 * the Git type.
167 * @return the object bitmap with only objects of the Git type.
168 */
169 public abstract EWAHCompressedBitmap ofObjectType(
170 EWAHCompressedBitmap bitmap, int type);
171
172 /**
173 * Returns the previously constructed bitmap for the object.
174 *
175 * @param objectId
176 * the id for which the bitmap will be found.
177 * @return the bitmap or null if the object was not found.
178 */
179 public abstract EWAHCompressedBitmap getBitmap(AnyObjectId objectId);
180
181 /**
182 * Obtain the total number of objects described by this index.
183 * {@code getObjectCount() - 1} is the largest bit that will be set in a
184 * bitmap.
185 *
186 * @return number of objects in this index, and likewise in the associated
187 * pack that this index was generated from.
188 */
189 public abstract int getObjectCount();
190
191 /**
192 * Returns the number of bitmaps in this bitmap index.
193 *
194 * @return the number of bitmaps in this bitmap index.
195 */
196 public abstract int getBitmapCount();
197 }