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