Search in sources :

Example 81 with DBCollection

use of com.mongodb.DBCollection in project sling by apache.

the class MongoDBResourceProvider method listChildren.

/**
     * TODO - we have to check for deleted and added resources
     * @see org.apache.sling.api.resource.ResourceProvider#listChildren(org.apache.sling.api.resource.Resource)
     */
public Iterator<Resource> listChildren(final Resource parent) {
    final String[] info = this.extractResourceInfo(parent.getPath());
    if (info != null) {
        if (info.length == 0) {
            // all collections
            final Set<String> names = new HashSet<String>(context.getDatabase().getCollectionNames());
            names.removeAll(this.context.getFilterCollectionNames());
            final Iterator<String> i = names.iterator();
            return new Iterator<Resource>() {

                public boolean hasNext() {
                    return i.hasNext();
                }

                public Resource next() {
                    final String name = i.next();
                    return new MongoDBCollectionResource(parent.getResourceResolver(), parent.getPath() + '/' + name);
                }

                public void remove() {
                    throw new UnsupportedOperationException("remove");
                }
            };
        }
        final DBCollection col = this.getCollection(info[0]);
        if (col != null) {
            final String pattern;
            if (info.length == 1) {
                pattern = "^([^/])*$";
            } else {
                pattern = "^" + Pattern.quote(info[1]) + "/([^/])*$";
            }
            final DBObject query = QueryBuilder.start(getPROP_PATH()).regex(Pattern.compile(pattern)).get();
            final DBCursor cur = col.find(query).sort(BasicDBObjectBuilder.start(getPROP_PATH(), 1).get());
            return new Iterator<Resource>() {

                public boolean hasNext() {
                    return cur.hasNext();
                }

                public Resource next() {
                    final DBObject obj = cur.next();
                    final String objPath = obj.get(getPROP_PATH()).toString();
                    final int lastSlash = objPath.lastIndexOf('/');
                    final String name;
                    if (lastSlash == -1) {
                        name = objPath;
                    } else {
                        name = objPath.substring(lastSlash + 1);
                    }
                    return new MongoDBResource(parent.getResourceResolver(), parent.getPath() + '/' + name, info[0], obj, MongoDBResourceProvider.this);
                }

                public void remove() {
                    throw new UnsupportedOperationException("remove");
                }
            };
        }
    }
    return null;
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) DBCollection(com.mongodb.DBCollection) DBCursor(com.mongodb.DBCursor) Iterator(java.util.Iterator) HashSet(java.util.HashSet)

Example 82 with DBCollection

use of com.mongodb.DBCollection in project sling by apache.

the class MongoDBResourceProvider method getResource.

/**
     * Get a resource
     */
protected Resource getResource(final ResourceResolver resourceResolver, final String path, final String[] info) {
    if (info.length == 0) {
        // special resource : all collections
        return new MongoDBCollectionResource(resourceResolver, path);
    } else if (info.length == 1) {
        // special resource : collection
        if (this.hasCollection(info[0])) {
            return new MongoDBCollectionResource(resourceResolver, path);
        }
        return null;
    }
    logger.debug("Searching {} in {}", info[1], info[0]);
    final DBCollection col = this.getCollection(info[0]);
    if (col != null) {
        final DBObject obj = col.findOne(QueryBuilder.start(getPROP_PATH()).is(info[1]).get());
        logger.debug("Found {}", obj);
        if (obj != null) {
            return new MongoDBResource(resourceResolver, path, info[0], obj, this);
        }
    }
    return null;
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject)

Example 83 with DBCollection

use of com.mongodb.DBCollection in project spring-data-mongodb by spring-projects.

the class PerformanceTests method writingObjectsUsingPlainDriver.

private long writingObjectsUsingPlainDriver(int numberOfPersons) {
    DBCollection collection = mongo.getDB(DATABASE_NAME).getCollection("driver");
    List<Person> persons = getPersonObjects(numberOfPersons);
    executeWatched(() -> persons.stream().map(it -> collection.save(new BasicDBObject(it.toDocument()))));
    return watch.getLastTaskTimeMillis();
}
Also used : Document(org.bson.Document) Arrays(java.util.Arrays) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) PersistenceConstructor(org.springframework.data.annotation.PersistenceConstructor) Date(java.util.Date) HashMap(java.util.HashMap) Random(java.util.Random) SimpleMongoDbFactory(org.springframework.data.mongodb.core.SimpleMongoDbFactory) MongoConverter(org.springframework.data.mongodb.core.convert.MongoConverter) BasicDBList(com.mongodb.BasicDBList) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) DBObject(com.mongodb.DBObject) Map(java.util.Map) Constants(org.springframework.core.Constants) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate) MongoRepository(org.springframework.data.mongodb.repository.MongoRepository) Before(org.junit.Before) MongoRepositoryFactoryBean(org.springframework.data.mongodb.repository.support.MongoRepositoryFactoryBean) Collection(java.util.Collection) DecimalFormat(java.text.DecimalFormat) BasicDBObject(com.mongodb.BasicDBObject) DBCollection(com.mongodb.DBCollection) Set(java.util.Set) Test(org.junit.Test) StopWatch(org.springframework.util.StopWatch) Collectors(java.util.stream.Collectors) Criteria(org.springframework.data.mongodb.core.query.Criteria) Query(org.springframework.data.mongodb.core.query.Query) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) List(java.util.List) DBCursor(com.mongodb.DBCursor) MongoClient(com.mongodb.MongoClient) ObjectId(org.bson.types.ObjectId) DB(com.mongodb.DB) Pattern(java.util.regex.Pattern) DefaultDbRefResolver(org.springframework.data.mongodb.core.convert.DefaultDbRefResolver) WriteConcern(com.mongodb.WriteConcern) Collections(java.util.Collections) Assert(org.springframework.util.Assert) StringUtils(org.springframework.util.StringUtils) DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject)

Example 84 with DBCollection

use of com.mongodb.DBCollection in project spring-data-mongodb by spring-projects.

the class PerformanceTests method queryUsingPlainDriver.

private long queryUsingPlainDriver() {
    executeWatched(() -> {
        DBCollection collection = mongo.getDB(DATABASE_NAME).getCollection("driver");
        BasicDBObject regex = new BasicDBObject("$regex", Pattern.compile(".*1.*"));
        BasicDBObject query = new BasicDBObject("addresses.zipCode", regex);
        return toPersons(collection.find(query));
    });
    return watch.getLastTaskTimeMillis();
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject)

Example 85 with DBCollection

use of com.mongodb.DBCollection in project cas by apereo.

the class MongoDbTicketRegistry method createTicketCollections.

private void createTicketCollections() {
    final Collection<TicketDefinition> definitions = ticketCatalog.findAll();
    final MongoDbConnectionFactory factory = new MongoDbConnectionFactory();
    definitions.forEach(t -> {
        final DBCollection c = createTicketCollection(t, factory);
        LOGGER.debug("Created MongoDb collection configuration for [{}]", c.getFullName());
    });
}
Also used : DBCollection(com.mongodb.DBCollection) MongoDbConnectionFactory(org.apereo.cas.mongo.MongoDbConnectionFactory) TicketDefinition(org.apereo.cas.ticket.TicketDefinition)

Aggregations

DBCollection (com.mongodb.DBCollection)174 DBObject (com.mongodb.DBObject)92 BasicDBObject (com.mongodb.BasicDBObject)91 Test (org.junit.Test)70 UsingDataSet (com.lordofthejars.nosqlunit.annotation.UsingDataSet)29 DBCursor (com.mongodb.DBCursor)25 DB (com.mongodb.DB)23 MongoException (com.mongodb.MongoException)22 BasicDBObjectBuilder (com.mongodb.BasicDBObjectBuilder)17 FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)12 JSONObject (org.json.JSONObject)12 MongoClientURI (com.mongodb.MongoClientURI)11 List (java.util.List)11 Map (java.util.Map)11 QueryBuilder (com.mongodb.QueryBuilder)10 HashMap (java.util.HashMap)10 Stopwatch (com.google.common.base.Stopwatch)9 WriteResult (com.mongodb.WriteResult)9 MongoClient (com.mongodb.MongoClient)8 IOException (java.io.IOException)8