View Javadoc
1   /*
2    * Copyright (C) 2017, Google Inc. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.internal.storage.reftable;
12  
13  import static org.eclipse.jgit.internal.storage.reftable.ReftableConstants.MAX_BLOCK_SIZE;
14  
15  import org.eclipse.jgit.lib.Config;
16  import org.eclipse.jgit.lib.Repository;
17  
18  /**
19   * Configuration used by a reftable writer when constructing the stream.
20   */
21  public class ReftableConfig {
22  	private int refBlockSize = 4 << 10;
23  	private int logBlockSize;
24  	private int restartInterval;
25  	private int maxIndexLevels;
26  	private boolean alignBlocks = true;
27  	private boolean indexObjects = true;
28  
29  	/**
30  	 * Create a default configuration.
31  	 */
32  	public ReftableConfig() {
33  	}
34  
35  	/**
36  	 * Create a configuration honoring the repository's settings.
37  	 *
38  	 * @param db
39  	 *            the repository to read settings from. The repository is not
40  	 *            retained by the new configuration, instead its settings are
41  	 *            copied during the constructor.
42  	 */
43  	public ReftableConfig(Repository db) {
44  		fromConfig(db.getConfig());
45  	}
46  
47  	/**
48  	 * Create a configuration honoring settings in a
49  	 * {@link org.eclipse.jgit.lib.Config}.
50  	 *
51  	 * @param cfg
52  	 *            the source to read settings from. The source is not retained
53  	 *            by the new configuration, instead its settings are copied
54  	 *            during the constructor.
55  	 */
56  	public ReftableConfig(Config cfg) {
57  		fromConfig(cfg);
58  	}
59  
60  	/**
61  	 * Copy an existing configuration to a new instance.
62  	 *
63  	 * @param cfg
64  	 *            the source configuration to copy from.
65  	 */
66  	public ReftableConfig="../../../../../../org/eclipse/jgit/internal/storage/reftable/ReftableConfig.html#ReftableConfig">ReftableConfig(ReftableConfig cfg) {
67  		this.refBlockSize = cfg.refBlockSize;
68  		this.logBlockSize = cfg.logBlockSize;
69  		this.restartInterval = cfg.restartInterval;
70  		this.maxIndexLevels = cfg.maxIndexLevels;
71  		this.alignBlocks = cfg.alignBlocks;
72  		this.indexObjects = cfg.indexObjects;
73  	}
74  
75  	/**
76  	 * Get desired output block size for references, in bytes.
77  	 *
78  	 * @return desired output block size for references, in bytes.
79  	 */
80  	public int getRefBlockSize() {
81  		return refBlockSize;
82  	}
83  
84  	/**
85  	 * Set desired output block size for references, in bytes.
86  	 *
87  	 * @param szBytes
88  	 *            desired output block size for references, in bytes.
89  	 */
90  	public void setRefBlockSize(int szBytes) {
91  		if (szBytes > MAX_BLOCK_SIZE) {
92  			throw new IllegalArgumentException();
93  		}
94  		refBlockSize = Math.max(0, szBytes);
95  	}
96  
97  	/**
98  	 * Get desired output block size for log entries, in bytes.
99  	 *
100 	 * @return desired output block size for log entries, in bytes. If 0 the
101 	 *         writer will default to {@code 2 * getRefBlockSize()}.
102 	 */
103 	public int getLogBlockSize() {
104 		return logBlockSize;
105 	}
106 
107 	/**
108 	 * Set desired output block size for log entries, in bytes.
109 	 *
110 	 * @param szBytes
111 	 *            desired output block size for log entries, in bytes. If 0 will
112 	 *            default to {@code 2 * getRefBlockSize()}.
113 	 */
114 	public void setLogBlockSize(int szBytes) {
115 		if (szBytes > MAX_BLOCK_SIZE) {
116 			throw new IllegalArgumentException();
117 		}
118 		logBlockSize = Math.max(0, szBytes);
119 	}
120 
121 	/**
122 	 * Get number of references between binary search markers.
123 	 *
124 	 * @return number of references between binary search markers.
125 	 */
126 	public int getRestartInterval() {
127 		return restartInterval;
128 	}
129 
130 	/**
131 	 * <p>Setter for the field <code>restartInterval</code>.</p>
132 	 *
133 	 * @param interval
134 	 *            number of references between binary search markers. If
135 	 *            {@code interval} is 0 (default), the writer will select a
136 	 *            default value based on the block size.
137 	 */
138 	public void setRestartInterval(int interval) {
139 		restartInterval = Math.max(0, interval);
140 	}
141 
142 	/**
143 	 * Get maximum depth of the index; 0 for unlimited.
144 	 *
145 	 * @return maximum depth of the index; 0 for unlimited.
146 	 */
147 	public int getMaxIndexLevels() {
148 		return maxIndexLevels;
149 	}
150 
151 	/**
152 	 * Set maximum number of levels to use in indexes.
153 	 *
154 	 * @param levels
155 	 *            maximum number of levels to use in indexes. Lower levels of
156 	 *            the index respect {@link #getRefBlockSize()}, and the highest
157 	 *            level may exceed that if the number of levels is limited.
158 	 */
159 	public void setMaxIndexLevels(int levels) {
160 		maxIndexLevels = Math.max(0, levels);
161 	}
162 
163 	/**
164 	 * Whether the writer should align blocks.
165 	 *
166 	 * @return {@code true} if the writer should align blocks.
167 	 */
168 	public boolean isAlignBlocks() {
169 		return alignBlocks;
170 	}
171 
172 	/**
173 	 * Whether blocks are written aligned to multiples of
174 	 * {@link #getRefBlockSize()}.
175 	 *
176 	 * @param align
177 	 *            if {@code true} blocks are written aligned to multiples of
178 	 *            {@link #getRefBlockSize()}. May increase file size due to NUL
179 	 *            padding bytes added between blocks. Default is {@code true}.
180 	 */
181 	public void setAlignBlocks(boolean align) {
182 		alignBlocks = align;
183 	}
184 
185 	/**
186 	 * Whether the writer should index object to ref.
187 	 *
188 	 * @return {@code true} if the writer should index object to ref.
189 	 */
190 	public boolean isIndexObjects() {
191 		return indexObjects;
192 	}
193 
194 	/**
195 	 * Whether the reftable may include additional storage to efficiently map
196 	 * from {@code ObjectId} to reference names.
197 	 *
198 	 * @param index
199 	 *            if {@code true} the reftable may include additional storage to
200 	 *            efficiently map from {@code ObjectId} to reference names. By
201 	 *            default, {@code true}.
202 	 */
203 	public void setIndexObjects(boolean index) {
204 		indexObjects = index;
205 	}
206 
207 	/**
208 	 * Update properties by setting fields from the configuration.
209 	 *
210 	 * If a property's corresponding variable is not defined in the supplied
211 	 * configuration, then it is left unmodified.
212 	 *
213 	 * @param rc
214 	 *            configuration to read properties from.
215 	 */
216 	public void fromConfig(Config rc) {
217 		refBlockSize = rc.getInt("reftable", "blockSize", refBlockSize); //$NON-NLS-1$ //$NON-NLS-2$
218 		logBlockSize = rc.getInt("reftable", "logBlockSize", logBlockSize); //$NON-NLS-1$ //$NON-NLS-2$
219 		restartInterval = rc.getInt("reftable", "restartInterval", restartInterval); //$NON-NLS-1$ //$NON-NLS-2$
220 		maxIndexLevels = rc.getInt("reftable", "indexLevels", maxIndexLevels); //$NON-NLS-1$ //$NON-NLS-2$
221 		alignBlocks = rc.getBoolean("reftable", "alignBlocks", alignBlocks); //$NON-NLS-1$ //$NON-NLS-2$
222 		indexObjects = rc.getBoolean("reftable", "indexObjects", indexObjects); //$NON-NLS-1$ //$NON-NLS-2$
223 	}
224 }