测试连接Sequoiadb

package com.iminido.nosql;

import com.sequoiadb.base.CollectionSpace;
import com.sequoiadb.base.DBCollection;
import com.sequoiadb.base.DBCursor;
import com.sequoiadb.base.Sequoiadb;
import com.sequoiadb.base.SequoiadbDatasource;
import com.sequoiadb.base.SequoiadbOption;
import com.sequoiadb.exception.BaseException;
import com.sequoiadb.net.ConfigOptions;
import ist.Const;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Semaphore;
import java.util.function.BiConsumer;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.logging.log4j.LogManager;
import org.bson.BSONObject;
import org.bson.BasicBSONObject;
import org.bson.util.JSON;
import org.json.JSONObject;

/**
 * @author Jade
 */
public class Squ {

    private static SequoiadbDatasource sequoiadbDatasource;
    protected static final org.apache.logging.log4j.Logger log = LogManager.getLogger(Squ.class);
    private static Semaphore semaphore = new Semaphore(1);
    private static Map<String, Sequoiadb> dbs = new HashMap<String, Sequoiadb>();

    public static void main(String[] args) {
//
        String connString = Const.SEQUOIADB_HOST + ":" + Const.SEQUOIADB_PORT;
        try {
            // 建立 SequoiaDB 数据库连接
            Sequoiadb sdb = new Sequoiadb(connString, "", "");
            // 获取所有 Collection 信息,并打印出来
            DBCursor cursor = sdb.listCollectionSpaces();
            while (cursor.hasNext()) {
                System.out.println(cursor.getCurrent());
            }
        } catch (BaseException e) {
            System.out.println("Sequoiadb driver error, error description:" + e.getErrorType());
        }

//        initSequoiadbDatasource();
//        getSequoiadb();
//        BSONObject dbo = new BasicBSONObject(); //添加异常处理 
//        dbo.put("key_a", "value_a");
//        dbo.put("key_b", "value_b");
//
//        DBCollection dBCollection = initCollection("test");
//        dBCollection.save(dbo);
//        DBCursor dbc = dBCollection.query();
//        while (dbc.hasNext()) {
//            System.out.println("" + dbc.getNext().toString());
//        }
//        sequoiadbDatasource.close(dBCollection.getSequoiadb());
    }

    static {

    }

    private static void initSequoiadbDatasource() {
        ArrayList<String> urls = new ArrayList<>();
        ConfigOptions nwOpt = new ConfigOptions();          // 定义连接选项
        SequoiadbOption dsOpt = new SequoiadbOption();	    // 定义连接池选项

        urls.add(Const.SEQUOIADB_HOST + ":" + Const.SEQUOIADB_PORT);//
//        urls.add("ubuntu-dev2:11810");
//        urls.add("ubuntu-dev3:11810");

        nwOpt.setConnectTimeout(500);                       // 设置若连接失败,超时时间(ms)
        nwOpt.setMaxAutoConnectRetryTime(0);	            // 设置若连接失败,重试次数

        // 以下设置的都是 SequoiadbOption 的默认值
        dsOpt.setMaxConnectionNum(500);                     // 设置连接池最大连接数
        dsOpt.setInitConnectionNum(10);                     // 初始化连接池时,创建连接的数量
        dsOpt.setDeltaIncCount(10);                        // 当池中没有可用连接时,增加连接的数量
        dsOpt.setMaxIdeNum(10);                             // 周期清理多余的空闲连接时,应保留连接的数量
        dsOpt.setTimeout(5 * 1000);                         // 当已使用的连接数到达设置的最大连接数时(500),请求连接的等待时间。
        dsOpt.setAbandonTime(10 * 60 * 1000);               // 连接存活时间,当连接空闲时间超过连接存活时间,将被连接池丢弃
        dsOpt.setRecheckCyclePeriod(1 * 60 * 1000);         // 清除多余空闲连接的周期
        dsOpt.setRecaptureConnPeriod(10 * 60 * 1000);       // 检测并取回异常地址的周期

        sequoiadbDatasource = new SequoiadbDatasource(urls, Const.SEQUOIADB_USERNAME, Const.SEQUOIADB_PASSWORD, nwOpt, dsOpt); // 创建连接池
    }

    private static synchronized Sequoiadb getSequoiadb() {
        return getSequoiadb("zy");
    }

    private static synchronized Sequoiadb getSequoiadb(String dbName) {
//        Sequoiadb sdb = dbs.get(dbName);
        Sequoiadb sdb = null;
        try {
            sdb = sequoiadbDatasource.getConnection();
        } catch (BaseException | InterruptedException ex) {
            Logger.getLogger(Squ.class.getName()).log(Level.SEVERE, null, ex);
        }

//        while (sdb == null) {
//            try {
//                semaphore.tryAcquire(1, 2, TimeUnit.SECONDS);
//            } catch (InterruptedException ex) {
//                Logger.getLogger(Squ.class.getName()).log(Level.SEVERE, null, ex);
//            }
//            try {
//                sdb = sequoiadbDatasource.getConnection();
//            } catch (BaseException | InterruptedException ex) {
//                Logger.getLogger(Squ.class.getName()).log(Level.SEVERE, null, ex);
//            }
//        }
//        dbs.put(dbName, sdb);
        semaphore.release();
        return sdb;

    }

    private static CollectionSpace initCollectionSpace(String csName) {
        try {
            Sequoiadb sdb = getSequoiadb();
            CollectionSpace cs;
            if (sdb.isCollectionSpaceExist(csName)) {
                cs = sdb.getCollectionSpace(csName);
            } else {
                cs = sdb.createCollectionSpace(csName);
            }
            return cs;
        } catch (BaseException ex) {
            Logger.getLogger(Squ.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    private static DBCollection initCollection(String collectionName) {
        CollectionSpace cs = initCollectionSpace(Const.SEQUOIADB_DATABASE);
        DBCollection cl;
        if (cs.isCollectionExist(collectionName)) {
            cl = cs.getCollection(collectionName);
        } else {
            cl = cs.createCollection(collectionName);
        }
        return cl;
    }

    public static void remove(String collectionName) {
        DBCollection cl = initCollection(collectionName);
        cl.delete(new BasicBSONObject());
        sequoiadbDatasource.close(cl.getSequoiadb());
    }

    public static boolean delete(String cl, String matcher) {
        try {
            DBCollection dbc = initCollection(cl);
            dbc.delete(matcher);
            sequoiadbDatasource.close(dbc.getSequoiadb());
            return true;
        } catch (Exception e) {
            log.error("delete " + cl + "matcher=>" + matcher + "失败", e);
            return false;
        }
    }

    public static void delete(String cl, String matcher, String hint) {
        DBCollection dbc = initCollection(cl);
        dbc.delete(matcher, hint);
        sequoiadbDatasource.close(dbc.getSequoiadb());
    }

    public static boolean insert(String collectionName, String... key_val) {
        String strJson = strs2json(key_val);
        if (strJson != null) {
            BSONObject dbo;
            try {
                dbo = (BasicBSONObject) JSON.parse(strJson); //添加异常处理 
            } catch (Exception e) {
                log.error("解析json字符串异常,传入的字符串为:" + strJson, e);
                return false;
            }

            try {
                DBCollection dbc = initCollection(collectionName);
                dbc.insert(dbo);
                sequoiadbDatasource.close(dbc.getSequoiadb());
                return true;
            } catch (Exception e) {
                return false;
            }
        }
        return false;
    }

    public static boolean insert(String collectionName, String strJson) {
        BSONObject dbo = null;
        try {
            dbo = (BasicBSONObject) JSON.parse(strJson); //添加异常处理 
        } catch (Exception e) {
            log.error("解析json字符串异常,传入的字符串为:" + strJson, e);
            return false;
        }

        DBCollection dBCollection = initCollection(collectionName);
        dBCollection.insert(dbo);
        sequoiadbDatasource.close(dBCollection.getSequoiadb());
        return true;
    }

    public static boolean insertNestJson(String collectionName, String strJson, String strJson2, String nameOfNest) {
        BSONObject dbo = null;
        try {
            dbo = (BasicBSONObject) JSON.parse(strJson); //添加异常处理 
        } catch (Exception e) {
            log.error("解析json字符串异常,传入的字符串为:" + strJson, e);
            return false;
        }

        BSONObject dbo2 = null;
        try {
            dbo2 = (BasicBSONObject) JSON.parse(strJson2); //添加异常处理 
        } catch (Exception e) {
            log.error("解析json字符串异常,传入的字符串为:" + strJson, e);
            return false;
        }

        dbo.put(nameOfNest, dbo2);

        DBCollection dBCollection = initCollection(collectionName);
        Object object = dBCollection.insert(dbo);
        sequoiadbDatasource.close(dBCollection.getSequoiadb());
        return true;
    }

    //1
    public static boolean isExist(String collectionName, String matcher) {
        if (null == queryOne(collectionName, matcher, matcher, matcher, null)) {
            return false;
        } else {
            return true;
        }
    }

    public static BasicBSONObject str2BSONObject(String jsonString) {
        return (BasicBSONObject) JSON.parse(jsonString);
    }

    public static List exec(String sql) {
        DBCursor c = null;
        Sequoiadb seq = null;
        try {
            seq = sequoiadbDatasource.getConnection();
            c = seq.exec(sql);
        } catch (BaseException e) {
            e.printStackTrace();
        } catch (InterruptedException ex) {
            Logger.getLogger(Squ.class.getName()).log(Level.SEVERE, null, ex);
        }

        if (c != null && c.hasNext()) {
            List list = new ArrayList();
            while (c.hasNext()) {
                list.add(c.getNext());
            }
            if (seq != null) {
                sequoiadbDatasource.close(seq);
            }
            return list;
        } else {
            if (seq != null) {
                sequoiadbDatasource.close(seq);
            }
            return null;
        }
    }

    public static String exeSql(String sql) {
        return list2String(exec(sql));
    }

    public static String exe(String sql) {
        return list2String(exec(sql));
    }

    public static boolean execUpdate(String sql) {
        Sequoiadb seq = null;
        try {
            seq = sequoiadbDatasource.getConnection();
            seq.execUpdate(sql);
            sequoiadbDatasource.close(seq);
            return true;
        } catch (BaseException e) {
            sequoiadbDatasource.close(seq);
            e.printStackTrace();
            log.warn(sql, e);
            return false;
        } catch (InterruptedException ex) {
            Logger.getLogger(Squ.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    private static String _query(String cl, String matcher, String selector, String orderBy, String hint, long skipRows, long returnRows) {
        DBCollection dbc = initCollection(cl);
        String jsonString = cur2jsonstr(dbc.query(matcher, selector, orderBy, hint, skipRows, returnRows));
        sequoiadbDatasource.close(dbc.getSequoiadb());
        return jsonString;
    }

    public static String query(String cl, String matcher, String selector, String orderBy, String hint, long skipRows, long returnRows) {
        return _query(cl, matcher, selector, orderBy, hint, skipRows, returnRows);
    }

    //matcher test
    public static String query(String cl, String matcher) {
        return _query(cl, matcher, null, null, null, 0, 0);
    }

    //matcher test
    public static String query(String cl, String matcher, String selector) {
        return _query(cl, matcher, selector, null, null, 0, 0);
    }

    public static List query4roomids(String cl, String matcher, String selector) {
        DBCollection dbc = initCollection(cl);
        DBCursor dBCursor = dbc.query(matcher, selector, null, null, 0, 0);
        sequoiadbDatasource.close(dbc.getSequoiadb());
        return cur2list(dBCursor);
    }

    //returnRows test
    public static String query(String cl, String matcher, long returnRows) {
        return _query(cl, matcher, null, null, null, 0L, returnRows);
    }

    // selector {filed:1}
    public static String query(String cl, String matcher, String selector, long returnRows) {
        return _query(cl, matcher, selector, null, null, 0L, returnRows);
    }

    // selector {filed:1}
    public static String query(String cl, String matcher, String selector, long skipRows, long returnRows) {
        return _query(cl, matcher, selector, null, null, skipRows, returnRows);
    }

//    orderBy {filed:1/-1}
    public static String query(String cl, String matcher, String selector, String orderBy, long returnRows) {
        return _query(cl, matcher, selector, orderBy, null, 0L, returnRows);
    }

    //hint (index) {}
    public static DBCursor query(String cl, String matcher, String selector, String orderBy, String hint, long returnRows) {
        DBCollection dbc = initCollection(cl);
        DBCursor dbcu = dbc.query(matcher, selector, orderBy, hint, 0L, returnRows);
        sequoiadbDatasource.close(dbc.getSequoiadb());
        return dbcu;
    }

    public static List query2(String cl, String matcher, String selector, String orderBy, String hint, long returnRows, List<String> list) {
        DBCollection dbc = initCollection(cl);
        DBCursor c = dbc.query(matcher, selector, orderBy, hint, 0L, returnRows);

        if (c != null && c.hasNext()) {
            while (c.hasNext()) {
                String str1 = (String) c.getNext().get("acc");
                for (int j = 0; j < list.size(); j++) {
                    String str2 = list.get(j);
                    if (str2.equals(str1)) {
                        list.remove(str1);
                    }
                }
            }
        } else {
            sequoiadbDatasource.close(dbc.getSequoiadb());
            return list;//直接返回生成的推荐号
        }
        sequoiadbDatasource.close(dbc.getSequoiadb());
        return list;
    }

    public static String query(String cl, String matcher, long returnRows, long skipRows) {
        return _query(cl, matcher, null, null, null, skipRows, returnRows);
    }

    //1
    public static BSONObject queryOne(String collectionName, String matcher, String selector, String orderBy, String hint) {
        DBCollection dBCollection = initCollection(collectionName);
        BSONObject bSONObject = dBCollection.queryOne(str2BSONObject(matcher), str2BSONObject(selector), str2BSONObject(orderBy), str2BSONObject(hint), 0);
        sequoiadbDatasource.close(dBCollection.getSequoiadb());
        return bSONObject;
    }

    public static String query(String collectionName) {
        return _query(collectionName, null, null, null, null, 0, 0);
    }

    public static String query(String collectionName, String matcher, String selector, String orderBy, String hint, int limitNum) {
        return _query(collectionName, matcher, selector, orderBy, hint, limitNum, limitNum);
    }

    public static void update(String cl, String matcher, String modifier, String hint) {
        DBCollection dbc = initCollection(cl);
        dbc.update(matcher, modifier, hint);
        sequoiadbDatasource.close(dbc.getSequoiadb());
    }

    public static void update$insert(String cl, String matcher, String modifier, String hint) {
        DBCollection dbc = initCollection(cl);
        dbc.update(matcher, modifier, hint);
        sequoiadbDatasource.close(dbc.getSequoiadb());
    }

    public static void update$unsetAll(String cl, String matcher, String field, String hint) {
        DBCollection dbc = initCollection(cl);
        dbc.update(matcher, "{$unset:" + field + ":[]}", hint); // Squ.update("friend", "{}", "{$unset:{label:[]}}", "{}");
        sequoiadbDatasource.close(dbc.getSequoiadb());
    }

    public static void update$unset(String cl, String matcher, String modifier, String hint) {
        DBCollection dbc = initCollection(cl);
        dbc.update(matcher, "{$unset:" + modifier + "}", hint); //  Squ.update("friend", "{}", "{$unset:{label:[33,44,55]}}", "{}");
        sequoiadbDatasource.close(dbc.getSequoiadb());

    }

    public static void update$addtoset(String cl, String matcher, String modifier, String hint) {
        DBCollection dbc = initCollection(cl);
        dbc.update(matcher, "{$addtoset:" + modifier + "}", hint); //    Squ.upsert("friend", "{}", "{$addtoset:{label:[33,44,55]}}", "{}");
        sequoiadbDatasource.close(dbc.getSequoiadb());
    }

    public static boolean update(String collectionName, String matcher, String modifier) {
        DBCollection dbc = initCollection(collectionName);
        dbc.update(matcher, modifier, null);
        sequoiadbDatasource.close(dbc.getSequoiadb());
        return true;
    }

    public static boolean update$set(String collectionName, String matcher, String modifier) {
        DBCollection dbc = null;
        try {
            dbc = initCollection(collectionName);
            dbc.update(matcher, "{$set:" + modifier + "}", null);
            sequoiadbDatasource.close(dbc.getSequoiadb());
        } catch (Exception e) {
            log.debug("", e);
            sequoiadbDatasource.close(dbc.getSequoiadb());
            return false;
        }

        return true;
    }

    /**
     * 不存在会自动插入新记录
     *
     * @param cl
     * @param matcher
     * @param modifier
     * @param hint
     */
    public static void upsert(String cl, String matcher, String modifier, String hint) {
        DBCollection dbc = initCollection(cl);
        BSONObject ma = null;
        BSONObject mo = null;
        BSONObject hi = null;
        if (matcher != null) {
            ma = (BSONObject) JSON.parse(matcher);
        }
        if (modifier != null) {
            mo = (BSONObject) JSON.parse(modifier);
        }
        if (hint != null) {
            hi = (BSONObject) JSON.parse(hint);
        }
        dbc.upsert(ma, mo, hi);
        sequoiadbDatasource.close(dbc.getSequoiadb());

    }

    public static String strs2json(String... key_val) {
        if (key_val.length % 2 != 0) {

        }
        String strJson = null;
        if (key_val != null) {
            StringBuilder sb = new StringBuilder();
            sb.append("{");
            int i = 0;
            while (key_val[i] != null) {
                sb.append(key_val[i]).append(":\"").append(key_val[++i]).append("\"");
                if (i < key_val.length - 1) {
                    sb.append(",");
                    i++;
                } else {
                    key_val[i] = null;
                }
            }
            sb.append("}");
            strJson = sb.toString();

        }
        return strJson;
    }

    public static List cur2list(DBCursor c) {
        if (c != null && c.hasNext()) {
            List list = new ArrayList();
            while (c.hasNext()) {
                list.add(c.getNext());
            }

            return list;
        }
        return null;
    }

    public static String cur2jsonstr(DBCursor c) {
        String jsonString = "";
        if (c != null && c.hasNext()) {
            while (c.hasNext()) {
                jsonString = jsonString + (c.getNext().toString());
            }
            c.close();
            return jsonString;
        }
        return "{}";
    }

    private static String list2String(List list) {
        if (list != null) {
            StringBuilder sb = new StringBuilder();
            list.stream().forEach((Object s) -> {
                sb.append(s).append(",");
            });
            return sb.toString();
        } else {
            return null;
        }

    }

    public static List<String> queryList(String collectionName, String string, String acc1_id0, String string0, String string1, int i, int DB_SEARCH_REM_COUNT, List<String> list) {
        return query2(collectionName, string, string1, string1, string, i, list);
    }

    public static void remove(String collectionName, String match) {
        DBCollection cl = initCollection(collectionName);
        cl.delete(parse2BasicBSONObject(match));
        sequoiadbDatasource.close(cl.getSequoiadb());
    }

    public static JSONObject parse2JSONObject(String json) {
        return new JSONObject(json);
    }

    static class a implements BiConsumer {

        @Override
        public void accept(Object t, Object u) {
//            System.out.println("t=>>"+t);
//            System.out.println("u=>>"+u);
        }
    }

    public static BasicBSONObject parse2BasicBSONObject(String json) {
        BasicBSONObject bbo = new BasicBSONObject();
        JSONObject obj = parse2JSONObject(json);
//        obj.forEach(new a() {});
        obj.entrySet().forEach(key -> {
            System.out.println("c=>>" + obj.getString(key.getKey()));
            System.out.println("c=>>" + obj.optString(key.getKey()));
        });
        return bbo;
    }

    public static void main2(String[] args) {
        parse2BasicBSONObject("{\n"
                + "  \"_id\":\"testuser8\",\n"
                + "  \"UID\":\"testuser\",\n"
                + "  \"CDATE\":\"2015-05-10 17:28:50\",\n"
                + "  \"IDATE\":\"2015-05-10 17:28:50\",\n"
                + "  \"CARDTL\":\"{\\\"HEADPIC\\\":\\\"\\\",\\\"NICK\\\":\\\"1\\\",\\\"name\\\":\\\"2\\\",\\\"skill\\\":\\\"3\\\"}\",\n"
                + "  \"NICK\":\"\",\n"
                + "  \"NID\":\"8\",\n"
                + "  \"HEADPIC\":\"\",\n"
                + "  \"UDATE\":{\n"
                + "    \"$date\":\"2015-05-10T09:28:50.668Z\"\n"
                + "  },\n"
                + "  \"UD\":\"testuser\",\n"
                + "  \"URID\":\"urid\",\n"
                + "  \"ORANGE\":\"uId:个人信息标签2\"\n"
                + "}");
    }
}

猜你喜欢

转载自jadeluo.iteye.com/blog/2227036