@Override public List<Input> allOfThisNode(final String nodeId) { final List<BasicDBObject> query = ImmutableList.of( new BasicDBObject(MessageInput.FIELD_NODE_ID, nodeId), new BasicDBObject(MessageInput.FIELD_GLOBAL, true)); final List<DBObject> ownInputs = query(InputImpl.class, new BasicDBObject("$or", query)); final ImmutableList.Builder<Input> inputs = ImmutableList.builder(); for (final DBObject o : ownInputs) { inputs.add(new InputImpl((ObjectId) o.get(InputImpl.FIELD_ID), o.toMap())); } return inputs.build(); }
@Override public List<IndexFailure> all(int limit, int offset) { final DBObject sort = new BasicDBObject("$natural", -1); final List<DBObject> results = query(IndexFailureImpl.class, new BasicDBObject(), sort, limit, offset); final List<IndexFailure> failures = new ArrayList<>(results.size()); for (DBObject o : results) { failures.add(new IndexFailureImpl((ObjectId) o.get("_id"), o.toMap())); } return failures; }
@Override public List<Dashboard> all() { final List<DBObject> results = query(DashboardImpl.class, new BasicDBObject()); final Stream<Dashboard> dashboardStream = results.stream() .map(o -> (Dashboard) new DashboardImpl((ObjectId) o.get(DashboardImpl.FIELD_ID), o.toMap())); return dashboardStream .collect(Collectors.toList()); }
@Override public List<Input> all() { final List<DBObject> ownInputs = query(InputImpl.class, new BasicDBObject()); final ImmutableList.Builder<Input> inputs = ImmutableList.builder(); for (final DBObject o : ownInputs) { inputs.add(new InputImpl((ObjectId) o.get(InputImpl.FIELD_ID), o.toMap())); } return inputs.build(); }
@Override @SuppressWarnings("unchecked") public List<SavedSearch> all() { List<SavedSearch> searches = Lists.newArrayList(); List<DBObject> results = query(SavedSearchImpl.class, new BasicDBObject()); for (DBObject o : results) { searches.add(new SavedSearchImpl((ObjectId) o.get("_id"), o.toMap())); } return searches; }
@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(userFactory.create((ObjectId) dbObject.get("_id"), dbObject.toMap())); } return users; }
@Override public Input findForThisNodeOrGlobal(String nodeId, String id) throws NotFoundException { final List<BasicDBObject> forThisNodeOrGlobal = ImmutableList.of( new BasicDBObject(MessageInput.FIELD_NODE_ID, nodeId), new BasicDBObject(MessageInput.FIELD_GLOBAL, true)); final List<BasicDBObject> query = ImmutableList.of( new BasicDBObject(InputImpl.FIELD_ID, new ObjectId(id)), new BasicDBObject("$or", forThisNodeOrGlobal)); final DBObject o = findOne(InputImpl.class, new BasicDBObject("$and", query)); return new InputImpl((ObjectId) o.get(InputImpl.FIELD_ID), o.toMap()); }
@Override public Collection<MongoDbSession> loadAll() { DBObject query = new BasicDBObject(); List<MongoDbSession> dbSessions = Lists.newArrayList(); final List<DBObject> sessions = query(MongoDbSession.class, query); for (DBObject session : sessions) { dbSessions.add(new MongoDbSession((ObjectId) session.get("_id"), session.toMap())); } return dbSessions; } }
@SuppressWarnings("unchecked") private StreamRule toStreamRule(DBObject dbObject) { final Map<String, Object> fields = dbObject.toMap(); return new StreamRuleImpl((ObjectId) dbObject.get("_id"), fields); } }
@Override public List<Notification> all() { final List<DBObject> dbObjects = query(NotificationImpl.class, new BasicDBObject(), new BasicDBObject(NotificationImpl.FIELD_TIMESTAMP, -1)); final List<Notification> notifications = Lists.newArrayListWithCapacity(dbObjects.size()); for (DBObject obj : dbObjects) { try { notifications.add(new NotificationImpl(new ObjectId(obj.get("_id").toString()), obj.toMap())); } catch (IllegalArgumentException e) { LOG.warn("There is a notification type we can't handle: [{}]", obj.get(NotificationImpl.FIELD_TYPE)); } } return notifications; }
@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); for (DBObject o : results) { messages.add(new SystemMessageImpl(new ObjectId(o.get("_id").toString()), o.toMap())); } return messages; }
@Override public Set<Dashboard> loadByIds(Collection<String> ids) { final Set<ObjectId> objectIds = ids.stream() .map(ObjectId::new) .collect(Collectors.toSet()); final DBObject query = BasicDBObjectBuilder.start() .push(DashboardImpl.FIELD_ID) .append("$in", objectIds) .get(); final List<DBObject> results = query(DashboardImpl.class, query); final Stream<Dashboard> dashboardStream = results.stream() .map(o -> (Dashboard) new DashboardImpl((ObjectId) o.get(DashboardImpl.FIELD_ID), o.toMap())); return dashboardStream .collect(Collectors.toSet()); }
@Override @SuppressWarnings("unchecked") public List<AccessToken> loadAll(String username) { DBObject query = new BasicDBObject(); query.put(AccessTokenImpl.USERNAME, username); final List<DBObject> objects = query(AccessTokenImpl.class, query); List<AccessToken> tokens = Lists.newArrayList(); for (DBObject tokenObject : objects) { final Object id = tokenObject.get("_id"); final AccessToken accessToken = new AccessTokenImpl((ObjectId) id, tokenObject.toMap()); tokens.add(accessToken); } return tokens; }
@Override public Set<Input> findByIds(Collection<String> ids) { final Set<ObjectId> objectIds = ids.stream() .map(ObjectId::new) .collect(Collectors.toSet()); final DBObject query = BasicDBObjectBuilder.start() .push(InputImpl.FIELD_ID) .append("$in", objectIds) .get(); final Stream<InputImpl> inputStream = query(InputImpl.class, query).stream() .map(o -> new InputImpl((ObjectId) o.get(InputImpl.FIELD_ID), o.toMap())); return inputStream .collect(Collectors.toSet()); }
@Override @Nullable public MongoDbSession load(String sessionId) { DBObject query = new BasicDBObject(); query.put(MongoDbSession.FIELD_SESSION_ID, sessionId); DBObject result = findOne(MongoDbSession.class, query); if (result == null) { return null; } final Object objectId = result.get("_id"); return new MongoDbSession((ObjectId) objectId, result.toMap()); }
@Override public Collection<User> loadAllForRole(Role role) { final String roleId = role.getId(); final DBObject query = BasicDBObjectBuilder.start(UserImpl.ROLES, new ObjectId(roleId)).get(); final List<DBObject> result = query(UserImpl.class, query); if (result == null || result.isEmpty()) { return Collections.emptySet(); } final Set<User> users = Sets.newHashSetWithExpectedSize(result.size()); for (DBObject dbObject : result) { //noinspection unchecked users.add(userFactory.create((ObjectId) dbObject.get("_id"), dbObject.toMap())); } return users; }
@Override public Map<String, Node> allActive(Node.Type type) { Map<String, Node> nodes = Maps.newHashMap(); BasicDBObject query = new BasicDBObject(); query.put("last_seen", new BasicDBObject("$gte", Tools.getUTCTimestamp() - pingTimeout)); query.put("type", type.toString()); for (DBObject obj : query(NodeImpl.class, query)) { Node node = new NodeImpl((ObjectId) obj.get("_id"), obj.toMap()); String nodeId = (String) obj.get("node_id"); nodes.put(nodeId, node); } return nodes; }
@Override public Node byNodeId(String nodeId) throws NodeNotFoundException { DBObject query = new BasicDBObject("node_id", nodeId); DBObject o = findOne(NodeImpl.class, query); if (o == null || !o.containsField("node_id")) { throw new NodeNotFoundException("Unable to find node " + nodeId); } return new NodeImpl((ObjectId) o.get("_id"), o.toMap()); }
@Override public Input find(String id) throws NotFoundException { if (!ObjectId.isValid(id)) { throw new NotFoundException("Input id <" + id + "> is invalid!"); } final DBObject o = get(org.graylog2.inputs.InputImpl.class, id); if (o == null) { throw new NotFoundException("Input <" + id + "> not found!"); } return new org.graylog2.inputs.InputImpl((ObjectId) o.get(InputImpl.FIELD_ID), o.toMap()); }
public Stream load(ObjectId id) throws NotFoundException { final DBObject o = get(StreamImpl.class, id); if (o == null) { throw new NotFoundException("Stream <" + id + "> not found!"); } final List<StreamRule> streamRules = streamRuleService.loadForStreamId(id.toHexString()); final Set<Output> outputs = loadOutputsForRawStream(o); @SuppressWarnings("unchecked") final Map<String, Object> fields = o.toMap(); return new StreamImpl((ObjectId) o.get("_id"), fields, streamRules, outputs, getIndexSet(o)); }