您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

DfsReftableDatabase.java 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Copyright (C) 2017, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  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
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  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.
  23. *
  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
  27. * written permission.
  28. *
  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.
  42. */
  43. package org.eclipse.jgit.internal.storage.dfs;
  44. import java.io.IOException;
  45. import java.util.Arrays;
  46. import java.util.HashSet;
  47. import java.util.List;
  48. import java.util.Map;
  49. import java.util.Set;
  50. import java.util.TreeSet;
  51. import java.util.concurrent.locks.ReentrantLock;
  52. import org.eclipse.jgit.annotations.Nullable;
  53. import org.eclipse.jgit.internal.storage.reftable.MergedReftable;
  54. import org.eclipse.jgit.internal.storage.reftable.ReftableConfig;
  55. import org.eclipse.jgit.internal.storage.reftable.ReftableDatabase;
  56. import org.eclipse.jgit.lib.BatchRefUpdate;
  57. import org.eclipse.jgit.lib.NullProgressMonitor;
  58. import org.eclipse.jgit.lib.ObjectId;
  59. import org.eclipse.jgit.lib.Ref;
  60. import org.eclipse.jgit.revwalk.RevWalk;
  61. import org.eclipse.jgit.transport.ReceiveCommand;
  62. import org.eclipse.jgit.util.RefList;
  63. import org.eclipse.jgit.util.RefMap;
  64. /**
  65. * A {@link org.eclipse.jgit.internal.storage.dfs.DfsRefDatabase} that uses
  66. * reftable for storage.
  67. * <p>
  68. * A {@code DfsRefDatabase} instance is thread-safe.
  69. * <p>
  70. * Implementors may wish to use
  71. * {@link org.eclipse.jgit.internal.storage.dfs.DfsPackDescription#getMaxUpdateIndex()}
  72. * as the primary key identifier for a
  73. * {@link org.eclipse.jgit.internal.storage.pack.PackExt#REFTABLE} only pack
  74. * description, ensuring that when there are competing transactions one wins,
  75. * and one will fail.
  76. */
  77. public class DfsReftableDatabase extends DfsRefDatabase {
  78. final ReftableDatabase reftableDatabase;
  79. private DfsReader ctx;
  80. private DfsReftableStack stack;
  81. /**
  82. * Initialize the reference database for a repository.
  83. *
  84. * @param repo
  85. * the repository this database instance manages references for.
  86. */
  87. protected DfsReftableDatabase(DfsRepository repo) {
  88. super(repo);
  89. reftableDatabase = new ReftableDatabase() {
  90. @Override
  91. public MergedReftable openMergedReftable() throws IOException {
  92. DfsReftableDatabase.this.getLock().lock();
  93. try {
  94. return new MergedReftable(stack().readers());
  95. } finally {
  96. DfsReftableDatabase.this.getLock().unlock();
  97. }
  98. }
  99. };
  100. stack = null;
  101. }
  102. /** {@inheritDoc} */
  103. @Override
  104. public boolean hasVersioning() {
  105. return true;
  106. }
  107. /** {@inheritDoc} */
  108. @Override
  109. public boolean performsAtomicTransactions() {
  110. return true;
  111. }
  112. /** {@inheritDoc} */
  113. @Override
  114. public BatchRefUpdate newBatchUpdate() {
  115. DfsObjDatabase odb = getRepository().getObjectDatabase();
  116. return new DfsReftableBatchRefUpdate(this, odb);
  117. }
  118. /**
  119. * Get configuration to write new reftables with.
  120. *
  121. * @return configuration to write new reftables with.
  122. */
  123. public ReftableConfig getReftableConfig() {
  124. return new ReftableConfig(getRepository());
  125. }
  126. /**
  127. * Get the lock protecting this instance's state.
  128. *
  129. * @return the lock protecting this instance's state.
  130. */
  131. protected ReentrantLock getLock() {
  132. return reftableDatabase.getLock();
  133. }
  134. /**
  135. * Whether to compact reftable instead of extending the stack depth.
  136. *
  137. * @return {@code true} if commit of a new small reftable should try to
  138. * replace a prior small reftable by performing a compaction,
  139. * instead of extending the stack depth.
  140. */
  141. protected boolean compactDuringCommit() {
  142. return true;
  143. }
  144. /**
  145. * Obtain a handle to the stack of reftables. Must hold lock.
  146. *
  147. * @return (possibly cached) handle to the stack.
  148. * @throws java.io.IOException
  149. * if tables cannot be opened.
  150. */
  151. protected DfsReftableStack stack() throws IOException {
  152. if (!getLock().isLocked()) {
  153. throw new IllegalStateException("most hold lock to access stack"); //$NON-NLS-1$
  154. }
  155. DfsObjDatabase odb = getRepository().getObjectDatabase();
  156. if (ctx == null) {
  157. ctx = odb.newReader();
  158. }
  159. if (stack == null) {
  160. stack = DfsReftableStack.open(ctx, Arrays.asList(odb.getReftables()));
  161. }
  162. return stack;
  163. }
  164. @Override
  165. public boolean isNameConflicting(String refName) throws IOException {
  166. return reftableDatabase.isNameConflicting(refName, new TreeSet<>(), new HashSet<>());
  167. }
  168. /** {@inheritDoc} */
  169. @Override
  170. public Ref exactRef(String name) throws IOException {
  171. return reftableDatabase.exactRef(name);
  172. }
  173. /** {@inheritDoc} */
  174. @Override
  175. public Map<String, Ref> getRefs(String prefix) throws IOException {
  176. List<Ref> refs = reftableDatabase.getRefsByPrefix(prefix);
  177. RefList.Builder<Ref> builder = new RefList.Builder<>(refs.size());
  178. for (Ref r : refs) {
  179. builder.add(r);
  180. }
  181. return new RefMap(prefix, builder.toRefList(), RefList.emptyList(),
  182. RefList.emptyList());
  183. }
  184. /** {@inheritDoc} */
  185. @Override
  186. public List<Ref> getRefsByPrefix(String prefix) throws IOException {
  187. return reftableDatabase.getRefsByPrefix(prefix);
  188. }
  189. /** {@inheritDoc} */
  190. @Override
  191. public Set<Ref> getTipsWithSha1(ObjectId id) throws IOException {
  192. if (!getReftableConfig().isIndexObjects()) {
  193. return super.getTipsWithSha1(id);
  194. }
  195. return reftableDatabase.getTipsWithSha1(id);
  196. }
  197. /** {@inheritDoc} */
  198. @Override
  199. public boolean hasFastTipsWithSha1() throws IOException {
  200. return reftableDatabase.hasFastTipsWithSha1();
  201. }
  202. /** {@inheritDoc} */
  203. @Override
  204. public Ref peel(Ref ref) throws IOException {
  205. Ref oldLeaf = ref.getLeaf();
  206. if (oldLeaf.isPeeled() || oldLeaf.getObjectId() == null) {
  207. return ref;
  208. }
  209. return recreate(ref, doPeel(oldLeaf), hasVersioning());
  210. }
  211. @Override
  212. boolean exists() throws IOException {
  213. DfsObjDatabase odb = getRepository().getObjectDatabase();
  214. return odb.getReftables().length > 0;
  215. }
  216. @Override
  217. void clearCache() {
  218. getLock().lock();
  219. try {
  220. if (ctx != null) {
  221. ctx.close();
  222. ctx = null;
  223. }
  224. reftableDatabase.clearCache();
  225. if (stack != null) {
  226. stack.close();
  227. stack = null;
  228. }
  229. } finally {
  230. getLock().unlock();
  231. }
  232. }
  233. /** {@inheritDoc} */
  234. @Override
  235. protected boolean compareAndPut(Ref oldRef, @Nullable Ref newRef)
  236. throws IOException {
  237. ReceiveCommand cmd = ReftableDatabase.toCommand(oldRef, newRef);
  238. try (RevWalk rw = new RevWalk(getRepository())) {
  239. rw.setRetainBody(false);
  240. newBatchUpdate().setAllowNonFastForwards(true).addCommand(cmd)
  241. .execute(rw, NullProgressMonitor.INSTANCE);
  242. }
  243. switch (cmd.getResult()) {
  244. case OK:
  245. return true;
  246. case REJECTED_OTHER_REASON:
  247. throw new IOException(cmd.getMessage());
  248. case LOCK_FAILURE:
  249. default:
  250. return false;
  251. }
  252. }
  253. /** {@inheritDoc} */
  254. @Override
  255. protected boolean compareAndRemove(Ref oldRef) throws IOException {
  256. return compareAndPut(oldRef, null);
  257. }
  258. /** {@inheritDoc} */
  259. @Override
  260. protected RefCache scanAllRefs() throws IOException {
  261. throw new UnsupportedOperationException();
  262. }
  263. @Override
  264. void stored(Ref ref) {
  265. // Unnecessary; DfsReftableBatchRefUpdate calls clearCache().
  266. }
  267. @Override
  268. void removed(String refName) {
  269. // Unnecessary; DfsReftableBatchRefUpdate calls clearCache().
  270. }
  271. /** {@inheritDoc} */
  272. @Override
  273. protected void cachePeeledState(Ref oldLeaf, Ref newLeaf) {
  274. // Do not cache peeled state in reftable.
  275. }
  276. }