Example usage for com.mongodb DBObject toMap

List of usage examples for com.mongodb DBObject toMap

Introduction

In this page you can find the example usage for com.mongodb DBObject toMap.

Prototype

Map toMap();

Source Link

Document

Returns a map representing this BSONObject.

Usage

From source file:org.graylog2.streams.StreamServiceImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
public Stream load(ObjectId id) throws NotFoundException {
    DBObject o = get(StreamImpl.class, id);

    if (o == null) {
        throw new NotFoundException("Stream <" + id + "> not found!");
    }/*  w  ww  .ja v a2s  .  c  om*/

    List<StreamRule> streamRules = streamRuleService.loadForStreamId(id.toHexString());

    Set<Output> outputs = loadOutputsForRawStream(o);

    return new StreamImpl((ObjectId) o.get("_id"), o.toMap(), streamRules, outputs);
}

From source file:org.graylog2.system.activities.SystemMessageServiceImpl.java

License:Open Source License

@Override
public List<SystemMessage> all(int page) {
    List<SystemMessage> messages = Lists.newArrayList();

    DBObject sort = new BasicDBObject();
    sort.put("timestamp", -1);

    List<DBObject> results = query(SystemMessageImpl.class, new BasicDBObject(), sort, PER_PAGE,
            PER_PAGE * page);/*from w  ww  .jav a 2s .c  o  m*/
    for (DBObject o : results) {
        messages.add(new SystemMessageImpl(new ObjectId(o.get("_id").toString()), o.toMap()));
    }

    return messages;
}

From source file:org.graylog2.users.UserServiceImpl.java

License:Open Source License

@Override
public User load(final String username) {
    LOG.debug("Loading user {}", username);

    // special case for the locally defined user, we don't store that in MongoDB.
    if (configuration.getRootUsername().equals(username)) {
        LOG.debug("User {} is the built-in admin user", username);
        return new UserImpl.LocalAdminUser(configuration);
    }//from   w ww . j  av a2 s  . c  o  m

    final DBObject query = new BasicDBObject();
    query.put(UserImpl.USERNAME, username);

    final List<DBObject> result = query(UserImpl.class, query);
    if (result == null || result.isEmpty()) {
        return null;
    }

    if (result.size() > 1) {
        final String msg = "There was more than one matching user for username " + username
                + ". This should never happen.";
        LOG.error(msg);
        throw new RuntimeException(msg);
    }

    final DBObject userObject = result.get(0);
    final Object userId = userObject.get("_id");

    LOG.debug("Loaded user {}/{} from MongoDB", username, userId);
    return new UserImpl((ObjectId) userId, userObject.toMap());
}

From source file:org.graylog2.users.UserServiceImpl.java

License:Open Source License

@Override
public List<User> loadAll() {
    final DBObject query = new BasicDBObject();
    final List<DBObject> result = query(UserImpl.class, query);

    final List<User> users = Lists.newArrayList();
    for (DBObject dbObject : result) {
        users.add(new UserImpl((ObjectId) dbObject.get("_id"), dbObject.toMap()));
    }/*from   ww  w.j  a v a 2  s .co  m*/

    return users;
}

From source file:org.hibernate.ogm.datastore.mongodb.utils.MongoDBTestHelper.java

License:LGPL

@Override
@SuppressWarnings("unchecked")
public Map<String, Object> extractEntityTuple(SessionFactory sessionFactory, EntityKey key) {
    MongoDBDatastoreProvider provider = MongoDBTestHelper.getProvider(sessionFactory);
    DBObject finder = new BasicDBObject(MongoDBDialect.ID_FIELDNAME, key.getColumnValues()[0]);
    DBObject result = provider.getDatabase().getCollection(key.getTable()).findOne(finder);
    replaceIdentifierColumnName(result, key);
    return result.toMap();
}

From source file:org.hibernate.ogm.test.utils.MongoDBTestHelper.java

License:Open Source License

private int countAssociationOnCollection(DBCollection collection) {
    DBCursor cursor = collection.find(new BasicDBObject(), new BasicDBObject(MongoDBDialect.ID_FIELDNAME, 0));
    Iterator<DBObject> it = cursor.iterator();
    int count = 0;
    while (it.hasNext()) {
        DBObject current = it.next();
        Map<?, ?> map = current.toMap();
        count += this.countAssociationOnDocument(map);
    }//from   w w  w  .j a v  a 2  s . co  m
    return count;
}

From source file:org.hibernate.ogm.test.utils.MongoDBTestHelper.java

License:Open Source License

@Override
public Map<String, Object> extractEntityTuple(SessionFactory sessionFactory, EntityKey key) {
    MongoDBDatastoreProvider provider = MongoDBTestHelper.getProvider(sessionFactory);
    DBObject finder = new BasicDBObject(MongoDBDialect.ID_FIELDNAME, key.getColumnValues()[0]);
    DBObject result = provider.getDatabase().getCollection(key.getTable()).findOne(finder);
    return result.toMap();
}

From source file:org.iternine.jeppetto.dao.mongodb.MongoDBQueryModelDAO.java

License:Apache License

protected final void trueSave(final DBObject identifyingQuery, final DirtyableDBObject dbo) {
    if (optimisticLockEnabled) {
        Integer optimisticLockVersion = (Integer) dbo.get(OPTIMISTIC_LOCK_VERSION_FIELD);

        if (optimisticLockVersion == null) {
            dbo.put(OPTIMISTIC_LOCK_VERSION_FIELD, 1);
        } else {/*from w  ww .  j a  v a2s .  com*/
            // TODO: should this modification of identifyingQuery been done earlier (in save())?
            identifyingQuery.put(OPTIMISTIC_LOCK_VERSION_FIELD, optimisticLockVersion);

            dbo.put(OPTIMISTIC_LOCK_VERSION_FIELD, optimisticLockVersion + 1);
        }
    }

    for (String shardKey : shardKeys) {
        identifyingQuery.put(shardKey, dbo.get(shardKey));
    }

    final DBObject optimalDbo = determineOptimalDBObject(dbo);

    if (queryLogger != null) {
        queryLogger.debug("Saving {} identified by {} with document {}", new Object[] {
                getCollectionClass().getSimpleName(), identifyingQuery.toMap(), optimalDbo.toMap() });
    }

    dbCollection.update(identifyingQuery, optimalDbo, true, false, getWriteConcern());

    dbo.markPersisted();
}

From source file:org.iternine.jeppetto.dao.mongodb.MongoDBQueryModelDAO.java

License:Apache License

protected final void trueRemove(final DBObject identifyingQuery) {
    if (optimisticLockEnabled) {
        // TODO:/*ww w. ja  v  a 2 s. c  o m*/
    }

    if (queryLogger != null) {
        queryLogger.debug("Removing {}s matching {}",
                new Object[] { getCollectionClass().getSimpleName(), identifyingQuery.toMap() });
    }

    dbCollection.remove(identifyingQuery, getWriteConcern());
}

From source file:org.iternine.jeppetto.dao.mongodb.MongoDBQueryModelDAO.java

License:Apache License

private Map<String, Set<String>> ensureIndexes(List<String> uniqueIndexes, final boolean unique) {
    if (uniqueIndexes == null || uniqueIndexes.size() == 0) {
        return Collections.emptyMap();
    }/* w w w  .jav a2  s.  co  m*/

    Map<String, Set<String>> result = new HashMap<String, Set<String>>();

    for (final String uniqueIndex : uniqueIndexes) {
        final DBObject index = new BasicDBObject();
        String[] indexFields = uniqueIndex.split(",");

        for (String indexField : indexFields) {
            indexField = indexField.trim();

            if (indexField.startsWith("+")) {
                index.put(indexField.substring(1), 1);
            } else if (indexField.startsWith("-")) {
                index.put(indexField.substring(1), -1);
            } else {
                index.put(indexField, 1);
            }
        }

        result.put(uniqueIndex, index.keySet());

        if (queryLogger != null) {
            queryLogger.debug("Ensuring index {} on {}",
                    new Object[] { index.toMap(), getCollectionClass().getSimpleName() });
        }

        dbCollection.ensureIndex(index, createIndexName(uniqueIndex), unique);
    }

    return result;
}