View Javadoc
1   /*
2    * Copyright (C) 2011, 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.dfs;
12  
13  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_CORE_SECTION;
14  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_DFS_SECTION;
15  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_DELTA_BASE_CACHE_LIMIT;
16  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_STREAM_BUFFER;
17  import static org.eclipse.jgit.lib.ConfigConstants.CONFIG_KEY_STREAM_FILE_TRESHOLD;
18  
19  import org.eclipse.jgit.lib.Config;
20  import org.eclipse.jgit.storage.pack.PackConfig;
21  
22  /**
23   * Options controlling how objects are read from a DFS stored repository.
24   */
25  public class DfsReaderOptions {
26  	/** 1024 (number of bytes in one kibibyte/kilobyte) */
27  	public static final int KiB = 1024;
28  
29  	/** 1024 {@link #KiB} (number of bytes in one mebibyte/megabyte) */
30  	public static final int MiB = 1024 * KiB;
31  
32  	private int deltaBaseCacheLimit;
33  	private int streamFileThreshold;
34  
35  	private int streamPackBufferSize;
36  
37  	/**
38  	 * Create a default reader configuration.
39  	 */
40  	public DfsReaderOptions() {
41  		setDeltaBaseCacheLimit(10 * MiB);
42  		setStreamFileThreshold(PackConfig.DEFAULT_BIG_FILE_THRESHOLD);
43  	}
44  
45  	/**
46  	 * Get maximum number of bytes to hold in per-reader DeltaBaseCache.
47  	 *
48  	 * @return maximum number of bytes to hold in per-reader DeltaBaseCache.
49  	 */
50  	public int getDeltaBaseCacheLimit() {
51  		return deltaBaseCacheLimit;
52  	}
53  
54  	/**
55  	 * Set the maximum number of bytes in the DeltaBaseCache.
56  	 *
57  	 * @param maxBytes
58  	 *            the new limit.
59  	 * @return {@code this}
60  	 */
61  	public DfsReaderOptions setDeltaBaseCacheLimit(int maxBytes) {
62  		deltaBaseCacheLimit = Math.max(0, maxBytes);
63  		return this;
64  	}
65  
66  	/**
67  	 * Get the size threshold beyond which objects must be streamed.
68  	 *
69  	 * @return the size threshold beyond which objects must be streamed.
70  	 */
71  	public int getStreamFileThreshold() {
72  		return streamFileThreshold;
73  	}
74  
75  	/**
76  	 * Set new byte limit for objects that must be streamed.
77  	 *
78  	 * @param newLimit
79  	 *            new byte limit for objects that must be streamed. Objects
80  	 *            smaller than this size can be obtained as a contiguous byte
81  	 *            array, while objects bigger than this size require using an
82  	 *            {@link org.eclipse.jgit.lib.ObjectStream}.
83  	 * @return {@code this}
84  	 */
85  	public DfsReaderOptions setStreamFileThreshold(int newLimit) {
86  		streamFileThreshold = Math.max(0, newLimit);
87  		return this;
88  	}
89  
90  	/**
91  	 * Get number of bytes to use for buffering when streaming a pack file
92  	 * during copying.
93  	 *
94  	 * @return number of bytes to use for buffering when streaming a pack file
95  	 *         during copying. If 0 the block size of the pack is used.
96  	 */
97  	public int getStreamPackBufferSize() {
98  		return streamPackBufferSize;
99  	}
100 
101 	/**
102 	 * Set new buffer size in bytes for buffers used when streaming pack files
103 	 * during copying.
104 	 *
105 	 * @param bufsz
106 	 *            new buffer size in bytes for buffers used when streaming pack
107 	 *            files during copying.
108 	 * @return {@code this}
109 	 */
110 	public DfsReaderOptions setStreamPackBufferSize(int bufsz) {
111 		streamPackBufferSize = Math.max(0, bufsz);
112 		return this;
113 	}
114 
115 	/**
116 	 * Update properties by setting fields from the configuration.
117 	 * <p>
118 	 * If a property is not defined in the configuration, then it is left
119 	 * unmodified.
120 	 *
121 	 * @param rc
122 	 *            configuration to read properties from.
123 	 * @return {@code this}
124 	 */
125 	public DfsReaderOptions fromConfig(Config rc) {
126 		setDeltaBaseCacheLimit(rc.getInt(
127 				CONFIG_CORE_SECTION,
128 				CONFIG_DFS_SECTION,
129 				CONFIG_KEY_DELTA_BASE_CACHE_LIMIT,
130 				getDeltaBaseCacheLimit()));
131 
132 		long maxMem = Runtime.getRuntime().maxMemory();
133 		long sft = rc.getLong(
134 				CONFIG_CORE_SECTION,
135 				CONFIG_DFS_SECTION,
136 				CONFIG_KEY_STREAM_FILE_TRESHOLD,
137 				getStreamFileThreshold());
138 		sft = Math.min(sft, maxMem / 4); // don't use more than 1/4 of the heap
139 		sft = Math.min(sft, Integer.MAX_VALUE); // cannot exceed array length
140 		setStreamFileThreshold((int) sft);
141 
142 		setStreamPackBufferSize(rc.getInt(
143 				CONFIG_CORE_SECTION,
144 				CONFIG_DFS_SECTION,
145 				CONFIG_KEY_STREAM_BUFFER,
146 				getStreamPackBufferSize()));
147 		return this;
148 	}
149 }