~ubuntu-branches/ubuntu/trusty/ehcache/trusty

« back to all changes in this revision

Viewing changes to src/main/java/net/sf/ehcache/store/disk/DiskStore.java

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg
  • Date: 2013-05-06 14:53:07 UTC
  • mfrom: (1.1.7) (2.1.8 sid)
  • Revision ID: package-import@ubuntu.com-20130506145307-v5bhw5yu70re00l3
Tags: 2.6.7-1
* Team upload.
* New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/**
2
 
 *  Copyright 2003-2010 Terracotta, Inc.
 
2
 *  Copyright Terracotta, Inc.
3
3
 *
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.
70
70
 */
71
71
public final class DiskStore extends AbstractStore implements TierableStore, PoolableStore, StripedReadWriteLockProvider {
72
72
 
73
 
    /**
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.
78
 
     */
79
 
    public static final String AUTO_DISK_PATH_DIRECTORY_PREFIX = "ehcache_auto_created";
80
 
 
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;
101
94
 
102
95
    private volatile CacheLockProvider lockProvider;
103
96
    private volatile Set<Object> keySet;
115
108
 
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());
119
112
        }
120
113
 
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;
126
 
    }
127
 
 
128
 
    /**
129
 
     * Wait until all elements have been written to disk
130
 
     *
131
 
     * @throws InterruptedException if the thread was interrupted while waiting
132
 
     * @param delayInMs the maximum time to wait, in milliseconds
133
 
     */
134
 
    void waitUntilEverythingGotFlushedToDisk(long delayInMs) throws InterruptedException {
135
 
        int iterations = (int) (delayInMs / SLEEP_INTERVAL_MS);
136
 
 
137
 
        for (Segment segment : segments) {
138
 
            int count = 0;
139
 
            while (segment.countOnHeap() != 0) {
140
 
                Thread.sleep(SLEEP_INTERVAL_MS);
141
 
                count++;
142
 
 
143
 
                if (count > iterations) {
144
 
                    throw new CacheException(delayInMs + " ms delay expired");
145
 
                }
146
 
            }
147
 
        }
 
119
        this.persistent = cache.getCacheConfiguration().isDiskPersistent();
148
120
    }
149
121
 
150
122
    /**
151
123
     * Creates a persitent-to-disk store for the given cache, using the given disk path.
152
124
     *
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
158
129
     */
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");
 
133
        }
 
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));
163
137
        return store;
168
142
     * Heap and disk usage are not tracked by the returned store.
169
143
     *
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
173
146
     */
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());
176
149
    }
177
150
 
178
151
    /**
198
171
 
199
172
 
200
173
    /**
201
 
     * Generates a unique directory name for use in automatically creating a diskStorePath where there is a conflict.
202
 
     *
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
205
 
     */
206
 
    public static String generateUniqueDirectory() {
207
 
        return DiskStore.AUTO_DISK_PATH_DIRECTORY_PREFIX + "_" + System.currentTimeMillis();
208
 
    }
209
 
 
210
 
    /**
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
459
422
    /**
460
423
     * {@inheritDoc}
461
424
     */
462
 
    public boolean removeIfTierNotPinned(final Object key) {
 
425
    public boolean removeIfNotPinned(final Object key) {
463
426
        return !tierPinned && remove(key) != null;
464
427
    }
465
428
 
581
544
            segmentFor(hash).removeNoReturn(key, hash);
582
545
        }
583
546
    }
584
 
    
 
547
 
 
548
    /**
 
549
     * {@inheritDoc}
 
550
     */
 
551
    public boolean isTierPinned() {
 
552
        return tierPinned;
 
553
    }
 
554
 
 
555
    /**
 
556
     * {@inheritDoc}
 
557
     */
 
558
    public Set getPresentPinnedKeys() {
 
559
        return Collections.emptySet();
 
560
    }
 
561
 
 
562
    /**
 
563
     * {@inheritDoc}
 
564
     */
 
565
    public boolean isPersistent() {
 
566
        return persistent;
 
567
    }
 
568
 
585
569
    /**
586
570
     * {@inheritDoc}
587
571
     */