com.khubla.cbean.mongokv.MongoKVService.java Source code

Java tutorial

Introduction

Here is the source code for com.khubla.cbean.mongokv.MongoKVService.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.mongokv;

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

import com.khubla.cbean.CBeanKey;
import com.khubla.cbean.kvservice.KVService;
import com.khubla.cbean.kvservice.KVServiceException;
import com.khubla.cbean.url.CBeanUrl;
import com.mongodb.DB;
import com.mongodb.MongoClient;
import com.mongodb.gridfs.GridFS;

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

    /**
     * ctor
     */
    public MongoKVService(CBeanUrl cBeanUrl) {
        this.cBeanUrl = cBeanUrl;
        mongoClientPool = new GenericObjectPool<MongoClient>(
                new MongoPoolableObjectFactoryImpl(cBeanUrl.getHostName()), MAX_STORAGESERVICE_CONNECTIONS);
    }

    @Override
    public void delete(CBeanKey cBeanKey) throws KVServiceException {
        MongoClient mongoClient = null;
        try {
            /*
             * get a pooled mongo connection
             */
            mongoClient = mongoClientPool.borrowObject();
            mongoClient.getDatabase(cBeanUrl.getClusterName());
            /*
             * query
             */
            // final Location location = new Location(new Namespace(clusterName), url);
            // 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 != mongoClient) {
                    mongoClientPool.returnObject(mongoClient);
                }
            } 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 {
        MongoClient mongoClient = null;
        try {
            /*
             * get a pooled mongo connection
             */
            mongoClient = mongoClientPool.borrowObject();
            final DB db = mongoClient.getDB(cBeanUrl.getClusterName());
            new GridFS(db, "");
            // List<> gfs.find(url);
            // final Location location = new Location(new Namespace(clusterName), url);
            // 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 '" + url + "'");
            // 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 != mongoClient) {
                    mongoClientPool.returnObject(mongoClient);
                }
            } catch (final Exception e) {
                e.printStackTrace();
            }
        }
    }

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