aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftable/ReftableReflogReader.java
blob: 597303301a3f05b1f19c6635a77c45b31a31bba8 (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
/*
 * Copyright (C) 2019, Google LLC and others
 *
 * 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.internal.storage.reftable;

import org.eclipse.jgit.lib.ReflogEntry;
import org.eclipse.jgit.lib.ReflogReader;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;

/**
 * Implement the ReflogReader interface for a reflog stored in reftable.
 */
public class ReftableReflogReader implements ReflogReader {
	private final Lock lock;

	private final Reftable reftable;

	private final String refname;

	ReftableReflogReader(Lock lock, Reftable merged, String refname) {
		this.lock = lock;
		this.reftable = merged;
		this.refname = refname;
	}

	@Override
	public ReflogEntry getLastEntry() throws IOException {
		lock.lock();
		try {
			LogCursor cursor = reftable.seekLog(refname);
			return cursor.next() ? cursor.getReflogEntry() : null;
		} finally {
			lock.unlock();
		}
	}

	@Override
	public List<ReflogEntry> getReverseEntries() throws IOException {
		return getReverseEntries(Integer.MAX_VALUE);
	}

	@Override
	public ReflogEntry getReverseEntry(int number) throws IOException {
		lock.lock();
		try {
			LogCursor cursor = reftable.seekLog(refname);
			while (true) {
				if (!cursor.next() || number < 0) {
					return null;
				}
				if (number == 0) {
					return cursor.getReflogEntry();
				}
				number--;
			}
		} finally {
			lock.unlock();
		}
	}

	@Override
	public List<ReflogEntry> getReverseEntries(int max) throws IOException {
		lock.lock();
		try {
			LogCursor cursor = reftable.seekLog(refname);

			List<ReflogEntry> result = new ArrayList<>();
			while (cursor.next() && result.size() < max) {
				result.add(cursor.getReflogEntry());
			}

			return result;
		} finally {
			lock.unlock();
		}
	}
}