aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/FakeIndexFactory.java
blob: eb23bec584ce7b519770ba879e3d59a28d13eac1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
 * Copyright (C) 2025, Google Inc.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */
package org.eclipse.jgit.junit;

import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toMap;
import static java.util.stream.Collectors.toUnmodifiableList;

import java.io.IOException;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.jgit.errors.CorruptObjectException;
import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.internal.storage.file.PackIndex;
import org.eclipse.jgit.internal.storage.file.PackIndex.EntriesIterator;
import org.eclipse.jgit.internal.storage.file.PackReverseIndex;
import org.eclipse.jgit.lib.AbbreviatedObjectId;
import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;

/**
 * Create indexes with predefined data
 *
 * @since 7.2
 */
public class FakeIndexFactory {

	/**
	 * An object for the fake index
	 *
	 * @param name
	 *            a sha1
	 * @param offset
	 *            the (fake) position of the object in the pack
	 */
	public record IndexObject(String name, long offset) {
		/**
		 * Name (sha1) as an objectId
		 *
		 * @return name (a sha1) as an objectId.
		 */
		public ObjectId getObjectId() {
			return ObjectId.fromString(name);
		}
	}

	/**
	 * Return an index populated with these objects
	 *
	 * @param objs
	 *            objects to be indexed
	 * @return a PackIndex implementation
	 */
	public static PackIndex indexOf(List<IndexObject> objs) {
		return new FakePackIndex(objs);
	}

	/**
	 * Return a reverse pack index with these objects
	 *
	 * @param objs
	 *            objects to be indexed
	 * @return a PackReverseIndex implementation
	 */
	public static PackReverseIndex reverseIndexOf(List<IndexObject> objs) {
		return new FakeReverseIndex(objs);
	}

	private FakeIndexFactory() {
	}

	private static class FakePackIndex implements PackIndex {
		private static final Comparator<IndexObject> SHA1_COMPARATOR = (o1,
				o2) -> String.CASE_INSENSITIVE_ORDER.compare(o1.name(),
						o2.name());

		private final Map<String, IndexObject> idx;

		private final List<IndexObject> sha1Ordered;

		private final long offset64count;

		FakePackIndex(List<IndexObject> objs) {
			sha1Ordered = objs.stream().sorted(SHA1_COMPARATOR)
					.collect(toUnmodifiableList());
			idx = objs.stream().collect(toMap(IndexObject::name, identity()));
			offset64count = objs.stream()
					.filter(o -> o.offset > Integer.MAX_VALUE).count();
		}

		@Override
		public Iterator<MutableEntry> iterator() {
			return new FakeEntriesIterator(sha1Ordered);
		}

		@Override
		public long getObjectCount() {
			return sha1Ordered.size();
		}

		@Override
		public long getOffset64Count() {
			return offset64count;
		}

		@Override
		public ObjectId getObjectId(long nthPosition) {
			return ObjectId
					.fromString(sha1Ordered.get((int) nthPosition).name());
		}

		@Override
		public long getOffset(long nthPosition) {
			return sha1Ordered.get((int) nthPosition).offset();
		}

		@Override
		public long findOffset(AnyObjectId objId) {
			IndexObject o = idx.get(objId.name());
			if (o == null) {
				return -1;
			}
			return o.offset();
		}

		@Override
		public int findPosition(AnyObjectId objId) {
			IndexObject o = idx.get(objId.name());
			if (o == null) {
				return -1;
			}
			return sha1Ordered.indexOf(o);
		}

		@Override
		public long findCRC32(AnyObjectId objId) throws MissingObjectException {
			throw new UnsupportedOperationException();
		}

		@Override
		public boolean hasCRC32Support() {
			return false;
		}

		@Override
		public void resolve(Set<ObjectId> matches, AbbreviatedObjectId id,
				int matchLimit) throws IOException {
			throw new UnsupportedOperationException();
		}

		@Override
		public byte[] getChecksum() {
			return new byte[0];
		}
	}

	private static class FakeReverseIndex implements PackReverseIndex {
		private static final Comparator<IndexObject> OFFSET_COMPARATOR = Comparator
				.comparingLong(IndexObject::offset);

		private final List<IndexObject> byOffset;

		private final Map<Long, IndexObject> ridx;

		FakeReverseIndex(List<IndexObject> objs) {
			byOffset = objs.stream().sorted(OFFSET_COMPARATOR)
					.collect(toUnmodifiableList());
			ridx = byOffset.stream()
					.collect(toMap(IndexObject::offset, identity()));
		}

		@Override
		public void verifyPackChecksum(String packFilePath) {
			// Do nothing
		}

		@Override
		public ObjectId findObject(long offset) {
			IndexObject indexObject = ridx.get(offset);
			if (indexObject == null) {
				return null;
			}
			return ObjectId.fromString(indexObject.name());
		}

		@Override
		public long findNextOffset(long offset, long maxOffset)
				throws CorruptObjectException {
			IndexObject o = ridx.get(offset);
			if (o == null) {
				throw new CorruptObjectException("Invalid offset"); //$NON-NLS-1$
			}
			int pos = byOffset.indexOf(o);
			if (pos == byOffset.size() - 1) {
				return maxOffset;
			}
			return byOffset.get(pos + 1).offset();
		}

		@Override
		public int findPosition(long offset) {
			IndexObject indexObject = ridx.get(offset);
			return byOffset.indexOf(indexObject);
		}

		@Override
		public ObjectId findObjectByPosition(int nthPosition) {
			return byOffset.get(nthPosition).getObjectId();
		}
	}

	private static class FakeEntriesIterator extends EntriesIterator {

		private static final byte[] buffer = new byte[Constants.OBJECT_ID_LENGTH];

		private final Iterator<IndexObject> it;

		FakeEntriesIterator(List<IndexObject> objs) {
			super(objs.size());
			it = objs.iterator();
		}

		@Override
		protected void readNext() {
			IndexObject next = it.next();
			next.getObjectId().copyRawTo(buffer, 0);
			setIdBuffer(buffer, 0);
			setOffset(next.offset());
		}
	}
}