Search in sources :

Example 1 with BasicDBList

use of com.mongodb.BasicDBList in project jetty.project by eclipse.

the class MongoSessionDataStore method doGetExpired.

/** 
     * @see org.eclipse.jetty.server.session.SessionDataStore#getExpired(Set)
     */
@Override
public Set<String> doGetExpired(Set<String> candidates) {
    long now = System.currentTimeMillis();
    long upperBound = now;
    Set<String> expiredSessions = new HashSet<>();
    //firstly ask mongo to verify if these candidate ids have expired - all of
    //these candidates will be for our node
    BasicDBObject query = new BasicDBObject();
    query.append(__ID, new BasicDBObject("$in", candidates));
    query.append(__EXPIRY, new BasicDBObject("$gt", 0).append("$lt", upperBound));
    DBCursor verifiedExpiredSessions = null;
    try {
        verifiedExpiredSessions = _dbSessions.find(query, new BasicDBObject(__ID, 1));
        for (DBObject session : verifiedExpiredSessions) {
            String id = (String) session.get(__ID);
            if (LOG.isDebugEnabled())
                LOG.debug("{} Mongo confirmed expired session {}", _context, id);
            expiredSessions.add(id);
        }
    } finally {
        if (verifiedExpiredSessions != null)
            verifiedExpiredSessions.close();
    }
    //if this is our first expiry check, make sure that we only grab really old sessions
    if (_lastExpiryCheckTime <= 0)
        upperBound = (now - (3 * (1000L * _gracePeriodSec)));
    else
        upperBound = _lastExpiryCheckTime - (1000L * _gracePeriodSec);
    query = new BasicDBObject();
    BasicDBObject gt = new BasicDBObject(__EXPIRY, new BasicDBObject("$gt", 0));
    BasicDBObject lt = new BasicDBObject(__EXPIRY, new BasicDBObject("$lt", upperBound));
    BasicDBList list = new BasicDBList();
    list.add(gt);
    list.add(lt);
    query.append("and", list);
    DBCursor oldExpiredSessions = null;
    try {
        BasicDBObject bo = new BasicDBObject(__ID, 1);
        bo.append(__EXPIRY, 1);
        oldExpiredSessions = _dbSessions.find(query, bo);
        for (DBObject session : oldExpiredSessions) {
            String id = (String) session.get(__ID);
            if (LOG.isDebugEnabled())
                LOG.debug("{} Mongo found old expired session {}", _context, id + " exp=" + session.get(__EXPIRY));
            expiredSessions.add(id);
        }
    } finally {
        oldExpiredSessions.close();
    }
    return expiredSessions;
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) BasicDBList(com.mongodb.BasicDBList) DBCursor(com.mongodb.DBCursor) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) HashSet(java.util.HashSet)

Example 2 with BasicDBList

use of com.mongodb.BasicDBList in project morphia by mongodb.

the class Mapper method toMongoObject.

/**
     * Converts a java object to a mongo-compatible object (possibly a DBObject for complex mappings).  Very similar to {@link
     * Mapper#toDBObject}.  Used (mainly) by query/update operations.
     *
     * @param mf    the MappedField for this value
     * @param mc    the MappedClass for this value
     * @param value the value to convert
     * @return the MongoDB compatible object
     */
@SuppressWarnings("deprecation")
public Object toMongoObject(final MappedField mf, final MappedClass mc, final Object value) {
    if (value == null) {
        return null;
    }
    Object mappedValue = value;
    if (value instanceof Query) {
        mappedValue = ((QueryImpl) value).getQueryObject();
    } else if (isAssignable(mf, value) || isEntity(mc)) {
        //convert the value to Key (DBRef) if the field is @Reference or type is Key/DBRef, or if the destination class is an @Entity
        try {
            if (value instanceof Iterable) {
                MappedClass mapped = getMappedClass(mf.getSubClass());
                if (mapped != null && (Key.class.isAssignableFrom(mapped.getClazz()) || mapped.getEntityAnnotation() != null)) {
                    mappedValue = getDBRefs(mf, (Iterable) value);
                } else {
                    if (mf.hasAnnotation(Reference.class)) {
                        mappedValue = getDBRefs(mf, (Iterable) value);
                    } else {
                        mappedValue = toMongoObject(value, false);
                    }
                }
            } else {
                if (mf != null) {
                    Reference refAnn = mf.getAnnotation(Reference.class);
                    Class<?> idType = null;
                    if (!mf.getType().equals(Key.class) && isMapped(mf.getType())) {
                        idType = getMappedClass(mf.getType()).getMappedIdField().getType();
                    }
                    boolean valueIsIdType = mappedValue.getClass().equals(idType);
                    if (refAnn != null) {
                        if (!valueIsIdType) {
                            Key<?> key = value instanceof Key ? (Key<?>) value : getKey(value);
                            if (key != null) {
                                mappedValue = refAnn.idOnly() ? keyToId(key) : keyToDBRef(key);
                            }
                        }
                    } else if (mf.getType().equals(Key.class)) {
                        mappedValue = keyToDBRef(valueIsIdType ? createKey(mf.getSubClass(), value) : value instanceof Key ? (Key<?>) value : getKey(value));
                        if (mappedValue == value) {
                            throw new ValidationException("cannot map to Key<T> field: " + value);
                        }
                    }
                }
                if (mappedValue == value) {
                    mappedValue = toMongoObject(value, false);
                }
            }
        } catch (Exception e) {
            LOG.error("Error converting value(" + value + ") to reference.", e);
            mappedValue = toMongoObject(value, false);
        }
    } else if (mf != null && mf.hasAnnotation(Serialized.class)) {
        //serialized
        try {
            mappedValue = Serializer.serialize(value, !mf.getAnnotation(Serialized.class).disableCompression());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    } else if (value instanceof DBObject) {
        //pass-through
        mappedValue = value;
    } else {
        mappedValue = toMongoObject(value, EmbeddedMapper.shouldSaveClassName(value, mappedValue, mf));
        if (mappedValue instanceof BasicDBList) {
            final BasicDBList list = (BasicDBList) mappedValue;
            if (list.size() != 0) {
                if (!EmbeddedMapper.shouldSaveClassName(extractFirstElement(value), list.get(0), mf)) {
                    for (Object o : list) {
                        if (o instanceof DBObject) {
                            ((DBObject) o).removeField(CLASS_NAME_FIELDNAME);
                        }
                    }
                }
            }
        } else if (mappedValue instanceof DBObject && !EmbeddedMapper.shouldSaveClassName(value, mappedValue, mf)) {
            ((DBObject) mappedValue).removeField(CLASS_NAME_FIELDNAME);
        }
    }
    return mappedValue;
}
Also used : ValidationException(org.mongodb.morphia.query.ValidationException) Query(org.mongodb.morphia.query.Query) ProxiedEntityReference(org.mongodb.morphia.mapping.lazy.proxy.ProxiedEntityReference) Reference(org.mongodb.morphia.annotations.Reference) Serialized(org.mongodb.morphia.annotations.Serialized) IOException(java.io.IOException) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) ValidationException(org.mongodb.morphia.query.ValidationException) IOException(java.io.IOException) BasicDBList(com.mongodb.BasicDBList) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) ReflectionUtils.getParameterizedClass(org.mongodb.morphia.utils.ReflectionUtils.getParameterizedClass) Key(org.mongodb.morphia.Key)

Example 3 with BasicDBList

use of com.mongodb.BasicDBList in project morphia by mongodb.

the class MappedFieldTest method nestedCollectionsMapping.

@Test
public void nestedCollectionsMapping() {
    final MappedField field = new MappedField(getField(TestEntity.class, "listOfListOfString"), TestEntity.class, getMorphia().getMapper());
    Assert.assertFalse(field.isSingleValue());
    Assert.assertTrue(field.isMultipleValues());
    Assert.assertFalse(field.isArray());
    Assert.assertTrue(List.class == field.getType());
    final List<MappedField> level1Types = field.getTypeParameters();
    final MappedField typeParameter = level1Types.get(0);
    Assert.assertTrue(List.class == typeParameter.getConcreteType());
    final List<MappedField> level2Types = typeParameter.getTypeParameters();
    final MappedField nested = level2Types.get(0);
    Assert.assertTrue(String.class == nested.getConcreteType());
    Assert.assertEquals("listOfListOfString", field.getJavaFieldName());
    Assert.assertEquals("listOfListOfString", field.getNameToStore());
    final BasicDBList list = new BasicDBList();
    list.add(dbList("a", "b", "c"));
    list.add(dbList("d", "e", "f"));
    final TestEntity entity = getMorphia().getMapper().fromDb(getDs(), new BasicDBObject("listOfListOfString", list), new TestEntity(), new DefaultEntityCache());
    final List<String> strings = asList("a", "b", "c");
    final List<String> strings1 = asList("d", "e", "f");
    final List<List<String>> expected = new ArrayList<List<String>>();
    expected.add(strings);
    expected.add(strings1);
    Assert.assertEquals(expected, entity.listOfListOfString);
}
Also used : BasicDBList(com.mongodb.BasicDBList) BasicDBObject(com.mongodb.BasicDBObject) ArrayList(java.util.ArrayList) BasicDBList(com.mongodb.BasicDBList) ArrayList(java.util.ArrayList) List(java.util.List) Arrays.asList(java.util.Arrays.asList) DefaultEntityCache(org.mongodb.morphia.mapping.cache.DefaultEntityCache) Test(org.junit.Test)

Example 4 with BasicDBList

use of com.mongodb.BasicDBList in project graylog2-server by Graylog2.

the class MongoConnectionImpl method getMongoVersion.

@Nullable
private Version getMongoVersion(DB adminDb) {
    final CommandResult buildInfoResult = adminDb.command("buildInfo");
    if (buildInfoResult.ok()) {
        final BasicDBList versionArray = (BasicDBList) buildInfoResult.get("versionArray");
        if (versionArray == null || versionArray.size() < 3) {
            LOG.debug("Couldn't retrieve MongoDB version");
            return null;
        }
        final int majorVersion = (int) versionArray.get(0);
        final int minorVersion = (int) versionArray.get(1);
        final int patchVersion = (int) versionArray.get(2);
        return Version.forIntegers(majorVersion, minorVersion, patchVersion);
    } else {
        LOG.debug("Couldn't retrieve MongoDB buildInfo: {}", buildInfoResult.getErrorMessage());
        return null;
    }
}
Also used : BasicDBList(com.mongodb.BasicDBList) CommandResult(com.mongodb.CommandResult) Nullable(javax.annotation.Nullable)

Example 5 with BasicDBList

use of com.mongodb.BasicDBList in project commons-dao by reportportal.

the class LaunchRepositoryCustomImpl method findLatestWithCallback.

@Override
public void findLatestWithCallback(Queryable filter, Sort sort, List<String> contentFields, long limit, DocumentCallbackHandler callbackHandler) {
    List<AggregationOperation> operations = latestLaunchesAggregationOperationsList(filter);
    operations.add(sort(sort));
    operations.add(limit(limit));
    DBObject results = mongoTemplate.aggregate(newAggregation(operations), mongoTemplate.getCollectionName(Launch.class), Launch.class).getRawResults();
    BasicDBList result = (BasicDBList) results.get(RESULT);
    result.stream().map(it -> (DBObject) it).forEach(callbackHandler::processDocument);
}
Also used : java.util(java.util) CacheConfiguration(com.epam.ta.reportportal.config.CacheConfiguration) Cacheable(org.springframework.cache.annotation.Cacheable) Status(com.epam.ta.reportportal.database.entity.Status) Autowired(org.springframework.beans.factory.annotation.Autowired) AggregationOperation(org.springframework.data.mongodb.core.aggregation.AggregationOperation) TestItemIssueType(com.epam.ta.reportportal.database.entity.item.issue.TestItemIssueType) QueryBuilder(com.epam.ta.reportportal.database.search.QueryBuilder) AggregationUtils.matchOperationFromFilter(com.epam.ta.reportportal.database.dao.aggregation.AggregationUtils.matchOperationFromFilter) BasicDBList(com.mongodb.BasicDBList) Query.query(org.springframework.data.mongodb.core.query.Query.query) IN_PROGRESS(com.epam.ta.reportportal.database.entity.Status.IN_PROGRESS) ModifiableQueryBuilder.findModifiedLaterThanPeriod(com.epam.ta.reportportal.database.search.ModifiableQueryBuilder.findModifiedLaterThanPeriod) Lists(com.google.common.collect.Lists) DBObject(com.mongodb.DBObject) DocumentCallbackHandler(org.springframework.data.mongodb.core.DocumentCallbackHandler) Launch(com.epam.ta.reportportal.database.entity.Launch) TestItem(com.epam.ta.reportportal.database.entity.item.TestItem) Duration(java.time.Duration) AddFieldsOperation.addFields(com.epam.ta.reportportal.database.dao.aggregation.AddFieldsOperation.addFields) Update(org.springframework.data.mongodb.core.query.Update) Pageable(org.springframework.data.domain.Pageable) Sort(org.springframework.data.domain.Sort) Project(com.epam.ta.reportportal.database.entity.Project) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate) ObjectId.isValid(org.bson.types.ObjectId.isValid) Criteria.where(org.springframework.data.mongodb.core.query.Criteria.where) Queryable(com.epam.ta.reportportal.database.search.Queryable) Filter(com.epam.ta.reportportal.database.search.Filter) AggregationResults(org.springframework.data.mongodb.core.aggregation.AggregationResults) Streams(com.google.common.collect.Streams) Page(org.springframework.data.domain.Page) Aggregation(org.springframework.data.mongodb.core.aggregation.Aggregation) Collectors(java.util.stream.Collectors) SortingOperation.sorting(com.epam.ta.reportportal.database.dao.aggregation.SortingOperation.sorting) Criteria(org.springframework.data.mongodb.core.query.Criteria) Query(org.springframework.data.mongodb.core.query.Query) Collectors.toList(java.util.stream.Collectors.toList) Stream(java.util.stream.Stream) UpdateStatisticsQueryBuilder(com.epam.ta.reportportal.database.search.UpdateStatisticsQueryBuilder) Modifiable(com.epam.ta.reportportal.database.entity.Modifiable) ObjectId(org.bson.types.ObjectId) GroupOperation(org.springframework.data.mongodb.core.aggregation.GroupOperation) Pattern(java.util.regex.Pattern) DESC(org.springframework.data.domain.Sort.Direction.DESC) StatisticSubType(com.epam.ta.reportportal.database.entity.statistics.StatisticSubType) PageImpl(org.springframework.data.domain.PageImpl) BasicDBList(com.mongodb.BasicDBList) DBObject(com.mongodb.DBObject) Launch(com.epam.ta.reportportal.database.entity.Launch) AggregationOperation(org.springframework.data.mongodb.core.aggregation.AggregationOperation)

Aggregations

BasicDBList (com.mongodb.BasicDBList)107 BasicDBObject (com.mongodb.BasicDBObject)69 DBObject (com.mongodb.DBObject)50 Test (org.junit.jupiter.api.Test)17 Document (org.springframework.data.mongodb.core.mapping.Document)14 DBCollection (com.mongodb.DBCollection)11 ArrayList (java.util.ArrayList)11 Test (org.junit.Test)11 DBCursor (com.mongodb.DBCursor)8 DBRef (com.mongodb.DBRef)8 List (java.util.List)7 Document (org.bson.Document)7 HashMap (java.util.HashMap)6 ObjectId (org.bson.types.ObjectId)6 MongoClient (com.mongodb.MongoClient)4 Map (java.util.Map)4 NotFoundException (org.graylog2.database.NotFoundException)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 MongoInputSplit (com.mongodb.hadoop.input.MongoInputSplit)3 IOException (java.io.IOException)3