Sitelet https://github.com/Mindgamesnl/ehcache3/commit/615f6f0bbc7ca6cc190932fbfe9b82a12f311bde
Skip to content

Commit 615f6f0

Browse files
tmesic99chrisdennis
authored andcommitted
Consolidated shared tier/provider logic into abstract superclass
1 parent 91dd624 commit 615f6f0

8 files changed

Lines changed: 133 additions & 249 deletions

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright Terracotta, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.ehcache.impl.internal.store.shared;
18+
19+
import org.ehcache.config.ResourceType;
20+
import org.ehcache.core.spi.service.StatisticsService;
21+
import org.ehcache.spi.service.OptionalServiceDependencies;
22+
import org.ehcache.spi.service.Service;
23+
import org.ehcache.spi.service.ServiceDependencies;
24+
import org.ehcache.spi.service.ServiceProvider;
25+
26+
import java.util.Set;
27+
28+
@ServiceDependencies({SharedStorageProvider.class})
29+
@OptionalServiceDependencies("org.ehcache.core.spi.service.StatisticsService")
30+
public abstract class AbstractSharedTierProvider implements Service {
31+
32+
protected SharedStorageProvider sharedStorage;
33+
protected StatisticsService statisticsService;
34+
35+
@Override
36+
public void start(ServiceProvider<Service> serviceProvider) {
37+
sharedStorage = serviceProvider.getService(SharedStorageProvider.class);
38+
statisticsService = serviceProvider.getService(StatisticsService.class);
39+
}
40+
41+
@Override
42+
public void stop() {
43+
sharedStorage = null;
44+
statisticsService = null;
45+
}
46+
47+
protected void associateStoreStatsWithPartition(Object toAssociate, Object parent) {
48+
if (statisticsService != null) {
49+
statisticsService.registerWithParent(toAssociate, parent);
50+
}
51+
}
52+
53+
protected <T> int rank(Class<T> type, Set<ResourceType<?>> resourceTypes) {
54+
if (resourceTypes.size() == 1) {
55+
ResourceType<?> resourceType = resourceTypes.iterator().next();
56+
if (resourceType instanceof ResourceType.SharedResource && sharedStorage.supports(type, ((ResourceType.SharedResource<?>) resourceType).getResourceType())) {
57+
return 1;
58+
} else {
59+
return 0;
60+
}
61+
} else {
62+
return 0;
63+
}
64+
}
65+
66+
protected static ResourceType.SharedResource<?> assertResourceIsShareable(Set<ResourceType<?>> resourceTypes) {
67+
if (resourceTypes.size() == 1) {
68+
ResourceType<?> resourceType = resourceTypes.iterator().next();
69+
if (resourceType instanceof ResourceType.SharedResource) {
70+
return (ResourceType.SharedResource<?>) resourceType;
71+
} else {
72+
throw new AssertionError();
73+
}
74+
} else {
75+
throw new AssertionError();
76+
}
77+
}
78+
}

‎ehcache-impl/src/main/java/org/ehcache/impl/internal/store/shared/SharedStorage.java‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import java.util.HashMap;
5151
import java.util.HashSet;
5252
import java.util.Map;
53+
import java.util.Objects;
5354
import java.util.Set;
5455

5556
import static org.ehcache.core.config.store.StoreEventSourceConfiguration.DEFAULT_DISPATCHER_CONCURRENCY;
@@ -73,19 +74,17 @@ public class SharedStorage implements Service {
7374
private Store<CompositeValue<?>, CompositeValue<?>> store = null;
7475

7576
public SharedStorage(ResourcePool resourcePool) {
76-
this.resourcePool = resourcePool;
77+
this.resourcePool = Objects.requireNonNull((resourcePool));
7778
}
7879

7980
public void start(ServiceProvider<Service> serviceProvider) {
8081
this.serviceProvider = serviceProvider;
81-
if (resourcePool != null) {
82-
Collection<ServiceConfiguration<?, ?>> serviceConfigs = new HashSet<>();
83-
// above from adjustedServiceConfigs during cache creation in EhcacheManager.createNewEhcache
84-
ClassLoader classLoader = ClassLoading.getDefaultClassLoader();
85-
// above from EhcacheManager: this.cacheManagerClassLoader = config.getClassLoader() != null ? config.getClassLoader() : ClassLoading.getDefaultClassLoader();
86-
CacheLoaderWriter<?, ?> cacheLoaderWriter = null; // placeholder for later implementation
87-
createSharedStore(classLoader, serviceConfigs, cacheLoaderWriter);
88-
}
82+
Collection<ServiceConfiguration<?, ?>> serviceConfigs = new HashSet<>();
83+
// above from adjustedServiceConfigs during cache creation in EhcacheManager.createNewEhcache
84+
ClassLoader classLoader = ClassLoading.getDefaultClassLoader();
85+
// above from EhcacheManager: this.cacheManagerClassLoader = config.getClassLoader() != null ? config.getClassLoader() : ClassLoading.getDefaultClassLoader();
86+
CacheLoaderWriter<?, ?> cacheLoaderWriter = null; // placeholder for later implementation
87+
createSharedStore(classLoader, serviceConfigs, cacheLoaderWriter);
8988
}
9089

9190
public void stop() {

‎ehcache-impl/src/main/java/org/ehcache/impl/internal/store/shared/SharedStorageProvider.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ public <T, U, K, V> U partition(ResourceType<?> resourceType, Store.Configuratio
6060
return storage.get(resourceType).createPartition(storeConfig, partitionFactory);
6161
}
6262

63-
public boolean supports(Class<?> storeageType, ResourceType<?> resourceType) {
63+
public boolean supports(Class<?> storageType, ResourceType<?> resourceType) {
6464
SharedStorage sharedStorage = storage.get(resourceType);
65-
return sharedStorage != null && sharedStorage.supports(storeageType);
65+
return sharedStorage != null && sharedStorage.supports(storageType);
6666
}
6767
}

‎ehcache-impl/src/main/java/org/ehcache/impl/internal/store/shared/authoritative/SharedAuthoritativeTierProvider.java‎

Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -17,68 +17,30 @@
1717
package org.ehcache.impl.internal.store.shared.authoritative;
1818

1919
import org.ehcache.config.ResourceType;
20-
import org.ehcache.core.spi.service.StatisticsService;
2120
import org.ehcache.core.spi.store.Store;
2221
import org.ehcache.core.spi.store.tiering.AuthoritativeTier;
22+
import org.ehcache.impl.internal.store.shared.AbstractSharedTierProvider;
2323
import org.ehcache.impl.internal.store.shared.composites.CompositeValue;
24-
import org.ehcache.impl.internal.store.shared.SharedStorageProvider;
25-
import org.ehcache.spi.service.Service;
2624
import org.ehcache.spi.service.ServiceConfiguration;
27-
import org.ehcache.spi.service.ServiceDependencies;
28-
import org.ehcache.spi.service.ServiceProvider;
2925

3026
import java.util.Collection;
3127
import java.util.Set;
3228

33-
@ServiceDependencies({SharedStorageProvider.class, StatisticsService.class})
34-
public class SharedAuthoritativeTierProvider implements AuthoritativeTier.Provider {
35-
private SharedStorageProvider sharedStorage;
36-
private StatisticsService statisticsService;
37-
38-
@Override
39-
public void start(ServiceProvider<Service> serviceProvider) {
40-
sharedStorage = serviceProvider.getService(SharedStorageProvider.class);
41-
statisticsService = serviceProvider.getService(StatisticsService.class);
42-
}
43-
44-
@Override
45-
public void stop() {
46-
sharedStorage = null;
47-
statisticsService = null;
48-
}
29+
public class SharedAuthoritativeTierProvider extends AbstractSharedTierProvider implements AuthoritativeTier.Provider {
4930

5031
@Override
5132
public int rankAuthority(Set<ResourceType<?>> resourceTypes, Collection<ServiceConfiguration<?, ?>> serviceConfigs) {
52-
if (resourceTypes.size() == 1 && sharedStorage != null) {
53-
ResourceType<?> resourceType = resourceTypes.iterator().next();
54-
if (resourceType instanceof ResourceType.SharedResource && sharedStorage.supports(AuthoritativeTier.class, ((ResourceType.SharedResource<?>) resourceType).getResourceType())) {
55-
return 1;
56-
} else {
57-
return 0;
58-
}
59-
} else {
60-
return 0;
61-
}
33+
return rank(AuthoritativeTier.class, resourceTypes);
6234
}
6335

6436
@Override
6537
public <K, V> AuthoritativeTier<K, V> createAuthoritativeTier(Set<ResourceType<?>> resourceTypes, Store.Configuration<K, V> storeConfig, ServiceConfiguration<?, ?>... serviceConfigs) {
66-
if (resourceTypes.size() == 1) {
67-
ResourceType<?> resourceType = resourceTypes.iterator().next();
68-
if (resourceType instanceof ResourceType.SharedResource) {
69-
return sharedStorage.<AuthoritativeTier<CompositeValue<K>, CompositeValue<V>>, AuthoritativeTier<K, V>, K, V>partition(((ResourceType.SharedResource<?>) resourceType).getResourceType(), storeConfig, (id, store, storage) -> {
70-
AuthoritativeTierPartition<K, V> partition = new AuthoritativeTierPartition<>(id, storeConfig.getKeyType(), storeConfig.getValueType(), store);
71-
if (statisticsService != null) {
72-
statisticsService.registerWithParent(store, partition);
73-
}
74-
return partition;
75-
});
76-
} else {
77-
throw new AssertionError();
78-
}
79-
} else {
80-
throw new AssertionError();
81-
}
38+
ResourceType.SharedResource<?> resourceType = assertResourceIsShareable(resourceTypes);
39+
return sharedStorage.<AuthoritativeTier<CompositeValue<K>, CompositeValue<V>>, AuthoritativeTier<K, V>, K, V>partition(resourceType.getResourceType(), storeConfig, (id, store, storage) -> {
40+
AuthoritativeTierPartition<K, V> partition = new AuthoritativeTierPartition<>(id, storeConfig.getKeyType(), storeConfig.getValueType(), store);
41+
associateStoreStatsWithPartition(store, partition);
42+
return partition;
43+
});
8244
}
8345

8446
@Override

‎ehcache-impl/src/main/java/org/ehcache/impl/internal/store/shared/caching/SharedCachingTierProvider.java‎

Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -17,68 +17,30 @@
1717
package org.ehcache.impl.internal.store.shared.caching;
1818

1919
import org.ehcache.config.ResourceType;
20-
import org.ehcache.core.spi.service.StatisticsService;
2120
import org.ehcache.core.spi.store.Store;
2221
import org.ehcache.core.spi.store.tiering.CachingTier;
22+
import org.ehcache.impl.internal.store.shared.AbstractSharedTierProvider;
2323
import org.ehcache.impl.internal.store.shared.composites.CompositeValue;
24-
import org.ehcache.impl.internal.store.shared.SharedStorageProvider;
25-
import org.ehcache.spi.service.Service;
2624
import org.ehcache.spi.service.ServiceConfiguration;
27-
import org.ehcache.spi.service.ServiceDependencies;
28-
import org.ehcache.spi.service.ServiceProvider;
2925

3026
import java.util.Collection;
3127
import java.util.Set;
3228

33-
@ServiceDependencies({SharedStorageProvider.class, StatisticsService.class})
34-
public class SharedCachingTierProvider implements CachingTier.Provider {
35-
private SharedStorageProvider sharedStorage;
36-
private StatisticsService statisticsService;
37-
38-
@Override
39-
public void start(ServiceProvider<Service> serviceProvider) {
40-
sharedStorage = serviceProvider.getService(SharedStorageProvider.class);
41-
statisticsService = serviceProvider.getService(StatisticsService.class);
42-
}
43-
44-
@Override
45-
public void stop() {
46-
sharedStorage = null;
47-
statisticsService = null;
48-
}
29+
public class SharedCachingTierProvider extends AbstractSharedTierProvider implements CachingTier.Provider {
4930

5031
@Override
5132
public int rankCachingTier(Set<ResourceType<?>> resourceTypes, Collection<ServiceConfiguration<?, ?>> serviceConfigs) {
52-
if (resourceTypes.size() == 1 && sharedStorage != null) {
53-
ResourceType<?> resourceType = resourceTypes.iterator().next();
54-
if (resourceType instanceof ResourceType.SharedResource && sharedStorage.supports(CachingTier.class, ((ResourceType.SharedResource<?>) resourceType).getResourceType())) {
55-
return 1;
56-
} else {
57-
return 0;
58-
}
59-
} else {
60-
return 0;
61-
}
33+
return rank(CachingTier.class, resourceTypes);
6234
}
6335

6436
@Override
6537
public <K, V> CachingTier<K, V> createCachingTier(Set<ResourceType<?>> resourceTypes, Store.Configuration<K, V> storeConfig, ServiceConfiguration<?, ?>... serviceConfigs) {
66-
if (resourceTypes.size() == 1) {
67-
ResourceType<?> resourceType = resourceTypes.iterator().next();
68-
if (resourceType instanceof ResourceType.SharedResource) {
69-
return sharedStorage.<CachingTier<CompositeValue<K>, CompositeValue<V>>, CachingTier<K, V>, K, V>partition(((ResourceType.SharedResource<?>) resourceType).getResourceType(), storeConfig, (id, store, shared) -> {
70-
CachingTierPartition<K, V> partition = new CachingTierPartition<>(id, store, shared.getInvalidationListeners());
71-
if (statisticsService != null) {
72-
statisticsService.registerWithParent(store, partition);
73-
}
74-
return partition;
75-
});
76-
} else {
77-
throw new AssertionError();
78-
}
79-
} else {
80-
throw new AssertionError();
81-
}
38+
ResourceType.SharedResource<?> resourceType = assertResourceIsShareable(resourceTypes);
39+
return sharedStorage.<CachingTier<CompositeValue<K>, CompositeValue<V>>, CachingTier<K, V>, K, V>partition(resourceType.getResourceType(), storeConfig, (id, store, shared) -> {
40+
CachingTierPartition<K, V> partition = new CachingTierPartition<>(id, store, shared.getInvalidationListeners());
41+
associateStoreStatsWithPartition(store, partition);
42+
return partition;
43+
});
8244
}
8345

8446
@Override

‎ehcache-impl/src/main/java/org/ehcache/impl/internal/store/shared/caching/higher/SharedHigherCachingTierProvider.java‎

Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -17,68 +17,30 @@
1717
package org.ehcache.impl.internal.store.shared.caching.higher;
1818

1919
import org.ehcache.config.ResourceType;
20-
import org.ehcache.core.spi.service.StatisticsService;
2120
import org.ehcache.core.spi.store.Store;
2221
import org.ehcache.core.spi.store.tiering.HigherCachingTier;
22+
import org.ehcache.impl.internal.store.shared.AbstractSharedTierProvider;
2323
import org.ehcache.impl.internal.store.shared.composites.CompositeValue;
24-
import org.ehcache.impl.internal.store.shared.SharedStorageProvider;
25-
import org.ehcache.spi.service.Service;
2624
import org.ehcache.spi.service.ServiceConfiguration;
27-
import org.ehcache.spi.service.ServiceDependencies;
28-
import org.ehcache.spi.service.ServiceProvider;
2925

3026
import java.util.Collection;
3127
import java.util.Set;
3228

33-
@ServiceDependencies({SharedStorageProvider.class, StatisticsService.class})
34-
public class SharedHigherCachingTierProvider implements HigherCachingTier.Provider {
35-
private SharedStorageProvider sharedStorage;
36-
private StatisticsService statisticsService;
37-
38-
@Override
39-
public void start(ServiceProvider<Service> serviceProvider) {
40-
sharedStorage = serviceProvider.getService(SharedStorageProvider.class);
41-
statisticsService = serviceProvider.getService(StatisticsService.class);
42-
}
43-
44-
@Override
45-
public void stop() {
46-
sharedStorage = null;
47-
statisticsService = null;
48-
}
29+
public class SharedHigherCachingTierProvider extends AbstractSharedTierProvider implements HigherCachingTier.Provider {
4930

5031
@Override
5132
public int rankHigherCachingTier(Set<ResourceType<?>> resourceTypes, Collection<ServiceConfiguration<?, ?>> serviceConfigs) {
52-
if (resourceTypes.size() == 1 && sharedStorage != null) {
53-
ResourceType<?> resourceType = resourceTypes.iterator().next();
54-
if (resourceType instanceof ResourceType.SharedResource && sharedStorage.supports(HigherCachingTier.class, ((ResourceType.SharedResource<?>) resourceType).getResourceType())) {
55-
return 1;
56-
} else {
57-
return 0;
58-
}
59-
} else {
60-
return 0;
61-
}
33+
return rank(HigherCachingTier.class, resourceTypes);
6234
}
6335

6436
@Override
6537
public <K, V> HigherCachingTier<K, V> createHigherCachingTier(Set<ResourceType<?>> resourceTypes, Store.Configuration<K, V> storeConfig, ServiceConfiguration<?, ?>... serviceConfigs) {
66-
if (resourceTypes.size() == 1) {
67-
ResourceType<?> resourceType = resourceTypes.iterator().next();
68-
if (resourceType instanceof ResourceType.SharedResource) {
69-
return sharedStorage.<HigherCachingTier<CompositeValue<K>, CompositeValue<V>>, HigherCachingTier<K, V>, K, V>partition(((ResourceType.SharedResource<?>) resourceType).getResourceType(), storeConfig, (id, store, shared) -> {
70-
HigherCachingTierPartition<K, V> partition = new HigherCachingTierPartition<>(id, store, shared.getInvalidationListeners());
71-
if (statisticsService != null) {
72-
statisticsService.registerWithParent(store, partition);
73-
}
74-
return partition;
75-
});
76-
} else {
77-
throw new AssertionError();
78-
}
79-
} else {
80-
throw new AssertionError();
81-
}
38+
ResourceType.SharedResource<?> resourceType = assertResourceIsShareable(resourceTypes);
39+
return sharedStorage.<HigherCachingTier<CompositeValue<K>, CompositeValue<V>>, HigherCachingTier<K, V>, K, V>partition(resourceType.getResourceType(), storeConfig, (id, store, shared) -> {
40+
HigherCachingTierPartition<K, V> partition = new HigherCachingTierPartition<>(id, store, shared.getInvalidationListeners());
41+
associateStoreStatsWithPartition(store, partition);
42+
return partition;
43+
});
8244
}
8345

8446
@Override

0 commit comments

Comments
 (0)