~ubuntu-branches/ubuntu/wily/monodevelop/wily

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2012-05-27 18:08:20 UTC
  • mfrom: (19.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20120527180820-fydl21qnbnfr8w2t
Tags: 3.0.2+dfsg-3
* [fcecfe7] Fix monodevelop-core-addins.pc.in to point to actual 
  installed location of assemblies.
* [26e1a07] DebSrc 3.0 does not support Quilt's -p parameter, so 
  manually adjust the path in the patch file.

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 NGit;
47
 
using NGit.Storage.File;
48
 
using NGit.Storage.Pack;
49
 
using NGit.Util;
50
 
using Sharpen;
51
 
 
52
 
namespace NGit.Storage.File
53
 
{
54
 
        /// <summary>
55
 
        /// The cached instance of an
56
 
        /// <see cref="ObjectDirectory">ObjectDirectory</see>
57
 
        /// .
58
 
        /// <p>
59
 
        /// This class caches the list of loose objects in memory, so the file system is
60
 
        /// not queried with stat calls.
61
 
        /// </summary>
62
 
        internal class CachedObjectDirectory : FileObjectDatabase
63
 
        {
64
 
                /// <summary>
65
 
                /// The set that contains unpacked objects identifiers, it is created when
66
 
                /// the cached instance is created.
67
 
                /// </summary>
68
 
                /// <remarks>
69
 
                /// The set that contains unpacked objects identifiers, it is created when
70
 
                /// the cached instance is created.
71
 
                /// </remarks>
72
 
                private readonly ObjectIdOwnerMap<CachedObjectDirectory.UnpackedObjectId> unpackedObjects
73
 
                         = new ObjectIdOwnerMap<CachedObjectDirectory.UnpackedObjectId>();
74
 
 
75
 
                private readonly ObjectDirectory wrapped;
76
 
 
77
 
                private FileObjectDatabase.AlternateHandle[] alts;
78
 
 
79
 
                /// <summary>The constructor</summary>
80
 
                /// <param name="wrapped">the wrapped database</param>
81
 
                internal CachedObjectDirectory(ObjectDirectory wrapped)
82
 
                {
83
 
                        this.wrapped = wrapped;
84
 
                        FilePath objects = wrapped.GetDirectory();
85
 
                        string[] fanout = objects.List();
86
 
                        if (fanout == null)
87
 
                        {
88
 
                                fanout = new string[0];
89
 
                        }
90
 
                        foreach (string d in fanout)
91
 
                        {
92
 
                                if (d.Length != 2)
93
 
                                {
94
 
                                        continue;
95
 
                                }
96
 
                                string[] entries = new FilePath(objects, d).List();
97
 
                                if (entries == null)
98
 
                                {
99
 
                                        continue;
100
 
                                }
101
 
                                foreach (string e in entries)
102
 
                                {
103
 
                                        if (e.Length != Constants.OBJECT_ID_STRING_LENGTH - 2)
104
 
                                        {
105
 
                                                continue;
106
 
                                        }
107
 
                                        try
108
 
                                        {
109
 
                                                ObjectId id = ObjectId.FromString(d + e);
110
 
                                                unpackedObjects.Add(new CachedObjectDirectory.UnpackedObjectId(id));
111
 
                                        }
112
 
                                        catch (ArgumentException)
113
 
                                        {
114
 
                                        }
115
 
                                }
116
 
                        }
117
 
                }
118
 
 
119
 
                // ignoring the file that does not represent loose object
120
 
                public override void Close()
121
 
                {
122
 
                }
123
 
 
124
 
                // Don't close anything.
125
 
                public override ObjectDatabase NewCachedDatabase()
126
 
                {
127
 
                        return this;
128
 
                }
129
 
 
130
 
                internal override FileObjectDatabase NewCachedFileObjectDatabase()
131
 
                {
132
 
                        return this;
133
 
                }
134
 
 
135
 
                internal override FilePath GetDirectory()
136
 
                {
137
 
                        return wrapped.GetDirectory();
138
 
                }
139
 
 
140
 
                internal override Config GetConfig()
141
 
                {
142
 
                        return wrapped.GetConfig();
143
 
                }
144
 
 
145
 
                internal override FS GetFS()
146
 
                {
147
 
                        return wrapped.GetFS();
148
 
                }
149
 
 
150
 
                /// <exception cref="System.IO.IOException"></exception>
151
 
                internal override ICollection<CachedPack> GetCachedPacks()
152
 
                {
153
 
                        return wrapped.GetCachedPacks();
154
 
                }
155
 
 
156
 
                internal override FileObjectDatabase.AlternateHandle[] MyAlternates()
157
 
                {
158
 
                        if (alts == null)
159
 
                        {
160
 
                                FileObjectDatabase.AlternateHandle[] src = wrapped.MyAlternates();
161
 
                                alts = new FileObjectDatabase.AlternateHandle[src.Length];
162
 
                                for (int i = 0; i < alts.Length; i++)
163
 
                                {
164
 
                                        FileObjectDatabase s = src[i].db;
165
 
                                        alts[i] = new FileObjectDatabase.AlternateHandle(s.NewCachedFileObjectDatabase());
166
 
                                }
167
 
                        }
168
 
                        return alts;
169
 
                }
170
 
 
171
 
                /// <exception cref="System.IO.IOException"></exception>
172
 
                internal override void Resolve(ICollection<ObjectId> matches, AbbreviatedObjectId
173
 
                         id)
174
 
                {
175
 
                        // In theory we could accelerate the loose object scan using our
176
 
                        // unpackedObjects map, but its not worth the huge code complexity.
177
 
                        // Scanning a single loose directory is fast enough, and this is
178
 
                        // unlikely to be called anyway.
179
 
                        //
180
 
                        wrapped.Resolve(matches, id);
181
 
                }
182
 
 
183
 
                internal override bool TryAgain1()
184
 
                {
185
 
                        return wrapped.TryAgain1();
186
 
                }
187
 
 
188
 
                public override bool Has(AnyObjectId objectId)
189
 
                {
190
 
                        return HasObjectImpl1(objectId);
191
 
                }
192
 
 
193
 
                internal override bool HasObject1(AnyObjectId objectId)
194
 
                {
195
 
                        return unpackedObjects.Contains(objectId) || wrapped.HasObject1(objectId);
196
 
                }
197
 
 
198
 
                /// <exception cref="System.IO.IOException"></exception>
199
 
                internal override ObjectLoader OpenObject(WindowCursor curs, AnyObjectId objectId
200
 
                        )
201
 
                {
202
 
                        return OpenObjectImpl1(curs, objectId);
203
 
                }
204
 
 
205
 
                /// <exception cref="System.IO.IOException"></exception>
206
 
                internal override ObjectLoader OpenObject1(WindowCursor curs, AnyObjectId objectId
207
 
                        )
208
 
                {
209
 
                        if (unpackedObjects.Contains(objectId))
210
 
                        {
211
 
                                return wrapped.OpenObject2(curs, objectId.Name, objectId);
212
 
                        }
213
 
                        return wrapped.OpenObject1(curs, objectId);
214
 
                }
215
 
 
216
 
                internal override bool HasObject2(string objectId)
217
 
                {
218
 
                        return unpackedObjects.Contains(ObjectId.FromString(objectId));
219
 
                }
220
 
 
221
 
                /// <exception cref="System.IO.IOException"></exception>
222
 
                internal override ObjectLoader OpenObject2(WindowCursor curs, string objectName, 
223
 
                        AnyObjectId objectId)
224
 
                {
225
 
                        if (unpackedObjects.Contains(objectId))
226
 
                        {
227
 
                                return wrapped.OpenObject2(curs, objectName, objectId);
228
 
                        }
229
 
                        return null;
230
 
                }
231
 
 
232
 
                /// <exception cref="System.IO.IOException"></exception>
233
 
                internal override long GetObjectSize1(WindowCursor curs, AnyObjectId objectId)
234
 
                {
235
 
                        if (unpackedObjects.Contains(objectId))
236
 
                        {
237
 
                                return wrapped.GetObjectSize2(curs, objectId.Name, objectId);
238
 
                        }
239
 
                        return wrapped.GetObjectSize1(curs, objectId);
240
 
                }
241
 
 
242
 
                /// <exception cref="System.IO.IOException"></exception>
243
 
                internal override long GetObjectSize2(WindowCursor curs, string objectName, AnyObjectId
244
 
                         objectId)
245
 
                {
246
 
                        if (unpackedObjects.Contains(objectId))
247
 
                        {
248
 
                                return wrapped.GetObjectSize2(curs, objectName, objectId);
249
 
                        }
250
 
                        return -1;
251
 
                }
252
 
 
253
 
                /// <exception cref="System.IO.IOException"></exception>
254
 
                internal override FileObjectDatabase.InsertLooseObjectResult InsertUnpackedObject
255
 
                        (FilePath tmp, ObjectId objectId, bool createDuplicate)
256
 
                {
257
 
                        FileObjectDatabase.InsertLooseObjectResult result = wrapped.InsertUnpackedObject(
258
 
                                tmp, objectId, createDuplicate);
259
 
                        switch (result)
260
 
                        {
261
 
                                case FileObjectDatabase.InsertLooseObjectResult.INSERTED:
262
 
                                case FileObjectDatabase.InsertLooseObjectResult.EXISTS_LOOSE:
263
 
                                {
264
 
                                        unpackedObjects.AddIfAbsent(new CachedObjectDirectory.UnpackedObjectId(objectId));
265
 
                                        break;
266
 
                                }
267
 
 
268
 
                                case FileObjectDatabase.InsertLooseObjectResult.EXISTS_PACKED:
269
 
                                case FileObjectDatabase.InsertLooseObjectResult.FAILURE:
270
 
                                {
271
 
                                        break;
272
 
                                }
273
 
                        }
274
 
                        return result;
275
 
                }
276
 
 
277
 
                /// <exception cref="System.IO.IOException"></exception>
278
 
                internal override PackFile OpenPack(FilePath pack, FilePath idx)
279
 
                {
280
 
                        return wrapped.OpenPack(pack, idx);
281
 
                }
282
 
 
283
 
                /// <exception cref="System.IO.IOException"></exception>
284
 
                internal override void SelectObjectRepresentation(PackWriter packer, ObjectToPack
285
 
                         otp, WindowCursor curs)
286
 
                {
287
 
                        wrapped.SelectObjectRepresentation(packer, otp, curs);
288
 
                }
289
 
 
290
 
                [System.Serializable]
291
 
                private class UnpackedObjectId : ObjectIdOwnerMap.Entry
292
 
                {
293
 
                        protected internal UnpackedObjectId(AnyObjectId id) : base(id)
294
 
                        {
295
 
                        }
296
 
                }
297
 
        }
298
 
}