Java Code Examples for org.bson.BasicBSONObject#put()

The following examples show how to use org.bson.BasicBSONObject#put() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SequoiaSQLParser.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
private int UpData(SQLUpdateStatement state) {
	SQLTableSource table=state.getTableSource();
	DBCollection coll =this._db.getCollection(table.toString());
	
	SQLExpr expr=state.getWhere();
	BSONObject query = parserWhere(expr);
	
	BasicBSONObject set = new BasicBSONObject();
	for(SQLUpdateSetItem col : state.getItems()){
		set.put(getFieldName2(col.getColumn()), getExpValue(col.getValue()));	
	}
	BSONObject mod = new BasicBSONObject("$set", set);
	//coll.updateMulti(query, mod);
	coll.update(query, mod, null);
	//System.out.println("changs count:"+coll.getStats().size());
	return 1;		
}
 
Example 2
Source File: SequoiaSQLParser.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
private void parserDBObject(BasicBSONObject ob,String akey, String aop,Object aval){
	boolean isok=false;
	if (!(ob.keySet().isEmpty())) {
         for (String field : ob.keySet()) {            
           if (akey.equals(field)){
              Object val = ob.get(field);	
             if (val instanceof BasicBSONObject) {
           	 ((BasicBSONObject) val).put(aop, aval);
           	 ob.put(field, (BasicBSONObject) val); 
           	 isok=true;
           	 break;
             } else if (val instanceof BasicBSONList) {
             //   newobj.put(field, ((BasicDBList)val).copy());
              }
           }  
         }    
       }    
	if (isok==false) {
		BasicBSONObject xo = new BasicBSONObject();
		xo.put(aop, aval);
		ob.put(akey,xo);	
	}
    
}
 
Example 3
Source File: SequoiaSQLParser.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unused")
private void opSQLExpr(SQLBinaryOpExpr expr,BasicBSONObject o) {
	   SQLExpr exprL=expr.getLeft();
	   if (!(exprL instanceof SQLBinaryOpExpr))
	   {
		  if (expr.getOperator().getName().equals("=")) {  
	        o.put(exprL.toString(), getExpValue(expr.getRight()));
		  }
		  else {
			  //BasicBSONObject xo = new BasicBSONObject();
			  String op="";
			  if (expr.getOperator().getName().equals("<")) {
				  op="$lt";
			  }
			  if (expr.getOperator().getName().equals("<=")) {
				  op = "$lte";
			  }
			  if (expr.getOperator().getName().equals(">")) {
				  op = "$gt";
			  }
			  if (expr.getOperator().getName().equals(">=")) {
				  op = "$gte";
			  }
			  if (expr.getOperator().getName().equals("!=")) {
				  op = "$ne";
			  }
			  if (expr.getOperator().getName().equals("<>")) {
				  op = "$ne";
			  }
			  //xo.put(op, getExpValue(expr.getRight()));
			 // o.put(exprL.toString(),xo);
			  parserDBObject(o,exprL.toString(),op, getExpValue(expr.getRight()));
		  }
	   }		
}
 
Example 4
Source File: SequoiaSQLParser.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
private void orWhere(SQLExpr exprL,SQLExpr exprR ,BasicBSONObject ob){ 
 BasicBSONObject xo = new BasicBSONObject(); 
 BasicBSONObject yo = new BasicBSONObject(); 
 parserWhere(exprL,xo);
 parserWhere(exprR,yo);
 ob.put("$or",new Object[]{xo,yo});
}
 
Example 5
Source File: ReplicaSetConfig.java    From brooklyn-library with Apache License 2.0 5 votes vote down vote up
/**
 * @return A {@link BasicBSONObject} representing the configuration that is suitable for a MongoDB server.
 */
public BasicBSONObject build() {
    setVotingMembers();
    BasicBSONObject config = new BasicBSONObject();
    config.put("_id", name);
    config.put("version", version);
    config.put("members", members);
    return config;
}
 
Example 6
Source File: SequoiaSQLParser.java    From Mycat2 with GNU General Public License v3.0 4 votes vote down vote up
private void parserWhere(SQLExpr aexpr,BasicBSONObject o){   
     if(aexpr instanceof SQLBinaryOpExpr){
	   SQLBinaryOpExpr expr=(SQLBinaryOpExpr)aexpr;  
	   SQLExpr exprL=expr.getLeft();
	   if (!(exprL instanceof SQLBinaryOpExpr))
	   {
		   //opSQLExpr((SQLBinaryOpExpr)aexpr,o);			   
		  if (expr.getOperator().getName().equals("=")) {  
	        o.put(exprL.toString(), getExpValue(expr.getRight()));
		  }
		  else {
			  String op="";
			  if (expr.getOperator().getName().equals("<")) {
				  op = "$lt";
			  }
			  if (expr.getOperator().getName().equals("<=")) {
				  op = "$lte";
			  }
			  if (expr.getOperator().getName().equals(">")) {
				  op = "$gt";
			  }
			  if (expr.getOperator().getName().equals(">=")) {
				  op = "$gte";
			  }
			  if (expr.getOperator().getName().equals("!=")) {
				  op = "$ne";
			  }
			  if (expr.getOperator().getName().equals("<>")) {
				  op = "$ne";
			  }

			  parserDBObject(o,exprL.toString(),op, getExpValue(expr.getRight()));
		  }
		  
	   }
	   else {
		 if (expr.getOperator().getName().equals("AND")) {  
		   parserWhere(exprL,o); 
		   parserWhere(expr.getRight(),o);
		 }
		 else if (expr.getOperator().getName().equals("OR")) {  
			orWhere(exprL,expr.getRight(),o); 				
		 }
		 else {
			 throw new RuntimeException("Can't identify the operation of  of where"); 
		 }
	   }
   }
  
}
 
Example 7
Source File: ReplicaSetConfig.java    From brooklyn-library with Apache License 2.0 4 votes vote down vote up
/**
 * Selects 1, 3, 5 or 7 members to have a vote. The primary member (as set by
 * {@link #primary(com.google.common.net.HostAndPort)}) is guaranteed a vote if
 * it is in {@link #members}.
 * <p/>
 *
 * Reconfiguring a server to be voters when they previously did not have votes generally triggers
 * a primary election. This confuses the MongoDB Java driver, which logs an error like:
 * <pre>
 * WARN  emptying DBPortPool to sams.home/192.168.1.64:27019 b/c of error
 * java.io.EOFException: null
 *    at org.bson.io.Bits.readFully(Bits.java:48) ~[mongo-java-driver-2.11.3.jar:na]
 * WARN  Command { "replSetReconfig" : ... } on sams.home/192.168.1.64:27019 failed
 * com.mongodb.MongoException$Network: Read operation to server sams.home/192.168.1.64:27019 failed on database admin
 *    at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:253) ~[mongo-java-driver-2.11.3.jar:na]
 * Caused by: java.io.EOFException: null
 *    at org.bson.io.Bits.readFully(Bits.java:48) ~[mongo-java-driver-2.11.3.jar:na]
 * </pre>
 *
 * The MongoDB documentation on <a href=http://docs.mongodb.org/manual/tutorial/configure-a-non-voting-replica-set-member/">
 * non-voting members</a> says:
 * <blockquote>
 *     Initializes a new replica set configuration. Disconnects the shell briefly and forces a
 *     reconnection as the replica set renegotiates which member will be primary. As a result,
 *     the shell will display an error even if this command succeeds.
 * </blockquote>
 *
 * So the problem is more that the MongoDB Java driver does not understand why the server
 * may have disconnected and is to eager to report a problem.
 */
private void setVotingMembers() {
    if (LOG.isDebugEnabled())
        LOG.debug("Setting voting and non-voting members of replica set: {}", name);
    boolean seenPrimary = false;
    String expectedPrimary = primary.isPresent()
            ? primary.get().getHostText() + ":" + primary.get().getPort()
            : "";

    // Ensure an odd number of voters
    int setSize = this.members.size();
    int nonPrimaryVotingMembers = Math.min(setSize % 2 == 0 ? setSize - 1 : setSize, MAXIMUM_VOTING_MEMBERS);
    if (primary.isPresent()) {
        if (LOG.isTraceEnabled())
            LOG.trace("Reserving vote for primary: " + expectedPrimary);
        nonPrimaryVotingMembers -= 1;
    }

    for (Object member : this.members) {
        if (member instanceof BasicBSONObject) {
            BasicBSONObject bsonObject = BasicBSONObject.class.cast(member);
            String host = bsonObject.getString("host");

            // is this member noted as the primary?
            if (this.primary.isPresent() && expectedPrimary.equals(host)) {
                bsonObject.put("votes", 1);
                seenPrimary = true;
                if (LOG.isDebugEnabled())
                    LOG.debug("Voting member (primary) of set {}: {}", name, host);
            } else if (nonPrimaryVotingMembers-- > 0) {
                bsonObject.put("votes", 1);
                if (LOG.isDebugEnabled())
                    LOG.debug("Voting member of set {}: {}", name, host);
            } else {
                bsonObject.put("votes", 0);
                if (LOG.isDebugEnabled())
                    LOG.debug("Non-voting member of set {}: {}", name, host);
            }
        } else {
            LOG.error("Unexpected entry in replica set members list: " + member);
        }
    }

    if (primary.isPresent() && !seenPrimary) {
        LOG.warn("Cannot give replica set primary a vote in reconfigured set: " +
                "primary was indicated as {} but no member with that host and port was seen in the set. " +
                "The replica set now has an even number of voters.",
                this.primary);
    }
}