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.
48
namespace NGit.Dircache
51
/// Generic update/editing support for
52
/// <see cref="DirCache">DirCache</see>
55
/// The different update strategies extend this class to provide their own unique
56
/// services to applications.
58
public abstract class BaseDirCacheEditor
61
/// The cache instance this editor updates during
62
/// <see cref="Finish()">Finish()</see>
65
protected internal DirCache cache;
68
/// Entry table this builder will eventually replace into
69
/// <see cref="cache">cache</see>
73
/// <see cref="FastAdd(DirCacheEntry)">FastAdd(DirCacheEntry)</see>
75
/// <see cref="FastKeep(int, int)">FastKeep(int, int)</see>
77
/// make additions to this table. The table is automatically expanded if it
78
/// is too small for a new addition.
80
/// Typically the entries in here are sorted by their path names, just like
81
/// they are in the DirCache instance.
83
protected internal DirCacheEntry[] entries;
86
/// Total number of valid entries in
87
/// <see cref="entries">entries</see>
90
protected internal int entryCnt;
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.
99
protected internal BaseDirCacheEditor(DirCache dc, int ecnt)
102
entries = new DirCacheEntry[ecnt];
106
/// the cache we will update on
107
/// <see cref="Finish()">Finish()</see>
110
public virtual DirCache GetDirCache()
115
/// <summary>Append one entry into the resulting entry list.</summary>
117
/// Append one entry into the resulting entry list.
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.
123
/// <see cref="entries">entries</see>
124
/// table is automatically expanded if there is
125
/// insufficient space for the new addition.
127
/// <param name="newEntry">the new entry to add.</param>
128
protected internal virtual void FastAdd(DirCacheEntry newEntry)
130
if (entries.Length == entryCnt)
132
DirCacheEntry[] n = new DirCacheEntry[(entryCnt + 16) * 3 / 2];
133
System.Array.Copy(entries, 0, n, 0, entryCnt);
136
entries[entryCnt++] = newEntry;
139
/// <summary>Add a range of existing entries from the destination cache.</summary>
141
/// Add a range of existing entries from the destination cache.
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.
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
153
/// <see cref="entries">entries</see>
154
/// table is automatically expanded if there is
155
/// insufficient space for the new additions.
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)
161
if (entryCnt + cnt > entries.Length)
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);
169
cache.ToArray(pos, entries, entryCnt, cnt);
174
/// Finish this builder and update the destination
175
/// <see cref="DirCache">DirCache</see>
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.
182
/// After completion the DirCache returned by
183
/// <see cref="GetDirCache()">GetDirCache()</see>
185
/// contain all modifications.
187
/// <i>Note to implementors:</i> Make sure
188
/// <see cref="entries">entries</see>
191
/// <see cref="Replace()">Replace()</see>
192
/// to update the DirCache with the new table.
194
public abstract void Finish();
197
/// Update the DirCache with the contents of
198
/// <see cref="entries">entries</see>
201
/// This method should be invoked only during an implementation of
202
/// <see cref="Finish()">Finish()</see>
204
/// <see cref="entries">entries</see>
207
protected internal virtual void Replace()
209
if (entryCnt < entries.Length / 2)
211
DirCacheEntry[] n = new DirCacheEntry[entryCnt];
212
System.Array.Copy(entries, 0, n, 0, entryCnt);
215
cache.Replace(entries, entryCnt);
218
/// <summary>Finish, write, commit this change, and release the index lock.</summary>
220
/// Finish, write, commit this change, and release the index lock.
222
/// If this method fails (returns false) the lock is still released.
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.
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
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
237
public virtual bool Commit()
241
return cache.Commit();