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

« back to all changes in this revision

Viewing changes to contrib/NGit/NGit.Storage.File/FileObjectDatabase.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.Collections.Generic;
 
45
using NGit;
 
46
using NGit.Storage.File;
 
47
using NGit.Storage.Pack;
 
48
using NGit.Util;
 
49
using Sharpen;
 
50
 
 
51
namespace NGit.Storage.File
 
52
{
 
53
        public abstract class FileObjectDatabase : ObjectDatabase
 
54
        {
 
55
                internal enum InsertLooseObjectResult
 
56
                {
 
57
                        INSERTED,
 
58
                        EXISTS_PACKED,
 
59
                        EXISTS_LOOSE,
 
60
                        FAILURE
 
61
                }
 
62
 
 
63
                public override ObjectReader NewReader()
 
64
                {
 
65
                        return new WindowCursor(this);
 
66
                }
 
67
 
 
68
                public override ObjectInserter NewInserter()
 
69
                {
 
70
                        return new ObjectDirectoryInserter(this, GetConfig());
 
71
                }
 
72
 
 
73
                /// <summary>
 
74
                /// Does the requested object exist in this database?
 
75
                /// <p>
 
76
                /// Alternates (if present) are searched automatically.
 
77
                /// </summary>
 
78
                /// <remarks>
 
79
                /// Does the requested object exist in this database?
 
80
                /// <p>
 
81
                /// Alternates (if present) are searched automatically.
 
82
                /// </remarks>
 
83
                /// <param name="objectId">identity of the object to test for existence of.</param>
 
84
                /// <returns>
 
85
                /// true if the specified object is stored in this database, or any
 
86
                /// of the alternate databases.
 
87
                /// </returns>
 
88
                public override bool Has(AnyObjectId objectId)
 
89
                {
 
90
                        return HasObjectImpl1(objectId) || HasObjectImpl2(objectId.Name);
 
91
                }
 
92
 
 
93
                /// <summary>Compute the location of a loose object file.</summary>
 
94
                /// <remarks>Compute the location of a loose object file.</remarks>
 
95
                /// <param name="objectId">identity of the loose object to map to the directory.</param>
 
96
                /// <returns>location of the object, if it were to exist as a loose object.</returns>
 
97
                internal virtual FilePath FileFor(AnyObjectId objectId)
 
98
                {
 
99
                        return FileFor(objectId.Name);
 
100
                }
 
101
 
 
102
                internal virtual FilePath FileFor(string objectName)
 
103
                {
 
104
                        string d = Sharpen.Runtime.Substring(objectName, 0, 2);
 
105
                        string f = Sharpen.Runtime.Substring(objectName, 2);
 
106
                        return new FilePath(new FilePath(GetDirectory(), d), f);
 
107
                }
 
108
 
 
109
                internal bool HasObjectImpl1(AnyObjectId objectId)
 
110
                {
 
111
                        if (HasObject1(objectId))
 
112
                        {
 
113
                                return true;
 
114
                        }
 
115
                        foreach (FileObjectDatabase.AlternateHandle alt in MyAlternates())
 
116
                        {
 
117
                                if (alt.db.HasObjectImpl1(objectId))
 
118
                                {
 
119
                                        return true;
 
120
                                }
 
121
                        }
 
122
                        return TryAgain1() && HasObject1(objectId);
 
123
                }
 
124
 
 
125
                internal bool HasObjectImpl2(string objectId)
 
126
                {
 
127
                        if (HasObject2(objectId))
 
128
                        {
 
129
                                return true;
 
130
                        }
 
131
                        foreach (FileObjectDatabase.AlternateHandle alt in MyAlternates())
 
132
                        {
 
133
                                if (alt.db.HasObjectImpl2(objectId))
 
134
                                {
 
135
                                        return true;
 
136
                                }
 
137
                        }
 
138
                        return false;
 
139
                }
 
140
 
 
141
                /// <exception cref="System.IO.IOException"></exception>
 
142
                internal abstract void Resolve(ICollection<ObjectId> matches, AbbreviatedObjectId
 
143
                         id);
 
144
 
 
145
                internal abstract Config GetConfig();
 
146
 
 
147
                internal abstract FS GetFS();
 
148
 
 
149
                /// <summary>Open an object from this database.</summary>
 
150
                /// <remarks>
 
151
                /// Open an object from this database.
 
152
                /// <p>
 
153
                /// Alternates (if present) are searched automatically.
 
154
                /// </remarks>
 
155
                /// <param name="curs">temporary working space associated with the calling thread.</param>
 
156
                /// <param name="objectId">identity of the object to open.</param>
 
157
                /// <returns>
 
158
                /// a
 
159
                /// <see cref="NGit.ObjectLoader">NGit.ObjectLoader</see>
 
160
                /// for accessing the data of the named
 
161
                /// object, or null if the object does not exist.
 
162
                /// </returns>
 
163
                /// <exception cref="System.IO.IOException">System.IO.IOException</exception>
 
164
                internal virtual ObjectLoader OpenObject(WindowCursor curs, AnyObjectId objectId)
 
165
                {
 
166
                        ObjectLoader ldr;
 
167
                        ldr = OpenObjectImpl1(curs, objectId);
 
168
                        if (ldr != null)
 
169
                        {
 
170
                                return ldr;
 
171
                        }
 
172
                        ldr = OpenObjectImpl2(curs, objectId.Name, objectId);
 
173
                        if (ldr != null)
 
174
                        {
 
175
                                return ldr;
 
176
                        }
 
177
                        return null;
 
178
                }
 
179
 
 
180
                /// <exception cref="System.IO.IOException"></exception>
 
181
                internal ObjectLoader OpenObjectImpl1(WindowCursor curs, AnyObjectId objectId)
 
182
                {
 
183
                        ObjectLoader ldr;
 
184
                        ldr = OpenObject1(curs, objectId);
 
185
                        if (ldr != null)
 
186
                        {
 
187
                                return ldr;
 
188
                        }
 
189
                        foreach (FileObjectDatabase.AlternateHandle alt in MyAlternates())
 
190
                        {
 
191
                                ldr = alt.db.OpenObjectImpl1(curs, objectId);
 
192
                                if (ldr != null)
 
193
                                {
 
194
                                        return ldr;
 
195
                                }
 
196
                        }
 
197
                        if (TryAgain1())
 
198
                        {
 
199
                                ldr = OpenObject1(curs, objectId);
 
200
                                if (ldr != null)
 
201
                                {
 
202
                                        return ldr;
 
203
                                }
 
204
                        }
 
205
                        return null;
 
206
                }
 
207
 
 
208
                /// <exception cref="System.IO.IOException"></exception>
 
209
                internal ObjectLoader OpenObjectImpl2(WindowCursor curs, string objectName, AnyObjectId
 
210
                         objectId)
 
211
                {
 
212
                        ObjectLoader ldr;
 
213
                        ldr = OpenObject2(curs, objectName, objectId);
 
214
                        if (ldr != null)
 
215
                        {
 
216
                                return ldr;
 
217
                        }
 
218
                        foreach (FileObjectDatabase.AlternateHandle alt in MyAlternates())
 
219
                        {
 
220
                                ldr = alt.db.OpenObjectImpl2(curs, objectName, objectId);
 
221
                                if (ldr != null)
 
222
                                {
 
223
                                        return ldr;
 
224
                                }
 
225
                        }
 
226
                        return null;
 
227
                }
 
228
 
 
229
                /// <exception cref="System.IO.IOException"></exception>
 
230
                internal virtual long GetObjectSize(WindowCursor curs, AnyObjectId objectId)
 
231
                {
 
232
                        long sz = GetObjectSizeImpl1(curs, objectId);
 
233
                        if (0 <= sz)
 
234
                        {
 
235
                                return sz;
 
236
                        }
 
237
                        return GetObjectSizeImpl2(curs, objectId.Name, objectId);
 
238
                }
 
239
 
 
240
                /// <exception cref="System.IO.IOException"></exception>
 
241
                internal long GetObjectSizeImpl1(WindowCursor curs, AnyObjectId objectId)
 
242
                {
 
243
                        long sz;
 
244
                        sz = GetObjectSize1(curs, objectId);
 
245
                        if (0 <= sz)
 
246
                        {
 
247
                                return sz;
 
248
                        }
 
249
                        foreach (FileObjectDatabase.AlternateHandle alt in MyAlternates())
 
250
                        {
 
251
                                sz = alt.db.GetObjectSizeImpl1(curs, objectId);
 
252
                                if (0 <= sz)
 
253
                                {
 
254
                                        return sz;
 
255
                                }
 
256
                        }
 
257
                        if (TryAgain1())
 
258
                        {
 
259
                                sz = GetObjectSize1(curs, objectId);
 
260
                                if (0 <= sz)
 
261
                                {
 
262
                                        return sz;
 
263
                                }
 
264
                        }
 
265
                        return -1;
 
266
                }
 
267
 
 
268
                /// <exception cref="System.IO.IOException"></exception>
 
269
                internal long GetObjectSizeImpl2(WindowCursor curs, string objectName, AnyObjectId
 
270
                         objectId)
 
271
                {
 
272
                        long sz;
 
273
                        sz = GetObjectSize2(curs, objectName, objectId);
 
274
                        if (0 <= sz)
 
275
                        {
 
276
                                return sz;
 
277
                        }
 
278
                        foreach (FileObjectDatabase.AlternateHandle alt in MyAlternates())
 
279
                        {
 
280
                                sz = alt.db.GetObjectSizeImpl2(curs, objectName, objectId);
 
281
                                if (0 <= sz)
 
282
                                {
 
283
                                        return sz;
 
284
                                }
 
285
                        }
 
286
                        return -1;
 
287
                }
 
288
 
 
289
                /// <exception cref="System.IO.IOException"></exception>
 
290
                internal abstract void SelectObjectRepresentation(PackWriter packer, ObjectToPack
 
291
                         otp, WindowCursor curs);
 
292
 
 
293
                internal abstract FilePath GetDirectory();
 
294
 
 
295
                /// <exception cref="System.IO.IOException"></exception>
 
296
                internal abstract ICollection<CachedPack> GetCachedPacks();
 
297
 
 
298
                internal abstract FileObjectDatabase.AlternateHandle[] MyAlternates();
 
299
 
 
300
                internal abstract bool TryAgain1();
 
301
 
 
302
                internal abstract bool HasObject1(AnyObjectId objectId);
 
303
 
 
304
                internal abstract bool HasObject2(string objectId);
 
305
 
 
306
                /// <exception cref="System.IO.IOException"></exception>
 
307
                internal abstract ObjectLoader OpenObject1(WindowCursor curs, AnyObjectId objectId
 
308
                        );
 
309
 
 
310
                /// <exception cref="System.IO.IOException"></exception>
 
311
                internal abstract ObjectLoader OpenObject2(WindowCursor curs, string objectName, 
 
312
                        AnyObjectId objectId);
 
313
 
 
314
                /// <exception cref="System.IO.IOException"></exception>
 
315
                internal abstract long GetObjectSize1(WindowCursor curs, AnyObjectId objectId);
 
316
 
 
317
                /// <exception cref="System.IO.IOException"></exception>
 
318
                internal abstract long GetObjectSize2(WindowCursor curs, string objectName, AnyObjectId
 
319
                         objectId);
 
320
 
 
321
                /// <exception cref="System.IO.IOException"></exception>
 
322
                internal abstract FileObjectDatabase.InsertLooseObjectResult InsertUnpackedObject
 
323
                        (FilePath tmp, ObjectId id, bool createDuplicate);
 
324
 
 
325
                /// <exception cref="System.IO.IOException"></exception>
 
326
                internal abstract PackFile OpenPack(FilePath pack, FilePath idx);
 
327
 
 
328
                internal abstract FileObjectDatabase NewCachedFileObjectDatabase();
 
329
 
 
330
                internal class AlternateHandle
 
331
                {
 
332
                        internal readonly FileObjectDatabase db;
 
333
 
 
334
                        internal AlternateHandle(FileObjectDatabase db)
 
335
                        {
 
336
                                this.db = db;
 
337
                        }
 
338
 
 
339
                        /// <exception cref="System.IO.IOException"></exception>
 
340
                        internal virtual ICollection<CachedPack> GetCachedPacks()
 
341
                        {
 
342
                                return (ICollection<CachedPack>)db.GetCachedPacks();
 
343
                        }
 
344
 
 
345
                        internal virtual void Close()
 
346
                        {
 
347
                                db.Close();
 
348
                        }
 
349
                }
 
350
 
 
351
                internal class AlternateRepository : FileObjectDatabase.AlternateHandle
 
352
                {
 
353
                        internal readonly FileRepository repository;
 
354
 
 
355
                        internal AlternateRepository(FileRepository r) : base(((ObjectDirectory)r.ObjectDatabase
 
356
                                ))
 
357
                        {
 
358
                                repository = r;
 
359
                        }
 
360
 
 
361
                        internal override void Close()
 
362
                        {
 
363
                                repository.Close();
 
364
                        }
 
365
                }
 
366
        }
 
367
}