You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BaseDirCacheEditor.java 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright (C) 2008, Google Inc.
  3. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.dircache;
  45. import static org.eclipse.jgit.lib.FileMode.TYPE_TREE;
  46. import static org.eclipse.jgit.util.Paths.compareSameName;
  47. import java.io.IOException;
  48. import org.eclipse.jgit.errors.DirCacheNameConflictException;
  49. /**
  50. * Generic update/editing support for {@link DirCache}.
  51. * <p>
  52. * The different update strategies extend this class to provide their own unique
  53. * services to applications.
  54. */
  55. abstract class BaseDirCacheEditor {
  56. /** The cache instance this editor updates during {@link #finish()}. */
  57. protected DirCache cache;
  58. /**
  59. * Entry table this builder will eventually replace into {@link #cache}.
  60. * <p>
  61. * Use {@link #fastAdd(DirCacheEntry)} or {@link #fastKeep(int, int)} to
  62. * make additions to this table. The table is automatically expanded if it
  63. * is too small for a new addition.
  64. * <p>
  65. * Typically the entries in here are sorted by their path names, just like
  66. * they are in the DirCache instance.
  67. */
  68. protected DirCacheEntry[] entries;
  69. /** Total number of valid entries in {@link #entries}. */
  70. protected int entryCnt;
  71. /**
  72. * Construct a new editor.
  73. *
  74. * @param dc
  75. * the cache this editor will eventually update.
  76. * @param ecnt
  77. * estimated number of entries the editor will have upon
  78. * completion. This sizes the initial entry table.
  79. */
  80. protected BaseDirCacheEditor(DirCache dc, int ecnt) {
  81. cache = dc;
  82. entries = new DirCacheEntry[ecnt];
  83. }
  84. /**
  85. * Get the {@code DirCache}
  86. *
  87. * @return the cache we will update on {@link #finish()}.
  88. */
  89. public DirCache getDirCache() {
  90. return cache;
  91. }
  92. /**
  93. * Append one entry into the resulting entry list.
  94. * <p>
  95. * The entry is placed at the end of the entry list. The caller is
  96. * responsible for making sure the final table is correctly sorted.
  97. * <p>
  98. * The {@link #entries} table is automatically expanded if there is
  99. * insufficient space for the new addition.
  100. *
  101. * @param newEntry
  102. * the new entry to add.
  103. */
  104. protected void fastAdd(DirCacheEntry newEntry) {
  105. if (entries.length == entryCnt) {
  106. final DirCacheEntry[] n = new DirCacheEntry[(entryCnt + 16) * 3 / 2];
  107. System.arraycopy(entries, 0, n, 0, entryCnt);
  108. entries = n;
  109. }
  110. entries[entryCnt++] = newEntry;
  111. }
  112. /**
  113. * Add a range of existing entries from the destination cache.
  114. * <p>
  115. * The entries are placed at the end of the entry list, preserving their
  116. * current order. The caller is responsible for making sure the final table
  117. * is correctly sorted.
  118. * <p>
  119. * This method copies from the destination cache, which has not yet been
  120. * updated with this editor's new table. So all offsets into the destination
  121. * cache are not affected by any updates that may be currently taking place
  122. * in this editor.
  123. * <p>
  124. * The {@link #entries} table is automatically expanded if there is
  125. * insufficient space for the new additions.
  126. *
  127. * @param pos
  128. * first entry to copy from the destination cache.
  129. * @param cnt
  130. * number of entries to copy.
  131. */
  132. protected void fastKeep(int pos, int cnt) {
  133. if (entryCnt + cnt > entries.length) {
  134. final int m1 = (entryCnt + 16) * 3 / 2;
  135. final int m2 = entryCnt + cnt;
  136. final DirCacheEntry[] n = new DirCacheEntry[Math.max(m1, m2)];
  137. System.arraycopy(entries, 0, n, 0, entryCnt);
  138. entries = n;
  139. }
  140. cache.toArray(pos, entries, entryCnt, cnt);
  141. entryCnt += cnt;
  142. }
  143. /**
  144. * Finish this builder and update the destination
  145. * {@link org.eclipse.jgit.dircache.DirCache}.
  146. * <p>
  147. * When this method completes this builder instance is no longer usable by
  148. * the calling application. A new builder must be created to make additional
  149. * changes to the index entries.
  150. * <p>
  151. * After completion the DirCache returned by {@link #getDirCache()} will
  152. * contain all modifications.
  153. * <p>
  154. * <i>Note to implementors:</i> Make sure {@link #entries} is fully sorted
  155. * then invoke {@link #replace()} to update the DirCache with the new table.
  156. */
  157. public abstract void finish();
  158. /**
  159. * Update the DirCache with the contents of {@link #entries}.
  160. * <p>
  161. * This method should be invoked only during an implementation of
  162. * {@link #finish()}, and only after {@link #entries} is sorted.
  163. */
  164. protected void replace() {
  165. checkNameConflicts();
  166. if (entryCnt < entries.length / 2) {
  167. final DirCacheEntry[] n = new DirCacheEntry[entryCnt];
  168. System.arraycopy(entries, 0, n, 0, entryCnt);
  169. entries = n;
  170. }
  171. cache.replace(entries, entryCnt);
  172. }
  173. private void checkNameConflicts() {
  174. int end = entryCnt - 1;
  175. for (int eIdx = 0; eIdx < end; eIdx++) {
  176. DirCacheEntry e = entries[eIdx];
  177. if (e.getStage() != 0) {
  178. continue;
  179. }
  180. byte[] ePath = e.path;
  181. int prefixLen = lastSlash(ePath) + 1;
  182. for (int nIdx = eIdx + 1; nIdx < entryCnt; nIdx++) {
  183. DirCacheEntry n = entries[nIdx];
  184. if (n.getStage() != 0) {
  185. continue;
  186. }
  187. byte[] nPath = n.path;
  188. if (!startsWith(ePath, nPath, prefixLen)) {
  189. // Different prefix; this entry is in another directory.
  190. break;
  191. }
  192. int s = nextSlash(nPath, prefixLen);
  193. int m = s < nPath.length ? TYPE_TREE : n.getRawMode();
  194. int cmp = compareSameName(
  195. ePath, prefixLen, ePath.length,
  196. nPath, prefixLen, s, m);
  197. if (cmp < 0) {
  198. break;
  199. } else if (cmp == 0) {
  200. throw new DirCacheNameConflictException(
  201. e.getPathString(),
  202. n.getPathString());
  203. }
  204. }
  205. }
  206. }
  207. private static int lastSlash(byte[] path) {
  208. for (int i = path.length - 1; i >= 0; i--) {
  209. if (path[i] == '/') {
  210. return i;
  211. }
  212. }
  213. return -1;
  214. }
  215. private static int nextSlash(byte[] b, int p) {
  216. final int n = b.length;
  217. for (; p < n; p++) {
  218. if (b[p] == '/') {
  219. return p;
  220. }
  221. }
  222. return n;
  223. }
  224. private static boolean startsWith(byte[] a, byte[] b, int n) {
  225. if (b.length < n) {
  226. return false;
  227. }
  228. for (n--; n >= 0; n--) {
  229. if (a[n] != b[n]) {
  230. return false;
  231. }
  232. }
  233. return true;
  234. }
  235. /**
  236. * Finish, write, commit this change, and release the index lock.
  237. * <p>
  238. * If this method fails (returns false) the lock is still released.
  239. * <p>
  240. * This is a utility method for applications as the finish-write-commit
  241. * pattern is very common after using a builder to update entries.
  242. *
  243. * @return true if the commit was successful and the file contains the new
  244. * data; false if the commit failed and the file remains with the
  245. * old data.
  246. * @throws java.lang.IllegalStateException
  247. * the lock is not held.
  248. * @throws java.io.IOException
  249. * the output file could not be created. The caller no longer
  250. * holds the lock.
  251. */
  252. public boolean commit() throws IOException {
  253. finish();
  254. cache.write();
  255. return cache.commit();
  256. }
  257. }