View Javadoc
1   /*
2    * Copyright (C) 2017, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.internal.storage.reftable;
45  
46  import static org.eclipse.jgit.internal.storage.reftable.ReftableConstants.MAX_BLOCK_SIZE;
47  
48  import org.eclipse.jgit.lib.Config;
49  import org.eclipse.jgit.lib.Repository;
50  
51  /**
52   * Configuration used by a reftable writer when constructing the stream.
53   */
54  public class ReftableConfig {
55  	private int refBlockSize = 4 << 10;
56  	private int logBlockSize;
57  	private int restartInterval;
58  	private int maxIndexLevels;
59  	private boolean alignBlocks = true;
60  	private boolean indexObjects = true;
61  
62  	/**
63  	 * Create a default configuration.
64  	 */
65  	public ReftableConfig() {
66  	}
67  
68  	/**
69  	 * Create a configuration honoring the repository's settings.
70  	 *
71  	 * @param db
72  	 *            the repository to read settings from. The repository is not
73  	 *            retained by the new configuration, instead its settings are
74  	 *            copied during the constructor.
75  	 */
76  	public ReftableConfig(Repository db) {
77  		fromConfig(db.getConfig());
78  	}
79  
80  	/**
81  	 * Create a configuration honoring settings in a
82  	 * {@link org.eclipse.jgit.lib.Config}.
83  	 *
84  	 * @param cfg
85  	 *            the source to read settings from. The source is not retained
86  	 *            by the new configuration, instead its settings are copied
87  	 *            during the constructor.
88  	 */
89  	public ReftableConfig(Config cfg) {
90  		fromConfig(cfg);
91  	}
92  
93  	/**
94  	 * Copy an existing configuration to a new instance.
95  	 *
96  	 * @param cfg
97  	 *            the source configuration to copy from.
98  	 */
99  	public ReftableConfig(ReftableConfig cfg) {
100 		this.refBlockSize = cfg.refBlockSize;
101 		this.logBlockSize = cfg.logBlockSize;
102 		this.restartInterval = cfg.restartInterval;
103 		this.maxIndexLevels = cfg.maxIndexLevels;
104 		this.alignBlocks = cfg.alignBlocks;
105 		this.indexObjects = cfg.indexObjects;
106 	}
107 
108 	/**
109 	 * Get desired output block size for references, in bytes.
110 	 *
111 	 * @return desired output block size for references, in bytes.
112 	 */
113 	public int getRefBlockSize() {
114 		return refBlockSize;
115 	}
116 
117 	/**
118 	 * Set desired output block size for references, in bytes.
119 	 *
120 	 * @param szBytes
121 	 *            desired output block size for references, in bytes.
122 	 */
123 	public void setRefBlockSize(int szBytes) {
124 		if (szBytes > MAX_BLOCK_SIZE) {
125 			throw new IllegalArgumentException();
126 		}
127 		refBlockSize = Math.max(0, szBytes);
128 	}
129 
130 	/**
131 	 * Get desired output block size for log entries, in bytes.
132 	 *
133 	 * @return desired output block size for log entries, in bytes. If 0 the
134 	 *         writer will default to {@code 2 * getRefBlockSize()}.
135 	 */
136 	public int getLogBlockSize() {
137 		return logBlockSize;
138 	}
139 
140 	/**
141 	 * Set desired output block size for log entries, in bytes.
142 	 *
143 	 * @param szBytes
144 	 *            desired output block size for log entries, in bytes. If 0 will
145 	 *            default to {@code 2 * getRefBlockSize()}.
146 	 */
147 	public void setLogBlockSize(int szBytes) {
148 		if (szBytes > MAX_BLOCK_SIZE) {
149 			throw new IllegalArgumentException();
150 		}
151 		logBlockSize = Math.max(0, szBytes);
152 	}
153 
154 	/**
155 	 * Get number of references between binary search markers.
156 	 *
157 	 * @return number of references between binary search markers.
158 	 */
159 	public int getRestartInterval() {
160 		return restartInterval;
161 	}
162 
163 	/**
164 	 * <p>Setter for the field <code>restartInterval</code>.</p>
165 	 *
166 	 * @param interval
167 	 *            number of references between binary search markers. If
168 	 *            {@code interval} is 0 (default), the writer will select a
169 	 *            default value based on the block size.
170 	 */
171 	public void setRestartInterval(int interval) {
172 		restartInterval = Math.max(0, interval);
173 	}
174 
175 	/**
176 	 * Get maximum depth of the index; 0 for unlimited.
177 	 *
178 	 * @return maximum depth of the index; 0 for unlimited.
179 	 */
180 	public int getMaxIndexLevels() {
181 		return maxIndexLevels;
182 	}
183 
184 	/**
185 	 * Set maximum number of levels to use in indexes.
186 	 *
187 	 * @param levels
188 	 *            maximum number of levels to use in indexes. Lower levels of
189 	 *            the index respect {@link #getRefBlockSize()}, and the highest
190 	 *            level may exceed that if the number of levels is limited.
191 	 */
192 	public void setMaxIndexLevels(int levels) {
193 		maxIndexLevels = Math.max(0, levels);
194 	}
195 
196 	/**
197 	 * Whether the writer should align blocks.
198 	 *
199 	 * @return {@code true} if the writer should align blocks.
200 	 */
201 	public boolean isAlignBlocks() {
202 		return alignBlocks;
203 	}
204 
205 	/**
206 	 * Whether blocks are written aligned to multiples of
207 	 * {@link #getRefBlockSize()}.
208 	 *
209 	 * @param align
210 	 *            if {@code true} blocks are written aligned to multiples of
211 	 *            {@link #getRefBlockSize()}. May increase file size due to NUL
212 	 *            padding bytes added between blocks. Default is {@code true}.
213 	 */
214 	public void setAlignBlocks(boolean align) {
215 		alignBlocks = align;
216 	}
217 
218 	/**
219 	 * Whether the writer should index object to ref.
220 	 *
221 	 * @return {@code true} if the writer should index object to ref.
222 	 */
223 	public boolean isIndexObjects() {
224 		return indexObjects;
225 	}
226 
227 	/**
228 	 * Whether the reftable may include additional storage to efficiently map
229 	 * from {@code ObjectId} to reference names.
230 	 *
231 	 * @param index
232 	 *            if {@code true} the reftable may include additional storage to
233 	 *            efficiently map from {@code ObjectId} to reference names. By
234 	 *            default, {@code true}.
235 	 */
236 	public void setIndexObjects(boolean index) {
237 		indexObjects = index;
238 	}
239 
240 	/**
241 	 * Update properties by setting fields from the configuration.
242 	 *
243 	 * If a property's corresponding variable is not defined in the supplied
244 	 * configuration, then it is left unmodified.
245 	 *
246 	 * @param rc
247 	 *            configuration to read properties from.
248 	 */
249 	public void fromConfig(Config rc) {
250 		refBlockSize = rc.getInt("reftable", "blockSize", refBlockSize); //$NON-NLS-1$ //$NON-NLS-2$
251 		logBlockSize = rc.getInt("reftable", "logBlockSize", logBlockSize); //$NON-NLS-1$ //$NON-NLS-2$
252 		restartInterval = rc.getInt("reftable", "restartInterval", restartInterval); //$NON-NLS-1$ //$NON-NLS-2$
253 		maxIndexLevels = rc.getInt("reftable", "indexLevels", maxIndexLevels); //$NON-NLS-1$ //$NON-NLS-2$
254 		alignBlocks = rc.getBoolean("reftable", "alignBlocks", alignBlocks); //$NON-NLS-1$ //$NON-NLS-2$
255 		indexObjects = rc.getBoolean("reftable", "indexObjects", indexObjects); //$NON-NLS-1$ //$NON-NLS-2$
256 	}
257 }