public DBObject selectOne(DBCollection collection) { DBObject fields = getFields(); DBObject query = getQuery(); DBObject orderByObject = getOrderByObject(); // 日志 log(fields, query, orderByObject); if (null == fields && null == orderByObject) { return collection.findOne(query); } else if (null != fields && null == orderByObject) { return collection.findOne(query, fields); } else { return collection.findOne(query, fields, orderByObject); } }
public List<String> getAllIds(final Map<String, Object> conditions) { DBObject query = new BasicDBObject(conditions); DBCursor cursor = coll.find(query); List<String> ids = new ArrayList<String>(cursor.count()); for (DBObject o : cursor) { ids.add(o.get("_id").toString()); } return ids; }
public void update(List<CanalEntry.Column> data, String schemaName, String tableName) { DBObject obj = DBConvertUtil.columnToJson(data); logger.debug("update:{}", obj.toString()); //订单库单独处理 if (schemaName.equals("order")) { if (tableName.startsWith("order_base_info")) { tableName = "order_base_info"; } else if (tableName.startsWith("order_detail_info")) { tableName = "order_detail_info"; } else { logger.info("unknown data:{}.{}:{}", schemaName, tableName, obj); } updateData(schemaName, tableName, new BasicDBObject("orderId", obj.get("orderId")), obj); } else { if (obj.containsField("id")) { updateData(schemaName, tableName, new BasicDBObject("_id", obj.get("id")), obj); } else { logger.info("unknown data structure"); } } }
public void insertData(String schemaName, String tableName, DBObject naive, DBObject complete) { int i = 0; DBObject logObj = (DBObject) ObjectUtils.clone(complete); //保存原始数据 try { String path = "/" + schemaName + "/" + tableName + "/" + CanalEntry.EventType.INSERT.getNumber(); i++; naiveMongoTemplate.getCollection(tableName).insert(naive); i++; SpringUtil.doEvent(path, complete); i++; } catch (MongoClientException | MongoSocketException clientException) { //客户端连接异常抛出,阻塞同步,防止mongodb宕机 throw clientException; } catch (Exception e) { logError(schemaName, tableName, 1, i, logObj, e); } }
public void deleteData(String schemaName, String tableName, DBObject obj) { int i = 0; String path = "/" + schemaName + "/" + tableName + "/" + CanalEntry.EventType.DELETE.getNumber(); DBObject newObj = (DBObject) ObjectUtils.clone(obj); DBObject logObj = (DBObject) ObjectUtils.clone(obj); //保存原始数据 try { i++; if (obj.containsField("id")) { naiveMongoTemplate.getCollection(tableName).remove(new BasicDBObject("_id", obj.get("id"))); } i++; SpringUtil.doEvent(path, newObj); } catch (MongoClientException | MongoSocketException clientException) { //客户端连接异常抛出,阻塞同步,防止mongodb宕机 throw clientException; } catch (Exception e) { logError(schemaName, tableName, 3, i, logObj, e); } }
/** * 从列表中删除已存在的数据 * * @param list * @param obj * @return */ public static List<DBObject> removeDBObject(List<DBObject> list, DBObject obj) { if (list == null || list.size() < 1) { return null; } int existFlag = -1; for (int i = 0; i < list.size(); i++) { if (list.get(i).get("id").equals(obj.get("id"))) { existFlag = i; } } if (existFlag >= 0) { list.remove(existFlag); } return list; }
/** * 从列表中替换已存在的数据 * * @param list * @param obj * @return */ public static List<DBObject> upsertDBObject(List<DBObject> list, DBObject obj) { if (list == null) { //列表不存在,添加 list = new ArrayList<>(); list.add(obj); } else { //列表存在,找id相同数据,有数据替换,没有数据添加 int existFlag = -1; for (int i = 0; i < list.size(); i++) { if (list.get(i).get("id").equals(obj.get("id"))) { existFlag = i; } } if (existFlag >= 0) { list.remove(existFlag); } list.add(obj); } return list; }
public static DBObject toDbObject(NameValues nameValues) { final BasicDBObject basicDBObject = new BasicDBObject(); nameValues.forEach(new NameValues.Foreach() { @Override public boolean forEach(String name, Object value) { basicDBObject.append(name, value); return true; } }); return basicDBObject; }
private Condition getCondition() throws DBException { final DBObject query = type == 0 ? new BasicDBObject() : this.query; if (type == 0 && regexNameValues != null) { String[] names = regexNameValues.getNames(); Object[] values = regexNameValues.getValues(); for (int i = 0; i < names.length; i++) { query.put(names[i], new BasicDBObject("$regex", values[i])); } } MongoCondition mongoCondition = new MongoCondition() { public Object toFinalObject() throws Condition.ConditionException { return query; } }; return mongoCondition; }
private int doBatchUpdate(DBCollection dbCollection, String collName, Collection<BatchUpdateOptions> options, boolean ordered) { DBObject command = new BasicDBObject(); command.put("update", collName); List<BasicDBObject> updateList = new ArrayList<BasicDBObject>(); for (BatchUpdateOptions option : options) { BasicDBObject update = new BasicDBObject(); update.put("q", option.getQuery().getQueryObject()); update.put("u", option.getUpdate().getUpdateObject()); update.put("upsert", option.isUpsert()); update.put("multi", option.isMulti()); updateList.add(update); } command.put("updates", updateList); command.put("ordered", ordered); CommandResult commandResult = dbCollection.getDB().command(command); return Integer.parseInt(commandResult.get("n").toString()); }
@SuppressWarnings("rawtypes") @Override public VirtualObject convert(DBObject source) { Integer rid = (Integer) source.get("rid"); Integer classId = (Integer) source.get("eClassId"); Long oid = (Long) source.get("oid"); Object featuresObject = source.get("features"); EClass eclass = platformService.getEClassForCid(classId.shortValue()); VirtualObject result = new VirtualObject(rid, classId.shortValue(), oid, eclass); if (featuresObject instanceof BasicDBObject) { Map map = (Map) featuresObject; processFeatures(map, result); } return result; }
@Test public void checkNonEncryptedMap() { MyBean bean = new MyBean(); Map<String, MySubBean> map = new HashMap(); map.put("one", new MySubBean("sky is blue", " earth is round")); map.put("two", new MySubBean("grass is green", "earth is flat")); bean.nonSensitiveMap = map; mongoTemplate.save(bean); MyBean fromDb = mongoTemplate.findOne(query(where("_id").is(bean.id)), MyBean.class); assertThat(fromDb.nonSensitiveMap.get("one").secretString, is(bean.nonSensitiveMap.get("one").secretString)); assertThat(fromDb.nonSensitiveMap.get("one").nonSensitiveData, is(bean.nonSensitiveMap.get("one").nonSensitiveData)); assertThat(fromDb.nonSensitiveMap.get("two").secretString, is(bean.nonSensitiveMap.get("two").secretString)); assertThat(fromDb.nonSensitiveMap.get("two").nonSensitiveData, is(bean.nonSensitiveMap.get("two").nonSensitiveData)); DBObject fromMongo = mongoTemplate.getCollection(MyBean.MONGO_MYBEAN).find(new BasicDBObject("_id", new ObjectId(bean.id))).next(); DBObject mapMongo = (DBObject) fromMongo.get(MyBean.MONGO_NONSENSITIVEMAP); DBObject oneMongo = (DBObject) mapMongo.get("one"); DBObject twoMongo = (DBObject) mapMongo.get("two"); assertThat(oneMongo.get(MySubBean.MONGO_NONSENSITIVEDATA), is(map.get("one").nonSensitiveData)); assertThat(twoMongo.get(MySubBean.MONGO_NONSENSITIVEDATA), is(map.get("two").nonSensitiveData)); assertCryptLength(oneMongo.get(MySubBean.MONGO_SECRETSTRING), map.get("one").secretString.length() + 12); assertCryptLength(twoMongo.get(MySubBean.MONGO_SECRETSTRING), map.get("two").secretString.length() + 12); }
@Override public void setQuery(DBObject query, BasicDBList orList) { // if (null == orList) { // query.put(this.name, value.getValue()); // } else { // orList.add(new BasicDBObject(this.name, value.getValue())); // } if (this.name.equals("_id")) { if (null == orList) { query.put(this.name, new ObjectId(value.getValue().toString())); } else { orList.add(new BasicDBObject(this.name, new ObjectId(value.getValue().toString()))); } } else { if (null == orList) { query.put(this.name, value.getValue()); } else { orList.add(new BasicDBObject(this.name, value.getValue())); } } }
private int InsertData(SQLInsertStatement state) { if (state.getValues().getValues().size() ==0 ){ throw new RuntimeException("number of columns error"); } if (state.getValues().getValues().size() != state.getColumns().size()){ throw new RuntimeException("number of values and columns have to match"); } SQLTableSource table=state.getTableSource(); BasicDBObject o = new BasicDBObject(); int i=0; for(SQLExpr col : state.getColumns()) { o.put(getFieldName2(col), getExpValue(state.getValues().getValues().get(i))); i++; } DBCollection coll =this._db.getCollection(table.toString()); coll.insert(new DBObject[] { o }); return 1; }
private int UpData(SQLUpdateStatement state) { SQLTableSource table=state.getTableSource(); DBCollection coll =this._db.getCollection(table.toString()); SQLExpr expr=state.getWhere(); DBObject query = parserWhere(expr); BasicDBObject set = new BasicDBObject(); for(SQLUpdateSetItem col : state.getItems()){ set.put(getFieldName2(col.getColumn()), getExpValue(col.getValue())); } DBObject mod = new BasicDBObject("$set", set); coll.updateMulti(query, mod); //System.out.println("changs count:"+coll.getStats().size()); return 1; }
@Test public void checkEncryptedSubdocument() { MyBean bean = new MyBean(); MySubBean subBean = new MySubBean("sky is blue", " earth is round"); bean.secretSubBean = subBean; mongoTemplate.save(bean); MyBean fromDb = mongoTemplate.findOne(query(where("_id").is(bean.id)), MyBean.class); assertThat(fromDb.secretSubBean.nonSensitiveData, is(bean.secretSubBean.nonSensitiveData)); assertThat(fromDb.secretSubBean.secretString, is(bean.secretSubBean.secretString)); DBObject fromMongo = mongoTemplate.getCollection(MyBean.MONGO_MYBEAN).find(new BasicDBObject("_id", new ObjectId(bean.id))).next(); int expectedLength = 12 + MySubBean.MONGO_NONSENSITIVEDATA.length() + subBean.secretString.length() + 7 + MySubBean.MONGO_SECRETSTRING.length() + subBean.nonSensitiveData.length() + 7; assertCryptLength(fromMongo.get(MyBean.MONGO_SECRETSUBBEAN), expectedLength); }
@Override public Object getObject(String columnLabel) throws SQLException { if (isSum) { if (isGroupBy){ Object ob=dblist.get(_row-1); if (ob instanceof DBObject) { return ((DBObject)ob).get(columnLabel); } else { return "0"; } } else{ return this._sum; } } else { return this._cur.get(columnLabel); } }
/** * This test demonstrates usage of {@code $comment} {@link Meta} usage. One can also enable profiling using * {@code --profile=2} when starting {@literal mongod}. * <p> * <strong>NOTE</strong>: Requires MongoDB v. 2.6.4+ */ @Test public void findByFirstnameUsingMetaAttributes() { // execute derived finder method just to get the comment in the profile log repository.findByFirstname(dave.getFirstname()); // execute another finder without meta attributes that should not be picked up repository.findByLastname(dave.getLastname(), new Sort("firstname")); DBCursor cursor = operations.getCollection(ApplicationConfiguration.SYSTEM_PROFILE_DB) .find(new BasicDBObject("query.$comment", AdvancedRepository.META_COMMENT)); while (cursor.hasNext()) { DBObject dbo = cursor.next(); DBObject query = (DBObject) dbo.get("query"); assertThat(query.containsField("$comment"), is(true)); } }
@Override public List<String> findTagsByPattern(Pattern pattern, int limit) { final Aggregation agg = newAggregation( project("meta.tags"), unwind("tags"), group("tags").count().as("count"), project("count").and("tags").previousOperation(), match(where("tags").regex(pattern)), sort(Direction.ASC, "tags"), limit(limit)); final AggregationResults<DBObject> results = mongoTemplate.aggregate(agg, Conversation.class, DBObject.class); return results.getMappedResults() .stream() .map(i -> (String) i.get("tags")) .collect(Collectors.toList()); }
public Object insert(DBCollection collection, WriteConcern writeConcern) { DBObject document = new BasicDBObject(); // 匹配_id for (int i = 0, n = columns.size(); i < n; i++) { // document.put(columns.get(i), values.get(i).getValue()); String tempColumn = columns.get(i); if (3 == tempColumn.length() && tempColumn.equals("_id")) { document.put(tempColumn, new ObjectId(values.get(i).getValue().toString())); } else { document.put(tempColumn, values.get(i).getValue()); } } log(document); // TODO: WriteConcern.ACKNOWLEDGED需要可以配置 // WriteResult result = collection.insert(document, WriteConcern.ACKNOWLEDGED); // collection.insert(document, MongoComponent.getInstance().getDefaultWriteConcern()); collection.insert(document, writeConcern); Object oid = document.get("_id"); if (null != oid) { return oid.toString(); } return null; }
@Test public void shouldExtractPrincipalNameFromAttributes() throws Exception { // given MongoSession toSerialize = new MongoSession(); String principalName = "john_the_springer"; toSerialize.setAttribute( FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, principalName); // when DBObject dbObject = convertToDBObject(toSerialize); // then assertThat(dbObject.get("principal")).isEqualTo(principalName); }
/** * Return a DBObject that can be used for updating the user in the mongo database. We only include * the fields that the client should be changing (leave out the internal fields). */ public DBObject getDBObjectForModify() { BasicDBObject user = new BasicDBObject(); user.append(JSON_KEY_USER_FIRST_NAME, firstName); user.append(JSON_KEY_USER_LAST_NAME, lastName); user.append(JSON_KEY_USER_WISH_LIST_LINK, wishListLink); // If the user logs in with Twitter, don't let them change their // twitter handle, username, or password. if (isTwitterLogin == false) { user.append(JSON_KEY_USER_TWITTER_HANDLE, twitterHandle); if (userName != null) { user.append(JSON_KEY_USER_NAME, userName); } if (passwordHash != null) { user.append(JSON_KEY_USER_PASSWORD_HASH, passwordHash); } if (passwordSalt != null) { user.append(JSON_KEY_USER_PASSWORD_SALT, passwordSalt); } } return user; }
public Object selectVar(String dsKey, String sql) { SelectVo selectVo = (SelectVo) sqlParser.parse(sql); DBCollection collection = MongoSupport.getCollection(dsKey, selectVo.getTable()); // return selectVo.selectVar(collection); fix bug Object result = selectVo.selectVar(collection); if (null == result) { return result; } if (result instanceof DBObject) { // return getXCOResults((DBObject) result, null); XCO one = getXCOResults((DBObject) result, null); return selectVo.selectVarOneField(one); } else { return result; } }
@Override public Model convert(DBObject dbObject) { final String type = dbObject.get("type").toString(); switch (ModelType.valueOf(type.toLowerCase())) { case TEXT: return new Text( stringOrElse(dbObject.get("value"), "") ); case BUTTON: return new Button( stringOrElse(dbObject.get("value"), "Click Here"), stringOrElse(dbObject.get("href"), "") ); } throw new UnsupportedOperationException( "Unsupported model type '" + type + "'." ); }
/** 회원의 게시글 정보를 aggregation을 활용하여 가져옴 * @param weaver * @return 회원의 커뮤니티 활동 내역 */ public DBObject getWeaverInfosInPost(Weaver weaver){ Criteria criteria = Criteria.where("writer.$id").is(weaver.getId()); AggregationOperation match = Aggregation.match(criteria); AggregationOperation group = Aggregation. group("writer").count().as("postCount").sum("push").as("push").sum("rePostCount").as("rePostCount"); Aggregation agg = newAggregation(match, group); return mongoTemplate.aggregate(agg, "post", DBObject.class).getUniqueMappedResult(); }
/** 회원의 저장소 정보를 aggregation을 활용하여 가져옴 * @param weaver * @return 회원의 저장소 활동 내역 */ public DBObject getWeaverInfosInRepository(Weaver weaver){ Criteria criteria = Criteria.where("creator.$id").is(weaver.getId()); AggregationOperation match = Aggregation.match(criteria); AggregationOperation group = Aggregation.group("creator").sum("push").as("repositoryPush"); Aggregation agg = newAggregation(match, group); return mongoTemplate.aggregate(agg, "repository", DBObject.class).getUniqueMappedResult(); }
@SuppressWarnings("unchecked") @Override public void concretProcessor(Object expKey, Map expValue, BasicDBObject set) { List<DBObject> orList = new ArrayList<DBObject>(); List<Map> orListMap = (List<Map>) expValue.get(expKey); for (int i = 0; i < orListMap.size(); i++) { Map expression = orListMap.get(i); Set keyset = expression.keySet(); Iterator iter = keyset.iterator(); BasicDBObject sub = new BasicDBObject(); while (iter.hasNext()) { String key = (String) iter.next(); if (("regex".equals(key)) || ("<".equals(key)) || (">".equals(key))) { RGLStrategy process = new RGLStrategy(); process.concretProcessor(key, expression, sub); } else { Object value = expression.get(key); sub.append(key, value); log.info(this, "@key: " + key + "@value:" + value); } } orList.add(sub); } set.append("$or", orList); }
public Map<String, Object> selectOne(String dsKey, String sql) { SelectVo selectVo = (SelectVo) sqlParser.parse(sql); DBCollection collection = MongoSupport.getCollection(dsKey, selectVo.getTable()); DBObject result = selectVo.selectOne(collection); if (null != result) { return getResults(result, null); } return null; }
@Override public void setQuery(DBObject query, BasicDBList orList) { if (null == orList) { query.put(this.name, new BasicDBObject("$lte", value.getValue())); } else { orList.add(new BasicDBObject(this.name, new BasicDBObject("$lte", value.getValue()))); } }
@Test public void sessionWrapperWithNoMaxIntervalShouldFallbackToDefaultValues() { // given MongoSession toSerialize = new MongoSession(); DBObject dbObject = convertToDBObject(toSerialize); Document document = new Document(dbObject.toMap()); document.remove("interval"); // when MongoSession convertedSession = this.mongoSessionConverter.convert(document); // then assertThat(convertedSession.getMaxInactiveInterval()).isEqualTo(Duration.ofMinutes(30)); }
public void delete(List<CanalEntry.Column> data, String schemaName, String tableName) { DBObject obj = DBConvertUtil.columnToJson(data); logger.debug("delete:{}", obj.toString()); if (schemaName.equals("order")) { logger.info("not support delete:{}.{}:{}", schemaName, tableName, obj); } else { deleteData(schemaName, tableName, obj); } }
public XCO selectOneXCO(String dsKey, String sql, MappingVo resultMap, Integer fetchSize) { SelectVo selectVo = (SelectVo) sqlParser.parse(sql); DBCollection collection = MongoSupport.getCollection(dsKey, selectVo.getTable()); DBObject result = selectVo.selectOne(collection); if (null != result) { return getXCOResults(result, resultMap); } return null; }
/** * 记录拼接表错误记录 * * @param schemaName * @param tableName * @param event 1:INSERT;2:UPDATE;3:DELETE * @param step 0:预处理数据错误;1:保存原始数据错误;2:保存组合数据错误, * @param obj * @param e */ private void logError(String schemaName, String tableName, int event, int step, DBObject obj, Exception e) { logger.error("error data:name[{},{}] , eventType : {} , data : [{}]", schemaName, tableName, event, obj); DBObject errObj = new BasicDBObject(); errObj.put("schemaName", schemaName); errObj.put("tableName", tableName); errObj.put("event", event); errObj.put("data", obj); errObj.put("step", step); errObj.put("time", new Date()); errObj.put("error", e.toString()); completeMongoTemplate.getCollection(NameConst.C_ERROR_RECORD).insert(errObj); }
public static Cubot deserialize(DBObject obj) { Cubot cubot = new Cubot(); cubot.setObjectId((long) obj.get("i")); cubot.setX((int) obj.get("x")); cubot.setY((int) obj.get("y")); cubot.hp = (int) obj.get("hp"); cubot.setDirection(Direction.getDirection((int) obj.get("direction"))); cubot.heldItem = (int) obj.get("heldItem"); cubot.energy = (int) obj.get("energy"); cubot.maxEnergy = GameServer.INSTANCE.getConfig().getInt("battery_max_energy"); return cubot; }
/** * 会进行sql注入处理. */ @Override public Object toFinalObject() throws ConditionException { DBObject baseObject = new BasicDBObject(); for (int i = 0; i < size(); i++) { Operator operator = getOperator(i); put(operator, get(i), baseObject); } return baseObject; }
/** * 用于$or * * @return */ private BasicDBList toDBList() { BasicDBList dbList = new BasicDBList(); for (int i = 0; i < size(); i++) { DBObject dbObject = new BasicDBObject(); Operator operator = getOperator(i); put(operator, get(i), dbObject); dbList.add(dbObject); } return dbList; }
private void log(DBObject document) { if (log.isInfoEnabled()) { if (null != document) { log.info("document:" + document.toString()); } } }
private void log(DBObject query) { if (log.isInfoEnabled()) { if (null != query) { log.info("query:" + query.toString()); } } }
public Step step2(){ return stepBuilderFactory.get("step2") .tasklet(new Tasklet(){ @Override public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception{ // checks if our collection exists Boolean doesexist = mongoTemplate.collectionExists("foo"); System.out.println("Status of collection returns :::::::::::::::::::::" + doesexist); // show all DBObjects in foo collection DBCursor alldocs = mongoTemplate.getCollection("foo").find(); List<DBObject> dbarray = alldocs.toArray(); System.out.println("list of db objects returns:::::::::::::::::::::" + dbarray); // execute the three methods we defined for querying the foo collection String result = doCollect(); String resultTwo = doCollectTwo(); String resultThree = doCollectThree(); System.out.println(" RESULT:::::::::::::::::::::" + result); System.out.println(" RESULT:::::::::::::::::::::" + resultTwo); System.out.println(" RESULT:::::::::::::::::::::" + resultThree); return RepeatStatus.FINISHED; } }).build(); }
public static CpuHardware deserialize(DBObject obj) { for (ServerPlugin plugin : GameServer.INSTANCE.getPluginManager().getPlugins()) { if (plugin instanceof CpuHardwareDeserializer) { CpuHardware hw = ((CpuHardwareDeserializer) plugin).deserializeHardware(obj); if (hw != null) { return hw; } } } return null; }