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 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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.TREE;
  46. import java.io.IOException;
  47. /**
  48. * Generic update/editing support for {@link DirCache}.
  49. * <p>
  50. * The different update strategies extend this class to provide their own unique
  51. * services to applications.
  52. */
  53. abstract class BaseDirCacheEditor {
  54. /** The cache instance this editor updates during {@link #finish()}. */
  55. protected DirCache cache;
  56. /**
  57. * Entry table this builder will eventually replace into {@link #cache}.
  58. * <p>
  59. * Use {@link #fastAdd(DirCacheEntry)} or {@link #fastKeep(int, int)} to
  60. * make additions to this table. The table is automatically expanded if it
  61. * is too small for a new addition.
  62. * <p>
  63. * Typically the entries in here are sorted by their path names, just like
  64. * they are in the DirCache instance.
  65. */
  66. protected DirCacheEntry[] entries;
  67. /** Total number of valid entries in {@link #entries}. */
  68. protected int entryCnt;
  69. /**
  70. * Construct a new editor.
  71. *
  72. * @param dc
  73. * the cache this editor will eventually update.
  74. * @param ecnt
  75. * estimated number of entries the editor will have upon
  76. * completion. This sizes the initial entry table.
  77. */
  78. protected BaseDirCacheEditor(final DirCache dc, final int ecnt) {
  79. cache = dc;
  80. entries = new DirCacheEntry[ecnt];
  81. }
  82. /**
  83. * @return the cache we will update on {@link #finish()}.
  84. */
  85. public DirCache getDirCache() {
  86. return cache;
  87. }
  88. /**
  89. * Append one entry into the resulting entry list.
  90. * <p>
  91. * The entry is placed at the end of the entry list. The caller is
  92. * responsible for making sure the final table is correctly sorted.
  93. * <p>
  94. * The {@link #entries} table is automatically expanded if there is
  95. * insufficient space for the new addition.
  96. *
  97. * @param newEntry
  98. * the new entry to add.
  99. */
  100. protected void fastAdd(final DirCacheEntry newEntry) {
  101. if (entries.length == entryCnt) {
  102. final DirCacheEntry[] n = new DirCacheEntry[(entryCnt + 16) * 3 / 2];
  103. System.arraycopy(entries, 0, n, 0, entryCnt);
  104. entries = n;
  105. }
  106. entries[entryCnt++] = newEntry;
  107. }
  108. /**
  109. * Add a range of existing entries from the destination cache.
  110. * <p>
  111. * The entries are placed at the end of the entry list, preserving their
  112. * current order. The caller is responsible for making sure the final table
  113. * is correctly sorted.
  114. * <p>
  115. * This method copies from the destination cache, which has not yet been
  116. * updated with this editor's new table. So all offsets into the destination
  117. * cache are not affected by any updates that may be currently taking place
  118. * in this editor.
  119. * <p>
  120. * The {@link #entries} table is automatically expanded if there is
  121. * insufficient space for the new additions.
  122. *
  123. * @param pos
  124. * first entry to copy from the destination cache.
  125. * @param cnt
  126. * number of entries to copy.
  127. */
  128. protected void fastKeep(final int pos, int cnt) {
  129. if (entryCnt + cnt > entries.length) {
  130. final int m1 = (entryCnt + 16) * 3 / 2;
  131. final int m2 = entryCnt + cnt;
  132. final DirCacheEntry[] n = new DirCacheEntry[Math.max(m1, m2)];
  133. System.arraycopy(entries, 0, n, 0, entryCnt);
  134. entries = n;
  135. }
  136. cache.toArray(pos, entries, entryCnt, cnt);
  137. entryCnt += cnt;
  138. }
  139. /**
  140. * Finish this builder and update the destination {@link DirCache}.
  141. * <p>
  142. * When this method completes this builder instance is no longer usable by
  143. * the calling application. A new builder must be created to make additional
  144. * changes to the index entries.
  145. * <p>
  146. * After completion the DirCache returned by {@link #getDirCache()} will
  147. * contain all modifications.
  148. * <p>
  149. * <i>Note to implementors:</i> Make sure {@link #entries} is fully sorted
  150. * then invoke {@link #replace()} to update the DirCache with the new table.
  151. */
  152. public abstract void finish();
  153. /**
  154. * Update the DirCache with the contents of {@link #entries}.
  155. * <p>
  156. * This method should be invoked only during an implementation of
  157. * {@link #finish()}, and only after {@link #entries} is sorted.
  158. */
  159. protected void replace() {
  160. if (entryCnt < entries.length / 2) {
  161. final DirCacheEntry[] n = new DirCacheEntry[entryCnt];
  162. System.arraycopy(entries, 0, n, 0, entryCnt);
  163. entries = n;
  164. }
  165. cache.replace(entries, entryCnt);
  166. }
  167. static int pathCompare(byte[] aPath, int aPos, int aEnd, int aMode,
  168. byte[] bPath, int bPos, int bEnd, int bMode) {
  169. while (aPos < aEnd && bPos < bEnd) {
  170. int cmp = (aPath[aPos++] & 0xff) - (bPath[bPos++] & 0xff);
  171. if (cmp != 0) {
  172. return cmp;
  173. }
  174. }
  175. if (aPos < aEnd) {
  176. return (aPath[aPos] & 0xff) - lastPathChar(bMode);
  177. }
  178. if (bPos < bEnd) {
  179. return lastPathChar(aMode) - (bPath[bPos] & 0xff);
  180. }
  181. return 0;
  182. }
  183. private static int lastPathChar(int mode) {
  184. return TREE.equals(mode) ? '/' : '\0';
  185. }
  186. /**
  187. * Finish, write, commit this change, and release the index lock.
  188. * <p>
  189. * If this method fails (returns false) the lock is still released.
  190. * <p>
  191. * This is a utility method for applications as the finish-write-commit
  192. * pattern is very common after using a builder to update entries.
  193. *
  194. * @return true if the commit was successful and the file contains the new
  195. * data; false if the commit failed and the file remains with the
  196. * old data.
  197. * @throws IllegalStateException
  198. * the lock is not held.
  199. * @throws IOException
  200. * the output file could not be created. The caller no longer
  201. * holds the lock.
  202. */
  203. public boolean commit() throws IOException {
  204. finish();
  205. cache.write();
  206. return cache.commit();
  207. }
  208. }