use of org.mongodb.morphia.mapping.cache.DefaultEntityCache in project morphia by mongodb.
the class TestInterfaces method testDynamicInstantiation.
@Test
public void testDynamicInstantiation() throws Exception {
final DBCollection shapes = getDb().getCollection("shapes");
final DBCollection shapeshifters = getDb().getCollection("shapeshifters");
getMorphia().map(Circle.class).map(Rectangle.class).map(ShapeShifter.class);
final Shape rectangle = new Rectangle(2, 5);
final DBObject rectangleDbObj = getMorphia().toDBObject(rectangle);
shapes.save(rectangleDbObj);
final BasicDBObject rectangleDbObjLoaded = (BasicDBObject) shapes.findOne(new BasicDBObject(Mapper.ID_KEY, rectangleDbObj.get(Mapper.ID_KEY)));
final Shape rectangleLoaded = getMorphia().fromDBObject(getDs(), Shape.class, rectangleDbObjLoaded, new DefaultEntityCache());
assertTrue(rectangle.getArea() == rectangleLoaded.getArea());
assertTrue(rectangleLoaded instanceof Rectangle);
final ShapeShifter shifter = new ShapeShifter();
shifter.setReferencedShape(rectangleLoaded);
shifter.setMainShape(new Circle(2.2));
shifter.getAvailableShapes().add(new Rectangle(3, 3));
shifter.getAvailableShapes().add(new Circle(4.4));
final DBObject shifterDbObj = getMorphia().toDBObject(shifter);
shapeshifters.save(shifterDbObj);
final BasicDBObject shifterDbObjLoaded = (BasicDBObject) shapeshifters.findOne(new BasicDBObject(Mapper.ID_KEY, shifterDbObj.get(Mapper.ID_KEY)));
final ShapeShifter shifterLoaded = getMorphia().fromDBObject(getDs(), ShapeShifter.class, shifterDbObjLoaded, new DefaultEntityCache());
assertNotNull(shifterLoaded);
assertNotNull(shifterLoaded.getReferencedShape());
assertNotNull(shifterLoaded.getReferencedShape().getArea());
assertNotNull(rectangle);
assertNotNull(rectangle.getArea());
assertTrue(rectangle.getArea() == shifterLoaded.getReferencedShape().getArea());
assertTrue(shifterLoaded.getReferencedShape() instanceof Rectangle);
assertTrue(shifter.getMainShape().getArea() == shifterLoaded.getMainShape().getArea());
assertEquals(shifter.getAvailableShapes().size(), shifterLoaded.getAvailableShapes().size());
}
use of org.mongodb.morphia.mapping.cache.DefaultEntityCache in project morphia by mongodb.
the class TestMapping method testMaps.
@Test
public void testMaps() throws Exception {
final DBCollection articles = getDb().getCollection("articles");
getMorphia().map(Article.class).map(Translation.class).map(Circle.class);
final Article related = new Article();
final BasicDBObject relatedDbObj = (BasicDBObject) getMorphia().toDBObject(related);
articles.save(relatedDbObj);
final Article relatedLoaded = getMorphia().fromDBObject(getDs(), Article.class, articles.findOne(new BasicDBObject(Mapper.ID_KEY, relatedDbObj.get(Mapper.ID_KEY))), new DefaultEntityCache());
final Article article = new Article();
article.setTranslation("en", new Translation("Hello World", "Just a test"));
article.setTranslation("is", new Translation("Halló heimur", "Bara að prófa"));
article.setAttribute("myDate", new Date());
article.setAttribute("myString", "Test");
article.setAttribute("myInt", 123);
article.putRelated("test", relatedLoaded);
final BasicDBObject articleDbObj = (BasicDBObject) getMorphia().toDBObject(article);
articles.save(articleDbObj);
final Article articleLoaded = getMorphia().fromDBObject(getDs(), Article.class, articles.findOne(new BasicDBObject(Mapper.ID_KEY, articleDbObj.get(Mapper.ID_KEY))), new DefaultEntityCache());
assertEquals(article.getTranslations().size(), articleLoaded.getTranslations().size());
assertEquals(article.getTranslation("en").getTitle(), articleLoaded.getTranslation("en").getTitle());
assertEquals(article.getTranslation("is").getBody(), articleLoaded.getTranslation("is").getBody());
assertEquals(article.getAttributes().size(), articleLoaded.getAttributes().size());
assertEquals(article.getAttribute("myDate"), articleLoaded.getAttribute("myDate"));
assertEquals(article.getAttribute("myString"), articleLoaded.getAttribute("myString"));
assertEquals(article.getAttribute("myInt"), articleLoaded.getAttribute("myInt"));
assertEquals(article.getRelated().size(), articleLoaded.getRelated().size());
assertEquals(article.getRelated("test").getId(), articleLoaded.getRelated("test").getId());
}
use of org.mongodb.morphia.mapping.cache.DefaultEntityCache in project morphia by mongodb.
the class TestMapping method testRecursiveReference.
@Test
public void testRecursiveReference() throws Exception {
final DBCollection stuff = getDb().getCollection("stuff");
getMorphia().map(RecursiveParent.class).map(RecursiveChild.class);
final RecursiveParent parent = new RecursiveParent();
final DBObject parentDbObj = getMorphia().toDBObject(parent);
stuff.save(parentDbObj);
final RecursiveChild child = new RecursiveChild();
final DBObject childDbObj = getMorphia().toDBObject(child);
stuff.save(childDbObj);
final RecursiveParent parentLoaded = getMorphia().fromDBObject(getDs(), RecursiveParent.class, stuff.findOne(new BasicDBObject(Mapper.ID_KEY, parentDbObj.get(Mapper.ID_KEY))), new DefaultEntityCache());
final RecursiveChild childLoaded = getMorphia().fromDBObject(getDs(), RecursiveChild.class, stuff.findOne(new BasicDBObject(Mapper.ID_KEY, childDbObj.get(Mapper.ID_KEY))), new DefaultEntityCache());
parentLoaded.setChild(childLoaded);
childLoaded.setParent(parentLoaded);
stuff.save(getMorphia().toDBObject(parentLoaded));
stuff.save(getMorphia().toDBObject(childLoaded));
final RecursiveParent finalParentLoaded = getMorphia().fromDBObject(getDs(), RecursiveParent.class, stuff.findOne(new BasicDBObject(Mapper.ID_KEY, parentDbObj.get(Mapper.ID_KEY))), new DefaultEntityCache());
final RecursiveChild finalChildLoaded = getMorphia().fromDBObject(getDs(), RecursiveChild.class, stuff.findOne(new BasicDBObject(Mapper.ID_KEY, childDbObj.get(Mapper.ID_KEY))), new DefaultEntityCache());
assertNotNull(finalParentLoaded.getChild());
assertNotNull(finalChildLoaded.getParent());
}
use of org.mongodb.morphia.mapping.cache.DefaultEntityCache in project morphia by mongodb.
the class TestAsListPerf method driverQueryAndMorphiaConverter.
public double driverQueryAndMorphiaConverter(final int nbOfHits) {
final long start = System.nanoTime();
final List<DBObject> list = getDs().getDB().getCollection("Address").find().sort(new BasicDBObject("name", 1)).toArray();
final EntityCache entityCache = new DefaultEntityCache();
final List<Address> resultList = new LinkedList<Address>();
for (final DBObject dbObject : list) {
final Address address = getMorphia().fromDBObject(getDs(), Address.class, dbObject, entityCache);
resultList.add(address);
}
//ns -> ms
final long duration = (System.nanoTime() - start) / 1000000;
Assert.assertEquals(nbOfHits, resultList.size());
return (double) duration / nbOfHits;
}
use of org.mongodb.morphia.mapping.cache.DefaultEntityCache in project morphia by mongodb.
the class TestMapping method testMapping.
@Test
public void testMapping() {
final BasicDAO<User, ObjectId> messageDAO = new BasicDAO<User, ObjectId>(User.class, getDs());
Assert.assertNotNull(messageDAO);
Mapper mapper = new Mapper();
User user = new User();
user.id = 1;
user.userObject = "just a String";
DBObject dbObject = mapper.toDBObject(user);
Object object = mapper.fromDBObject(getDs(), User.class, dbObject, new DefaultEntityCache());
Assert.assertEquals(user.userObject, ((User) object).userObject);
user.userObject = 33;
dbObject = mapper.toDBObject(user);
object = mapper.fromDBObject(getDs(), User.class, dbObject, new DefaultEntityCache());
Assert.assertEquals(user.userObject, ((User) object).userObject);
user.userObject = 33.3;
dbObject = mapper.toDBObject(user);
object = mapper.fromDBObject(getDs(), User.class, dbObject, new DefaultEntityCache());
Assert.assertEquals(user.userObject, ((User) object).userObject);
}
Aggregations