Package org.bson

Examples of org.bson.BasicBSONObject


    private void serveRequest() throws IOException {
      BSONObject reqHead = decoder.readObject(clientSocket.getInputStream());
      BSONObject reqBody = decoder.readObject(clientSocket.getInputStream());

      BSONObject respHead = new BasicBSONObject();
      respHead.put(Constants.SERVICE_METHOD,
          ((String) reqHead.get(Constants.SERVICE_METHOD)).getBytes());
      respHead.put(Constants.SEQ, (UnsignedLong) reqHead.get(Constants.SEQ));
      if (!"Arith.Multiply".equals((String) reqHead.get(Constants.SERVICE_METHOD))) {
        respHead
            .put(Constants.ERROR, ("rpc: can't find method " + (String) reqHead
                .get(Constants.SERVICE_METHOD)).getBytes());
      }

      BSONObject respBody = new BasicBSONObject();
      if (!respHead.containsField(Constants.ERROR)) {
        long A = 0;
        long B = 0;
        if (reqBody.containsField("A")) {
          A = (long) reqBody.get("A");
        }
        if (reqBody.containsField("B")) {
          B = (long) reqBody.get("B");
        }
        respBody.put(BsonClientCodec.MAGIC_TAG, A * B);
      }

      clientSocket.getOutputStream().write(encoder.encode(respHead));
      clientSocket.getOutputStream().write(encoder.encode(respBody));
    }
View Full Code Here


    private BSONObject projectDocument(BSONObject document, BSONObject fields) {

        if (document == null)
            return null;

        BSONObject newDocument = new BasicBSONObject();
        for (String key : fields.keySet()) {
            if (Utils.isTrue(fields.get(key))) {
                projectField(document, newDocument, key);
            }
        }

        // implicitly add _id if not mentioned
        // http://docs.mongodb.org/manual/core/read-operations/#result-projections
        if (!fields.containsField(idField)) {
            newDocument.put(idField, document.get(idField));
        }

        return newDocument;
    }
View Full Code Here

            Object object = document.get(mainKey);
            // do not project the subdocument if it is not of type BSONObject
            if (object instanceof BSONObject) {
                if (!newDocument.containsField(mainKey)) {
                    newDocument.put(mainKey, new BasicBSONObject());
                }
                projectField((BSONObject) object, (BSONObject) newDocument.get(mainKey), subKey);
            }
        } else {
            newDocument.put(key, document.get(key));
View Full Code Here

            BSONObject document = documents.get(pos.intValue());
            if (document.containsField(key))
                values.add(document.get(key));
        }

        BSONObject response = new BasicBSONObject("values", new ArrayList<Object>(values));
        Utils.markOkay(response);
        return response;
    }
View Full Code Here

            if (!isMulti) {
                break;
            }
        }

        BSONObject result = new BasicBSONObject();

        // insert?
        if (n == 0 && isUpsert) {
            BSONObject newDocument = handleUpsert(updateQuery, selector);
            if (!selector.containsField(idField)) {
                result.put("upserted", newDocument.get(idField));
            }
            n++;
        }

        result.put("n", Integer.valueOf(n));
        result.put("updatedExisting", Boolean.valueOf(updatedExisting));
        return result;
    }
View Full Code Here

    private BSONObject updateDocument(BSONObject document, BSONObject updateQuery, Integer matchPos)
            throws MongoServerException {
        synchronized (document) {
            // copy document
            BSONObject oldDocument = new BasicBSONObject();
            cloneInto(oldDocument, document);

            BSONObject newDocument = calculateUpdateDocument(document, updateQuery, matchPos, false);

            if (!newDocument.equals(oldDocument)) {
View Full Code Here

        }
    }

    protected Object cloneValue(Object value) {
        if (value instanceof BSONObject) {
            BSONObject newValue = new BasicBSONObject();
            cloneInto(newValue, (BSONObject) value);
            return newValue;
        } else if (value instanceof List<?>) {
            @SuppressWarnings("unchecked")
            List<Object> list = (List<Object>) value;
            List<Object> newValue = new ArrayList<Object>();
            for (Object v : list) {
                newValue.add(cloneValue(v));
            }
            return newValue;
        } else {
            return value;
        }
View Full Code Here

    /**
     * convert selector used in an upsert statement into a document
     */
    BSONObject convertSelectorToDocument(BSONObject selector) throws MongoServerException {
        BSONObject document = new BasicBSONObject();
        for (String key : selector.keySet()) {
            if (key.startsWith("$"))
                continue;

            Object value = selector.get(key);
View Full Code Here

        }
        return count;
    }

    public BSONObject getStats() {
        BSONObject response = new BasicBSONObject("ns", getFullName());
        response.put("count", Integer.valueOf(documents.size()));
        response.put("size", Long.valueOf(dataSize.get()));

        double averageSize = 0;
        if (!documents.isEmpty()) {
            averageSize = dataSize.get() / (double) documents.size();
        }
        response.put("avgObjSize", Double.valueOf(averageSize));
        response.put("storageSize", Integer.valueOf(0));
        response.put("numExtents", Integer.valueOf(0));
        response.put("nindexes", Integer.valueOf(indexes.size()));
        BSONObject indexSizes = new BasicBSONObject();
        for (Index index : indexes) {
            indexSizes.put(index.getName(), Long.valueOf(index.getDataSize()));
        }

        response.put("indexSize", indexSizes);
        Utils.markOkay(response);
        return response;
View Full Code Here

        Utils.markOkay(response);
        return response;
    }

    public BSONObject validate() {
        BSONObject response = new BasicBSONObject("ns", getFullName());
        response.put("extentCount", Integer.valueOf(0));
        response.put("datasize", Long.valueOf(dataSize.get()));
        response.put("nrecords", Integer.valueOf(documents.size()));
        response.put("padding", Integer.valueOf(1));
        response.put("deletedCount", Integer.valueOf(emptyPositions.size()));
        response.put("deletedSize", Integer.valueOf(0));

        response.put("nIndexes", Integer.valueOf(indexes.size()));
        BSONObject keysPerIndex = new BasicBSONObject();
        for (Index index : indexes) {
            keysPerIndex.put(index.getName(), Long.valueOf(index.getCount()));
        }

        response.put("keysPerIndex", keysPerIndex);
        response.put("valid", Boolean.TRUE);
        response.put("errors", Arrays.asList());
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.