Package org.bson

Examples of org.bson.BasicBSONObject


import java.util.List;
import java.util.Map;

public class Bsonify {
  public static BSONObject queryToBson(Query query) {
    BSONObject b = new BasicBSONObject();
    b.put("Sql", query.getSql());
    b.put("Keyspace", query.getKeyspace());
    b.put("TabletType", query.getTabletType());
    if (query.getBindVars() != null) {
      b.put("BindVariables", bindVarsToBSON(query.getBindVars()));
    }
    if (query.getKeyspaceIds() != null) {
      b.put("KeyspaceIds", query.getKeyspaceIds());
    } else {
      b.put("KeyRanges", query.getKeyRanges());
    }
    if (query.getSession() != null) {
      b.put("Session", query.getSession());
    }
    return b;
  }
View Full Code Here


    }
    return b;
  }

  public static BSONObject bindVarsToBSON(List<BindVariable> bindVars) {
    BSONObject bindVariables = new BasicBSONObject();
    for (BindVariable b : bindVars) {
      if (b.getType().equals(BindVariable.Type.NULL)) {
        bindVariables.put(b.getName(), null);
      }
      if (b.getType().equals(BindVariable.Type.LONG)) {
        bindVariables.put(b.getName(), b.getLongVal());
      }
      if (b.getType().equals(BindVariable.Type.UNSIGNED_LONG)) {
        bindVariables.put(b.getName(), b.getULongVal());
      }
      if (b.getType().equals(BindVariable.Type.DOUBLE)) {
        bindVariables.put(b.getName(), b.getDoubleVal());
      }
      if (b.getType().equals(BindVariable.Type.BYTE_ARRAY)) {
        bindVariables.put(b.getName(), b.getByteArrayVal());
      }
    }
    return bindVariables;
  }
View Full Code Here

    }
    return bindVariables;
  }

  public static BSONObject batchQueryToBson(BatchQuery batchQuery) {
    BSONObject b = new BasicBSONObject();
    List<Map<String, Object>> queries = new LinkedList<>();
    Iterator<String> sqlIter = batchQuery.getSqls().iterator();
    Iterator<List<BindVariable>> bvIter = batchQuery.getBindVarsList().iterator();
    while (sqlIter.hasNext()) {
      Map<String, Object> query = new HashMap<>();
      query.put("Sql", sqlIter.next());
      List<BindVariable> bindVars = bvIter.next();
      if (bindVars != null) {
        query.put("BindVariables", bindVarsToBSON(bindVars));
      }
      queries.add(query);
    }
    b.put("Queries", queries);
    b.put("Keyspace", batchQuery.getKeyspace());
    b.put("TabletType", batchQuery.getTabletType());
    b.put("KeyspaceIds", batchQuery.getKeyspaceIds());
    if (batchQuery.getSession() != null) {
      b.put("Session", batchQuery.getSession());
    }
    return b;
  }
View Full Code Here

    }
    return rowList;
  }

  public static BSONObject splitQueryRequestToBson(SplitQueryRequest request) {
    BSONObject query = new BasicBSONObject();
    query.put("Sql", request.getSql());
    BSONObject b = new BasicBSONObject();
    b.put("Keyspace", request.getKeyspace());
    b.put("Query", query);
    b.put("SplitsPerShard", request.getSplitsPerShard());
    return b;
  }
View Full Code Here

    return sqlQueryResponseFactory.create(methodName, bsonBody);
  }


  protected BSONObject createBsonHeader(String methodName, int sequence) {
    BSONObject bsonObject = new BasicBSONObject();
    bsonObject.put("ServiceMethod", methodToString(methodName));
    bsonObject.put("Seq", sequence);
    return bsonObject;
  }
View Full Code Here

    encoder = new GoRpcBsonEncoder();
    decoder = new GoRpcBsonDecoder();
  }

  public void WriteRequest(Request request, Object args) throws IOException {
    BSONObject header = new BasicBSONObject();
    header.put(Constants.SERVICE_METHOD, request.getServiceMethod());
    header.put(Constants.SEQ, request.getSeq());
    byte[] headerBytes = encoder.encode(header);
    byte[] bodyBytes = encoder.encode((BSONObject) args);
    byte[] bytes = new byte[headerBytes.length + bodyBytes.length];
    System.arraycopy(headerBytes, 0, bytes, 0, headerBytes.length);
    System.arraycopy(bodyBytes, 0, bytes, headerBytes.length, bodyBytes.length);
View Full Code Here

            }

            // handle $all
            if (queryValue instanceof BSONObject && ((BSONObject) queryValue).keySet().contains("$all")) {
                // clone first
                queryValue = new BasicBSONObject(((BSONObject) queryValue).toMap());
                Object allQuery = ((BSONObject) queryValue).removeField("$all");
                if (!checkMatchesAllDocuments(allQuery, keys, document)) {
                    return false;
                }
                // continue matching the remainder of queryValue
            }

            return checkMatchesAnyDocument(queryValue, keys, document);
        }

        if (!subKeys.isEmpty()) {
            Object subObject = Utils.getListSafe(document, firstKey);
            return checkMatch(queryValue, subKeys, subObject);
        }

        if (!(document instanceof BSONObject)) {
            return false;
        }

        Object value = ((BSONObject) document).get(firstKey);
        boolean valueExists = ((BSONObject) document).containsField(firstKey);

        if (value instanceof Collection<?>) {
            if (queryValue instanceof BSONObject) {
                Set<String> keySet = ((BSONObject) queryValue).keySet();

                // clone first
                BSONObject queryValueClone = new BasicBSONObject(((BSONObject) queryValue).toMap());

                for (String queryOperator : keySet) {

                    Object subQuery = queryValueClone.removeField(queryOperator);

                    if (queryOperator.equals(QueryOperator.ALL.getValue())) {
                        if (!checkMatchesAllValues(subQuery, value)) {
                            return false;
                        }
                    } else if (queryOperator.equals(QueryOperator.IN.getValue())) {
                        final BasicBSONObject inQuery = new BasicBSONObject(queryOperator, subQuery);
                        if (!checkMatchesAnyValue(inQuery, value))
                            return false;
                    } else if (queryOperator.equals(QueryOperator.NOT_IN.getValue())) {
                        if (checkMatchesAllValues(subQuery, value))
                            return false;
View Full Code Here

    protected BSONObject handleAdminCommand(Channel channel, String command, BSONObject query)
            throws MongoServerException {

        if (command.equalsIgnoreCase("listdatabases")) {
            BSONObject response = new BasicBSONObject();
            List<BSONObject> dbs = new ArrayList<BSONObject>();
            for (MongoDatabase db : databases.values()) {
                BasicBSONObject dbObj = new BasicBSONObject("name", db.getDatabaseName());
                dbObj.put("empty", Boolean.valueOf(db.isEmpty()));
                dbs.add(dbObj);
            }
            response.put("databases", dbs);
            Utils.markOkay(response);
            return response;
View Full Code Here

        }
    }

    private BSONObject getLog(String argument) throws MongoServerException {
        log.debug("getLog: {}", argument);
        BSONObject response = new BasicBSONObject();
        if (argument.equals("*")) {
            response.put("names", Arrays.asList("startupWarnings"));
            Utils.markOkay(response);
        } else if (argument.equals("startupWarnings")) {
            response.put("totalLinesWritten", Integer.valueOf(0));
            response.put("log", new ArrayList<String>());
            Utils.markOkay(response);
        } else {
            throw new MongoSilentServerException("no RamLog named: " + argument);
        }
        return response;
View Full Code Here

    @Override
    public BSONObject handleCommand(Channel channel, String databaseName, String command, BSONObject query)
            throws MongoServerException {

        if (command.equalsIgnoreCase("whatsmyuri")) {
            BSONObject response = new BasicBSONObject();
            InetSocketAddress remoteAddress = (InetSocketAddress) channel.remoteAddress();
            response.put("you", remoteAddress.getAddress().getHostAddress() + ":" + remoteAddress.getPort());
            Utils.markOkay(response);
            return response;
        } else if (command.equalsIgnoreCase("ismaster")) {
            BSONObject response = new BasicBSONObject("ismaster", Boolean.TRUE);
            response.put("maxBsonObjectSize", Integer.valueOf(BsonConstants.MAX_BSON_OBJECT_SIZE));
            response.put("maxMessageSizeBytes", Integer.valueOf(MongoWireProtocolHandler.MAX_MESSAGE_SIZE_BYTES));
            response.put("localTime", new Date());
            Utils.markOkay(response);
            return response;
        } else if (command.equalsIgnoreCase("buildinfo")) {
            BSONObject response = new BasicBSONObject("version", Utils.join(VERSION, '.'));
            response.put("versionArray", VERSION);
            response.put("maxBsonObjectSize", Integer.valueOf(BsonConstants.MAX_BSON_OBJECT_SIZE));
            Utils.markOkay(response);
            return response;
        }

        if (databaseName.equals("admin")) {
View Full Code Here

TOP

Related Classes of org.bson.BasicBSONObject

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.