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");
}
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");
}
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");
}
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);
}
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());
}
Aggregations