com.khubla.cbean.riakkv.RiakKVService.java Source code

Java tutorial

Introduction

Here is the source code for com.khubla.cbean.riakkv.RiakKVService.java

Source

/*
 * cBean Copyright 2016, Tom Everett <tom@khubla.com>
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.khubla.cbean.riakkv;

import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.basho.riak.client.api.RiakClient;
import com.basho.riak.client.api.commands.kv.DeleteValue;
import com.basho.riak.client.api.commands.kv.FetchValue;
import com.basho.riak.client.api.commands.kv.StoreValue;
import com.basho.riak.client.core.query.Location;
import com.basho.riak.client.core.query.Namespace;
import com.basho.riak.client.core.query.RiakObject;
import com.basho.riak.client.core.util.BinaryValue;
import com.khubla.cbean.CBeanKey;
import com.khubla.cbean.kvservice.KVService;
import com.khubla.cbean.kvservice.KVServiceException;
import com.khubla.cbean.url.CBeanUrl;

public class RiakKVService implements KVService {
    /**
     * logger
     */
    private static final Logger logger = LoggerFactory.getLogger(RiakKVService.class);
    /**
     * max connections
     */
    private static final int MAX_STORAGESERVICE_CONNECTIONS = 5;
    /**
     * pool of StorageService connections
     */
    private final ObjectPool<RiakClient> riakClientPool;
    /**
     * configuration
     */
    private final CBeanUrl cBeanUrl;

    /**
     * ctor
     */
    public RiakKVService(CBeanUrl cBeanUrl) {
        this.cBeanUrl = cBeanUrl;
        riakClientPool = new GenericObjectPool<RiakClient>(
                new RiakPoolableObjectFactoryImpl(cBeanUrl.getHostName()), MAX_STORAGESERVICE_CONNECTIONS);
    }

    @Override
    public void delete(CBeanKey cBeanKey) throws KVServiceException {
        RiakClient riakClient = null;
        try {
            /*
             * get a pooled riak connection
             */
            riakClient = riakClientPool.borrowObject();
            /*
             * query
             */
            final Location location = new Location(new Namespace(cBeanUrl.getClusterName()), cBeanKey.getKey());
            final DeleteValue dv = new DeleteValue.Builder(location).build();
            riakClient.execute(dv);
            logger.info("Deleted asset '" + cBeanKey.getKey() + "'");
        } catch (final Exception e) {
            logger.info("Unable to delete asset '" + cBeanKey.getKey() + "'");
            throw new KVServiceException(e);
        } finally {
            try {
                if (null != riakClient) {
                    riakClientPool.returnObject(riakClient);
                }
            } catch (final Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public String[] list(CBeanKey cBeanKey) throws KVServiceException {
        throw new KVServiceException("Not Implemented");
    }

    @Override
    public byte[] load(CBeanKey cBeanKey) throws KVServiceException {
        RiakClient riakClient = null;
        try {
            /*
             * get a pooled riak connection
             */
            riakClient = riakClientPool.borrowObject();
            /*
             * query
             */
            final Location location = new Location(new Namespace(cBeanUrl.getClusterName()), cBeanKey.getKey());
            final FetchValue fv = new FetchValue.Builder(location).build();
            final FetchValue.Response response = riakClient.execute(fv);
            /*
             * response
             */
            if (null != response) {
                final RiakObject riakObject = response.getValue(RiakObject.class);
                if (null != riakObject) {
                    final BinaryValue binaryValue = riakObject.getValue();
                    if (null != binaryValue) {
                        logger.info("Loaded asset '" + cBeanKey.getKey() + "'");
                        return binaryValue.getValue();
                    }
                }
            }
            logger.info("Unable to load asset '" + cBeanKey.getKey() + "'");
            return null;
        } catch (final Exception e) {
            throw new KVServiceException(e);
        } finally {
            try {
                if (null != riakClient) {
                    riakClientPool.returnObject(riakClient);
                }
            } catch (final Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void save(CBeanKey cBeanKey, byte[] asset) throws KVServiceException {
        RiakClient riakClient = null;
        try {
            /*
             * get a pooled riak connection
             */
            riakClient = riakClientPool.borrowObject();
            /*
             * query
             */
            final Location location = new Location(new Namespace(cBeanUrl.getClusterName()), cBeanKey.getKey());
            final RiakObject riakObject = new RiakObject();
            riakObject.setValue(BinaryValue.create(asset));
            final StoreValue sv = new StoreValue.Builder(riakObject).withLocation(location).build();
            riakClient.execute(sv);
            logger.info("Saved asset '" + cBeanKey.getKey() + "'");
        } catch (final Exception e) {
            throw new KVServiceException(e);
        } finally {
            try {
                if (null != riakClient) {
                    riakClientPool.returnObject(riakClient);
                }
            } catch (final Exception e) {
                e.printStackTrace();
                logger.info("Failed to save asset '" + cBeanKey.getKey() + "'");
            }
        }
    }
}