Search in sources :

Example 41 with Datastore

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

the class TestMaxMin method testMin.

@Test
public void testMin() {
    final IndexedEntity a = new IndexedEntity("a");
    final IndexedEntity b = new IndexedEntity("b");
    final IndexedEntity c = new IndexedEntity("c");
    Datastore ds = getDs();
    ds.save(a);
    ds.save(b);
    ds.save(c);
    Assert.assertEquals(ds.find(IndexedEntity.class).iterator(new FindOptions().sort(ascending("id")).hint("testField").min(new Document("testField", "b"))).next().id, b.id, "last");
    Assert.assertEquals(ds.find(IndexedEntity.class).iterator(new FindOptions().sort(ascending("id")).hint("testField").min(new Document("testField", "b"))).next().id, b.id, "last");
}
Also used : FindOptions(dev.morphia.query.FindOptions) Datastore(dev.morphia.Datastore) Document(org.bson.Document) Test(org.testng.annotations.Test)

Example 42 with Datastore

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

the class TestMaxMin method testMinCompoundIndex.

@Test
public void testMinCompoundIndex() {
    final IndexedEntity a1 = new IndexedEntity("a");
    final IndexedEntity a2 = new IndexedEntity("a");
    final IndexedEntity b1 = new IndexedEntity("b");
    final IndexedEntity b2 = new IndexedEntity("b");
    final IndexedEntity c1 = new IndexedEntity("c");
    final IndexedEntity c2 = new IndexedEntity("c");
    Datastore ds = getDs();
    ds.save(a1);
    ds.save(a2);
    ds.save(b1);
    ds.save(b2);
    ds.save(c1);
    ds.save(c2);
    List<IndexedEntity> l = ds.find(IndexedEntity.class).iterator(new FindOptions().sort(ascending("testField"), ascending("id")).hint(new Document("testField", 1).append("_id", 1)).min(new Document("testField", "b").append("_id", b1.id))).toList();
    Assert.assertEquals(l.size(), 4, "size");
    Assert.assertEquals(l.get(0).id, b1.id, "item");
    l = ds.find(IndexedEntity.class).iterator(new FindOptions().sort(ascending("testField"), ascending("id")).hint(new Document("testField", 1).append("_id", 1)).min(new Document("testField", "b").append("_id", b1.id))).toList();
    Assert.assertEquals(l.size(), 4, "item");
    Assert.assertEquals(l.get(0).id, b1.id, "item");
}
Also used : FindOptions(dev.morphia.query.FindOptions) Datastore(dev.morphia.Datastore) Document(org.bson.Document) Test(org.testng.annotations.Test)

Example 43 with Datastore

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

the class TestMaxMin method testMax.

@Test
public void testMax() {
    final IndexedEntity a = new IndexedEntity("a");
    final IndexedEntity b = new IndexedEntity("b");
    final IndexedEntity c = new IndexedEntity("c");
    Datastore ds = getDs();
    ds.save(a);
    ds.save(b);
    ds.save(c);
    ds.ensureIndexes();
    Assert.assertEquals(ds.find(IndexedEntity.class).iterator(new FindOptions().sort(descending("id")).hint("testField").max(new Document("testField", "c"))).next().id, b.id, "last");
    Assert.assertEquals(ds.find(IndexedEntity.class).iterator(new FindOptions().sort(descending("id")).hint("testField").max(new Document("testField", "c"))).next().id, b.id, "last");
}
Also used : FindOptions(dev.morphia.query.FindOptions) Datastore(dev.morphia.Datastore) Document(org.bson.Document) Test(org.testng.annotations.Test)

Example 44 with Datastore

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

the class TestLegacyQuery method testTailableCursors.

@Test
public void testTailableCursors() {
    getMapper().map(CappedPic.class);
    final Datastore ds = getDs();
    ds.ensureCaps();
    final Query<CappedPic> query = ds.find(CappedPic.class);
    final List<CappedPic> found = new ArrayList<>();
    final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
    assertEquals(query.count(), 0);
    ScheduledFuture<?> scheduledFuture = executorService.scheduleAtFixedRate(() -> ds.save(new CappedPic()), 0, 100, TimeUnit.MILLISECONDS);
    Awaitility.await().atMost(10, TimeUnit.SECONDS).until(() -> getDs().find(CappedPic.class).count() > 0);
    final Iterator<CappedPic> tail = query.execute(new FindOptions().cursorType(CursorType.Tailable));
    Awaitility.await().pollDelay(500, TimeUnit.MILLISECONDS).atMost(10, TimeUnit.SECONDS).until(() -> {
        if (tail.hasNext()) {
            found.add(tail.next());
        }
        return found.size() >= 10;
    });
    executorService.shutdownNow();
    assertTrue(found.size() >= 10);
    assertTrue(query.count() >= 10);
}
Also used : FindOptions(dev.morphia.query.FindOptions) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Datastore(dev.morphia.Datastore) ArrayList(java.util.ArrayList) Test(org.testng.annotations.Test)

Example 45 with Datastore

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

the class TestQuery method testKeyList.

@Test
public void testKeyList() {
    final Rectangle rect = new Rectangle(1000, 1);
    Rectangle rectangle = getDs().save(rect);
    assertEquals(rectangle.getId(), rect.getId());
    final FacebookUser fbUser1 = new FacebookUser(1, "scott");
    final FacebookUser fbUser2 = new FacebookUser(2, "tom");
    final FacebookUser fbUser3 = new FacebookUser(3, "oli");
    final FacebookUser fbUser4 = new FacebookUser(4, "frank");
    final List<FacebookUser> users = getDs().save(asList(fbUser1, fbUser2, fbUser3, fbUser4));
    assertEquals(fbUser1.getId(), 1);
    final List<Key<FacebookUser>> fbUserKeys = new ArrayList<>();
    for (FacebookUser user : users) {
        fbUserKeys.add(getMapper().getKey(user));
    }
    assertEquals(fbUserKeys.get(0).getId(), fbUser1.getId());
    assertEquals(fbUserKeys.get(1).getId(), fbUser2.getId());
    assertEquals(fbUserKeys.get(2).getId(), fbUser3.getId());
    assertEquals(fbUserKeys.get(3).getId(), fbUser4.getId());
    final Keys k1 = new Keys(getMapper().getKey(rectangle), fbUserKeys);
    final Keys keys = getDs().save(k1);
    assertEquals(k1.getId(), keys.getId());
    final Datastore datastore = getDs();
    final Keys k1Loaded = datastore.find(Keys.class).filter(eq("_id", k1.getId())).first();
    for (Key<FacebookUser> key : k1Loaded.getUsers()) {
        assertNotNull(key.getId());
    }
    assertNotNull(k1Loaded.getRect().getId());
}
Also used : Datastore(dev.morphia.Datastore) Keys(dev.morphia.test.models.Keys) Rectangle(dev.morphia.test.models.Rectangle) ArrayList(java.util.ArrayList) Key(dev.morphia.Key) FacebookUser(dev.morphia.test.models.FacebookUser) 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