2
* Copyright 2003-2010 Terracotta, Inc.
2
* Copyright Terracotta, Inc.
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
71
71
public final class DiskStore extends AbstractStore implements TierableStore, PoolableStore, StripedReadWriteLockProvider {
74
* If the CacheManager needs to resolve a conflict with the disk path, it will create a
75
* subdirectory in the given disk path with this prefix followed by a number. The presence of this
76
* name is used to determined whether it makes sense for a persistent DiskStore to be loaded. Loading
77
* persistent DiskStores will only have useful semantics where the diskStore path has not changed.
79
public static final String AUTO_DISK_PATH_DIRECTORY_PREFIX = "ehcache_auto_created";
81
73
private static final int FFFFCD7D = 0xffffcd7d;
82
74
private static final int FIFTEEN = 15;
83
75
private static final int TEN = 10;
98
90
private final int segmentShift;
99
91
private final AtomicReference<Status> status = new AtomicReference<Status>(Status.STATUS_UNINITIALISED);
100
92
private final boolean tierPinned;
93
private final boolean persistent;
102
95
private volatile CacheLockProvider lockProvider;
103
96
private volatile Set<Object> keySet;
116
109
for (int i = 0; i < this.segments.length; ++i) {
117
110
this.segments[i] = new Segment(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR,
118
disk, cache.getCacheConfiguration(), onHeapPoolAccessor, onDiskPoolAccessor);
111
disk, cache.getCacheConfiguration(), onHeapPoolAccessor, onDiskPoolAccessor, cache.getCacheEventNotificationService());
121
114
this.disk = disk;
123
116
this.status.set(Status.STATUS_ALIVE);
124
117
this.tierPinned = cache.getCacheConfiguration().getPinningConfiguration() != null &&
125
118
cache.getCacheConfiguration().getPinningConfiguration().getStore() == PinningConfiguration.Store.INCACHE;
129
* Wait until all elements have been written to disk
131
* @throws InterruptedException if the thread was interrupted while waiting
132
* @param delayInMs the maximum time to wait, in milliseconds
134
void waitUntilEverythingGotFlushedToDisk(long delayInMs) throws InterruptedException {
135
int iterations = (int) (delayInMs / SLEEP_INTERVAL_MS);
137
for (Segment segment : segments) {
139
while (segment.countOnHeap() != 0) {
140
Thread.sleep(SLEEP_INTERVAL_MS);
143
if (count > iterations) {
144
throw new CacheException(delayInMs + " ms delay expired");
119
this.persistent = cache.getCacheConfiguration().isDiskPersistent();
151
123
* Creates a persitent-to-disk store for the given cache, using the given disk path.
153
125
* @param cache cache that fronts this store
154
* @param diskStorePath disk path to store data in
155
126
* @param onHeapPool pool to track heap usage
156
127
* @param onDiskPool pool to track disk usage
157
128
* @return a fully initialized store
159
public static DiskStore create(Ehcache cache, String diskStorePath, Pool onHeapPool, Pool onDiskPool) {
160
DiskStorageFactory disk = new DiskStorageFactory(cache, diskStorePath, cache.getCacheEventNotificationService());
130
public static DiskStore create(Ehcache cache, Pool onHeapPool, Pool onDiskPool) {
131
if (cache.getCacheManager() == null) {
132
throw new CacheException("Can't create diskstore without a cache manager");
134
DiskStorageFactory disk = new DiskStorageFactory(cache, cache.getCacheEventNotificationService());
161
135
DiskStore store = new DiskStore(disk, cache, onHeapPool, onDiskPool);
162
136
cache.getCacheConfiguration().addConfigurationListener(new CacheConfigurationListenerAdapter(disk, onDiskPool));
168
142
* Heap and disk usage are not tracked by the returned store.
170
144
* @param cache cache that fronts this store
171
* @param diskStorePath disk path to store data in
172
145
* @return a fully initialized store
174
public static DiskStore create(Cache cache, String diskStorePath) {
175
return create(cache, diskStorePath, new UnboundedPool(), new UnboundedPool());
147
public static DiskStore create(Cache cache) {
148
return create(cache, new UnboundedPool(), new UnboundedPool());
201
* Generates a unique directory name for use in automatically creating a diskStorePath where there is a conflict.
203
* @return a path consisting of {@link #AUTO_DISK_PATH_DIRECTORY_PREFIX} followed by "_" followed by the current
204
* time as a long e.g. ehcache_auto_created_1149389837006
206
public static String generateUniqueDirectory() {
207
return DiskStore.AUTO_DISK_PATH_DIRECTORY_PREFIX + "_" + System.currentTimeMillis();
211
174
* Will check whether a Placeholder that failed to flush to disk is lying around
212
175
* If so, it'll try to evict it
213
176
* @param key the key
581
544
segmentFor(hash).removeNoReturn(key, hash);
551
public boolean isTierPinned() {
558
public Set getPresentPinnedKeys() {
559
return Collections.emptySet();
565
public boolean isPersistent() {