package com.iminido.nosql; import com.google.gson.Gson; import com.iminido.constant.Const; import com.iminido.instruction.core.IstRequ; import com.iminido.log.Log; import com.iminido.util.SU; import static com.iminido.util.SU.parseJSON2Map; import com.iminido.util.TL; 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 java.util.ArrayList; import java.util.List; import java.util.concurrent.Semaphore; import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; import org.bson.BSONObject; import org.bson.BasicBSONObject; import org.bson.util.JSON; /** * * @author JadeLuo */ public class NoSQLDb { private static SequoiadbDatasource sequoiadbDatasource; private static final Log log = Log.init(NoSQLDb.class); private static Semaphore semaphore = new Semaphore(1); static { initSequoiadbDatasource(); } public 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); // 创建连接池 } public static synchronized Sequoiadb getSequoiadb() { Sequoiadb sdb = null; try { sdb = sequoiadbDatasource.getConnection(); } catch (BaseException ex) { System.out.println("getIdleConnNum1 " + sequoiadbDatasource.getIdleConnNum()); System.out.println("getUsedConnNum" + sequoiadbDatasource.getUsedConnNum()); Logger.getLogger(NoSQLDb.class.getName()).log(Level.SEVERE, null, ex); } catch (InterruptedException ex) { System.out.println("getIdleConnNum2 " + sequoiadbDatasource.getIdleConnNum()); System.out.println("getUsedConnNum" + sequoiadbDatasource.getUsedConnNum()); Logger.getLogger(NoSQLDb.class.getName()).log(Level.SEVERE, null, ex); } if (sdb == null) { while (sdb == null) { try { semaphore.tryAcquire(1, 2, TimeUnit.SECONDS); } catch (InterruptedException ex) { System.out.println("getIdleConnNum3 " + sequoiadbDatasource.getIdleConnNum()); System.out.println("getUsedConnNum" + sequoiadbDatasource.getUsedConnNum()); Logger.getLogger(NoSQLDb.class.getName()).log(Level.SEVERE, null, ex); } try { sdb = sequoiadbDatasource.getConnection(); } catch (BaseException | InterruptedException ex) { System.out.println("getIdleConnNum4 " + sequoiadbDatasource.getIdleConnNum()); System.out.println("getUsedConnNum" + sequoiadbDatasource.getUsedConnNum()); Logger.getLogger(NoSQLDb.class.getName()).log(Level.SEVERE, null, ex); } } semaphore.release(); return sdb; } return sdb; } public 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(NoSQLDb.class.getName()).log(Level.SEVERE, null, ex); log.debug("D:\\working\\workspace\\LF_S_SignalProc\\src\\com\\iminido\\nosql\\NoSQLDb.java initCollectionSpace 方法获取 Sequoiadb 为空"); return null; } } public 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 removeAll(String collectionName) { DBCollection cl = initCollection(collectionName); cl.delete(new BasicBSONObject()); sequoiadbDatasource.close(cl.getSequoiadb()); } 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); log.debug("D:\\working\\workspace\\LF_S_SignalProc\\src\\com\\iminido\\nosql\\NoSQLDb.java save=>" + strJson); 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); Object object = 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; } public static boolean insert(String collectionName, IstRequ requ) { String strJson = TL.requ2json(requ); 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 Object insert(String collectionName, IstRequ requ, String args) { String strJson = TL.requ2json(requ, args); BSONObject dbo = null; try { dbo = (BasicBSONObject) JSON.parse(strJson); //添加异常处理 } catch (Exception e) { log.error("解析json字符串异常,传入的字符串为:" + strJson, e); } DBCollection dBCollection = initCollection(collectionName); Object object = dBCollection.insert(dbo); sequoiadbDatasource.close(dBCollection.getSequoiadb()); return object; } // public static void savej(String collectionName, IstRequ req) { // String strJson = req.get("j"); // strJson = strJson.replaceAll("\'", "\""); //把单引号替换成双引号,不用这句也可以执行成功。加这句是为了让我记住json应用中存在单双引号问题,在jquery中,如果json是用单引号的话,就会出错,在php中json用单引号也会出错。 // insert(collectionName, strJson); // } public static boolean isExist(String collectionName, String matcher) { if (null == queryOne(collectionName, matcher, matcher, matcher, null)) { return false; } else { return true; } } 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 DBCursor findandclose(String collectionName, String... key_val) { DBCollection dBCollection = initCollection(collectionName); DBCursor dBCursor = dBCollection.query(); sequoiadbDatasource.close(dBCollection.getSequoiadb()); return dBCursor; } public static DBCursor query(String collectionName, String... key_val) { return initCollection(collectionName).query(); } public static DBCursor query(String collectionName) { return initCollection(collectionName).query(); } public static DBCursor query(String collectionName, String matcher, String selector, String orderBy, String hint, int limitNum) { return initCollection(collectionName).query(matcher, selector, orderBy, hint); } 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(NoSQLDb.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) { try { Sequoiadb seq = sequoiadbDatasource.getConnection(); seq.execUpdate(sql); sequoiadbDatasource.close(seq); return true; } catch (BaseException e) { e.printStackTrace(); log.warn(sql, e); return false; } catch (InterruptedException ex) { Logger.getLogger(NoSQLDb.class.getName()).log(Level.SEVERE, null, ex); return false; } } //matcher test public static List query(String cl, String matcher) { DBCollection dbc = initCollection(cl); DBCursor c = dbc.query(matcher, null, null, null, 0L, 100L); List<String> list = null;//new ArrayList<>(); if (c != null && c.hasNext()) { list = cur2list(c); } else { sequoiadbDatasource.close(dbc.getSequoiadb()); return list; } sequoiadbDatasource.close(dbc.getSequoiadb()); return list; } //matcher test public static List query(String cl, String matcher,String selector) { DBCollection dbc = initCollection(cl); DBCursor c = dbc.query(matcher, selector, null, null, 0L, 100L); List<String> list = null;//new ArrayList<>(); if (c != null && c.hasNext()) { list = cur2list(c); } else { sequoiadbDatasource.close(dbc.getSequoiadb()); return list; } sequoiadbDatasource.close(dbc.getSequoiadb()); return list; } //returnRows test public static DBCursor query(String cl, String matcher, long returnRows) { return initCollection(cl).query(matcher, null, null, null, 0L, returnRows); } // selector {filed:1} public static DBCursor query(String cl, String matcher, String selector, long returnRows) { return initCollection(cl).query(matcher, selector, null, null, 0L, returnRows); } //orderBy {filed:1/-1} public static DBCursor query(String cl, String matcher, String selector, String orderBy, long returnRows) { return initCollection(cl).query(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 DBCursor query(String cl, String matcher, long returnRows, long skipRows) { return initCollection(cl).query(matcher, null, null, null, skipRows, returnRows); } public 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 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$unsetAll(String cl, String matcher, String field, String hint) { DBCollection dbc = initCollection(cl); dbc.update(matcher, "{$unset:" + field + ":[]}", hint); // NoSQLDb.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); // NoSQLDb.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); // NoSQLDb.upsert("friend", "{}", "{$addtoset:{label:[33,44,55]}}", "{}"); sequoiadbDatasource.close(dbc.getSequoiadb()); } /** * 不存在会自动插入新记录 * * @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) { 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 void main(String[] args) { // NoSQLDb.insertNestJson(Const.TEST, "{a:'a'}","{cc:'dd'}","nestJson"); String s = "{ \"_id\": { \"$oid\": \"544634608e849c2f20465015\" }, \"a\": \"a\", \"nestJson\": { \"cc\": \"dd\" } }"; s = s.replace("\\", ""); SU.parseJSON2Map(s); } }