Package org.bson

Examples of org.bson.BasicBSONObject


                    documents.add(obj);
                }
            }
        } catch (NoSuchCommandException e) {
            log.error("unknown command: {}", query, e);
            BSONObject obj = new BasicBSONObject();
            obj.put("errmsg", "no such cmd: " + e.getCommand());
            obj.put("bad cmd", query.getQuery());
            obj.put("code", Integer.valueOf(e.getCode()));
            obj.put("ok", Integer.valueOf(0));
            documents.add(obj);
        } catch (MongoServerError e) {
            log.error("failed to handle query {}", query, e);
            BSONObject obj = new BasicBSONObject();
            obj.put("errmsg", e.getMessage());
            obj.put("code", Integer.valueOf(e.getCode()));
            obj.put("ok", Integer.valueOf(0));
            documents.add(obj);
        } catch (MongoSilentServerException e) {
            BSONObject obj = new BasicBSONObject();
            obj.put("errmsg", e.getMessage());
            obj.put("ok", Integer.valueOf(0));
            documents.add(obj);
        } catch (MongoServerException e) {
            log.error("failed to handle query {}", query, e);
            BSONObject obj = new BasicBSONObject();
            obj.put("errmsg", e.getMessage());
            obj.put("ok", Integer.valueOf(0));
            documents.add(obj);
        }

        return new MongoReply(header, documents);
    }
View Full Code Here


    protected BSONObject handleCommand(Channel channel, MongoQuery query, List<BSONObject> documents)
            throws MongoServerException {
        String collectionName = query.getCollectionName();
        if (collectionName.equals("$cmd.sys.inprog")) {
            Collection<BSONObject> currentOperations = mongoBackend.getCurrentOperations(query);
            return new BasicBSONObject("inprog", currentOperations);
        }

        if (collectionName.equals("$cmd")) {
            String command = query.getQuery().keySet().iterator().next();
            if (command.equals("serverStatus")) {
View Full Code Here

        throw new MongoServerException("unknown collection: " + collectionName);
    }

    private BSONObject getServerStatus() throws MongoServerException {
        BSONObject serverStatus = new BasicBSONObject();
        try {
            serverStatus.put("host", InetAddress.getLocalHost().getHostName());
        } catch (UnknownHostException e) {
            throw new MongoServerException("failed to get hostname", e);
        }
        serverStatus.put("version", Utils.join(mongoBackend.getVersion(), '.'));
        serverStatus.put("process", "java");
        serverStatus.put("pid", getProcessId());

        serverStatus.put("uptime", Integer.valueOf((int) TimeUnit.NANOSECONDS.toSeconds(System.nanoTime() - started)));
        serverStatus.put("uptimeMillis", Long.valueOf(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - started)));
        serverStatus.put("localTime", new Date());

        BSONObject connections = new BasicBSONObject();
        connections.put("current", Integer.valueOf(channelGroup.size()));

        serverStatus.put("connections", connections);

        BSONObject cursors = new BasicBSONObject();
        cursors.put("totalOpen", Integer.valueOf(0)); // TODO

        serverStatus.put("cursors", cursors);

        Utils.markOkay(serverStatus);
View Full Code Here

    }

    @Test
    public void testMatchesPattern() throws Exception {
        BSONObject document = new BasicDBObject("name", "john");
        assertThat(matcher.matches(document, new BasicBSONObject("name", Pattern.compile("jo.*")))).isTrue();
        assertThat(
                matcher.matches(document,
                        new BasicBSONObject("name", Pattern.compile("Jo.*", Pattern.CASE_INSENSITIVE)))).isTrue();
        assertThat(matcher.matches(document, new BasicBSONObject("name", Pattern.compile("marta")))).isFalse();
        assertThat(matcher.matches(document, new BasicBSONObject("name", Pattern.compile("John")))).isFalse();

        String name = "\u0442\u0435\u0441\u0442";
        assertThat(name).hasSize(4);
        document.put("name", name);
        assertThat(matcher.matches(document, new BasicBSONObject("name", Pattern.compile(name)))).isTrue();

        assertThat(matcher.matches(document, new BasicBSONObject("name", new BasicBSONObject("$regex", name))))
                .isTrue();

        document.put("name", name.toLowerCase());
        assertThat(name.toLowerCase()).isNotEqualTo(name.toUpperCase());
        assertThat(
                matcher.matches(document,
                        new BasicBSONObject("name", new BasicBSONObject("$regex", name.toUpperCase())))).isFalse();
        assertThat(
                matcher.matches(
                        document,
                        new BasicBSONObject("name", new BasicBSONObject("$regex", name.toUpperCase()).append(
                                "$options", "i")))).isTrue();
    }
View Full Code Here

    @Test
    public void testMatchesNotPattern() throws Exception {

        // { item: { $not: /^p.*/ } }
        BSONObject query = new BasicBSONObject("item", new BasicBSONObject("$not", Pattern.compile("^p.*")));

        BSONObject document = json("{}");
        assertThat(matcher.matches(document, query)).isTrue();
        document.put("item", "p");
        assertThat(matcher.matches(document, query)).isFalse();
View Full Code Here

        BSONObject document = json("{}");

        for (String op : new String[] { "$and", "$or", "$nor" }) {

            BSONObject query = new BasicBSONObject(op, null);
            try {
                matcher.matches(document, query);
                fail("MongoServerError expected");
            } catch (MongoServerError e) {
                assertThat(e.getCode()).isEqualTo(14816);
                assertThat(e.getMessage()).isEqualTo(op + " expression must be a nonempty array");
            }

            query.put(op, 2);
            try {
                matcher.matches(document, query);
                fail("MongoServerError expected");
            } catch (MongoServerError e) {
                assertThat(e.getCode()).isEqualTo(14816);
                assertThat(e.getMessage()).isEqualTo(op + " expression must be a nonempty array");
            }

            query.put(op, new ArrayList<Object>());
            try {
                matcher.matches(document, query);
                fail("MongoServerError expected");
            } catch (MongoServerError e) {
                assertThat(e.getCode()).isEqualTo(14816);
                assertThat(e.getMessage()).isEqualTo(op + " expression must be a nonempty array");
            }

            query.put(op, Arrays.asList("a"));
            try {
                matcher.matches(document, query);
                fail("MongoServerError expected");
            } catch (MongoServerError e) {
                assertThat(e.getCode()).isEqualTo(14817);
View Full Code Here

    }

    @Test
    public void testDecodeObjects() throws Exception {
        List<BSONObject> objects = new ArrayList<BSONObject>();
        objects.add(new BasicBSONObject("key", new MaxKey()).append("foo", "bar"));
        objects.add(new BasicBSONObject("key", new MinKey()).append("test", new MaxKey()));

        for (BSONObject object : objects) {
            byte[] encodedData = new BasicBSONEncoder().encode(object);
            ByteBuf buf = Unpooled.wrappedBuffer(encodedData).order(ByteOrder.LITTLE_ENDIAN);
            BSONObject decodedObject = new BsonDecoder().decodeBson(buf);
View Full Code Here

public class DocumentComparatorTest {

    @Test
    public void testCompareSingleKey() {
        DocumentComparator comparator = new DocumentComparator(new BasicBSONObject("a", 1));

        List<BSONObject> list = new ArrayList<BSONObject>();
        list.add(new BasicBSONObject("a", 10));
        list.add(new BasicBSONObject("a", 15));
        list.add(new BasicBSONObject("a", 5));
        list.add(new BasicBSONObject("b", 1));

        Collections.sort(list, comparator);
        assertThat(list).containsExactly(new BasicBSONObject("b", 1), //
                new BasicBSONObject("a", 5), //
                new BasicBSONObject("a", 10), //
                new BasicBSONObject("a", 15));
    }
View Full Code Here

                new BasicBSONObject("a", 15));
    }

    @Test
    public void testCompareMultiKey() {
        DocumentComparator comparator = new DocumentComparator(new BasicBSONObject("a", 1).append("b", -1));

        List<BSONObject> list = new ArrayList<BSONObject>();
        list.add(new BasicBSONObject("a", 15).append("b", 3));
        list.add(new BasicBSONObject("a", 15).append("b", 2));
        list.add(new BasicBSONObject("a", 5));
        list.add(new BasicBSONObject("b", 1));
        list.add(new BasicBSONObject("b", 2));
        list.add(new BasicBSONObject("b", 3));

        Random rnd = new Random(4711);

        for (int i = 0; i < 10; i++) {
            Collections.shuffle(list, rnd);

            Collections.sort(list, comparator);
            assertThat(list).containsExactly(new BasicBSONObject("b", 3), //
                    new BasicBSONObject("b", 2), //
                    new BasicBSONObject("b", 1), //
                    new BasicBSONObject("a", 5), //
                    new BasicBSONObject("a", 15).append("b", 3), //
                    new BasicBSONObject("a", 15).append("b", 2));
        }
    }
View Full Code Here

        }
    }

    @Test
    public void testCompareCompoundKey() throws Exception {
        DocumentComparator comparator = new DocumentComparator(new BasicBSONObject("a.b", 1).append("c", -1));

        BSONObject a = new BasicBSONObject("a", new BasicBSONObject("b", 10));
        BSONObject b = new BasicBSONObject("a", new BasicBSONObject("b", 15));
        BSONObject c = new BasicBSONObject("a", new BasicBSONObject("b", 15)).append("x", 70);

        assertThat(comparator.compare(a, b)).isLessThan(0);
        assertThat(comparator.compare(b, a)).isGreaterThan(0);
        assertThat(comparator.compare(b, c)).isZero();
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.