~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

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 System.Collections.Generic;
 
46
using System.IO;
 
47
using NGit;
 
48
using NGit.Storage.File;
 
49
using NGit.Util;
 
50
using Sharpen;
 
51
 
 
52
namespace NGit.Storage.File
 
53
{
 
54
        /// <summary>
 
55
        /// Access path to locate objects by
 
56
        /// <see cref="NGit.ObjectId">NGit.ObjectId</see>
 
57
        /// in a
 
58
        /// <see cref="PackFile">PackFile</see>
 
59
        /// .
 
60
        /// <p>
 
61
        /// Indexes are strictly redundant information in that we can rebuild all of the
 
62
        /// data held in the index file from the on disk representation of the pack file
 
63
        /// itself, but it is faster to access for random requests because data is stored
 
64
        /// by ObjectId.
 
65
        /// </p>
 
66
        /// </summary>
 
67
        public abstract class PackIndex : Iterable<PackIndex.MutableEntry>
 
68
        {
 
69
                /// <summary>Open an existing pack <code>.idx</code> file for reading.</summary>
 
70
                /// <remarks>
 
71
                /// Open an existing pack <code>.idx</code> file for reading.
 
72
                /// <p>
 
73
                /// The format of the file will be automatically detected and a proper access
 
74
                /// implementation for that format will be constructed and returned to the
 
75
                /// caller. The file may or may not be held open by the returned instance.
 
76
                /// </p>
 
77
                /// </remarks>
 
78
                /// <param name="idxFile">existing pack .idx to read.</param>
 
79
                /// <returns>access implementation for the requested file.</returns>
 
80
                /// <exception cref="System.IO.FileNotFoundException">the file does not exist.</exception>
 
81
                /// <exception cref="System.IO.IOException">
 
82
                /// the file exists but could not be read due to security errors,
 
83
                /// unrecognized data version, or unexpected data corruption.
 
84
                /// </exception>
 
85
                public static PackIndex Open(FilePath idxFile)
 
86
                {
 
87
                        FileInputStream fd = new FileInputStream(idxFile);
 
88
                        try
 
89
                        {
 
90
                                byte[] hdr = new byte[8];
 
91
                                IOUtil.ReadFully(fd, hdr, 0, hdr.Length);
 
92
                                if (IsTOC(hdr))
 
93
                                {
 
94
                                        int v = NB.DecodeInt32(hdr, 4);
 
95
                                        switch (v)
 
96
                                        {
 
97
                                                case 2:
 
98
                                                {
 
99
                                                        return new PackIndexV2(fd);
 
100
                                                }
 
101
 
 
102
                                                default:
 
103
                                                {
 
104
                                                        throw new IOException(MessageFormat.Format(JGitText.Get().unsupportedPackIndexVersion
 
105
                                                                , v));
 
106
                                                }
 
107
                                        }
 
108
                                }
 
109
                                return new PackIndexV1(fd, hdr);
 
110
                        }
 
111
                        catch (IOException ioe)
 
112
                        {
 
113
                                string path = idxFile.GetAbsolutePath();
 
114
                                IOException err;
 
115
                                err = new IOException(MessageFormat.Format(JGitText.Get().unreadablePackIndex, path
 
116
                                        ));
 
117
                                Sharpen.Extensions.InitCause(err, ioe);
 
118
                                throw err;
 
119
                        }
 
120
                        finally
 
121
                        {
 
122
                                try
 
123
                                {
 
124
                                        fd.Close();
 
125
                                }
 
126
                                catch (IOException)
 
127
                                {
 
128
                                }
 
129
                        }
 
130
                }
 
131
 
 
132
                // ignore
 
133
                private static bool IsTOC(byte[] h)
 
134
                {
 
135
                        byte[] toc = PackIndexWriter.TOC;
 
136
                        for (int i = 0; i < toc.Length; i++)
 
137
                        {
 
138
                                if (h[i] != toc[i])
 
139
                                {
 
140
                                        return false;
 
141
                                }
 
142
                        }
 
143
                        return true;
 
144
                }
 
145
 
 
146
                /// <summary>Footer checksum applied on the bottom of the pack file.</summary>
 
147
                /// <remarks>Footer checksum applied on the bottom of the pack file.</remarks>
 
148
                protected internal byte[] packChecksum;
 
149
 
 
150
                /// <summary>Determine if an object is contained within the pack file.</summary>
 
151
                /// <remarks>Determine if an object is contained within the pack file.</remarks>
 
152
                /// <param name="id">the object to look for. Must not be null.</param>
 
153
                /// <returns>true if the object is listed in this index; false otherwise.</returns>
 
154
                public virtual bool HasObject(AnyObjectId id)
 
155
                {
 
156
                        return FindOffset(id) != -1;
 
157
                }
 
158
 
 
159
                /// <summary>Provide iterator that gives access to index entries.</summary>
 
160
                /// <remarks>
 
161
                /// Provide iterator that gives access to index entries. Note, that iterator
 
162
                /// returns reference to mutable object, the same reference in each call -
 
163
                /// for performance reason. If client needs immutable objects, it must copy
 
164
                /// returned object on its own.
 
165
                /// <p>
 
166
                /// Iterator returns objects in SHA-1 lexicographical order.
 
167
                /// </p>
 
168
                /// </remarks>
 
169
                /// <returns>iterator over pack index entries</returns>
 
170
                public abstract override Sharpen.Iterator<PackIndex.MutableEntry> Iterator();
 
171
 
 
172
                /// <summary>Obtain the total number of objects described by this index.</summary>
 
173
                /// <remarks>Obtain the total number of objects described by this index.</remarks>
 
174
                /// <returns>
 
175
                /// number of objects in this index, and likewise in the associated
 
176
                /// pack that this index was generated from.
 
177
                /// </returns>
 
178
                internal abstract long GetObjectCount();
 
179
 
 
180
                /// <summary>Obtain the total number of objects needing 64 bit offsets.</summary>
 
181
                /// <remarks>Obtain the total number of objects needing 64 bit offsets.</remarks>
 
182
                /// <returns>
 
183
                /// number of objects in this index using a 64 bit offset; that is an
 
184
                /// object positioned after the 2 GB position within the file.
 
185
                /// </returns>
 
186
                internal abstract long GetOffset64Count();
 
187
 
 
188
                /// <summary>
 
189
                /// Get ObjectId for the n-th object entry returned by
 
190
                /// <see cref="Iterator()">Iterator()</see>
 
191
                /// .
 
192
                /// <p>
 
193
                /// This method is a constant-time replacement for the following loop:
 
194
                /// <pre>
 
195
                /// Iterator&lt;MutableEntry&gt; eItr = index.iterator();
 
196
                /// int curPosition = 0;
 
197
                /// while (eItr.hasNext() &amp;&amp; curPosition++ &lt; nthPosition)
 
198
                /// eItr.next();
 
199
                /// ObjectId result = eItr.next().toObjectId();
 
200
                /// </pre>
 
201
                /// </summary>
 
202
                /// <param name="nthPosition">
 
203
                /// position within the traversal of
 
204
                /// <see cref="Iterator()">Iterator()</see>
 
205
                /// that the
 
206
                /// caller needs the object for. The first returned
 
207
                /// <see cref="MutableEntry">MutableEntry</see>
 
208
                /// is 0, the second is 1, etc.
 
209
                /// </param>
 
210
                /// <returns>the ObjectId for the corresponding entry.</returns>
 
211
                internal abstract ObjectId GetObjectId(long nthPosition);
 
212
 
 
213
                /// <summary>
 
214
                /// Get ObjectId for the n-th object entry returned by
 
215
                /// <see cref="Iterator()">Iterator()</see>
 
216
                /// .
 
217
                /// <p>
 
218
                /// This method is a constant-time replacement for the following loop:
 
219
                /// <pre>
 
220
                /// Iterator&lt;MutableEntry&gt; eItr = index.iterator();
 
221
                /// int curPosition = 0;
 
222
                /// while (eItr.hasNext() &amp;&amp; curPosition++ &lt; nthPosition)
 
223
                /// eItr.next();
 
224
                /// ObjectId result = eItr.next().toObjectId();
 
225
                /// </pre>
 
226
                /// </summary>
 
227
                /// <param name="nthPosition">
 
228
                /// unsigned 32 bit position within the traversal of
 
229
                /// <see cref="Iterator()">Iterator()</see>
 
230
                /// that the caller needs the object for. The
 
231
                /// first returned
 
232
                /// <see cref="MutableEntry">MutableEntry</see>
 
233
                /// is 0, the second is 1,
 
234
                /// etc. Positions past 2**31-1 are negative, but still valid.
 
235
                /// </param>
 
236
                /// <returns>the ObjectId for the corresponding entry.</returns>
 
237
                internal ObjectId GetObjectId(int nthPosition)
 
238
                {
 
239
                        if (nthPosition >= 0)
 
240
                        {
 
241
                                return GetObjectId((long)nthPosition);
 
242
                        }
 
243
                        int u31 = (int)(((uint)nthPosition) >> 1);
 
244
                        int one = nthPosition & 1;
 
245
                        return GetObjectId(((long)u31) << 1 | one);
 
246
                }
 
247
 
 
248
                /// <summary>Locate the file offset position for the requested object.</summary>
 
249
                /// <remarks>Locate the file offset position for the requested object.</remarks>
 
250
                /// <param name="objId">name of the object to locate within the pack.</param>
 
251
                /// <returns>
 
252
                /// offset of the object's header and compressed content; -1 if the
 
253
                /// object does not exist in this index and is thus not stored in the
 
254
                /// associated pack.
 
255
                /// </returns>
 
256
                internal abstract long FindOffset(AnyObjectId objId);
 
257
 
 
258
                /// <summary>
 
259
                /// Retrieve stored CRC32 checksum of the requested object raw-data
 
260
                /// (including header).
 
261
                /// </summary>
 
262
                /// <remarks>
 
263
                /// Retrieve stored CRC32 checksum of the requested object raw-data
 
264
                /// (including header).
 
265
                /// </remarks>
 
266
                /// <param name="objId">id of object to look for</param>
 
267
                /// <returns>CRC32 checksum of specified object (at 32 less significant bits)</returns>
 
268
                /// <exception cref="NGit.Errors.MissingObjectException">when requested ObjectId was not found in this index
 
269
                ///     </exception>
 
270
                /// <exception cref="System.NotSupportedException">when this index doesn't support CRC32 checksum
 
271
                ///     </exception>
 
272
                internal abstract long FindCRC32(AnyObjectId objId);
 
273
 
 
274
                /// <summary>Check whether this index supports (has) CRC32 checksums for objects.</summary>
 
275
                /// <remarks>Check whether this index supports (has) CRC32 checksums for objects.</remarks>
 
276
                /// <returns>true if CRC32 is stored, false otherwise</returns>
 
277
                internal abstract bool HasCRC32Support();
 
278
 
 
279
                /// <exception cref="System.IO.IOException"></exception>
 
280
                internal abstract void Resolve(ICollection<ObjectId> matches, AbbreviatedObjectId
 
281
                         id, int matchLimit);
 
282
 
 
283
                /// <summary>
 
284
                /// Represent mutable entry of pack index consisting of object id and offset
 
285
                /// in pack (both mutable).
 
286
                /// </summary>
 
287
                /// <remarks>
 
288
                /// Represent mutable entry of pack index consisting of object id and offset
 
289
                /// in pack (both mutable).
 
290
                /// </remarks>
 
291
                public class MutableEntry
 
292
                {
 
293
                        internal readonly MutableObjectId idBuffer = new MutableObjectId();
 
294
 
 
295
                        internal long offset;
 
296
 
 
297
                        /// <summary>Returns offset for this index object entry</summary>
 
298
                        /// <returns>offset of this object in a pack file</returns>
 
299
                        public virtual long GetOffset()
 
300
                        {
 
301
                                return offset;
 
302
                        }
 
303
 
 
304
                        /// <returns>hex string describing the object id of this entry.</returns>
 
305
                        public virtual string Name()
 
306
                        {
 
307
                                EnsureId();
 
308
                                return idBuffer.Name;
 
309
                        }
 
310
 
 
311
                        /// <returns>a copy of the object id.</returns>
 
312
                        public virtual ObjectId ToObjectId()
 
313
                        {
 
314
                                EnsureId();
 
315
                                return idBuffer.ToObjectId();
 
316
                        }
 
317
 
 
318
                        /// <returns>a complete copy of this entry, that won't modify</returns>
 
319
                        public virtual PackIndex.MutableEntry CloneEntry()
 
320
                        {
 
321
                                PackIndex.MutableEntry r = new PackIndex.MutableEntry();
 
322
                                EnsureId();
 
323
                                r.idBuffer.FromObjectId(idBuffer);
 
324
                                r.offset = offset;
 
325
                                return r;
 
326
                        }
 
327
 
 
328
                        internal virtual void EnsureId()
 
329
                        {
 
330
                        }
 
331
                        // Override in implementations.
 
332
                }
 
333
 
 
334
                internal abstract class EntriesIterator : Iterator<PackIndex.MutableEntry>
 
335
                {
 
336
                        protected internal readonly PackIndex.MutableEntry entry;
 
337
 
 
338
                        protected internal long returnedNumber = 0;
 
339
 
 
340
                        protected internal abstract PackIndex.MutableEntry InitEntry();
 
341
                        
 
342
                        public override bool HasNext()
 
343
                        {
 
344
                                return this.returnedNumber < this._enclosing.GetObjectCount();
 
345
                        }
 
346
 
 
347
                        /// <summary>
 
348
                        /// Implementation must update
 
349
                        /// <see cref="returnedNumber">returnedNumber</see>
 
350
                        /// before returning
 
351
                        /// element.
 
352
                        /// </summary>
 
353
                        public abstract override PackIndex.MutableEntry Next();
 
354
 
 
355
                        public override void Remove()
 
356
                        {
 
357
                                throw new NotSupportedException();
 
358
                        }
 
359
 
 
360
                        internal EntriesIterator(PackIndex _enclosing)
 
361
                        {
 
362
                                entry = InitEntry();
 
363
                                this._enclosing = _enclosing;
 
364
                        }
 
365
 
 
366
                        private readonly PackIndex _enclosing;
 
367
                }
 
368
        }
 
369
}