001package org.cache2k; 002 003/* 004 * #%L 005 * cache2k API only package 006 * %% 007 * Copyright (C) 2000 - 2016 headissue GmbH, Munich 008 * %% 009 * This program is free software: you can redistribute it and/or modify 010 * it under the terms of the GNU General Public License as 011 * published by the Free Software Foundation, either version 3 of the 012 * License, or (at your option) any later version. 013 * 014 * This program is distributed in the hope that it will be useful, 015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 017 * GNU General Public License for more details. 018 * 019 * You should have received a copy of the GNU General Public 020 * License along with this program. If not, see 021 * <http://www.gnu.org/licenses/gpl-3.0.html>. 022 * #L% 023 */ 024 025import org.cache2k.spi.SingleProviderResolver; 026import org.cache2k.spi.StorageImplementation; 027import org.cache2k.storage.SimpleSingleFileStorage; 028 029import java.util.concurrent.TimeUnit; 030 031/** 032 * @author Jens Wilke; created: 2014-04-18 033 */ 034public class StorageConfiguration<EX> { 035 036 boolean reliable; 037 038 boolean purgeOnStartup; 039 040 boolean ignoreModifications; 041 042 Class<? extends StorageImplementation> implementation = SimpleSingleFileStorage.class; 043 044 boolean passivation = false; 045 046 boolean readOnly = false; 047 048 String location; 049 050 String storageName; 051 052 int entryCapacity = -1; 053 054 int bytesCapacity; 055 056 long syncInterval = 7 * 1000; 057 058 EX extraConfiguration; 059 060 boolean flushOnClose = false; 061 062 /** 063 * @see Builder#reliable 064 */ 065 public boolean isReliable() { 066 return reliable; 067 } 068 069 /** 070 * @see Builder#reliable 071 */ 072 public void setReliable(boolean reliable) { 073 this.reliable = reliable; 074 } 075 076 /** 077 * @see Builder#purgeOnStartup 078 */ 079 public void setPurgeOnStartup(boolean purgeOnStartup) { 080 this.purgeOnStartup = purgeOnStartup; 081 } 082 083 /** 084 * @see Builder#purgeOnStartup 085 */ 086 public boolean isPurgeOnStartup() { 087 return purgeOnStartup; 088 } 089 090 public void setIgnoreModifications(boolean ignoreModifications) { 091 this.ignoreModifications = ignoreModifications; 092 } 093 094 public void setImplementation(Class<? extends StorageImplementation> c) { 095 implementation = c; 096 } 097 098 public void setPassivation(boolean passivation) { 099 this.passivation = passivation; 100 } 101 102 public void setLocation(String location) { 103 this.location = location; 104 } 105 106 /** 107 * Capacity limit for the number of entries. Default is -1, capacity is 108 * limited by other means. 109 */ 110 public void setEntryCapacity(int entryCapacity) { 111 this.entryCapacity = entryCapacity; 112 } 113 114 public void setBytesCapacity(int bytesCapacity) { 115 this.bytesCapacity = bytesCapacity; 116 } 117 118 public void setSyncInterval(long v, TimeUnit u) { 119 this.syncInterval = u.toMillis(v); 120 } 121 122 public boolean isIgnoreModifications() { 123 return ignoreModifications; 124 } 125 126 public Class<?> getImplementation() { 127 return implementation; 128 } 129 130 public boolean isPassivation() { 131 return passivation; 132 } 133 134 public String getLocation() { 135 return location; 136 } 137 138 public int getEntryCapacity() { 139 return entryCapacity; 140 } 141 142 public int getBytesCapacity() { 143 return bytesCapacity; 144 } 145 146 /** 147 * Sync interval in milliseconds. 148 */ 149 public long getFlushIntervalMillis() { 150 return syncInterval; 151 } 152 153 public EX getExtraConfiguration() { 154 return extraConfiguration; 155 } 156 157 public void setExtraConfiguration(EX extraConfiguration) { 158 this.extraConfiguration = extraConfiguration; 159 } 160 161 public boolean isFlushOnClose() { 162 return flushOnClose; 163 } 164 165 /** 166 * When closing flush data, if the storage does need this. 167 * The parameter is false by default. We prefer no delay on closing. 168 */ 169 public void setFlushOnClose(boolean f) { 170 this.flushOnClose = f; 171 } 172 173 public String getStorageName() { 174 return storageName; 175 } 176 177 public void setStorageName(String v) { 178 storageName = v; 179 } 180 181 public boolean isReadOnly() { 182 return readOnly; 183 } 184 185 public void setReadOnly(boolean readOnly) { 186 this.readOnly = readOnly; 187 } 188 189 public static class Builder<K, T, OPT_EXTRA_CONFIG> 190 extends BaseAnyBuilder<K, T, StorageConfiguration> { 191 192 private StorageConfiguration<Object> config = new StorageConfiguration(); 193 private AnyBuilder<K, T, ?> extraConfigurationBuilder = null; 194 195 /** 196 * Only store entries in the storage that don't live in the 197 * memory any more. E.g. when an entry gets evicted it is 198 * stored. 199 */ 200 public Builder<K, T, OPT_EXTRA_CONFIG> passivation(boolean f) { 201 config.passivation = f; 202 return this; 203 } 204 205 /** 206 * False means single storage errors may be ignored. 207 * True will propagate errors and flush the contents on shutdown. 208 */ 209 public Builder<K, T, OPT_EXTRA_CONFIG> reliable(boolean f) { 210 config.reliable = f; 211 return this; 212 } 213 214 public Builder<K, T, OPT_EXTRA_CONFIG> purgeOnStartup(boolean f) { 215 config.purgeOnStartup = f; 216 return this; 217 } 218 219 /** 220 * Flush contents on close. The default is off. 221 * 222 * <p>All defaults will be towards a "cache alike" behavior, not a 223 * reliable storage behavior. 224 */ 225 public Builder<K, T, OPT_EXTRA_CONFIG> flushOnClose(boolean f) { 226 config.flushOnClose = f; 227 return this; 228 } 229 230 /** 231 * Switch storage to read only mode, e.g. to examine the contents. 232 * This parameter is not supported by all storage implementations. 233 */ 234 public Builder<K, T, OPT_EXTRA_CONFIG> readOnly(boolean f) { 235 config.readOnly = f; 236 return this; 237 } 238 239 public Builder<K, T, OPT_EXTRA_CONFIG> location(String s) { 240 config.location = s; 241 return this; 242 } 243 244 public Builder<K, T, OPT_EXTRA_CONFIG> entryCapacity(int v) { 245 config.entryCapacity = v; 246 return this; 247 } 248 249 public Builder<K, T, OPT_EXTRA_CONFIG> bytesCapacity(int v) { 250 config.bytesCapacity = v; 251 return this; 252 } 253 254 public Builder<K, T, OPT_EXTRA_CONFIG> syncInterval(int v, TimeUnit u) { 255 config.syncInterval = (int) u.toMillis(v); 256 return this; 257 } 258 259 public Builder<K, T, OPT_EXTRA_CONFIG> storageName(String s) { 260 config.storageName = s; 261 return this; 262 } 263 264 265 public <EXTRA_CONFIG_BUILDER extends AnyBuilder<K, T, ?>> Builder<K, T, EXTRA_CONFIG_BUILDER> implementation( 266 Class<? extends StorageImplementation<EXTRA_CONFIG_BUILDER>> c) { 267 StorageImplementation<EXTRA_CONFIG_BUILDER> imp = SingleProviderResolver.getInstance().resolve(c); 268 config.setImplementation(c); 269 extraConfigurationBuilder = imp.createConfigurationBuilder(root()); 270 return (Builder<K, T, EXTRA_CONFIG_BUILDER>) this; 271 } 272 273 public <EXTRA_CONFIG_BUILDER extends AnyBuilder<K, T, ?>> EXTRA_CONFIG_BUILDER extra( 274 Class<? extends StorageImplementation<EXTRA_CONFIG_BUILDER>> c) { 275 StorageImplementation<EXTRA_CONFIG_BUILDER> imp = SingleProviderResolver.getInstance().resolve(c); 276 config.setImplementation(c); 277 extraConfigurationBuilder = imp.createConfigurationBuilder(root()); 278 return (EXTRA_CONFIG_BUILDER) extraConfigurationBuilder; 279 } 280 281 public OPT_EXTRA_CONFIG extra() { 282 if (extraConfigurationBuilder == null) { 283 throw new IllegalArgumentException("storage implementation has no extra configuration"); 284 } 285 return (OPT_EXTRA_CONFIG) extraConfigurationBuilder; 286 } 287 288 @Override 289 public StorageConfiguration createConfiguration() { 290 if (extraConfigurationBuilder != null) { 291 config.setExtraConfiguration( 292 extraConfigurationBuilder.createConfiguration()); 293 } 294 return config; 295 } 296 297 } 298 299}