Allow a DirCache to be created with no backing store file
[egit/charleso.git] / org.spearce.jgit / src / org / spearce / jgit / dircache / DirCacheBuilder.java
blob3a370547b023eda2e5316e2a0f6e7e6a2e777844
1 /*
2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
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
21 * written permission.
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;
42 /**
43 * Updates a {@link DirCache} by adding individual {@link DirCacheEntry}s.
44 * <p>
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
47 * its new content.
48 * <p>
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.
54 * @see DirCacheEditor
56 public class DirCacheBuilder extends BaseDirCacheEditor {
57 private boolean sorted;
59 /**
60 * Construct a new builder.
62 * @param dc
63 * the cache this builder will eventually update.
64 * @param ecnt
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) {
69 super(dc, ecnt);
72 /**
73 * Append one entry into the resulting entry list.
74 * <p>
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()}.
78 * <p>
79 * The internal entry table is automatically expanded if there is
80 * insufficient space for the new addition.
82 * @param newEntry
83 * the new entry to add.
85 public void add(final DirCacheEntry newEntry) {
86 beforeAdd(newEntry);
87 fastAdd(newEntry);
90 /**
91 * Add a range of existing entries from the destination cache.
92 * <p>
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()}.
96 * <p>
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
100 * in this editor.
101 * <p>
102 * The internal entry table is automatically expanded if there is
103 * insufficient space for the new additions.
105 * @param pos
106 * first entry to copy from the destination cache.
107 * @param cnt
108 * number of entries to copy.
110 public void keep(final int pos, int cnt) {
111 beforeAdd(cache.getEntry(pos));
112 fastKeep(pos, cnt);
115 public void finish() {
116 if (!sorted)
117 resort();
118 replace();
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);
125 if (cr > 0) {
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.
130 sorted = false;
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)
142 sorted = false;
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);
154 if (cr == 0) {
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");
167 sorted = true;
170 private static IllegalStateException bad(final DirCacheEntry a,
171 final String msg) {
172 return new IllegalStateException(msg + ": " + a.getStage() + " "
173 + a.getPathString());