~ubuntu-branches/ubuntu/quantal/monodevelop/quantal

« back to all changes in this revision

Viewing changes to contrib/NGit/NGit.Storage.File/PackReverseIndex.cs

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Mitchell
  • Date: 2011-06-29 06:56:25 UTC
  • mfrom: (1.8.1 upstream) (1.3.11 experimental)
  • Revision ID: james.westby@ubuntu.com-20110629065625-7xx19c4vb95j65pl
Tags: 2.5.92+dfsg-1ubuntu1
* Merge from Debian experimental:
 - Dropped patches & changes to debian/control for Moonlight
   + debian/patches/remove_support_for_moonlight.patch,
   + debian/patches/dont_add_moonlight_to_core_addins.patch,
 - Remaining patches:
   + debian/patches/no_appmenu:

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
This code is derived from jgit (http://eclipse.org/jgit).
 
3
Copyright owners are documented in jgit'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
using System;
 
45
using NGit;
 
46
using NGit.Errors;
 
47
using NGit.Storage.File;
 
48
using Sharpen;
 
49
 
 
50
namespace NGit.Storage.File
 
51
{
 
52
        /// <summary>
 
53
        /// <p>
 
54
        /// Reverse index for forward pack index.
 
55
        /// </summary>
 
56
        /// <remarks>
 
57
        /// <p>
 
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
 
60
        /// O(log n) time.
 
61
        /// </p>
 
62
        /// </remarks>
 
63
        /// <seealso cref="PackIndex">PackIndex</seealso>
 
64
        /// <seealso cref="PackFile">PackFile</seealso>
 
65
        internal class PackReverseIndex
 
66
        {
 
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;
 
70
 
 
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;
 
74
 
 
75
                /// <summary>Offsets not accommodating in 31 bits.</summary>
 
76
                /// <remarks>Offsets not accommodating in 31 bits.</remarks>
 
77
                private readonly long[] offsets64;
 
78
 
 
79
                /// <summary>
 
80
                /// Position of the corresponding
 
81
                /// <see cref="offsets32">offsets32</see>
 
82
                /// in
 
83
                /// <see cref="index">index</see>
 
84
                /// .
 
85
                /// </summary>
 
86
                private readonly int[] nth32;
 
87
 
 
88
                /// <summary>
 
89
                /// Position of the corresponding
 
90
                /// <see cref="offsets64">offsets64</see>
 
91
                /// in
 
92
                /// <see cref="index">index</see>
 
93
                /// .
 
94
                /// </summary>
 
95
                private readonly int[] nth64;
 
96
 
 
97
                /// <summary>
 
98
                /// Create reverse index from straight/forward pack index, by indexing all
 
99
                /// its entries.
 
100
                /// </summary>
 
101
                /// <remarks>
 
102
                /// Create reverse index from straight/forward pack index, by indexing all
 
103
                /// its entries.
 
104
                /// </remarks>
 
105
                /// <param name="packIndex">forward index - entries to (reverse) index.</param>
 
106
                internal PackReverseIndex(PackIndex packIndex)
 
107
                {
 
108
                        index = 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
 
113
                                )))
 
114
                        {
 
115
                                throw new ArgumentException(JGitText.Get().hugeIndexesAreNotSupportedByJgitYet);
 
116
                        }
 
117
                        offsets32 = new int[(int)n32];
 
118
                        offsets64 = new long[(int)n64];
 
119
                        nth32 = new int[offsets32.Length];
 
120
                        nth64 = new int[offsets64.Length];
 
121
                        int i32 = 0;
 
122
                        int i64 = 0;
 
123
                        foreach (PackIndex.MutableEntry me in index)
 
124
                        {
 
125
                                long o = me.GetOffset();
 
126
                                if (o < int.MaxValue)
 
127
                                {
 
128
                                        offsets32[i32++] = (int)o;
 
129
                                }
 
130
                                else
 
131
                                {
 
132
                                        offsets64[i64++] = o;
 
133
                                }
 
134
                        }
 
135
                        Arrays.Sort(offsets32);
 
136
                        Arrays.Sort(offsets64);
 
137
                        int nth = 0;
 
138
                        foreach (PackIndex.MutableEntry me_1 in index)
 
139
                        {
 
140
                                long o = me_1.GetOffset();
 
141
                                if (o < int.MaxValue)
 
142
                                {
 
143
                                        nth32[System.Array.BinarySearch(offsets32, (int)o)] = nth++;
 
144
                                }
 
145
                                else
 
146
                                {
 
147
                                        nth64[System.Array.BinarySearch(offsets64, o)] = nth++;
 
148
                                }
 
149
                        }
 
150
                }
 
151
 
 
152
                /// <summary>
 
153
                /// Search for object id with the specified start offset in this pack
 
154
                /// (reverse) index.
 
155
                /// </summary>
 
156
                /// <remarks>
 
157
                /// Search for object id with the specified start offset in this pack
 
158
                /// (reverse) index.
 
159
                /// </remarks>
 
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)
 
163
                {
 
164
                        if (offset <= int.MaxValue)
 
165
                        {
 
166
                                int i32 = System.Array.BinarySearch(offsets32, (int)offset);
 
167
                                if (i32 < 0)
 
168
                                {
 
169
                                        return null;
 
170
                                }
 
171
                                return index.GetObjectId(nth32[i32]);
 
172
                        }
 
173
                        else
 
174
                        {
 
175
                                int i64 = System.Array.BinarySearch(offsets64, offset);
 
176
                                if (i64 < 0)
 
177
                                {
 
178
                                        return null;
 
179
                                }
 
180
                                return index.GetObjectId(nth64[i64]);
 
181
                        }
 
182
                }
 
183
 
 
184
                /// <summary>
 
185
                /// Search for the next offset to the specified offset in this pack (reverse)
 
186
                /// index.
 
187
                /// </summary>
 
188
                /// <remarks>
 
189
                /// Search for the next offset to the specified offset in this pack (reverse)
 
190
                /// index.
 
191
                /// </remarks>
 
192
                /// <param name="offset">
 
193
                /// start offset of previous object (must be valid-existing
 
194
                /// offset).
 
195
                /// </param>
 
196
                /// <param name="maxOffset">
 
197
                /// maximum offset in a pack (returned when there is no next
 
198
                /// offset).
 
199
                /// </param>
 
200
                /// <returns>
 
201
                /// offset of the next object in a pack or maxOffset if provided
 
202
                /// offset was the last one.
 
203
                /// </returns>
 
204
                /// <exception cref="NGit.Errors.CorruptObjectException">when there is no object with the provided offset.
 
205
                ///     </exception>
 
206
                internal virtual long FindNextOffset(long offset, long maxOffset)
 
207
                {
 
208
                        if (offset <= int.MaxValue)
 
209
                        {
 
210
                                int i32 = System.Array.BinarySearch(offsets32, (int)offset);
 
211
                                if (i32 < 0)
 
212
                                {
 
213
                                        throw new CorruptObjectException(MessageFormat.Format(JGitText.Get().cantFindObjectInReversePackIndexForTheSpecifiedOffset
 
214
                                                , offset));
 
215
                                }
 
216
                                if (i32 + 1 == offsets32.Length)
 
217
                                {
 
218
                                        if (offsets64.Length > 0)
 
219
                                        {
 
220
                                                return offsets64[0];
 
221
                                        }
 
222
                                        return maxOffset;
 
223
                                }
 
224
                                return offsets32[i32 + 1];
 
225
                        }
 
226
                        else
 
227
                        {
 
228
                                int i64 = System.Array.BinarySearch(offsets64, offset);
 
229
                                if (i64 < 0)
 
230
                                {
 
231
                                        throw new CorruptObjectException(MessageFormat.Format(JGitText.Get().cantFindObjectInReversePackIndexForTheSpecifiedOffset
 
232
                                                , offset));
 
233
                                }
 
234
                                if (i64 + 1 == offsets64.Length)
 
235
                                {
 
236
                                        return maxOffset;
 
237
                                }
 
238
                                return offsets64[i64 + 1];
 
239
                        }
 
240
                }
 
241
        }
 
242
}