2
This code is derived from jgit (http://eclipse.org/jgit).
3
Copyright owners are documented in jgit's IP log.
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
12
Redistribution and use in source and binary forms, with or
13
without modification, are permitted provided that the following
16
- Redistributions of source code must retain the above copyright
17
notice, this list of conditions and the following disclaimer.
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.
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
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.
47
using NGit.Storage.File;
50
namespace NGit.Storage.File
54
/// Reverse index for forward pack index.
58
/// Reverse index for forward pack index. Provides operations based on offset
59
/// instead of object id. Such offset-based reverse lookups are performed in
63
/// <seealso cref="PackIndex">PackIndex</seealso>
64
/// <seealso cref="PackFile">PackFile</seealso>
65
internal class PackReverseIndex
67
/// <summary>Index we were created from, and that has our ObjectId data.</summary>
68
/// <remarks>Index we were created from, and that has our ObjectId data.</remarks>
69
private readonly PackIndex index;
71
/// <summary>(offset31, truly) Offsets accommodating in 31 bits.</summary>
72
/// <remarks>(offset31, truly) Offsets accommodating in 31 bits.</remarks>
73
private readonly int[] offsets32;
75
/// <summary>Offsets not accommodating in 31 bits.</summary>
76
/// <remarks>Offsets not accommodating in 31 bits.</remarks>
77
private readonly long[] offsets64;
80
/// Position of the corresponding
81
/// <see cref="offsets32">offsets32</see>
83
/// <see cref="index">index</see>
86
private readonly int[] nth32;
89
/// Position of the corresponding
90
/// <see cref="offsets64">offsets64</see>
92
/// <see cref="index">index</see>
95
private readonly int[] nth64;
98
/// Create reverse index from straight/forward pack index, by indexing all
102
/// Create reverse index from straight/forward pack index, by indexing all
105
/// <param name="packIndex">forward index - entries to (reverse) index.</param>
106
internal PackReverseIndex(PackIndex packIndex)
109
long cnt = index.GetObjectCount();
110
long n64 = index.GetOffset64Count();
111
long n32 = cnt - n64;
112
if (n32 > int.MaxValue || n64 > int.MaxValue || cnt > unchecked((long)(0xffffffffL
115
throw new ArgumentException(JGitText.Get().hugeIndexesAreNotSupportedByJgitYet);
117
offsets32 = new int[(int)n32];
118
offsets64 = new long[(int)n64];
119
nth32 = new int[offsets32.Length];
120
nth64 = new int[offsets64.Length];
123
foreach (PackIndex.MutableEntry me in index)
125
long o = me.GetOffset();
126
if (o < int.MaxValue)
128
offsets32[i32++] = (int)o;
132
offsets64[i64++] = o;
135
Arrays.Sort(offsets32);
136
Arrays.Sort(offsets64);
138
foreach (PackIndex.MutableEntry me_1 in index)
140
long o = me_1.GetOffset();
141
if (o < int.MaxValue)
143
nth32[System.Array.BinarySearch(offsets32, (int)o)] = nth++;
147
nth64[System.Array.BinarySearch(offsets64, o)] = nth++;
153
/// Search for object id with the specified start offset in this pack
157
/// Search for object id with the specified start offset in this pack
160
/// <param name="offset">start offset of object to find.</param>
161
/// <returns>object id for this offset, or null if no object was found.</returns>
162
internal virtual ObjectId FindObject(long offset)
164
if (offset <= int.MaxValue)
166
int i32 = System.Array.BinarySearch(offsets32, (int)offset);
171
return index.GetObjectId(nth32[i32]);
175
int i64 = System.Array.BinarySearch(offsets64, offset);
180
return index.GetObjectId(nth64[i64]);
185
/// Search for the next offset to the specified offset in this pack (reverse)
189
/// Search for the next offset to the specified offset in this pack (reverse)
192
/// <param name="offset">
193
/// start offset of previous object (must be valid-existing
196
/// <param name="maxOffset">
197
/// maximum offset in a pack (returned when there is no next
201
/// offset of the next object in a pack or maxOffset if provided
202
/// offset was the last one.
204
/// <exception cref="NGit.Errors.CorruptObjectException">when there is no object with the provided offset.
206
internal virtual long FindNextOffset(long offset, long maxOffset)
208
if (offset <= int.MaxValue)
210
int i32 = System.Array.BinarySearch(offsets32, (int)offset);
213
throw new CorruptObjectException(MessageFormat.Format(JGitText.Get().cantFindObjectInReversePackIndexForTheSpecifiedOffset
216
if (i32 + 1 == offsets32.Length)
218
if (offsets64.Length > 0)
224
return offsets32[i32 + 1];
228
int i64 = System.Array.BinarySearch(offsets64, offset);
231
throw new CorruptObjectException(MessageFormat.Format(JGitText.Get().cantFindObjectInReversePackIndexForTheSpecifiedOffset
234
if (i64 + 1 == offsets64.Length)
238
return offsets64[i64 + 1];