001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: EnvironmentMutableConfig.java,v 1.29.2.3 2008/01/07 15:14:08 cwl Exp $
007: */
008:
009: package com.sleepycat.je;
010:
011: import java.util.Enumeration;
012: import java.util.Iterator;
013: import java.util.Properties;
014:
015: import com.sleepycat.je.config.ConfigParam;
016: import com.sleepycat.je.config.EnvironmentParams;
017: import com.sleepycat.je.dbi.DbConfigManager;
018: import com.sleepycat.je.dbi.EnvironmentImpl;
019:
020: /**
021: * Javadoc for this public class is generated
022: * via the doc templates in the doc_src directory.
023: */
024: public class EnvironmentMutableConfig implements Cloneable {
025:
026: /*
027: * Change copyHandlePropsTo and Environment.copyToHandleConfig
028: * when adding fields here.
029: */
030: private boolean txnNoSync = false;
031: private boolean txnWriteNoSync = false;
032:
033: /*
034: * Cache size is a category of property that is calculated within the
035: * environment. It is only supplied when returning the cache size to the
036: * application and never used internally; internal code directly checks
037: * with the MemoryBudget class;
038: */
039: protected long cacheSize;
040:
041: /**
042: * Note that in the implementation we choose not to extend Properties
043: * in order to keep the configuration type safe.
044: */
045: protected Properties props;
046:
047: /**
048: * For unit testing, to prevent loading of je.properties.
049: */
050: private boolean loadPropertyFile = true;
051:
052: /**
053: * Internal boolean that says whether or not to validate params. Setting
054: * it to false means that parameter value validatation won't be performed
055: * during setVal() calls. Only should be set to false by unit tests using
056: * DbInternal.
057: */
058: boolean validateParams = true;
059:
060: /**
061: * Javadoc for this public method is generated via
062: * the doc templates in the doc_src directory.
063: */
064: public EnvironmentMutableConfig() {
065: props = new Properties();
066: }
067:
068: /**
069: * Used by EnvironmentConfig to construct from properties.
070: */
071: EnvironmentMutableConfig(Properties properties)
072: throws IllegalArgumentException {
073:
074: DbConfigManager.validateProperties(properties, false, // forReplication
075: this .getClass().getName());
076: /* For safety, copy the passed in properties. */
077: props = new Properties();
078: props.putAll(properties);
079: }
080:
081: /**
082: * Javadoc for this public method is generated via
083: * the doc templates in the doc_src directory.
084: */
085: public void setTxnNoSync(boolean noSync) {
086: txnNoSync = noSync;
087: }
088:
089: /**
090: * Javadoc for this public method is generated via
091: * the doc templates in the doc_src directory.
092: */
093: public boolean getTxnNoSync() {
094: return txnNoSync;
095: }
096:
097: /**
098: * Javadoc for this public method is generated via
099: * the doc templates in the doc_src directory.
100: */
101: public void setTxnWriteNoSync(boolean writeNoSync) {
102: txnWriteNoSync = writeNoSync;
103: }
104:
105: /**
106: * Javadoc for this public method is generated via
107: * the doc templates in the doc_src directory.
108: */
109: public boolean getTxnWriteNoSync() {
110: return txnWriteNoSync;
111: }
112:
113: /**
114: * Javadoc for this public method is generated via
115: * the doc templates in the doc_src directory.
116: */
117: public void setCacheSize(long totalBytes)
118: throws IllegalArgumentException {
119:
120: DbConfigManager.setVal(props, EnvironmentParams.MAX_MEMORY,
121: Long.toString(totalBytes), validateParams);
122: }
123:
124: /**
125: * Javadoc for this public method is generated via
126: * the doc templates in the doc_src directory.
127: */
128: public long getCacheSize() {
129:
130: /*
131: * CacheSize is filled in from the EnvironmentImpl by way of
132: * copyHandleProps.
133: */
134: return cacheSize;
135: }
136:
137: /**
138: * Javadoc for this public method is generated via
139: * the doc templates in the doc_src directory.
140: */
141: public void setCachePercent(int percent)
142: throws IllegalArgumentException {
143:
144: DbConfigManager.setVal(props,
145: EnvironmentParams.MAX_MEMORY_PERCENT, Integer
146: .toString(percent), validateParams);
147: }
148:
149: /**
150: * Javadoc for this public method is generated via
151: * the doc templates in the doc_src directory.
152: */
153: public int getCachePercent() {
154:
155: String val = DbConfigManager.getVal(props,
156: EnvironmentParams.MAX_MEMORY_PERCENT);
157: try {
158: return Integer.parseInt(val);
159: } catch (NumberFormatException e) {
160: throw new IllegalArgumentException(
161: "Cache percent is not a valid integer: "
162: + e.getMessage());
163: }
164: }
165:
166: /**
167: * Javadoc for this public method is generated via
168: * the doc templates in the doc_src directory.
169: */
170: public void setConfigParam(String paramName, String value)
171: throws IllegalArgumentException {
172:
173: DbConfigManager.setConfigParam(props, paramName, value, true, /* require mutability. */
174: validateParams, false /* forReplication */);
175: }
176:
177: /**
178: * Javadoc for this public method is generated via
179: * the doc templates in the doc_src directory.
180: */
181: public String getConfigParam(String paramName)
182: throws IllegalArgumentException {
183:
184: return DbConfigManager.getConfigParam(props, paramName);
185: }
186:
187: /*
188: * Helpers
189: */
190: void setValidateParams(boolean validateParams) {
191: this .validateParams = validateParams;
192: }
193:
194: /**
195: * Check that the immutable values in the environment config used to open
196: * an environment match those in the config object saved by the underlying
197: * shared EnvironmentImpl.
198: */
199: void checkImmutablePropsForEquality(
200: EnvironmentMutableConfig passedConfig)
201: throws IllegalArgumentException {
202:
203: Properties passedProps = passedConfig.props;
204: Iterator iter = EnvironmentParams.SUPPORTED_PARAMS.keySet()
205: .iterator();
206: while (iter.hasNext()) {
207: String paramName = (String) iter.next();
208: ConfigParam param = (ConfigParam) EnvironmentParams.SUPPORTED_PARAMS
209: .get(paramName);
210: assert param != null;
211: if (!param.isMutable()) {
212: String paramVal = props.getProperty(paramName);
213: String useParamVal = passedProps.getProperty(paramName);
214: if ((paramVal != null) ? (!paramVal.equals(useParamVal))
215: : (useParamVal != null)) {
216: throw new IllegalArgumentException(paramName
217: + " is set to " + useParamVal
218: + " in the config parameter"
219: + " which is incompatible"
220: + " with the value of " + paramVal
221: + " in the" + " underlying environment");
222: }
223: }
224: }
225: }
226:
227: /**
228: * Overrides Object.clone() to clone all properties, used by this class and
229: * EnvironmentConfig.
230: */
231: protected Object clone() throws CloneNotSupportedException {
232:
233: EnvironmentMutableConfig copy = (EnvironmentMutableConfig) super
234: .clone();
235: copy.props = (Properties) props.clone();
236: return copy;
237: }
238:
239: /**
240: * Used by Environment to create a copy of the application
241: * supplied configuration. Done this way to provide non-public cloning.
242: */
243: EnvironmentMutableConfig cloneMutableConfig() {
244: try {
245: EnvironmentMutableConfig copy = (EnvironmentMutableConfig) clone();
246: /* Remove all immutable properties. */
247: copy.clearImmutableProps();
248: return copy;
249: } catch (CloneNotSupportedException willNeverOccur) {
250: return null;
251: }
252: }
253:
254: /**
255: * Copies the per-handle properties of this object to the given config
256: * object.
257: */
258: void copyHandlePropsTo(EnvironmentMutableConfig other) {
259: other.txnNoSync = txnNoSync;
260: other.txnWriteNoSync = txnWriteNoSync;
261: }
262:
263: /**
264: * Copies all mutable props to the given config object.
265: */
266: void copyMutablePropsTo(EnvironmentMutableConfig toConfig) {
267:
268: Properties toProps = toConfig.props;
269: Enumeration propNames = props.propertyNames();
270: while (propNames.hasMoreElements()) {
271: String paramName = (String) propNames.nextElement();
272: ConfigParam param = (ConfigParam) EnvironmentParams.SUPPORTED_PARAMS
273: .get(paramName);
274: assert param != null;
275: if (param.isMutable()) {
276: String newVal = props.getProperty(paramName);
277: toProps.setProperty(paramName, newVal);
278: }
279: }
280: }
281:
282: /**
283: * Fill in the properties calculated by the environment to the given
284: * config object.
285: */
286: void fillInEnvironmentGeneratedProps(EnvironmentImpl envImpl) {
287: cacheSize = envImpl.getMemoryBudget().getMaxMemory();
288: }
289:
290: /**
291: * Removes all immutable props.
292: */
293: private void clearImmutableProps() {
294: Enumeration propNames = props.propertyNames();
295: while (propNames.hasMoreElements()) {
296: String paramName = (String) propNames.nextElement();
297: ConfigParam param = (ConfigParam) EnvironmentParams.SUPPORTED_PARAMS
298: .get(paramName);
299: assert param != null;
300: if (!param.isMutable()) {
301: props.remove(paramName);
302: }
303: }
304: }
305:
306: Properties getProps() {
307: return props;
308: }
309:
310: /**
311: * For unit testing, to prevent loading of je.properties.
312: */
313: void setLoadPropertyFile(boolean loadPropertyFile) {
314: this .loadPropertyFile = loadPropertyFile;
315: }
316:
317: /**
318: * For unit testing, to prevent loading of je.properties.
319: */
320: boolean getLoadPropertyFile() {
321: return loadPropertyFile;
322: }
323:
324: /**
325: * Testing support
326: */
327: int getNumExplicitlySetParams() {
328: return props.size();
329: }
330:
331: public String toString() {
332: return props.toString();
333: }
334: }
|