aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/BitmapWalkerTest.java
blob: a59a71d19f64d5a56cf8d4fd262218f0ddfa0a22 (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
/*
 * Copyright (C) 2023, 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.revwalk;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.List;

import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.internal.storage.file.GC;
import org.eclipse.jgit.junit.LocalDiskRepositoryTestCase;
import org.eclipse.jgit.junit.TestRepository;
import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.BitmapIndex;
import org.eclipse.jgit.lib.BitmapIndex.Bitmap;
import org.eclipse.jgit.lib.BitmapIndex.BitmapBuilder;
import org.eclipse.jgit.lib.BitmapIndex.BitmapLookupListener;
import org.eclipse.jgit.lib.NullProgressMonitor;
import org.eclipse.jgit.lib.ObjectReader;
import org.junit.Before;
import org.junit.Test;

public class BitmapWalkerTest extends LocalDiskRepositoryTestCase {

	private static final String MAIN = "refs/heads/main";

	TestRepository<FileRepository> repo;

	RevCommit tipWithBitmap;

	@Override
	@Before
	public void setUp() throws Exception {
		super.setUp();
		FileRepository db = createWorkRepository();
		repo = new TestRepository<>(db);

		RevCommit base = repo.commit().create();
		RevCommit one = repo.commit().parent(base).create();
		tipWithBitmap = repo.commit().parent(one).create();
		repo.update(MAIN, tipWithBitmap);

		GC gc = new GC(repo.getRepository());
		gc.setAuto(false);
		gc.gc().get();

		assertNotNull(repo.getRevWalk().getObjectReader().getBitmapIndex());
	}

	private static class BitmapWalkCounter implements BitmapLookupListener {
		int withBitmap;

		int withoutBitmap;

		@Override
		public void onBitmapFound(AnyObjectId oid) {
			withBitmap += 1;
		}

		@Override
		public void onBitmapNotFound(AnyObjectId oid) {
			withoutBitmap += 1;
		}
	}

	@Test
	public void counters_bitmapAtTip() throws Exception {
		try (RevWalk rw = repo.getRevWalk();
				ObjectReader or = rw.getObjectReader()) {
			BitmapWalkCounter counter = new BitmapWalkCounter();
			BitmapIndex bitmapIndex = or.getBitmapIndex();
			bitmapIndex.addBitmapLookupListener(counter);
			BitmapWalker bw = new BitmapWalker(rw.toObjectWalkWithSameObjects(),
					bitmapIndex, NullProgressMonitor.INSTANCE);
			BitmapBuilder bitmap = bw.findObjects(List.of(tipWithBitmap), null,
					true);
			// First commit has a tree, so in total 4 objects
			assertEquals(4, bitmap.cardinality());
			assertEquals(1, counter.withBitmap);
			assertEquals(0, counter.withoutBitmap);
			assertEquals(0, bw.getCountOfBitmapIndexMisses());
		}
	}

	@Test
	public void counters_bitmapAfterAStep() throws Exception {
		System.out.println("Old tip: " + tipWithBitmap);
		RevCommit newTip = repo.commit().parent(tipWithBitmap).create();
		System.out.println("New tip: " + newTip);
		try (RevWalk rw = repo.getRevWalk();
				ObjectReader or = rw.getObjectReader()) {
			BitmapWalkCounter counter = new BitmapWalkCounter();
			BitmapIndex bitmapIndex = or.getBitmapIndex();
			bitmapIndex.addBitmapLookupListener(counter);
			BitmapWalker bw = new BitmapWalker(rw.toObjectWalkWithSameObjects(),
					bitmapIndex, NullProgressMonitor.INSTANCE);

			bw.findObjects(List.of(newTip), null, true);

			assertEquals(1, counter.withBitmap);
			// It checks bitmap before marking as interesting, and again in the
			// walk
			assertEquals(2, counter.withoutBitmap);
			assertEquals(1, bw.getCountOfBitmapIndexMisses());
		}
	}

	@Test
	public void counters_bitmapAfterThreeSteps() throws Exception {
		RevCommit newOne = repo.commit().parent(tipWithBitmap).create();
		RevCommit newTwo = repo.commit().parent(newOne).create();
		RevCommit newTip = repo.commit().parent(newTwo).create();

		try (RevWalk rw = repo.getRevWalk();
				ObjectReader or = rw.getObjectReader()) {
			BitmapWalkCounter counter = new BitmapWalkCounter();
			BitmapIndex bitmapIndex = or.getBitmapIndex();
			bitmapIndex.addBitmapLookupListener(counter);
			BitmapWalker bw = new BitmapWalker(rw.toObjectWalkWithSameObjects(),
					bitmapIndex, NullProgressMonitor.INSTANCE);

			bw.findObjects(List.of(newTip), null, true);

			assertEquals(1, counter.withBitmap);
			assertEquals(4, counter.withoutBitmap);
			assertEquals(3, bw.getCountOfBitmapIndexMisses());
		}
	}

	@Test
	public void counters_bitmapAfterThreeStepsWithSeen() throws Exception {
		RevCommit newOne = repo.commit().parent(tipWithBitmap).create();
		RevCommit newTwo = repo.commit().parent(newOne).create();
		RevCommit newTip = repo.commit().parent(newTwo).create();

		try (RevWalk rw = repo.getRevWalk();
				ObjectReader or = rw.getObjectReader()) {
			BitmapIndex bitmapIndex = or.getBitmapIndex();
			Bitmap seen = bitmapIndex.getBitmap(tipWithBitmap);
			BitmapBuilder seenBB = bitmapIndex.newBitmapBuilder().or(seen);
			BitmapWalkCounter counter = new BitmapWalkCounter();
			bitmapIndex.addBitmapLookupListener(counter);
			BitmapWalker bw = new BitmapWalker(rw.toObjectWalkWithSameObjects(),
					bitmapIndex, NullProgressMonitor.INSTANCE);

			bw.findObjects(List.of(newTip), seenBB, true);

			assertEquals(0, counter.withBitmap);
			assertEquals(4, counter.withoutBitmap);
			assertEquals(3, bw.getCountOfBitmapIndexMisses());
		}
	}
}