View Javadoc
1   /*
2    * Copyright (C) 2011, Google Inc. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.internal.storage.dfs;
12  
13  import static java.nio.charset.StandardCharsets.UTF_8;
14  
15  import java.util.Arrays;
16  
17  import org.eclipse.jgit.annotations.Nullable;
18  import org.eclipse.jgit.internal.storage.pack.PackExt;
19  
20  /**
21   * Key used by {@link org.eclipse.jgit.internal.storage.dfs.DfsBlockCache} to disambiguate streams.
22   */
23  public abstract class DfsStreamKey {
24  	/**
25  	 * Create a {@code DfsStreamKey}
26  	 *
27  	 * @param repo
28  	 *            description of the containing repository.
29  	 * @param name
30  	 *            compute the key from a string name.
31  	 * @param ext
32  	 *            pack file extension, or {@code null}.
33  	 * @return key for {@code name}
34  	 */
35  	public static DfsStreamKey of(DfsRepositoryDescription repo, String name,
36  			@Nullable PackExt ext) {
37  		return new ByteArrayDfsStreamKey(repo, name.getBytes(UTF_8), ext);
38  	}
39  
40  	final int hash;
41  
42  	final int packExtPos;
43  
44  	/**
45  	 * Constructor for DfsStreamKey.
46  	 *
47  	 * @param hash
48  	 *            hash of the other identifying components of the key.
49  	 * @param ext
50  	 *            pack file extension, or {@code null}.
51  	 */
52  	protected DfsStreamKey(int hash, @Nullable PackExt ext) {
53  		// Multiply by 31 here so we can more directly combine with another
54  		// value without doing the multiply there.
55  		this.hash = hash * 31;
56  		this.packExtPos = ext == null ? 0 : ext.getPosition();
57  	}
58  
59  	/** {@inheritDoc} */
60  	@Override
61  	public int hashCode() {
62  		return hash;
63  	}
64  
65  	/** {@inheritDoc} */
66  	@Override
67  	public abstract boolean equals(Object o);
68  
69  	/** {@inheritDoc} */
70  	@SuppressWarnings("boxing")
71  	@Override
72  	public String toString() {
73  		return String.format("DfsStreamKey[hash=%08x]", hash); //$NON-NLS-1$
74  	}
75  
76  	private static final class ByteArrayDfsStreamKey extends DfsStreamKey {
77  		private final DfsRepositoryDescription repo;
78  
79  		private final byte[] name;
80  
81  		ByteArrayDfsStreamKey(DfsRepositoryDescription repo, byte[] name,
82  				@Nullable PackExt ext) {
83  			super(repo.hashCode() * 31 + Arrays.hashCode(name), ext);
84  			this.repo = repo;
85  			this.name = name;
86  		}
87  
88  		@Override
89  		public boolean equals(Object o) {
90  			if (o instanceof ByteArrayDfsStreamKey) {
91  				ByteArrayDfsStreamKey k = (ByteArrayDfsStreamKey) o;
92  				return hash == k.hash && repo.equals(k.repo)
93  						&& Arrays.equals(name, k.name);
94  			}
95  			return false;
96  		}
97  	}
98  
99  	static final class ForReverseIndex extends DfsStreamKey {
100 		private final DfsStreamKey idxKey;
101 
102 		ForReverseIndex(DfsStreamKey idxKey) {
103 			super(idxKey.hash + 1, null);
104 			this.idxKey = idxKey;
105 		}
106 
107 		@Override
108 		public boolean equals(Object o) {
109 			return o instanceof ForReverseIndex
110 					&& idxKey.equals(((ForReverseIndex) o).idxKey);
111 		}
112 	}
113 }