2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org
.spearce
.jgit
.dircache
;
40 import java
.util
.Arrays
;
43 * Updates a {@link DirCache} by adding individual {@link DirCacheEntry}s.
45 * A builder always starts from a clean slate and appends in every single
46 * <code>DirCacheEntry</code> which the final updated index must have to reflect
49 * For maximum performance applications should add entries in path name order.
50 * Adding entries out of order is permitted, however a final sorting pass will
51 * be implicitly performed during {@link #finish()} to correct any out-of-order
52 * entries. Duplicate detection is also delayed until the sorting is complete.
56 public class DirCacheBuilder
extends BaseDirCacheEditor
{
57 private boolean sorted
;
60 * Construct a new builder.
63 * the cache this builder will eventually update.
65 * estimated number of entries the builder will have upon
66 * completion. This sizes the initial entry table.
68 protected DirCacheBuilder(final DirCache dc
, final int ecnt
) {
73 * Append one entry into the resulting entry list.
75 * The entry is placed at the end of the entry list. If the entry causes the
76 * list to now be incorrectly sorted a final sorting phase will be
77 * automatically enabled within {@link #finish()}.
79 * The internal entry table is automatically expanded if there is
80 * insufficient space for the new addition.
83 * the new entry to add.
85 public void add(final DirCacheEntry newEntry
) {
91 * Add a range of existing entries from the destination cache.
93 * The entries are placed at the end of the entry list. If any of the
94 * entries causes the list to now be incorrectly sorted a final sorting
95 * phase will be automatically enabled within {@link #finish()}.
97 * This method copies from the destination cache, which has not yet been
98 * updated with this editor's new table. So all offsets into the destination
99 * cache are not affected by any updates that may be currently taking place
102 * The internal entry table is automatically expanded if there is
103 * insufficient space for the new additions.
106 * first entry to copy from the destination cache.
108 * number of entries to copy.
110 public void keep(final int pos
, int cnt
) {
111 beforeAdd(cache
.getEntry(pos
));
115 public void finish() {
121 private void beforeAdd(final DirCacheEntry newEntry
) {
122 if (sorted
&& entryCnt
> 0) {
123 final DirCacheEntry lastEntry
= entries
[entryCnt
- 1];
124 final int cr
= DirCache
.cmp(lastEntry
, newEntry
);
126 // The new entry sorts before the old entry; we are
127 // no longer sorted correctly. We'll need to redo
128 // the sorting before we can close out the build.
131 } else if (cr
== 0) {
132 // Same file path; we can only insert this if the
133 // stages won't be violated.
135 final int peStage
= lastEntry
.getStage();
136 final int dceStage
= newEntry
.getStage();
137 if (peStage
== dceStage
)
138 throw bad(newEntry
, "Duplicate stages not allowed");
139 if (peStage
== 0 || dceStage
== 0)
140 throw bad(newEntry
, "Mixed stages not allowed");
141 if (peStage
> dceStage
)
147 private void resort() {
148 Arrays
.sort(entries
, 0, entryCnt
, DirCache
.ENT_CMP
);
150 for (int entryIdx
= 1; entryIdx
< entryCnt
; entryIdx
++) {
151 final DirCacheEntry pe
= entries
[entryIdx
- 1];
152 final DirCacheEntry ce
= entries
[entryIdx
];
153 final int cr
= DirCache
.cmp(pe
, ce
);
155 // Same file path; we can only allow this if the stages
156 // are 1-3 and no 0 exists.
158 final int peStage
= pe
.getStage();
159 final int ceStage
= ce
.getStage();
160 if (peStage
== ceStage
)
161 throw bad(ce
, "Duplicate stages not allowed");
162 if (peStage
== 0 || ceStage
== 0)
163 throw bad(ce
, "Mixed stages not allowed");
170 private static IllegalStateException
bad(final DirCacheEntry a
,
172 return new IllegalStateException(msg
+ ": " + a
.getStage() + " "
173 + a
.getPathString());