Example usage for com.mongodb QueryBuilder start

List of usage examples for com.mongodb QueryBuilder start

Introduction

In this page you can find the example usage for com.mongodb QueryBuilder start.

Prototype

public static QueryBuilder start(final String key) 

Source Link

Document

Creates a new query with a document key

Usage

From source file:org.sglover.entities.dao.mongo.MongoSimilarityDAO.java

License:Open Source License

@Override
public List<Similarity> getSimilar(Node node) {
    List<Similarity> nodes = new LinkedList<>();

    List<DBObject> ors = new LinkedList<>();
    ors.add(QueryBuilder.start("n1").is(node.getNodeId()).and("v1").is(node.getNodeVersion()).get());
    ors.add(QueryBuilder.start("n2").is(node.getNodeId()).and("v2").is(node.getNodeVersion()).get());

    DBObject query = QueryBuilder.start().or(ors.toArray(new DBObject[0])).get();

    DBObject orderBy = BasicDBObjectBuilder.start("s", -1).get();

    DBCursor cursor = similarityData.find(query).sort(orderBy);
    try {//w ww  .j ava2s  .co m
        for (DBObject dbObject : cursor) {
            String node1Id = (String) dbObject.get("n1");
            String node1Version = (String) dbObject.get("v1");
            String node2Id = (String) dbObject.get("n2");
            String node2Version = (String) dbObject.get("v2");
            double similarity = (Double) dbObject.get("s");

            if (node1Id.equals(node.getNodeId())) {
                if (!node2Id.equals(node.getNodeId())) {
                    Node n = Node.build().nodeId(node2Id).versionLabel(node2Version);
                    Similarity s = new Similarity(n, similarity);
                    nodes.add(s);
                }
            } else {
                if (node2Id.equals(node.getNodeId())) {
                    Node n = Node.build().nodeId(node1Id).versionLabel(node1Version);
                    Similarity s = new Similarity(n, similarity);
                    nodes.add(s);
                }
            }
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    return nodes;
}

From source file:org.sglover.entities.dao.mongo.MongoUserTrackingDAO.java

License:Open Source License

@Override
public List<ViewedNode> viewedNodes(String username, long timeDelta) {
    List<ViewedNode> viewedNodes = new LinkedList<>();

    long time = System.currentTimeMillis() - timeDelta;

    DBObject query = QueryBuilder.start("ic").is(true).and("u").is(username).and("t").greaterThanEquals(time)
            .get();/*from  ww w. j  a  v a  2 s  .c om*/
    DBCursor cursor = data.find(query);

    try {
        for (DBObject dbObject : cursor) {
            String nodeId = (String) dbObject.get("n");
            long nodeInternalId = (Long) dbObject.get("ni");
            String nodeVersion = (String) dbObject.get("v");
            long timestamp = (Long) dbObject.get("t");
            ViewedNode viewedNode = new ViewedNode(username, nodeId, nodeInternalId, nodeVersion, timestamp);
            viewedNodes.add(viewedNode);
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    return viewedNodes;
}

From source file:org.sglover.entities.dao.mongo.MongoUserTrackingDAO.java

License:Open Source License

@Override
public void txnCommitted(TransactionCommittedEvent event) {
    DBObject query = QueryBuilder.start("tx").is(event.getTxnId()).get();

    DBObject update = BasicDBObjectBuilder.start("$set", BasicDBObjectBuilder.start("ic", true).get()).get();

    WriteResult result = data.update(query, update, false, false);
    checkResult(result);/* w  ww.  j a  va  2s.c om*/
}

From source file:org.sipfoundry.authcode.AuthCodeManager.java

License:Open Source License

public static AuthCodeConfig getAuthCode(String code) {
    DBCollection entityCol = UnfortunateLackOfSpringSupportFactory.getImdb().getCollection("entity");
    Pattern codePattern = Pattern.compile("AuthCode.*");
    DBObject query = QueryBuilder.start(ID).is(codePattern).and(MongoConstants.AUTH_CODE).is(code).get();
    DBObject result = entityCol.findOne(query);
    if (result != null) {
        AuthCodeConfig conf = new AuthCodeConfig();
        conf.setAuthCode(getStringValue(result, MongoConstants.AUTH_CODE));
        conf.setAuthName(getStringValue(result, MongoConstants.UID));
        conf.setAuthPassword(getStringValue(result, MongoConstants.PASSTOKEN));
        return conf;
    }//from   w ww .  j  a  v  a 2 s  . c o m
    return null;
}

From source file:org.sipfoundry.commons.userdb.ValidUsers.java

License:Open Source License

/**
 * Loading all users into memory is an extremely expensive call for large systems (10K-50K user system).
 * Consider refactoring your code to not call this method.
 *
 * @return/*  ww w . j a v  a2 s  .c o m*/
 */
public List<User> getValidUsers() {
    List<User> users = new ArrayList<User>();
    try {
        DBCursor cursor = getEntityCollection().find(QueryBuilder.start(VALID_USER).is(Boolean.TRUE).get());
        Iterator<DBObject> objects = cursor.iterator();
        while (objects.hasNext()) {
            DBObject validUser = objects.next();
            if (!validUser.get(ID).toString().startsWith("User")) {
                BasicDBList aliasesObj = (BasicDBList) validUser.get(ALIASES);
                if (aliasesObj != null) {
                    for (int i = 0; i < aliasesObj.size(); i++) {
                        DBObject aliasObj = (DBObject) aliasesObj.get(i);
                        users.add(extractValidUserFromAlias(aliasObj));
                    }
                }
            } else {
                users.add(extractValidUser(validUser));
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return users;
}

From source file:org.sipfoundry.commons.userdb.ValidUsers.java

License:Open Source License

public DBCursor getUsersWithSpeedDial() {
    DBCursor cursor = getEntityCollection().find(QueryBuilder.start(ENTITY_NAME).is(ENTITY_NAME_USER)
            .or(QueryBuilder.start(SPEEDDIAL).exists(true).get(), new BasicDBObject(IM_ENABLED, "true")).get());
    return cursor;
}