aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/reftable/ReftableConfig.java
blob: 84a7c3e8c64a5730542341f2b28710156976b930 (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
/*
 * Copyright (C) 2017, 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.reftable;

import static org.eclipse.jgit.internal.storage.reftable.ReftableConstants.MAX_BLOCK_SIZE;

import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.Repository;

/**
 * Configuration used by a reftable writer when constructing the stream.
 */
public class ReftableConfig {
	private int refBlockSize = 4 << 10;
	private int logBlockSize;
	private int restartInterval;
	private int maxIndexLevels;
	private boolean alignBlocks = true;
	private boolean indexObjects = true;

	/**
	 * Create a default configuration.
	 */
	public ReftableConfig() {
	}

	/**
	 * Create a configuration honoring the repository's settings.
	 *
	 * @param db
	 *            the repository to read settings from. The repository is not
	 *            retained by the new configuration, instead its settings are
	 *            copied during the constructor.
	 */
	public ReftableConfig(Repository db) {
		fromConfig(db.getConfig());
	}

	/**
	 * Create a configuration honoring settings in a
	 * {@link org.eclipse.jgit.lib.Config}.
	 *
	 * @param cfg
	 *            the source to read settings from. The source is not retained
	 *            by the new configuration, instead its settings are copied
	 *            during the constructor.
	 */
	public ReftableConfig(Config cfg) {
		fromConfig(cfg);
	}

	/**
	 * Copy an existing configuration to a new instance.
	 *
	 * @param cfg
	 *            the source configuration to copy from.
	 */
	public ReftableConfig(ReftableConfig cfg) {
		this.refBlockSize = cfg.refBlockSize;
		this.logBlockSize = cfg.logBlockSize;
		this.restartInterval = cfg.restartInterval;
		this.maxIndexLevels = cfg.maxIndexLevels;
		this.alignBlocks = cfg.alignBlocks;
		this.indexObjects = cfg.indexObjects;
	}

	/**
	 * Get desired output block size for references, in bytes.
	 *
	 * @return desired output block size for references, in bytes.
	 */
	public int getRefBlockSize() {
		return refBlockSize;
	}

	/**
	 * Set desired output block size for references, in bytes.
	 *
	 * @param szBytes
	 *            desired output block size for references, in bytes.
	 */
	public void setRefBlockSize(int szBytes) {
		if (szBytes > MAX_BLOCK_SIZE) {
			throw new IllegalArgumentException();
		}
		refBlockSize = Math.max(0, szBytes);
	}

	/**
	 * Get desired output block size for log entries, in bytes.
	 *
	 * @return desired output block size for log entries, in bytes. If 0 the
	 *         writer will default to {@code 2 * getRefBlockSize()}.
	 */
	public int getLogBlockSize() {
		return logBlockSize;
	}

	/**
	 * Set desired output block size for log entries, in bytes.
	 *
	 * @param szBytes
	 *            desired output block size for log entries, in bytes. If 0 will
	 *            default to {@code 2 * getRefBlockSize()}.
	 */
	public void setLogBlockSize(int szBytes) {
		if (szBytes > MAX_BLOCK_SIZE) {
			throw new IllegalArgumentException();
		}
		logBlockSize = Math.max(0, szBytes);
	}

	/**
	 * Get number of references between binary search markers.
	 *
	 * @return number of references between binary search markers.
	 */
	public int getRestartInterval() {
		return restartInterval;
	}

	/**
	 * <p>Setter for the field <code>restartInterval</code>.</p>
	 *
	 * @param interval
	 *            number of references between binary search markers. If
	 *            {@code interval} is 0 (default), the writer will select a
	 *            default value based on the block size.
	 */
	public void setRestartInterval(int interval) {
		restartInterval = Math.max(0, interval);
	}

	/**
	 * Get maximum depth of the index; 0 for unlimited.
	 *
	 * @return maximum depth of the index; 0 for unlimited.
	 */
	public int getMaxIndexLevels() {
		return maxIndexLevels;
	}

	/**
	 * Set maximum number of levels to use in indexes.
	 *
	 * @param levels
	 *            maximum number of levels to use in indexes. Lower levels of
	 *            the index respect {@link #getRefBlockSize()}, and the highest
	 *            level may exceed that if the number of levels is limited.
	 */
	public void setMaxIndexLevels(int levels) {
		maxIndexLevels = Math.max(0, levels);
	}

	/**
	 * Whether the writer should align blocks.
	 *
	 * @return {@code true} if the writer should align blocks.
	 */
	public boolean isAlignBlocks() {
		return alignBlocks;
	}

	/**
	 * Whether blocks are written aligned to multiples of
	 * {@link #getRefBlockSize()}.
	 *
	 * @param align
	 *            if {@code true} blocks are written aligned to multiples of
	 *            {@link #getRefBlockSize()}. May increase file size due to NUL
	 *            padding bytes added between blocks. Default is {@code true}.
	 */
	public void setAlignBlocks(boolean align) {
		alignBlocks = align;
	}

	/**
	 * Whether the writer should index object to ref.
	 *
	 * @return {@code true} if the writer should index object to ref.
	 */
	public boolean isIndexObjects() {
		return indexObjects;
	}

	/**
	 * Whether the reftable may include additional storage to efficiently map
	 * from {@code ObjectId} to reference names.
	 *
	 * @param index
	 *            if {@code true} the reftable may include additional storage to
	 *            efficiently map from {@code ObjectId} to reference names. By
	 *            default, {@code true}.
	 */
	public void setIndexObjects(boolean index) {
		indexObjects = index;
	}

	/**
	 * Update properties by setting fields from the configuration.
	 *
	 * If a property's corresponding variable is not defined in the supplied
	 * configuration, then it is left unmodified.
	 *
	 * @param rc
	 *            configuration to read properties from.
	 */
	public void fromConfig(Config rc) {
		refBlockSize = rc.getInt("reftable", "blockSize", refBlockSize); //$NON-NLS-1$ //$NON-NLS-2$
		logBlockSize = rc.getInt("reftable", "logBlockSize", logBlockSize); //$NON-NLS-1$ //$NON-NLS-2$
		restartInterval = rc.getInt("reftable", "restartInterval", restartInterval); //$NON-NLS-1$ //$NON-NLS-2$
		maxIndexLevels = rc.getInt("reftable", "indexLevels", maxIndexLevels); //$NON-NLS-1$ //$NON-NLS-2$
		alignBlocks = rc.getBoolean("reftable", "alignBlocks", alignBlocks); //$NON-NLS-1$ //$NON-NLS-2$
		indexObjects = rc.getBoolean("reftable", "indexObjects", indexObjects); //$NON-NLS-1$ //$NON-NLS-2$
	}
}