1 8 9 package com.sleepycat.je; 10 11 import java.util.Enumeration ; 12 import java.util.Iterator ; 13 import java.util.Properties ; 14 15 import com.sleepycat.je.config.ConfigParam; 16 import com.sleepycat.je.config.EnvironmentParams; 17 import com.sleepycat.je.dbi.DbConfigManager; 18 import com.sleepycat.je.dbi.EnvironmentImpl; 19 20 24 public class EnvironmentMutableConfig implements Cloneable { 25 26 30 private boolean txnNoSync = false; 31 private boolean txnWriteNoSync = false; 32 33 39 protected long cacheSize; 40 41 45 protected Properties props; 46 47 50 private boolean loadPropertyFile = true; 51 52 58 boolean validateParams = true; 59 60 64 public EnvironmentMutableConfig() { 65 props = new Properties (); 66 } 67 68 71 EnvironmentMutableConfig(Properties properties) 72 throws IllegalArgumentException { 73 74 DbConfigManager.validateProperties(properties, 75 false, this.getClass().getName()); 77 78 props = new Properties (); 79 props.putAll(properties); 80 } 81 82 86 public void setTxnNoSync(boolean noSync) { 87 txnNoSync = noSync; 88 } 89 90 94 public boolean getTxnNoSync() { 95 return txnNoSync; 96 } 97 98 102 public void setTxnWriteNoSync(boolean writeNoSync) { 103 txnWriteNoSync = writeNoSync; 104 } 105 106 110 public boolean getTxnWriteNoSync() { 111 return txnWriteNoSync; 112 } 113 114 118 public void setCacheSize(long totalBytes) 119 throws IllegalArgumentException { 120 121 DbConfigManager.setVal(props, EnvironmentParams.MAX_MEMORY, 122 Long.toString(totalBytes), validateParams); 123 } 124 125 129 public long getCacheSize() { 130 131 135 return cacheSize; 136 } 137 138 142 public void setCachePercent(int percent) 143 throws IllegalArgumentException { 144 145 DbConfigManager.setVal(props, EnvironmentParams.MAX_MEMORY_PERCENT, 146 Integer.toString(percent), validateParams); 147 } 148 149 153 public int getCachePercent() { 154 155 String val = 156 DbConfigManager.getVal(props, 157 EnvironmentParams.MAX_MEMORY_PERCENT); 158 try { 159 return Integer.parseInt(val); 160 } catch (NumberFormatException e) { 161 throw new IllegalArgumentException 162 ("Cache percent is not a valid integer: " + e.getMessage()); 163 } 164 } 165 166 170 public void setConfigParam(String paramName, String value) 171 throws IllegalArgumentException { 172 173 DbConfigManager.setConfigParam(props, 174 paramName, 175 value, 176 true, 177 validateParams, 178 false ); 179 } 180 181 185 public String getConfigParam(String paramName) 186 throws IllegalArgumentException { 187 188 return DbConfigManager.getConfigParam(props, 189 paramName); 190 } 191 192 195 void setValidateParams(boolean validateParams) { 196 this.validateParams = validateParams; 197 } 198 199 204 void checkImmutablePropsForEquality(EnvironmentMutableConfig passedConfig) 205 throws IllegalArgumentException { 206 207 Properties passedProps = passedConfig.props; 208 Iterator iter = EnvironmentParams.SUPPORTED_PARAMS.keySet().iterator(); 209 while (iter.hasNext()) { 210 String paramName = (String ) iter.next(); 211 ConfigParam param = (ConfigParam) 212 EnvironmentParams.SUPPORTED_PARAMS.get(paramName); 213 assert param != null; 214 if (!param.isMutable()) { 215 String paramVal = props.getProperty(paramName); 216 String useParamVal = passedProps.getProperty(paramName); 217 if ((paramVal != null) ? (!paramVal.equals(useParamVal)) 218 : (useParamVal != null)) { 219 throw new IllegalArgumentException 220 (paramName + " is set to " + 221 useParamVal + 222 " in the config parameter" + 223 " which is incompatible" + 224 " with the value of " + 225 paramVal + " in the" + 226 " underlying environment"); 227 } 228 } 229 } 230 } 231 232 236 protected Object clone() 237 throws CloneNotSupportedException { 238 239 EnvironmentMutableConfig copy = 240 (EnvironmentMutableConfig) super.clone(); 241 copy.props = (Properties ) props.clone(); 242 return copy; 243 } 244 245 249 EnvironmentMutableConfig cloneMutableConfig() { 250 try { 251 EnvironmentMutableConfig copy = (EnvironmentMutableConfig) clone(); 252 253 copy.clearImmutableProps(); 254 return copy; 255 } catch (CloneNotSupportedException willNeverOccur) { 256 return null; 257 } 258 } 259 260 264 void copyHandlePropsTo(EnvironmentMutableConfig other) { 265 other.txnNoSync = txnNoSync; 266 other.txnWriteNoSync = txnWriteNoSync; 267 } 268 269 272 void copyMutablePropsTo(EnvironmentMutableConfig toConfig) { 273 274 Properties toProps = toConfig.props; 275 Enumeration propNames = props.propertyNames(); 276 while (propNames.hasMoreElements()) { 277 String paramName = (String ) propNames.nextElement(); 278 ConfigParam param = (ConfigParam) 279 EnvironmentParams.SUPPORTED_PARAMS.get(paramName); 280 assert param != null; 281 if (param.isMutable()) { 282 String newVal = props.getProperty(paramName); 283 toProps.setProperty(paramName, newVal); 284 } 285 } 286 } 287 288 292 void fillInEnvironmentGeneratedProps(EnvironmentImpl envImpl) { 293 cacheSize = envImpl.getMemoryBudget().getMaxMemory(); 294 } 295 296 299 private void clearImmutableProps() { 300 Enumeration propNames = props.propertyNames(); 301 while (propNames.hasMoreElements()) { 302 String paramName = (String ) propNames.nextElement(); 303 ConfigParam param = (ConfigParam) 304 EnvironmentParams.SUPPORTED_PARAMS.get(paramName); 305 assert param != null; 306 if (!param.isMutable()) { 307 props.remove(paramName); 308 } 309 } 310 } 311 312 Properties getProps() { 313 return props; 314 } 315 316 319 void setLoadPropertyFile(boolean loadPropertyFile) { 320 this.loadPropertyFile = loadPropertyFile; 321 } 322 323 326 boolean getLoadPropertyFile() { 327 return loadPropertyFile; 328 } 329 330 333 int getNumExplicitlySetParams() { 334 return props.size(); 335 } 336 337 public String toString() { 338 return props.toString(); 339 } 340 } 341 | Popular Tags |