2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.translate.v3po.util.cache;
19 import static com.google.common.base.Preconditions.checkNotNull;
21 import com.google.common.base.Optional;
22 import io.fd.honeycomb.translate.ModificationCache;
23 import io.fd.honeycomb.translate.v3po.util.cache.exceptions.check.DumpCheckFailedException;
24 import io.fd.honeycomb.translate.v3po.util.cache.exceptions.execution.DumpExecutionFailedException;
25 import io.fd.honeycomb.translate.v3po.util.cache.noop.NoopDumpPostProcessingFunction;
26 import javax.annotation.Nonnull;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * Manager responsible for returning Data object dumps<br> either from cache or by invoking specified {@link
34 public final class DumpCacheManager<T, U> {
36 private static final Logger LOG = LoggerFactory.getLogger(DumpCacheManager.class);
38 private final EntityDumpExecutor<T, U> dumpExecutor;
39 private final EntityDumpNonEmptyCheck<T> dumpNonEmptyCheck;
40 private final EntityDumpPostProcessingFunction<T> postProcessor;
42 private DumpCacheManager(DumpCacheManagerBuilder<T, U> builder) {
43 this.dumpExecutor = builder.dumpExecutor;
44 this.dumpNonEmptyCheck = builder.dumpNonEmptyCheck;
45 this.postProcessor = builder.postProcessingFunction;
49 * Returns {@link Optional<T>} of dump
51 public Optional<T> getDump(@Nonnull String entityKey, @Nonnull ModificationCache cache, final U dumpParams)
52 throws DumpExecutionFailedException {
54 // this key binding to every log has its logic ,because every customizer have its own cache manager and if
55 // there is need for debugging/fixing some complex call with a lot of data,you can get lost in those logs
56 LOG.debug("Loading dump for KEY[{}]", entityKey);
58 T dump = (T) cache.get(entityKey);
61 LOG.debug("Dump for KEY[{}] not present in cache,invoking dump executor", entityKey);
62 // binds and execute dump to be thread-save
63 dump = dumpExecutor.executeDump(dumpParams);
65 // TODO (HONEYCOMB-210): remove empty check (empty dump is normal state, special handling is not needed)
67 dumpNonEmptyCheck.assertNotEmpty(dump);
68 } catch (DumpCheckFailedException e) {
69 LOG.debug("Dump for KEY[{}] has been resolved as empty: {}", entityKey, e.getMessage());
70 return Optional.absent();
73 // no need to check if post processor active,if wasn't set,default no-op will be used
74 LOG.debug("Post-processing dump for KEY[{}]", entityKey);
75 dump = postProcessor.apply(dump);
77 LOG.debug("Caching dump for KEY[{}]", entityKey);
78 cache.put(entityKey, dump);
79 return Optional.of(dump);
81 return Optional.of(dump);
85 public static final class DumpCacheManagerBuilder<T, U> {
87 private EntityDumpExecutor<T, U> dumpExecutor;
88 private EntityDumpNonEmptyCheck<T> dumpNonEmptyCheck;
89 private EntityDumpPostProcessingFunction<T> postProcessingFunction;
91 public DumpCacheManagerBuilder() {
92 // for cases when user does not set specific post-processor
93 postProcessingFunction = new NoopDumpPostProcessingFunction<T>();
96 public DumpCacheManagerBuilder<T, U> withExecutor(@Nonnull EntityDumpExecutor<T, U> executor) {
97 this.dumpExecutor = executor;
101 public DumpCacheManagerBuilder<T, U> withNonEmptyPredicate(@Nonnull EntityDumpNonEmptyCheck<T> check) {
102 this.dumpNonEmptyCheck = check;
106 public DumpCacheManagerBuilder<T, U> withPostProcessingFunction(
107 EntityDumpPostProcessingFunction<T> postProcessingFunction) {
108 this.postProcessingFunction = postProcessingFunction;
112 public DumpCacheManager<T, U> build() {
113 checkNotNull(dumpExecutor, "Dump executor cannot be null");
114 checkNotNull(dumpNonEmptyCheck, "Dump verifier cannot be null");
115 checkNotNull(postProcessingFunction,
116 "Dump post-processor cannot be null cannot be null, default implementation is used if its not set");
118 return new DumpCacheManager<>(this);