- 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;
-
-
-
-
- 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 sdb = new Sequoiadb(connString, "", "");
-
- 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());
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
-
- 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);
-
-
-
- nwOpt.setConnectTimeout(500);
- nwOpt.setMaxAutoConnectRetryTime(0);
-
-
- dsOpt.setMaxConnectionNum(500);
- dsOpt.setInitConnectionNum(10);
- dsOpt.setDeltaIncCount(10);
- dsOpt.setMaxIdeNum(10);
- dsOpt.setTimeout(5 * 1000);
- 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 = null;
- try {
- sdb = sequoiadbDatasource.getConnection();
- } catch (BaseException | InterruptedException ex) {
- Logger.getLogger(Squ.class.getName()).log(Level.SEVERE, null, ex);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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;
- }
-
-
- 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);
- }
-
-
- public static String query(String cl, String matcher) {
- return _query(cl, matcher, null, null, null, 0, 0);
- }
-
-
- 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);
- }
-
-
- public static String query(String cl, String matcher, long returnRows) {
- return _query(cl, matcher, null, null, null, 0L, returnRows);
- }
-
-
- public static String query(String cl, String matcher, String selector, long returnRows) {
- return _query(cl, matcher, selector, null, null, 0L, returnRows);
- }
-
-
- public static String query(String cl, String matcher, String selector, long skipRows, long returnRows) {
- return _query(cl, matcher, selector, null, null, skipRows, returnRows);
- }
-
-
- public static String query(String cl, String matcher, String selector, String orderBy, long returnRows) {
- return _query(cl, matcher, selector, orderBy, null, 0L, returnRows);
- }
-
-
- 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);
- }
-
-
- 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);
- 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);
- 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);
- 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;
- }
-
-
-
-
-
-
-
-
-
- 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) {
-
-
- }
- }
-
- public static BasicBSONObject parse2BasicBSONObject(String json) {
- BasicBSONObject bbo = new BasicBSONObject();
- JSONObject obj = parse2JSONObject(json);
-
- 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"
- + "}");
- }
- }