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.
44
using System.Collections.Generic;
46
using NGit.Storage.File;
47
using NGit.Storage.Pack;
51
namespace NGit.Storage.File
53
public abstract class FileObjectDatabase : ObjectDatabase
55
internal enum InsertLooseObjectResult
63
public override ObjectReader NewReader()
65
return new WindowCursor(this);
68
public override ObjectInserter NewInserter()
70
return new ObjectDirectoryInserter(this, GetConfig());
74
/// Does the requested object exist in this database?
76
/// Alternates (if present) are searched automatically.
79
/// Does the requested object exist in this database?
81
/// Alternates (if present) are searched automatically.
83
/// <param name="objectId">identity of the object to test for existence of.</param>
85
/// true if the specified object is stored in this database, or any
86
/// of the alternate databases.
88
public override bool Has(AnyObjectId objectId)
90
return HasObjectImpl1(objectId) || HasObjectImpl2(objectId.Name);
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)
99
return FileFor(objectId.Name);
102
internal virtual FilePath FileFor(string objectName)
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);
109
internal bool HasObjectImpl1(AnyObjectId objectId)
111
if (HasObject1(objectId))
115
foreach (FileObjectDatabase.AlternateHandle alt in MyAlternates())
117
if (alt.db.HasObjectImpl1(objectId))
122
return TryAgain1() && HasObject1(objectId);
125
internal bool HasObjectImpl2(string objectId)
127
if (HasObject2(objectId))
131
foreach (FileObjectDatabase.AlternateHandle alt in MyAlternates())
133
if (alt.db.HasObjectImpl2(objectId))
141
/// <exception cref="System.IO.IOException"></exception>
142
internal abstract void Resolve(ICollection<ObjectId> matches, AbbreviatedObjectId
145
internal abstract Config GetConfig();
147
internal abstract FS GetFS();
149
/// <summary>Open an object from this database.</summary>
151
/// Open an object from this database.
153
/// Alternates (if present) are searched automatically.
155
/// <param name="curs">temporary working space associated with the calling thread.</param>
156
/// <param name="objectId">identity of the object to open.</param>
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.
163
/// <exception cref="System.IO.IOException">System.IO.IOException</exception>
164
internal virtual ObjectLoader OpenObject(WindowCursor curs, AnyObjectId objectId)
167
ldr = OpenObjectImpl1(curs, objectId);
172
ldr = OpenObjectImpl2(curs, objectId.Name, objectId);
180
/// <exception cref="System.IO.IOException"></exception>
181
internal ObjectLoader OpenObjectImpl1(WindowCursor curs, AnyObjectId objectId)
184
ldr = OpenObject1(curs, objectId);
189
foreach (FileObjectDatabase.AlternateHandle alt in MyAlternates())
191
ldr = alt.db.OpenObjectImpl1(curs, objectId);
199
ldr = OpenObject1(curs, objectId);
208
/// <exception cref="System.IO.IOException"></exception>
209
internal ObjectLoader OpenObjectImpl2(WindowCursor curs, string objectName, AnyObjectId
213
ldr = OpenObject2(curs, objectName, objectId);
218
foreach (FileObjectDatabase.AlternateHandle alt in MyAlternates())
220
ldr = alt.db.OpenObjectImpl2(curs, objectName, objectId);
229
/// <exception cref="System.IO.IOException"></exception>
230
internal virtual long GetObjectSize(WindowCursor curs, AnyObjectId objectId)
232
long sz = GetObjectSizeImpl1(curs, objectId);
237
return GetObjectSizeImpl2(curs, objectId.Name, objectId);
240
/// <exception cref="System.IO.IOException"></exception>
241
internal long GetObjectSizeImpl1(WindowCursor curs, AnyObjectId objectId)
244
sz = GetObjectSize1(curs, objectId);
249
foreach (FileObjectDatabase.AlternateHandle alt in MyAlternates())
251
sz = alt.db.GetObjectSizeImpl1(curs, objectId);
259
sz = GetObjectSize1(curs, objectId);
268
/// <exception cref="System.IO.IOException"></exception>
269
internal long GetObjectSizeImpl2(WindowCursor curs, string objectName, AnyObjectId
273
sz = GetObjectSize2(curs, objectName, objectId);
278
foreach (FileObjectDatabase.AlternateHandle alt in MyAlternates())
280
sz = alt.db.GetObjectSizeImpl2(curs, objectName, objectId);
289
/// <exception cref="System.IO.IOException"></exception>
290
internal abstract void SelectObjectRepresentation(PackWriter packer, ObjectToPack
291
otp, WindowCursor curs);
293
internal abstract FilePath GetDirectory();
295
/// <exception cref="System.IO.IOException"></exception>
296
internal abstract ICollection<CachedPack> GetCachedPacks();
298
internal abstract FileObjectDatabase.AlternateHandle[] MyAlternates();
300
internal abstract bool TryAgain1();
302
internal abstract bool HasObject1(AnyObjectId objectId);
304
internal abstract bool HasObject2(string objectId);
306
/// <exception cref="System.IO.IOException"></exception>
307
internal abstract ObjectLoader OpenObject1(WindowCursor curs, AnyObjectId objectId
310
/// <exception cref="System.IO.IOException"></exception>
311
internal abstract ObjectLoader OpenObject2(WindowCursor curs, string objectName,
312
AnyObjectId objectId);
314
/// <exception cref="System.IO.IOException"></exception>
315
internal abstract long GetObjectSize1(WindowCursor curs, AnyObjectId objectId);
317
/// <exception cref="System.IO.IOException"></exception>
318
internal abstract long GetObjectSize2(WindowCursor curs, string objectName, AnyObjectId
321
/// <exception cref="System.IO.IOException"></exception>
322
internal abstract FileObjectDatabase.InsertLooseObjectResult InsertUnpackedObject
323
(FilePath tmp, ObjectId id, bool createDuplicate);
325
/// <exception cref="System.IO.IOException"></exception>
326
internal abstract PackFile OpenPack(FilePath pack, FilePath idx);
328
internal abstract FileObjectDatabase NewCachedFileObjectDatabase();
330
internal class AlternateHandle
332
internal readonly FileObjectDatabase db;
334
internal AlternateHandle(FileObjectDatabase db)
339
/// <exception cref="System.IO.IOException"></exception>
340
internal virtual ICollection<CachedPack> GetCachedPacks()
342
return (ICollection<CachedPack>)db.GetCachedPacks();
345
internal virtual void Close()
351
internal class AlternateRepository : FileObjectDatabase.AlternateHandle
353
internal readonly FileRepository repository;
355
internal AlternateRepository(FileRepository r) : base(((ObjectDirectory)r.ObjectDatabase
361
internal override void Close()