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.

DhtRefUpdate.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (C) 2011, 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.storage.dht;
  44. import java.io.IOException;
  45. import java.util.concurrent.TimeoutException;
  46. import org.eclipse.jgit.errors.MissingObjectException;
  47. import org.eclipse.jgit.generated.storage.dht.proto.GitStore.RefData;
  48. import org.eclipse.jgit.lib.ObjectId;
  49. import org.eclipse.jgit.lib.Ref;
  50. import org.eclipse.jgit.lib.RefUpdate;
  51. import org.eclipse.jgit.revwalk.RevObject;
  52. import org.eclipse.jgit.revwalk.RevTag;
  53. import org.eclipse.jgit.revwalk.RevWalk;
  54. import org.eclipse.jgit.storage.dht.DhtRefDatabase.DhtRef;
  55. import org.eclipse.jgit.storage.dht.spi.Database;
  56. class DhtRefUpdate extends RefUpdate {
  57. private final DhtRefDatabase refdb;
  58. private final RepositoryKey repo;
  59. private final Database db;
  60. private RefKey refKey;
  61. private RefData oldData;
  62. private RefData newData;
  63. private Ref dstRef;
  64. private RevWalk rw;
  65. DhtRefUpdate(DhtRefDatabase refdb, RepositoryKey repo, Database db, Ref ref) {
  66. super(ref);
  67. this.refdb = refdb;
  68. this.repo = repo;
  69. this.db = db;
  70. }
  71. @Override
  72. protected DhtRefDatabase getRefDatabase() {
  73. return refdb;
  74. }
  75. @Override
  76. protected DhtRepository getRepository() {
  77. return refdb.getRepository();
  78. }
  79. @Override
  80. public Result update(RevWalk walk) throws IOException {
  81. try {
  82. rw = walk;
  83. return super.update(walk);
  84. } finally {
  85. rw = null;
  86. }
  87. }
  88. @Override
  89. protected boolean tryLock(boolean deref) throws IOException {
  90. dstRef = getRef();
  91. if (deref)
  92. dstRef = dstRef.getLeaf();
  93. refKey = RefKey.create(repo, dstRef.getName());
  94. oldData = ((DhtRef) dstRef).getRefData();
  95. if (dstRef.isSymbolic())
  96. setOldObjectId(null);
  97. else
  98. setOldObjectId(dstRef.getObjectId());
  99. return true;
  100. }
  101. @Override
  102. protected void unlock() {
  103. // No state is held while "locked".
  104. }
  105. @Override
  106. protected Result doUpdate(Result desiredResult) throws IOException {
  107. try {
  108. newData = newData();
  109. boolean r = db.ref().compareAndPut(refKey, oldData, newData);
  110. if (r) {
  111. getRefDatabase().stored(dstRef.getName(), newData);
  112. return desiredResult;
  113. } else {
  114. getRefDatabase().clearCache();
  115. return Result.LOCK_FAILURE;
  116. }
  117. } catch (TimeoutException e) {
  118. return Result.IO_FAILURE;
  119. }
  120. }
  121. @Override
  122. protected Result doDelete(Result desiredResult) throws IOException {
  123. try {
  124. boolean r = db.ref().compareAndRemove(refKey, oldData);
  125. if (r) {
  126. getRefDatabase().removed(dstRef.getName());
  127. return desiredResult;
  128. } else {
  129. getRefDatabase().clearCache();
  130. return Result.LOCK_FAILURE;
  131. }
  132. } catch (TimeoutException e) {
  133. return Result.IO_FAILURE;
  134. }
  135. }
  136. @Override
  137. protected Result doLink(String target) throws IOException {
  138. try {
  139. RefData.Builder d = RefData.newBuilder(oldData);
  140. clearRefData(d);
  141. updateSequence(d);
  142. d.setSymref(target);
  143. newData = d.build();
  144. boolean r = db.ref().compareAndPut(refKey, oldData, newData);
  145. if (r) {
  146. getRefDatabase().stored(dstRef.getName(), newData);
  147. if (getRef().getStorage() == Ref.Storage.NEW)
  148. return Result.NEW;
  149. return Result.FORCED;
  150. } else {
  151. getRefDatabase().clearCache();
  152. return Result.LOCK_FAILURE;
  153. }
  154. } catch (TimeoutException e) {
  155. return Result.IO_FAILURE;
  156. }
  157. }
  158. private RefData newData() throws IOException {
  159. RefData.Builder d = RefData.newBuilder(oldData);
  160. clearRefData(d);
  161. updateSequence(d);
  162. ObjectId newId = getNewObjectId();
  163. d.getTargetBuilder().setObjectName(newId.name());
  164. try {
  165. DhtReader ctx = (DhtReader) rw.getObjectReader();
  166. RevObject obj = rw.parseAny(newId);
  167. ChunkKey oKey = ctx.findChunk(newId);
  168. if (oKey != null)
  169. d.getTargetBuilder().setChunkKey(oKey.asString());
  170. if (obj instanceof RevTag) {
  171. ObjectId pId = rw.peel(obj);
  172. ChunkKey pKey = ctx.findChunk(pId);
  173. if (pKey != null)
  174. d.getPeeledBuilder().setChunkKey(pKey.asString());
  175. d.getPeeledBuilder().setObjectName(pId.name());
  176. }
  177. } catch (MissingObjectException e) {
  178. // Automatic peeling failed. Ignore the problem and deal with it
  179. // during reading later, this is the classical Git behavior on disk.
  180. }
  181. return d.build();
  182. }
  183. private static void clearRefData(RefData.Builder d) {
  184. // Clear fields individually rather than discarding the RefData.
  185. // This way implementation specific extensions are carried
  186. // through from the old version to the new version.
  187. d.clearSymref();
  188. d.clearTarget();
  189. d.clearPeeled();
  190. d.clearIsPeeled();
  191. }
  192. private static void updateSequence(RefData.Builder d) {
  193. d.setSequence(d.getSequence() + 1);
  194. }
  195. }