URL-based sharding with Baratine store:

../../_images/example-sharding.png

Multi-item sharding with Baratine store:

Installation

Prerequisites

Installing Baratine

Download Baratine from http://baratine.io/download and extract it to any directory.

Source Code for the Example

Maven repository

Add the following maven dependency:

<dependencies>
  <dependency>
    <groupId>io.baratine</groupId>
    <artifactId>baratine</artifactId>
    <version>0.10.0</version>
  </dependency>
</dependencies>

ListManagerImpl.java

Create the class ListManagerImpl.java:

package example;

import javax.inject.Inject;
import io.baratine.core.Lookup;
import io.baratine.core.Modify;
import io.baratine.core.Result;
import io.baratine.core.Service;
import io.baratine.store.Store;
import java.util.*;

@Service("public:///list")
public class ListManagerImpl
{
  @Inject @Lookup("store:///list")
  Store<List<String>> _store;

  @OnLookup
  public Object onLookup(String path)
  {
    return new ListItem(path, _store.lookup(path));
  }
}

ListItem.java

Create the class ListItem.java:

package example;

import javax.inject.Inject;
import io.baratine.core.Lookup;
import io.baratine.core.Modify;
import io.baratine.core.Result;
import io.baratine.core.Service;
import io.baratine.store.Store;
import java.util.*;

public class ListItem implements ListService
{
  private final String _path;
  private final Store<List<String>> _store;

  private ArrayList<String> _list;

  ListItem(String path, Store<List<String>> _store)
  {
    _path = path;
  }

  public void get(Result<List<String>> result)
  {
    result.complete(_list);
  }

  @Modify
  public void add(String value)
  {
    _list.add(value);
  }

  @Modify
  public void pop(Result<String> result)
  {
    if (_list.size() > 0) {
      result.complete(_list.remove(0));
    }
    else {
      result.complete(null);
    }
  }

  @OnLoad
  private void onLoad(Result<Boolean> result)
  {
    _list = _store.get(_path, result.from(v->afterLoad(v)));
  }

  private boolean afterLoad(List<String> list)
  {
    if (list != null) {
      _list = list;
    }
    else {
      _list = new ArrayList<>();
    }

    return true;
  }

  @OnSave
  private void onSave()
  {
    _store.put(_path, _list);
  }
}

ListService.java

Create the class ListService.java:

package example;

import io.baratine.core.Result;

public interface ListService
{
  void get(Result<List<String>> result);

  void add(String value);

  void pop(Result<String> result);
}

ListServiceSync.java

Optionally, create the class ListServiceSync.java for Java clients:

package example;

public interface ListServiceSync extends ListService
{
  List<String> get();

  void add(String value);

  String pop();
}

Deploying your service

Compile the class and create a jar named list.jar:

$ mvn clean package

Deploy your new service:

$ bin/baratine deploy list.jar
  deployed list.jar to bfs:///config/pods/pod.cf

Remote Clients

Calling your service from HTTP

Test our service by calling the JSON REST endpoint:

$ curl 'http://localhost:8085/s/pod/list/a?m=add&p0=hello'
{"status":"ok", "value" : null}

$ curl 'http://localhost:8085/s/pod/list/b?m=add&p0=world'
{"status":"ok", "value" : null}

$ curl 'http://localhost:8085/s/pod/list/a?m=get'
{"status":"ok", "value" : ["hello"]}

$ curl 'http://localhost:8085/s/pod/list/b?m=get'
{"status":"ok", "value" : ["world"]}

Calling your service from Java

The Java remote client code:

package example;

import io.baratine.core.Result;
import io.baratine.core.ServiceClient;

public class CounterClient
{
  public static void main(String[] args)
    throws Exception
  {
    String url = "http://127.0.0.1:8085/s/pod";

    try (ServiceClient client = ServiceClient.newClient(url).build()) {
      ListServiceSync listA;
      listA = client.lookup("remote:///list/a")
                          .as(ListServiceSync.class);

      listA.add("hello");
      listA.add("world");

      System.out.println("get: " + listA.get());
      System.out.println("pop: " + listA.pop());
      System.out.println("get: " + listA.get());
    }
  }
}