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

« back to all changes in this revision

Viewing changes to contrib/NGit/NGit.Dircache/BaseDirCacheEditor.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 NGit.Dircache;
 
46
using Sharpen;
 
47
 
 
48
namespace NGit.Dircache
 
49
{
 
50
        /// <summary>
 
51
        /// Generic update/editing support for
 
52
        /// <see cref="DirCache">DirCache</see>
 
53
        /// .
 
54
        /// <p>
 
55
        /// The different update strategies extend this class to provide their own unique
 
56
        /// services to applications.
 
57
        /// </summary>
 
58
        public abstract class BaseDirCacheEditor
 
59
        {
 
60
                /// <summary>
 
61
                /// The cache instance this editor updates during
 
62
                /// <see cref="Finish()">Finish()</see>
 
63
                /// .
 
64
                /// </summary>
 
65
                protected internal DirCache cache;
 
66
 
 
67
                /// <summary>
 
68
                /// Entry table this builder will eventually replace into
 
69
                /// <see cref="cache">cache</see>
 
70
                /// .
 
71
                /// <p>
 
72
                /// Use
 
73
                /// <see cref="FastAdd(DirCacheEntry)">FastAdd(DirCacheEntry)</see>
 
74
                /// or
 
75
                /// <see cref="FastKeep(int, int)">FastKeep(int, int)</see>
 
76
                /// to
 
77
                /// make additions to this table. The table is automatically expanded if it
 
78
                /// is too small for a new addition.
 
79
                /// <p>
 
80
                /// Typically the entries in here are sorted by their path names, just like
 
81
                /// they are in the DirCache instance.
 
82
                /// </summary>
 
83
                protected internal DirCacheEntry[] entries;
 
84
 
 
85
                /// <summary>
 
86
                /// Total number of valid entries in
 
87
                /// <see cref="entries">entries</see>
 
88
                /// .
 
89
                /// </summary>
 
90
                protected internal int entryCnt;
 
91
 
 
92
                /// <summary>Construct a new editor.</summary>
 
93
                /// <remarks>Construct a new editor.</remarks>
 
94
                /// <param name="dc">the cache this editor will eventually update.</param>
 
95
                /// <param name="ecnt">
 
96
                /// estimated number of entries the editor will have upon
 
97
                /// completion. This sizes the initial entry table.
 
98
                /// </param>
 
99
                protected internal BaseDirCacheEditor(DirCache dc, int ecnt)
 
100
                {
 
101
                        cache = dc;
 
102
                        entries = new DirCacheEntry[ecnt];
 
103
                }
 
104
 
 
105
                /// <returns>
 
106
                /// the cache we will update on
 
107
                /// <see cref="Finish()">Finish()</see>
 
108
                /// .
 
109
                /// </returns>
 
110
                public virtual DirCache GetDirCache()
 
111
                {
 
112
                        return cache;
 
113
                }
 
114
 
 
115
                /// <summary>Append one entry into the resulting entry list.</summary>
 
116
                /// <remarks>
 
117
                /// Append one entry into the resulting entry list.
 
118
                /// <p>
 
119
                /// The entry is placed at the end of the entry list. The caller is
 
120
                /// responsible for making sure the final table is correctly sorted.
 
121
                /// <p>
 
122
                /// The
 
123
                /// <see cref="entries">entries</see>
 
124
                /// table is automatically expanded if there is
 
125
                /// insufficient space for the new addition.
 
126
                /// </remarks>
 
127
                /// <param name="newEntry">the new entry to add.</param>
 
128
                protected internal virtual void FastAdd(DirCacheEntry newEntry)
 
129
                {
 
130
                        if (entries.Length == entryCnt)
 
131
                        {
 
132
                                DirCacheEntry[] n = new DirCacheEntry[(entryCnt + 16) * 3 / 2];
 
133
                                System.Array.Copy(entries, 0, n, 0, entryCnt);
 
134
                                entries = n;
 
135
                        }
 
136
                        entries[entryCnt++] = newEntry;
 
137
                }
 
138
 
 
139
                /// <summary>Add a range of existing entries from the destination cache.</summary>
 
140
                /// <remarks>
 
141
                /// Add a range of existing entries from the destination cache.
 
142
                /// <p>
 
143
                /// The entries are placed at the end of the entry list, preserving their
 
144
                /// current order. The caller is responsible for making sure the final table
 
145
                /// is correctly sorted.
 
146
                /// <p>
 
147
                /// This method copies from the destination cache, which has not yet been
 
148
                /// updated with this editor's new table. So all offsets into the destination
 
149
                /// cache are not affected by any updates that may be currently taking place
 
150
                /// in this editor.
 
151
                /// <p>
 
152
                /// The
 
153
                /// <see cref="entries">entries</see>
 
154
                /// table is automatically expanded if there is
 
155
                /// insufficient space for the new additions.
 
156
                /// </remarks>
 
157
                /// <param name="pos">first entry to copy from the destination cache.</param>
 
158
                /// <param name="cnt">number of entries to copy.</param>
 
159
                protected internal virtual void FastKeep(int pos, int cnt)
 
160
                {
 
161
                        if (entryCnt + cnt > entries.Length)
 
162
                        {
 
163
                                int m1 = (entryCnt + 16) * 3 / 2;
 
164
                                int m2 = entryCnt + cnt;
 
165
                                DirCacheEntry[] n = new DirCacheEntry[Math.Max(m1, m2)];
 
166
                                System.Array.Copy(entries, 0, n, 0, entryCnt);
 
167
                                entries = n;
 
168
                        }
 
169
                        cache.ToArray(pos, entries, entryCnt, cnt);
 
170
                        entryCnt += cnt;
 
171
                }
 
172
 
 
173
                /// <summary>
 
174
                /// Finish this builder and update the destination
 
175
                /// <see cref="DirCache">DirCache</see>
 
176
                /// .
 
177
                /// <p>
 
178
                /// When this method completes this builder instance is no longer usable by
 
179
                /// the calling application. A new builder must be created to make additional
 
180
                /// changes to the index entries.
 
181
                /// <p>
 
182
                /// After completion the DirCache returned by
 
183
                /// <see cref="GetDirCache()">GetDirCache()</see>
 
184
                /// will
 
185
                /// contain all modifications.
 
186
                /// <p>
 
187
                /// <i>Note to implementors:</i> Make sure
 
188
                /// <see cref="entries">entries</see>
 
189
                /// is fully sorted
 
190
                /// then invoke
 
191
                /// <see cref="Replace()">Replace()</see>
 
192
                /// to update the DirCache with the new table.
 
193
                /// </summary>
 
194
                public abstract void Finish();
 
195
 
 
196
                /// <summary>
 
197
                /// Update the DirCache with the contents of
 
198
                /// <see cref="entries">entries</see>
 
199
                /// .
 
200
                /// <p>
 
201
                /// This method should be invoked only during an implementation of
 
202
                /// <see cref="Finish()">Finish()</see>
 
203
                /// , and only after
 
204
                /// <see cref="entries">entries</see>
 
205
                /// is sorted.
 
206
                /// </summary>
 
207
                protected internal virtual void Replace()
 
208
                {
 
209
                        if (entryCnt < entries.Length / 2)
 
210
                        {
 
211
                                DirCacheEntry[] n = new DirCacheEntry[entryCnt];
 
212
                                System.Array.Copy(entries, 0, n, 0, entryCnt);
 
213
                                entries = n;
 
214
                        }
 
215
                        cache.Replace(entries, entryCnt);
 
216
                }
 
217
 
 
218
                /// <summary>Finish, write, commit this change, and release the index lock.</summary>
 
219
                /// <remarks>
 
220
                /// Finish, write, commit this change, and release the index lock.
 
221
                /// <p>
 
222
                /// If this method fails (returns false) the lock is still released.
 
223
                /// <p>
 
224
                /// This is a utility method for applications as the finish-write-commit
 
225
                /// pattern is very common after using a builder to update entries.
 
226
                /// </remarks>
 
227
                /// <returns>
 
228
                /// true if the commit was successful and the file contains the new
 
229
                /// data; false if the commit failed and the file remains with the
 
230
                /// old data.
 
231
                /// </returns>
 
232
                /// <exception cref="System.InvalidOperationException">the lock is not held.</exception>
 
233
                /// <exception cref="System.IO.IOException">
 
234
                /// the output file could not be created. The caller no longer
 
235
                /// holds the lock.
 
236
                /// </exception>
 
237
                public virtual bool Commit()
 
238
                {
 
239
                        Finish();
 
240
                        cache.Write();
 
241
                        return cache.Commit();
 
242
                }
 
243
        }
 
244
}