aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsReftableBatchRefUpdate.java
blob: 8157a13675c7e1436a32acb9c32b8239bb5fcb3b (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
/*
 * Copyright (C) 2019, Google Inc. 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.dfs;

import org.eclipse.jgit.internal.storage.dfs.DfsObjDatabase.PackSource;
import org.eclipse.jgit.internal.storage.io.BlockSource;
import org.eclipse.jgit.internal.storage.pack.PackExt;
import org.eclipse.jgit.internal.storage.reftable.ReftableBatchRefUpdate;
import org.eclipse.jgit.internal.storage.reftable.ReftableCompactor;
import org.eclipse.jgit.internal.storage.reftable.ReftableConfig;
import org.eclipse.jgit.internal.storage.reftable.ReftableReader;
import org.eclipse.jgit.internal.storage.reftable.ReftableWriter;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.transport.ReceiveCommand;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import static org.eclipse.jgit.internal.storage.pack.PackExt.REFTABLE;

/**
 * {@link org.eclipse.jgit.lib.BatchRefUpdate} for
 * {@link org.eclipse.jgit.internal.storage.dfs.DfsReftableDatabase}.
 */
public class DfsReftableBatchRefUpdate extends ReftableBatchRefUpdate {
	private static final int AVG_BYTES = 36;

	private final DfsReftableDatabase refdb;

	private final DfsObjDatabase odb;

	/**
	 * Initialize batch update.
	 *
	 * @param refdb
	 *            database the update will modify.
	 * @param odb
	 *            object database to store the reftable.
	 */
	protected DfsReftableBatchRefUpdate(DfsReftableDatabase refdb,
			DfsObjDatabase odb) {
		super(refdb, refdb.reftableDatabase, refdb.getLock(), refdb.getRepository());
		this.refdb = refdb;
		this.odb = odb;
	}

	@Override
	protected void applyUpdates(List<Ref> newRefs, List<ReceiveCommand> pending)
			throws IOException {
		Set<DfsPackDescription> prune = Collections.emptySet();
		DfsPackDescription pack = odb.newPack(PackSource.INSERT);
		try (DfsOutputStream out = odb.writeFile(pack, REFTABLE)) {
			ReftableConfig cfg = DfsPackCompactor
					.configureReftable(refdb.getReftableConfig(), out);

			ReftableWriter.Stats stats;
			if (refdb.compactDuringCommit()
					&& newRefs.size() * AVG_BYTES <= cfg.getRefBlockSize()
					&& canCompactTopOfStack(cfg)) {
				ByteArrayOutputStream tmp = new ByteArrayOutputStream();
				ReftableWriter rw = new ReftableWriter(cfg, tmp);
				write(rw, newRefs, pending);
				rw.finish();
				stats = compactTopOfStack(out, cfg, tmp.toByteArray());
				prune = toPruneTopOfStack();
			} else {
				ReftableWriter rw = new ReftableWriter(cfg, out);
				write(rw, newRefs, pending);
				rw.finish();
				stats = rw.getStats();
			}
			pack.addFileExt(REFTABLE);
			pack.setReftableStats(stats);
		}

		odb.commitPack(Collections.singleton(pack), prune);
		odb.addReftable(pack, prune);
		refdb.clearCache();
	}

	private boolean canCompactTopOfStack(ReftableConfig cfg)
			throws IOException {
		refdb.getLock().lock();
		try {
			DfsReftableStack stack = refdb.stack();
			List<ReftableReader> readers = stack.readers();
			if (readers.isEmpty()) {
				return false;
			}

			int lastIdx = readers.size() - 1;
			DfsReftable last = stack.files().get(lastIdx);
			DfsPackDescription desc = last.getPackDescription();
			if (desc.getPackSource() != PackSource.INSERT
				|| !packOnlyContainsReftable(desc)) {
				return false;
			}

			ReftableReader table = readers.get(lastIdx);
			int bs = cfg.getRefBlockSize();
			return table.size() <= 3 * bs;
		} finally {
			refdb.getLock().unlock();
		}
	}

	private ReftableWriter.Stats compactTopOfStack(OutputStream out,
			ReftableConfig cfg, byte[] newTable) throws IOException {
		refdb.getLock().lock();
		try {
			List<ReftableReader> stack = refdb.stack().readers();

			ReftableReader last = stack.get(stack.size() - 1);

			List<ReftableReader> tables = new ArrayList<>(2);
			tables.add(last);
			tables.add(new ReftableReader(BlockSource.from(newTable)));

			ReftableCompactor compactor = new ReftableCompactor(out);
			compactor.setConfig(cfg);
			compactor.setIncludeDeletes(true);
			compactor.addAll(tables);
			compactor.compact();
			return compactor.getStats();
		} finally {
			refdb.getLock().unlock();
		}
	}

	private Set<DfsPackDescription> toPruneTopOfStack() throws IOException {
		refdb.getLock().lock();
		try {
			List<DfsReftable> stack = refdb.stack().files();

			DfsReftable last = stack.get(stack.size() - 1);
			return Collections.singleton(last.getPackDescription());
		} finally {
			refdb.getLock().unlock();
		}
	}

	private boolean packOnlyContainsReftable(DfsPackDescription desc) {
		for (PackExt ext : PackExt.values()) {
			if (ext != REFTABLE && desc.hasFileExt(ext)) {
				return false;
			}
		}
		return true;
	}
}