Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright 2016 Smoke Turner, LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.smoketurner.notification.application.store;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ExecutionException;
import javax.annotation.Nonnull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.basho.riak.client.api.RiakClient;
import com.basho.riak.client.api.commands.datatypes.Context;
import com.basho.riak.client.api.commands.datatypes.FetchDatatype;
import com.basho.riak.client.api.commands.datatypes.FetchMap;
import com.basho.riak.client.api.commands.datatypes.MapUpdate;
import com.basho.riak.client.api.commands.datatypes.RegisterUpdate;
import com.basho.riak.client.api.commands.datatypes.UpdateMap;
import com.basho.riak.client.api.commands.kv.DeleteValue;
import com.basho.riak.client.core.query.Location;
import com.basho.riak.client.core.query.Namespace;
import com.basho.riak.client.core.query.crdt.types.RiakMap;
import com.basho.riak.client.core.query.crdt.types.RiakRegister;
import com.basho.riak.client.core.util.BinaryValue;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.SharedMetricRegistries;
import com.codahale.metrics.Timer;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableMap;
import com.google.common.primitives.Ints;
import com.smoketurner.notification.api.Rule;
import com.smoketurner.notification.application.exceptions.NotificationStoreException;
import io.dropwizard.util.Duration;
public class RuleStore {
private static final Logger LOGGER = LoggerFactory
.getLogger(RuleStore.class);
private static final String BUCKET_NAME = "rules";
private static final Namespace NAMESPACE = new Namespace("maps",
BUCKET_NAME);
private static final Location LOCATION = new Location(NAMESPACE,
BUCKET_NAME);
private final RiakClient client;
private final LoadingCache> cache;
// timers
private final Timer fetchTimer;
private final Timer storeTimer;
private final Timer deleteTimer;
/**
* Constructor
*
* @param client
* Riak client
* @param cacheTimeout
* Rule cache refresh timeout
*/
public RuleStore(@Nonnull final RiakClient client,
@Nonnull final Duration cacheTimeout) {
final MetricRegistry registry = SharedMetricRegistries
.getOrCreate("default");
this.fetchTimer = registry
.timer(MetricRegistry.name(RuleStore.class, "fetch"));
this.storeTimer = registry
.timer(MetricRegistry.name(RuleStore.class, "store"));
this.deleteTimer = registry
.timer(MetricRegistry.name(RuleStore.class, "delete"));
this.client = Objects.requireNonNull(client);
// set up a cache for the rules
this.cache = CacheBuilder.newBuilder()
.refreshAfterWrite(cacheTimeout.getQuantity(),
cacheTimeout.getUnit())
.build(new CacheLoader>() {
@Override
public Map load(String key)
throws NotificationStoreException {
// all rules are stored under a common key, so we don't
// need to reference it
return fetch().or(Collections.emptyMap());
}
});
}
/**
* Fetch a copy of the rules from the cache.
*
* @return the fetched rules or an empty map of rules
*/
public Map fetchCached() {
try {
return cache.get(BUCKET_NAME);
} catch (ExecutionException e) {
LOGGER.warn("Unable to fetch rules from cache, returning no rules",
e);
return Collections.emptyMap();
}
}
/**
* Fetch the rules from Riak
*
* @return the fetched rules
* @throws NotificationStoreException
* if unable to fetch the rules
*/
public Optional