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