Package org.bson

Examples of org.bson.BSONObject


    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


    System.arraycopy(bodyBytes, 0, bytes, headerBytes.length, bodyBytes.length);
    socket.getOutputStream().write(bytes);
  }

  public void ReadResponseHeader(Response response) throws IOException {
    BSONObject headerBson = decoder.readObject(socket.getInputStream());
    response.setServiceMethod(new String((byte[]) headerBson.get(Constants.SERVICE_METHOD)));
    response.setSeq((UnsignedLong) headerBson.get(Constants.SEQ));
    if (headerBson.containsField(Constants.ERROR)) {
      Object error = headerBson.get(Constants.ERROR);
      if (error instanceof byte[] && ((byte[]) error).length != 0) {
        String errorMsg = new String((byte[]) error, CharEncoding.UTF_8);
        response.setError(errorMsg);
      }
    }
View Full Code Here

      }
    }
  }

  public void ReadResponseBody(Response response) throws IOException {
    BSONObject bodyBson = decoder.readObject(socket.getInputStream());
    if (bodyBson.containsField(MAGIC_TAG)) {
      response.setReply(bodyBson.get(MAGIC_TAG));
    } else {
      response.setReply(bodyBson);
    }
  }
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;
        } else if (command.equalsIgnoreCase("replSetGetStatus")) {
            throw new MongoSilentServerException("not running with --replSet");
        } else if (command.equalsIgnoreCase("getLog")) {
            final Object argument = query.get(command);
            BSONObject response = getLog(argument == null ? null : argument.toString());
            return response;
        } else {
            throw new NoSuchCommandException(command);
        }
    }
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

        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;
                        }
View Full Code Here

        return false;
    }

    private boolean checkMatchesValue(Object queryValue, Object value, boolean valueExists) throws MongoServerException {
        if (queryValue instanceof BSONObject) {
            BSONObject queryObject = (BSONObject) queryValue;

            if (queryObject.containsField("$regex")) {
                String options = "";
                if (queryObject.containsField("$options")) {
                    options = queryObject.get("$options").toString();
                }

                Pattern pattern = Utils.createPattern(queryObject.get("$regex").toString(), options);
                return pattern.matcher(value.toString()).find();
            }

            for (String key : queryObject.keySet()) {
                Object querySubvalue = queryObject.get(key);
                if (key.startsWith("$")) {
                    if (!checkExpressionMatch(value, valueExists, querySubvalue, key)) {
                        return false;
                    }
                } else {
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

    @Override
    public void handleDelete(MongoDelete delete) throws MongoServerException {
        Channel channel = delete.getChannel();
        final String collectionName = delete.getCollectionName();
        final BSONObject selector = delete.getSelector();
        final int limit = delete.isSingleRemove() ? 1 : Integer.MAX_VALUE;

        try {
            deleteDocuments(channel, collectionName, selector, limit);
        } catch (MongoServerException e) {
View Full Code Here

    @Override
    public void handleUpdate(MongoUpdate updateCommand) throws MongoServerException {
        final Channel channel = updateCommand.getChannel();
        final String collectionName = updateCommand.getCollectionName();
        final BSONObject selector = updateCommand.getSelector();
        final BSONObject update = updateCommand.getUpdate();
        final boolean multi = updateCommand.isMulti();
        final boolean upsert = updateCommand.isUpsert();

        try {
            BSONObject result = updateDocuments(channel, collectionName, selector, update, multi, upsert);
            putLastResult(channel, result);
        } catch (MongoServerException e) {
            log.error("failed to update {}", updateCommand, e);
        }
    }
View Full Code Here

TOP

Related Classes of org.bson.BSONObject

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.