Search in sources :

Example 31 with Datastore

use of dev.morphia.Datastore in project morphia by mongodb.

the class ClassMethodPair method invoke.

void invoke(Datastore datastore, Document document, Object entity) {
    try {
        Object instance;
        if (type != null) {
            instance = getOrCreateInstance(type);
        } else {
            instance = entity;
        }
        final Method method = getMethod();
        method.setAccessible(true);
        Sofia.logCallingLifecycleMethod(event.getSimpleName(), method, instance);
        List<Object> args = new ArrayList<>();
        for (Class<?> parameterType : method.getParameterTypes()) {
            if (parameterType.equals(Document.class)) {
                args.add(document);
            } else if (parameterType.equals(Datastore.class)) {
                args.add(datastore);
            } else {
                args.add(entity);
            }
        }
        method.invoke(instance, args.toArray());
    } catch (ReflectiveOperationException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}
Also used : Datastore(dev.morphia.Datastore) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method)

Example 32 with Datastore

use of dev.morphia.Datastore in project morphia by mongodb.

the class TestUpdateOperations method testRemoveAllList.

@Test
public void testRemoveAllList() {
    LogHolder logs = new LogHolder();
    Date date = new Date();
    logs.logs.addAll(asList(new Log(1), new Log(2), new Log(3), new Log(1), new Log(2), new Log(3)));
    Datastore ds = getDs();
    ds.save(logs);
    UpdateResult results = ds.find(LogHolder.class).update(pullAll("logs", singletonList(new Log(3)))).execute();
    Assert.assertEquals(results.getModifiedCount(), 1);
    LogHolder updated = ds.find(LogHolder.class).iterator(new FindOptions().limit(1)).next();
    Assert.assertEquals(updated.logs.size(), 4);
    Assert.assertTrue(updated.logs.stream().allMatch(log -> log.equals(new Log(1)) || log.equals(new Log(2))));
}
Also used : Document(org.bson.Document) CoreMatchers.is(org.hamcrest.CoreMatchers.is) Matchers.arrayContaining(org.hamcrest.Matchers.arrayContaining) CoreMatchers(org.hamcrest.CoreMatchers) Filters.regex(dev.morphia.query.experimental.filters.Filters.regex) Date(java.util.Date) Embedded(dev.morphia.annotations.Embedded) Test(org.testng.annotations.Test) Book(dev.morphia.test.models.Book) Datastore(dev.morphia.Datastore) Id(dev.morphia.annotations.Id) Collections.singletonList(java.util.Collections.singletonList) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) UpdateOperators.pullAll(dev.morphia.query.experimental.updates.UpdateOperators.pullAll) TypeSpecification(dev.morphia.query.experimental.updates.CurrentDateOperator.TypeSpecification) UpdateOperators.pull(dev.morphia.query.experimental.updates.UpdateOperators.pull) UpdateOperators.setOnInsert(dev.morphia.query.experimental.updates.UpdateOperators.setOnInsert) UpdateResult(com.mongodb.client.result.UpdateResult) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContainsPic(dev.morphia.test.query.TestQuery.ContainsPic) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) UpdateOperators.dec(dev.morphia.query.experimental.updates.UpdateOperators.dec) Entity(dev.morphia.annotations.Entity) Circle(dev.morphia.test.models.Circle) UpdateOperators.addToSet(dev.morphia.query.experimental.updates.UpdateOperators.addToSet) UpdateOptions(dev.morphia.UpdateOptions) ValidationException(dev.morphia.query.ValidationException) User(dev.morphia.test.models.User) PreLoad(dev.morphia.annotations.PreLoad) FindOptions(dev.morphia.query.FindOptions) UpdateOperators.pop(dev.morphia.query.experimental.updates.UpdateOperators.pop) Set(java.util.Set) UpdateOperators.min(dev.morphia.query.experimental.updates.UpdateOperators.min) Assert.assertNotNull(org.testng.Assert.assertNotNull) Instant(java.time.Instant) Objects(java.util.Objects) List(java.util.List) MatcherAssert(org.hamcrest.MatcherAssert) UpdateOperators.xor(dev.morphia.query.experimental.updates.UpdateOperators.xor) MorphiaCursor(dev.morphia.query.MorphiaCursor) Query(dev.morphia.query.Query) UpdateOperators.and(dev.morphia.query.experimental.updates.UpdateOperators.and) LocalDate(java.time.LocalDate) Expressions.literal(dev.morphia.aggregation.experimental.expressions.Expressions.literal) UpdateOperators.set(dev.morphia.query.experimental.updates.UpdateOperators.set) UpdateOperators.max(dev.morphia.query.experimental.updates.UpdateOperators.max) UpdateOperators.inc(dev.morphia.query.experimental.updates.UpdateOperators.inc) Set.set(dev.morphia.aggregation.experimental.stages.Set.set) Assert.assertNull(org.testng.Assert.assertNull) UpdateOperators.push(dev.morphia.query.experimental.updates.UpdateOperators.push) LocalDateTime(java.time.LocalDateTime) Assert.assertEquals(org.testng.Assert.assertEquals) HashMap(java.util.HashMap) DeleteOptions(dev.morphia.DeleteOptions) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Update(dev.morphia.query.Update) Calendar(java.util.Calendar) Assert(org.testng.Assert) Assert.assertThrows(org.testng.Assert.assertThrows) BsonTimestamp(org.bson.BsonTimestamp) Sort(dev.morphia.query.Sort) Rectangle(dev.morphia.test.models.Rectangle) UpdateOperators.rename(dev.morphia.query.experimental.updates.UpdateOperators.rename) UpdateOperators.mul(dev.morphia.query.experimental.updates.UpdateOperators.mul) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) UpdateOperators.unset(dev.morphia.query.experimental.updates.UpdateOperators.unset) CoreMatchers.nullValue(org.hamcrest.CoreMatchers.nullValue) TestEntity(dev.morphia.test.models.TestEntity) Month(java.time.Month) PathTarget(dev.morphia.internal.PathTarget) Matchers(org.hamcrest.Matchers) Filters(dev.morphia.query.experimental.filters.Filters) Indexed(dev.morphia.annotations.Indexed) UpdateOperators.or(dev.morphia.query.experimental.updates.UpdateOperators.or) DAYS(java.time.temporal.ChronoUnit.DAYS) Matchers.hasItem(org.hamcrest.Matchers.hasItem) NOW(dev.morphia.aggregation.experimental.expressions.SystemVariables.NOW) UpdateOperators.currentDate(dev.morphia.query.experimental.updates.UpdateOperators.currentDate) StringJoiner(java.util.StringJoiner) Filters.eq(dev.morphia.query.experimental.filters.Filters.eq) Pic(dev.morphia.test.query.TestQuery.Pic) ObjectId(org.bson.types.ObjectId) Nullable(com.mongodb.lang.Nullable) MorphiaReference(dev.morphia.mapping.experimental.MorphiaReference) FindOptions(dev.morphia.query.FindOptions) Datastore(dev.morphia.Datastore) Date(java.util.Date) LocalDate(java.time.LocalDate) UpdateOperators.currentDate(dev.morphia.query.experimental.updates.UpdateOperators.currentDate) UpdateResult(com.mongodb.client.result.UpdateResult) Test(org.testng.annotations.Test)

Example 33 with Datastore

use of dev.morphia.Datastore in project morphia by mongodb.

the class TestUpdateOperations method testSetUnset.

@Test
public void testSetUnset() {
    Datastore ds = getDs();
    final ObjectId key = ds.save(new Circle(1)).getId();
    Query<Circle> circle = ds.find(Circle.class).filter(eq("radius", 1D));
    assertUpdated(circle.update(set("radius", 2D)).execute(), 1);
    Query<Circle> idQuery = ds.find(Circle.class).filter(eq("_id", key));
    MatcherAssert.assertThat(idQuery.first().getRadius(), is(2D));
    circle = ds.find(Circle.class).filter(eq("radius", 2D));
    assertUpdated(circle.update(unset("radius")).execute(new UpdateOptions().multi(false)), 1);
    MatcherAssert.assertThat(idQuery.first().getRadius(), is(0D));
    Book article = new Book();
    ds.save(article);
    Query<Book> query = ds.find(Book.class);
    query.update(set("title", "Some Title")).execute();
    query.update(unset("title")).execute();
}
Also used : Circle(dev.morphia.test.models.Circle) Datastore(dev.morphia.Datastore) ObjectId(org.bson.types.ObjectId) Book(dev.morphia.test.models.Book) UpdateOptions(dev.morphia.UpdateOptions) Test(org.testng.annotations.Test)

Example 34 with Datastore

use of dev.morphia.Datastore in project morphia by mongodb.

the class TestDatastoreMerge method merge.

@Test
public void merge() {
    Datastore ds = getDs();
    Test1 test1 = new Test1();
    test1.name = "foobar";
    ds.save(test1);
    Test2 test2 = ds.find(Test2.class).first();
    Assert.assertNotNull(test2.id);
    test2.blarg = "barfoo";
    long version = test2.version;
    ds.merge(test2);
    Assert.assertEquals(version + 1, test2.version);
    test1 = ds.find(Test1.class).filter(eq("_id", test1.id)).first();
    Assert.assertNotNull(test1.name);
}
Also used : Datastore(dev.morphia.Datastore) Test(org.testng.annotations.Test)

Example 35 with Datastore

use of dev.morphia.Datastore in project morphia by mongodb.

the class TestGenerics method testMethodMappedGenericEntities.

@Test
public void testMethodMappedGenericEntities() {
    Datastore datastore = createDatastore(getMongoClient(), TEST_DB_NAME, MapperOptions.builder().propertyDiscovery(PropertyDiscovery.METHODS).build());
    EntityModel entityModel = datastore.getMapper().map(MethodMappedSpecializedEntity.class).get(0);
    PropertyModel test = entityModel.getProperty("test");
    assertEquals(test.getType(), UUID.class);
    MethodMappedSpecializedEntity beforeDB = new MethodMappedSpecializedEntity();
    beforeDB.setId(UUID.randomUUID());
    beforeDB.setTest(UUID.randomUUID());
    datastore.save(beforeDB);
    MethodMappedSpecializedEntity loaded = datastore.find(MethodMappedSpecializedEntity.class).filter(eq("_id", beforeDB.getId())).first();
    assertEquals(loaded.getId(), beforeDB.getId());
    assertEquals(loaded.getTest(), beforeDB.getTest());
}
Also used : Datastore(dev.morphia.Datastore) Morphia.createDatastore(dev.morphia.Morphia.createDatastore) MethodMappedSpecializedEntity(dev.morphia.test.models.methods.MethodMappedSpecializedEntity) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel) Test(org.testng.annotations.Test)

Aggregations

Datastore (dev.morphia.Datastore)46 Test (org.testng.annotations.Test)38 Morphia.createDatastore (dev.morphia.Morphia.createDatastore)12 FindOptions (dev.morphia.query.FindOptions)12 ArrayList (java.util.ArrayList)9 Document (org.bson.Document)9 EntityModel (dev.morphia.mapping.codec.pojo.EntityModel)6 UpdateOptions (dev.morphia.UpdateOptions)5 UpdateResult (com.mongodb.client.result.UpdateResult)4 MapperOptions (dev.morphia.mapping.MapperOptions)4 ObjectId (org.bson.types.ObjectId)4 Mapper (dev.morphia.mapping.Mapper)3 DocumentWriter (dev.morphia.mapping.codec.writer.DocumentWriter)3 Circle (dev.morphia.test.models.Circle)3 Rectangle (dev.morphia.test.models.Rectangle)3 Versioned (dev.morphia.test.models.versioned.Versioned)3 Point (com.mongodb.client.model.geojson.Point)2 Position (com.mongodb.client.model.geojson.Position)2 DeleteOptions (dev.morphia.DeleteOptions)2 Key (dev.morphia.Key)2