com.basho.riak.client.api.RiakClient Java Examples

The following examples show how to use com.basho.riak.client.api.RiakClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NotificationApplication.java    From notification with Apache License 2.0 6 votes vote down vote up
/**
 * Get and configure the {@link RuleStore}
 *
 * @param client Riak client
 * @param configuration Notification configuration
 * @return RuleStore
 */
private static RuleStore getRuleStore(
    final RiakClient client, final NotificationConfiguration configuration) {

  if (RULE_STORE.get() != null) {
    return RULE_STORE.get();
  }

  final RuleStore store =
      new RuleStore(
          client,
          configuration.getRuleCacheTimeout(),
          configuration.getRiakTimeout(),
          configuration.getRiakRequestTimeout());
  if (RULE_STORE.compareAndSet(null, store)) {
    return store;
  }
  return getRuleStore(client, configuration);
}
 
Example #2
Source File: NotificationApplication.java    From notification with Apache License 2.0 6 votes vote down vote up
/**
 * Get and configure the {@link CursorStore}
 *
 * @param client Riak client
 * @param configuration Notification configuration
 * @return CursorStore
 */
private static CursorStore getCursorStore(
    final RiakClient client, final NotificationConfiguration configuration) {

  if (CURSOR_STORE.get() != null) {
    return CURSOR_STORE.get();
  }

  final CursorStore store =
      new CursorStore(
          client, configuration.getRiakTimeout(), configuration.getRiakRequestTimeout());
  if (CURSOR_STORE.compareAndSet(null, store)) {
    return store;
  }
  return getCursorStore(client, configuration);
}
 
Example #3
Source File: NotificationStore.java    From notification with Apache License 2.0 6 votes vote down vote up
/**
 * Constructor
 *
 * @param client Riak client
 * @param idGenerator ID Generator
 * @param cursors Cursor data store
 * @param ruleStore Rule data store
 * @param timeout Riak server-side timeout
 * @param requestTimeout Riak client-side timeout
 */
public NotificationStore(
    final RiakClient client,
    final IdGenerator idGenerator,
    final CursorStore cursors,
    final RuleStore ruleStore,
    final Duration timeout,
    final Duration requestTimeout) {

  final MetricRegistry registry = SharedMetricRegistries.getOrCreate("default");
  this.fetchTimer = registry.timer(MetricRegistry.name(NotificationStore.class, "fetch"));
  this.updateTimer = registry.timer(MetricRegistry.name(NotificationStore.class, "store"));
  this.deleteTimer = registry.timer(MetricRegistry.name(NotificationStore.class, "delete"));

  this.client = Objects.requireNonNull(client, "client == null");
  this.idGenerator = Objects.requireNonNull(idGenerator, "idGenerator == null");
  this.cursors = Objects.requireNonNull(cursors, "cursors == null");
  this.ruleStore = Objects.requireNonNull(ruleStore, "ruleStore == null");

  this.timeout =
      Optional.ofNullable(timeout)
          .map(t -> Math.toIntExact(t.toMilliseconds()))
          .orElse(DEFAULT_TIMEOUT_MS);
  this.requestTimeout = Objects.requireNonNull(requestTimeout, "requestTimeout == null");
}
 
Example #4
Source File: CursorStore.java    From notification with Apache License 2.0 6 votes vote down vote up
/**
 * Constructor
 *
 * @param client Riak client
 * @param timeout Riak server-side timeout
 * @param requestTimeout Riak client-side timeout
 */
public CursorStore(
    final RiakClient client, final Duration timeout, final Duration requestTimeout) {

  final MetricRegistry registry = SharedMetricRegistries.getOrCreate("default");
  this.fetchTimer = registry.timer(MetricRegistry.name(CursorStore.class, "fetch"));
  this.storeTimer = registry.timer(MetricRegistry.name(CursorStore.class, "store"));
  this.deleteTimer = registry.timer(MetricRegistry.name(CursorStore.class, "delete"));

  this.client = Objects.requireNonNull(client, "client == null");

  this.timeout =
      Optional.ofNullable(timeout)
          .map(t -> Math.toIntExact(t.toMilliseconds()))
          .orElse(DEFAULT_TIMEOUT_MS);
  this.requestTimeout = Objects.requireNonNull(requestTimeout, "requestTimeout == null");
}
 
Example #5
Source File: RiakStorage.java    From greycat with Apache License 2.0 5 votes vote down vote up
@Override
public void connect(Graph graph, Callback<Boolean> callback) {
    try {
        _graph = graph;
        _client = RiakClient.newClient("localhost:32773", "localhost:32769", "localhost:32771");
        callback.on(true);
    } catch (Exception e) {
        e.printStackTrace();
        callback.on(false);
    }
}
 
Example #6
Source File: NotificationApplication.java    From notification with Apache License 2.0 5 votes vote down vote up
@Override
public void run(final NotificationConfiguration configuration, final Environment environment)
    throws Exception {

  // returns all DateTime objects as ISO8601 strings
  environment.getObjectMapper().configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
  environment.jersey().register(NotificationExceptionMapper.class);
  // adds charset=UTF-8 to the response headers
  environment.jersey().register(CharsetUtf8Filter.class);
  // adds a X-Request-Id response header
  environment.jersey().register(RequestIdFilter.class);
  // adds a X-Runtime response header
  environment.jersey().register(RuntimeFilter.class);
  environment.jersey().register(WebSecurityFilter.class);

  // riak
  final RiakClient client = configuration.getRiak().build();

  // data stores
  final RuleStore ruleStore = getRuleStore(client, configuration);
  final CursorStore cursorStore = getCursorStore(client, configuration);
  final NotificationStore store = getNotificationStore(client, configuration);

  environment.lifecycle().manage(new CursorStoreManager(cursorStore));
  environment.lifecycle().manage(new NotificationStoreManager(store));

  // resources
  environment.jersey().register(new NotificationResource(store));
  environment.jersey().register(new RuleResource(ruleStore));
  environment.jersey().register(new PingResource());
  environment.jersey().register(new VersionResource());
}
 
Example #7
Source File: NotificationApplication.java    From notification with Apache License 2.0 5 votes vote down vote up
/**
 * Get and configure the {@link NotificationStore}
 *
 * @param client Riak client
 * @param configuration Notification configuration
 * @return NotificationStore
 */
private static NotificationStore getNotificationStore(
    final RiakClient client, final NotificationConfiguration configuration) {

  if (NOTIFICATION_STORE.get() != null) {
    return NOTIFICATION_STORE.get();
  }

  // KSUID
  final IdGenerator idGenerator = new IdGenerator();

  final CursorStore cursorStore = getCursorStore(client, configuration);
  final RuleStore ruleStore = getRuleStore(client, configuration);

  final NotificationStore store =
      new NotificationStore(
          client,
          idGenerator,
          cursorStore,
          ruleStore,
          configuration.getRiakTimeout(),
          configuration.getRiakRequestTimeout());
  if (NOTIFICATION_STORE.compareAndSet(null, store)) {
    return store;
  }
  return getNotificationStore(client, configuration);
}
 
Example #8
Source File: NotificationApplication.java    From notification with Apache License 2.0 5 votes vote down vote up
/**
 * Build the GraphQL {@link RuntimeWiring}
 *
 * @param configuration Notification configuration
 * @return the GraphQL runtime wiring
 * @throws Exception if unable to connect to Riak
 */
private static RuntimeWiring buildWiring(NotificationConfiguration configuration)
    throws Exception {

  final RiakClient client = configuration.getRiak().build();
  final NotificationStore store = getNotificationStore(client, configuration);
  final RuleStore ruleStore = getRuleStore(client, configuration);

  final RuntimeWiring wiring =
      RuntimeWiring.newRuntimeWiring()
          .scalar(Scalars.graphQLMapScalar("Map"))
          .type(
              "Query",
              typeWiring ->
                  typeWiring
                      .dataFetcher("notifications", new NotificationDataFetcher(store))
                      .dataFetcher("rules", new RuleDataFetcher(ruleStore)))
          .type(
              "Mutation",
              typeWiring ->
                  typeWiring
                      .dataFetcher("createNotification", new CreateNotificationMutation(store))
                      .dataFetcher("removeNotification", new RemoveNotificationMutation(store))
                      .dataFetcher(
                          "removeAllNotifications", new RemoveAllNotificationsMutation(store))
                      .dataFetcher("createRule", new CreateRuleMutation(ruleStore))
                      .dataFetcher("removeRule", new RemoveRuleMutation(ruleStore))
                      .dataFetcher("removeAllRules", new RemoveAllRulesMutation(ruleStore)))
          .build();

  return wiring;
}
 
Example #9
Source File: RuleStore.java    From notification with Apache License 2.0 5 votes vote down vote up
/**
 * Constructor
 *
 * @param client Riak client
 * @param cacheTimeout Rule cache refresh timeout
 * @param timeout Riak server-side timeout
 * @param requestTimeout Riak client-side timeout
 */
public RuleStore(
    final RiakClient client,
    final Duration cacheTimeout,
    final Duration timeout,
    final Duration requestTimeout) {

  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.cacheMisses = registry.meter(MetricRegistry.name(RuleStore.class, "cache-misses"));

  this.client = Objects.requireNonNull(client, "client == null");

  this.timeout =
      Optional.ofNullable(timeout)
          .map(t -> Math.toIntExact(t.toMilliseconds()))
          .orElse(DEFAULT_TIMEOUT_MS);
  this.requestTimeout = Objects.requireNonNull(requestTimeout, "requestTimeout == null");

  // set up a cache for the rules
  this.cache =
      CacheBuilder.newBuilder()
          .refreshAfterWrite(cacheTimeout.getQuantity(), cacheTimeout.getUnit())
          .build(
              new CacheLoader<String, Map<String, Rule>>() {
                @Override
                public Map<String, Rule> load(String key) throws NotificationStoreException {
                  cacheMisses.mark();

                  // all rules are stored under a common key, so we don't need to reference it
                  return fetch().orElse(Collections.emptyMap());
                }
              });
}
 
Example #10
Source File: RiakEntityStoreMixin.java    From attic-polygene-java with Apache License 2.0 5 votes vote down vote up
@Override
public void activateService() throws Exception
{
    // Load configuration
    configuration.refresh();
    RiakEntityStoreConfiguration config = configuration.get();
    String bucketName = config.bucket().get();
    List<String> hosts = config.hosts().get();

    // Setup Riak Cluster Client
    List<HostAndPort> hostsAndPorts = parseHosts( hosts );
    RiakNode.Builder nodeBuilder = new RiakNode.Builder();
    nodeBuilder = configureNodes( config, nodeBuilder );
    nodeBuilder = configureAuthentication( config, nodeBuilder );
    List<RiakNode> nodes = new ArrayList<>();
    for( HostAndPort host : hostsAndPorts )
    {
        nodes.add( nodeBuilder.withRemoteAddress( host ).build() );
    }
    RiakCluster.Builder clusterBuilder = RiakCluster.builder( nodes );
    clusterBuilder = configureCluster( config, clusterBuilder );

    // Start Riak Cluster
    RiakCluster cluster = clusterBuilder.build();
    cluster.start();
    namespace = new Namespace( bucketName );
    riakClient = new RiakClient( cluster );

    // Initialize Bucket
    riakClient.execute( new StoreBucketProperties.Builder( namespace ).build() );
}
 
Example #11
Source File: RiakEntityStoreMixin.java    From attic-polygene-java with Apache License 2.0 4 votes vote down vote up
@Override
public RiakClient riakClient()
{
    return riakClient;
}
 
Example #12
Source File: RiakFixture.java    From attic-polygene-java with Apache License 2.0 4 votes vote down vote up
RiakFixture( RiakClient client, Namespace namespace )
{
    this.client = client;
    this.namespace = namespace;
}
 
Example #13
Source File: RiakAccessors.java    From attic-polygene-java with Apache License 2.0 votes vote down vote up
RiakClient riakClient();