2 * Copyright (C) 2008 Shawn Pearce <spearce@spearce.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License, version 2, as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
17 package org
.spearce
.jgit
.lib
;
20 import java
.io
.FileInputStream
;
21 import java
.io
.FileNotFoundException
;
22 import java
.io
.IOException
;
24 import org
.spearce
.jgit
.util
.NB
;
27 * Access path to locate objects by {@link ObjectId} in a {@link PackFile}.
29 * Indexes are strictly redundant information in that we can rebuild all of the
30 * data held in the index file from the on disk representation of the pack file
31 * itself, but it is faster to access for random requests because data is stored
35 public abstract class PackIndex
{
37 * Open an existing pack <code>.idx</code> file for reading.
39 * The format of the file will be automatically detected and a proper access
40 * implementation for that format will be constructed and returned to the
41 * caller. The file may or may not be held open by the returned instance.
45 * existing pack .idx to read.
46 * @return access implementation for the requested file.
47 * @throws FileNotFoundException
48 * the file does not exist.
50 * the file exists but could not be read due to security errors,
51 * unrecognized data version, or unexpected data corruption.
53 public static PackIndex
open(final File idxFile
) throws IOException
{
54 final FileInputStream fd
= new FileInputStream(idxFile
);
56 final byte[] hdr
= new byte[8];
57 NB
.readFully(fd
, hdr
, 0, hdr
.length
);
59 final int v
= NB
.decodeInt32(hdr
, 4);
62 return new PackIndexV2(fd
);
64 throw new IOException("Unsupported pack index version " + v
);
67 return new PackIndexV1(fd
, hdr
);
68 } catch (IOException ioe
) {
69 final String path
= idxFile
.getAbsolutePath();
70 final IOException err
;
71 err
= new IOException("Unreadable pack index: " + path
);
77 } catch (IOException err2
) {
83 private static boolean isTOC(final byte[] h
) {
84 return h
[0] == -1 && h
[1] == 't' && h
[2] == 'O' && h
[3] == 'c';
88 * Determine if an object is contained within the pack file.
91 * the object to look for. Must not be null.
92 * @return true if the object is listed in this index; false otherwise.
94 public boolean hasObject(final AnyObjectId id
) {
95 return findOffset(id
) != -1;
99 * Obtain the total number of objects described by this index.
101 * @return number of objects in this index, and likewise in the associated
102 * pack that this index was generated from.
104 abstract long getObjectCount();
107 * Locate the file offset position for the requested object.
110 * name of the object to locate within the pack.
111 * @return offset of the object's header and compressed content; -1 if the
112 * object does not exist in this index and is thus not stored in the
115 abstract long findOffset(AnyObjectId objId
);