View Javadoc
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  
44  package org.eclipse.jgit.internal.storage.dfs;
45  
46  import static org.eclipse.jgit.lib.Ref.Storage.NEW;
47  
48  import java.io.IOException;
49  import java.util.Collections;
50  import java.util.List;
51  import java.util.Map;
52  import java.util.concurrent.atomic.AtomicReference;
53  
54  import org.eclipse.jgit.errors.MissingObjectException;
55  import org.eclipse.jgit.lib.ObjectIdRef;
56  import org.eclipse.jgit.lib.Ref;
57  import org.eclipse.jgit.lib.RefDatabase;
58  import org.eclipse.jgit.lib.RefRename;
59  import org.eclipse.jgit.lib.RefUpdate;
60  import org.eclipse.jgit.lib.SymbolicRef;
61  import org.eclipse.jgit.revwalk.RevObject;
62  import org.eclipse.jgit.revwalk.RevTag;
63  import org.eclipse.jgit.revwalk.RevWalk;
64  import org.eclipse.jgit.util.RefList;
65  import org.eclipse.jgit.util.RefMap;
66  
67  /** */
68  public abstract class DfsRefDatabase extends RefDatabase {
69  	private final DfsRepository repository;
70  
71  	private final AtomicReference<RefCache> cache;
72  
73  	/**
74  	 * Initialize the reference database for a repository.
75  	 *
76  	 * @param repository
77  	 *            the repository this database instance manages references for.
78  	 */
79  	protected DfsRefDatabase(DfsRepository repository) {
80  		this.repository = repository;
81  		this.cache = new AtomicReference<>();
82  	}
83  
84  	/** @return the repository the database holds the references of. */
85  	protected DfsRepository getRepository() {
86  		return repository;
87  	}
88  
89  	boolean exists() throws IOException {
90  		return 0 < read().size();
91  	}
92  
93  	@Override
94  	public Ref exactRef(String name) throws IOException {
95  		RefCache curr = read();
96  		Ref ref = curr.ids.get(name);
97  		return ref != null ? resolve(ref, 0, curr.ids) : null;
98  	}
99  
100 	@Override
101 	public Ref getRef(String needle) throws IOException {
102 		RefCache curr = read();
103 		for (String prefix : SEARCH_PATH) {
104 			Ref ref = curr.ids.get(prefix + needle);
105 			if (ref != null) {
106 				ref = resolve(ref, 0, curr.ids);
107 				return ref;
108 			}
109 		}
110 		return null;
111 	}
112 
113 	@Override
114 	public List<Ref> getAdditionalRefs() {
115 		return Collections.emptyList();
116 	}
117 
118 	@Override
119 	public Map<String, Ref> getRefs(String prefix) throws IOException {
120 		RefCache curr = read();
121 		RefList<Ref> packed = RefList.emptyList();
122 		RefList<Ref> loose = curr.ids;
123 		RefList.Builder<Ref> sym = new RefList.Builder<>(curr.sym.size());
124 
125 		for (int idx = 0; idx < curr.sym.size(); idx++) {
126 			Ref ref = curr.sym.get(idx);
127 			String name = ref.getName();
128 			ref = resolve(ref, 0, loose);
129 			if (ref != null && ref.getObjectId() != null) {
130 				sym.add(ref);
131 			} else {
132 				// A broken symbolic reference, we have to drop it from the
133 				// collections the client is about to receive. Should be a
134 				// rare occurrence so pay a copy penalty.
135 				int toRemove = loose.find(name);
136 				if (0 <= toRemove)
137 					loose = loose.remove(toRemove);
138 			}
139 		}
140 
141 		return new RefMap(prefix, packed, loose, sym.toRefList());
142 	}
143 
144 	private Ref resolve(Ref ref, int depth, RefList<Ref> loose)
145 			throws IOException {
146 		if (!ref.isSymbolic())
147 			return ref;
148 
149 		Ref dst = ref.getTarget();
150 
151 		if (MAX_SYMBOLIC_REF_DEPTH <= depth)
152 			return null; // claim it doesn't exist
153 
154 		dst = loose.get(dst.getName());
155 		if (dst == null)
156 			return ref;
157 
158 		dst = resolve(dst, depth + 1, loose);
159 		if (dst == null)
160 			return null;
161 		return new SymbolicRef(ref.getName(), dst);
162 	}
163 
164 	@Override
165 	public Ref peel(Ref ref) throws IOException {
166 		final Ref oldLeaf = ref.getLeaf();
167 		if (oldLeaf.isPeeled() || oldLeaf.getObjectId() == null)
168 			return ref;
169 
170 		Ref newLeaf = doPeel(oldLeaf);
171 
172 		RefCache cur = read();
173 		int idx = cur.ids.find(oldLeaf.getName());
174 		if (0 <= idx && cur.ids.get(idx) == oldLeaf) {
175 			RefList<Ref> newList = cur.ids.set(idx, newLeaf);
176 			cache.compareAndSet(cur, new RefCache(newList, cur));
177 			cachePeeledState(oldLeaf, newLeaf);
178 		}
179 
180 		return recreate(ref, newLeaf);
181 	}
182 
183 	private Ref doPeel(final Ref leaf) throws MissingObjectException,
184 			IOException {
185 		try (RevWalk rw = new RevWalk(repository)) {
186 			RevObject obj = rw.parseAny(leaf.getObjectId());
187 			if (obj instanceof RevTag) {
188 				return new ObjectIdRef.PeeledTag(
189 						leaf.getStorage(),
190 						leaf.getName(),
191 						leaf.getObjectId(),
192 						rw.peel(obj).copy());
193 			} else {
194 				return new ObjectIdRef.PeeledNonTag(
195 						leaf.getStorage(),
196 						leaf.getName(),
197 						leaf.getObjectId());
198 			}
199 		}
200 	}
201 
202 	private static Ref recreate(Ref old, Ref leaf) {
203 		if (old.isSymbolic()) {
204 			Ref dst = recreate(old.getTarget(), leaf);
205 			return new SymbolicRef(old.getName(), dst);
206 		}
207 		return leaf;
208 	}
209 
210 	@Override
211 	public RefUpdate newUpdate(String refName, boolean detach)
212 			throws IOException {
213 		boolean detachingSymbolicRef = false;
214 		Ref ref = exactRef(refName);
215 		if (ref == null)
216 			ref = new ObjectIdRef.Unpeeled(NEW, refName, null);
217 		else
218 			detachingSymbolicRef = detach && ref.isSymbolic();
219 
220 		DfsRefUpdate update = new DfsRefUpdate(this, ref);
221 		if (detachingSymbolicRef)
222 			update.setDetachingSymbolicRef();
223 		return update;
224 	}
225 
226 	@Override
227 	public RefRename newRename(String fromName, String toName)
228 			throws IOException {
229 		RefUpdate src = newUpdate(fromName, true);
230 		RefUpdate dst = newUpdate(toName, true);
231 		return new DfsRefRename(src, dst);
232 	}
233 
234 	@Override
235 	public boolean isNameConflicting(String refName) throws IOException {
236 		RefList<Ref> all = read().ids;
237 
238 		// Cannot be nested within an existing reference.
239 		int lastSlash = refName.lastIndexOf('/');
240 		while (0 < lastSlash) {
241 			String needle = refName.substring(0, lastSlash);
242 			if (all.contains(needle))
243 				return true;
244 			lastSlash = refName.lastIndexOf('/', lastSlash - 1);
245 		}
246 
247 		// Cannot be the container of an existing reference.
248 		String prefix = refName + '/';
249 		int idx = -(all.find(prefix) + 1);
250 		if (idx < all.size() && all.get(idx).getName().startsWith(prefix))
251 			return true;
252 		return false;
253 	}
254 
255 	@Override
256 	public void create() {
257 		// Nothing to do.
258 	}
259 
260 	@Override
261 	public void refresh() {
262 		clearCache();
263 	}
264 
265 	@Override
266 	public void close() {
267 		clearCache();
268 	}
269 
270 	void clearCache() {
271 		cache.set(null);
272 	}
273 
274 	void stored(Ref ref) {
275 		RefCache oldCache, newCache;
276 		do {
277 			oldCache = cache.get();
278 			if (oldCache == null)
279 				return;
280 			newCache = oldCache.put(ref);
281 		} while (!cache.compareAndSet(oldCache, newCache));
282 	}
283 
284 	void removed(String refName) {
285 		RefCache oldCache, newCache;
286 		do {
287 			oldCache = cache.get();
288 			if (oldCache == null)
289 				return;
290 			newCache = oldCache.remove(refName);
291 		} while (!cache.compareAndSet(oldCache, newCache));
292 	}
293 
294 	private RefCache read() throws IOException {
295 		RefCache c = cache.get();
296 		if (c == null) {
297 			c = scanAllRefs();
298 			cache.set(c);
299 		}
300 		return c;
301 	}
302 
303 	/**
304 	 * Read all known references in the repository.
305 	 *
306 	 * @return all current references of the repository.
307 	 * @throws IOException
308 	 *             references cannot be accessed.
309 	 */
310 	protected abstract RefCache scanAllRefs() throws IOException;
311 
312 	/**
313 	 * Compare a reference, and put if it matches.
314 	 * <p>
315 	 * Two reference match if and only if they satisfy the following:
316 	 *
317 	 * <ul>
318 	 * <li>If one reference is a symbolic ref, the other one should be a symbolic
319 	 * ref.
320 	 * <li>If both are symbolic refs, the target names should be same.
321 	 * <li>If both are object ID refs, the object IDs should be same.
322 	 * </ul>
323 	 *
324 	 * @param oldRef
325 	 *            old value to compare to. If the reference is expected to not
326 	 *            exist the old value has a storage of
327 	 *            {@link org.eclipse.jgit.lib.Ref.Storage#NEW} and an ObjectId
328 	 *            value of {@code null}.
329 	 * @param newRef
330 	 *            new reference to store.
331 	 * @return true if the put was successful; false otherwise.
332 	 * @throws IOException
333 	 *             the reference cannot be put due to a system error.
334 	 */
335 	protected abstract boolean compareAndPut(Ref oldRef, Ref newRef)
336 			throws IOException;
337 
338 	/**
339 	 * Compare a reference, and delete if it matches.
340 	 *
341 	 * @param oldRef
342 	 *            the old reference information that was previously read.
343 	 * @return true if the remove was successful; false otherwise.
344 	 * @throws IOException
345 	 *             the reference could not be removed due to a system error.
346 	 */
347 	protected abstract boolean compareAndRemove(Ref oldRef) throws IOException;
348 
349 	/**
350 	 * Update the cached peeled state of a reference
351 	 * <p>
352 	 * The ref database invokes this method after it peels a reference that had
353 	 * not been peeled before. This allows the storage to cache the peel state
354 	 * of the reference, and if it is actually peelable, the target that it
355 	 * peels to, so that on-the-fly peeling doesn't have to happen on the next
356 	 * reference read.
357 	 *
358 	 * @param oldLeaf
359 	 *            the old reference.
360 	 * @param newLeaf
361 	 *            the new reference, with peel information.
362 	 */
363 	protected void cachePeeledState(Ref oldLeaf, Ref newLeaf) {
364 		try {
365 			compareAndPut(oldLeaf, newLeaf);
366 		} catch (IOException e) {
367 			// Ignore an exception during caching.
368 		}
369 	}
370 
371 	/** Collection of references managed by this database. */
372 	public static class RefCache {
373 		final RefList<Ref> ids;
374 
375 		final RefList<Ref> sym;
376 
377 		/**
378 		 * Initialize a new reference cache.
379 		 * <p>
380 		 * The two reference lists supplied must be sorted in correct order
381 		 * (string compare order) by name.
382 		 *
383 		 * @param ids
384 		 *            references that carry an ObjectId, and all of {@code sym}.
385 		 * @param sym
386 		 *            references that are symbolic references to others.
387 		 */
388 		public RefCache(RefList<Ref> ids, RefList<Ref> sym) {
389 			this.ids = ids;
390 			this.sym = sym;
391 		}
392 
393 		RefCache(RefList<Ref> ids, RefCache old) {
394 			this(ids, old.sym);
395 		}
396 
397 		/** @return number of references in this cache. */
398 		public int size() {
399 			return ids.size();
400 		}
401 
402 		/**
403 		 * Find a reference by name.
404 		 *
405 		 * @param name
406 		 *            full name of the reference.
407 		 * @return the reference, if it exists, otherwise null.
408 		 */
409 		public Ref get(String name) {
410 			return ids.get(name);
411 		}
412 
413 		/**
414 		 * Obtain a modified copy of the cache with a ref stored.
415 		 * <p>
416 		 * This cache instance is not modified by this method.
417 		 *
418 		 * @param ref
419 		 *            reference to add or replace.
420 		 * @return a copy of this cache, with the reference added or replaced.
421 		 */
422 		public RefCache put(Ref ref) {
423 			RefList<Ref> newIds = this.ids.put(ref);
424 			RefList<Ref> newSym = this.sym;
425 			if (ref.isSymbolic()) {
426 				newSym = newSym.put(ref);
427 			} else {
428 				int p = newSym.find(ref.getName());
429 				if (0 <= p)
430 					newSym = newSym.remove(p);
431 			}
432 			return new RefCache(newIds, newSym);
433 		}
434 
435 		/**
436 		 * Obtain a modified copy of the cache with the ref removed.
437 		 * <p>
438 		 * This cache instance is not modified by this method.
439 		 *
440 		 * @param refName
441 		 *            reference to remove, if it exists.
442 		 * @return a copy of this cache, with the reference removed.
443 		 */
444 		public RefCache remove(String refName) {
445 			RefList<Ref> newIds = this.ids;
446 			int p = newIds.find(refName);
447 			if (0 <= p)
448 				newIds = newIds.remove(p);
449 
450 			RefList<Ref> newSym = this.sym;
451 			p = newSym.find(refName);
452 			if (0 <= p)
453 				newSym = newSym.remove(p);
454 			return new RefCache(newIds, newSym);
455 		}
456 	}
457 }