private Set<String> getTableMetadataNames(String schemaName) throws TableNotFoundException { MongoDatabase db = client.getDatabase(schemaName); MongoCursor<Document> cursor = db.getCollection(schemaCollection) .find().projection(new Document(TABLE_NAME_KEY, true)).iterator(); HashSet<String> names = new HashSet<>(); while (cursor.hasNext()) { names.add((cursor.next()).getString(TABLE_NAME_KEY)); } return names; }
private <T> void assertListEquals(final List<Key<T>> list, final MongoCursor<?> cursor) { for (Key<T> tKey : list) { assertEquals(list.toString(), tKey, cursor.next()); } }
@Override public Page getNextPage() { verify(pageBuilder.isEmpty()); count = 0; for (int i = 0; i < ROWS_PER_REQUEST; i++) { if (!cursor.hasNext()) { finished = true; break; } currentDoc = cursor.next(); count++; pageBuilder.declarePosition(); for (int column = 0; column < columnTypes.size(); column++) { BlockBuilder output = pageBuilder.getBlockBuilder(column); appendTo(columnTypes.get(column), currentDoc.get(columnNames.get(column)), output); } } Page page = pageBuilder.build(); pageBuilder.reset(); return page; }
@Override public void forEach(final Block<? super TResult> block) { MongoCursor<TResult> cursor = iterator(); try { while (cursor.hasNext()) { block.apply(cursor.next()); } } finally { cursor.close(); } }
@Test public void onException() throws Exception { when(cursor.hasNext()).thenReturn(true); when(cursor.next()).thenThrow(new IllegalStateException("boom")); try { repository.findAll().fetchAll().getUnchecked(); fail("Shouldn't get here"); } catch (Exception ignore) { // expected } verify(cursor, times(1)).close(); } }
@Test public void singleResult() throws Exception { when(cursor.hasNext()).thenReturn(true).thenReturn(false); when(cursor.next()).thenReturn(ImmutableEntity.of("foo")); check(repository.findAll().fetchAll().getUnchecked()).has(ImmutableEntity.of("foo")); verify(cursor, times(1)).close(); }
@Test(expected = MongoException.class) public void testExceptionForIndexMismatch() { getDs().find(IndexedEntity.class).find(new FindOptions() .limit(1) .modifier("$min", new BasicDBObject("doesNotExist", 1))) .next(); }
@Test public void testInQuery() { getDs().save(new Photo(asList("red", "green", "blue"))); assertNotNull(getDs() .find(Photo.class) .field("keywords").in(asList("red", "yellow")) .find(new FindOptions().limit(1)).next()); }
private void empties() { Datastore ds = getDs(); ds.delete(ds.find(Employee.class)); Employee employee = new Employee(); employee.byteList = asList((byte) 1, (byte) 2); ds.save(employee); Employee loaded = ds.find(Employee.class) .find(new FindOptions().limit(1)) .next(); assertEquals(employee.byteList, loaded.byteList); assertNull(loaded.floatList); }
@Test public void testInQueryWithObjects() { getDs().save(asList(new PhotoWithKeywords(), new PhotoWithKeywords("Scott", "Joe", "Sarah"))); final Query<PhotoWithKeywords> query = getDs() .find(PhotoWithKeywords.class) .field("keywords").in(asList(new Keyword("Scott"), new Keyword("Randy"))); assertNotNull(query.find(new FindOptions().limit(1)).next()); }
@Test public void example() throws Exception { ChildEntity entity = new ChildEntity(); entity.setEmbeddedList(asList(new ChildEmbedded("first"), new ChildEmbedded("second"), new AnotherChildEmbedded("third"))); getDs().save(entity); ChildEntity childEntity = getDs().find(ChildEntity.class) .find(new FindOptions().limit(1)) .next(); Assert.assertEquals(entity, childEntity); } }
@Test public void testIdFieldNameQuery() { getDs().save(new PhotoWithKeywords("scott", "hernandez")); assertNotNull(getDs().find(PhotoWithKeywords.class).filter("id !=", "scott") .find(new FindOptions().limit(1)) .next()); }
@Test public void testReferenceQueryWithoutValidation() { Ref ref = new Ref("no validation"); getDs().save(ref); final Container container = new Container(singletonList(ref)); getDs().save(container); final Query<Container> query = getDs().find(Container.class) .disableValidation() .field("singleRef").equal(ref); Assert.assertNotNull(query.find(new FindOptions().limit(1)).next()); }
@Test public void testEmbeddedDBObject() { getMorphia().map(ContainsDBObject.class); getDs().save(new ContainsDBObject()); assertNotNull(getDs().find(ContainsDBObject.class) .find(new FindOptions().limit(1)) .next()); }
@Test public void testRegexInsensitiveQuery() { getDs().save(new PhotoWithKeywords(new Keyword("california"), new Keyword("nevada"), new Keyword("arizona"))); final Pattern p = Pattern.compile("(?i)caLifornia"); assertNotNull(getDs().find(PhotoWithKeywords.class).disableValidation().filter("keywords.keyword", p) .find(new FindOptions().limit(1)) .next()); assertNull(getDs().find(PhotoWithKeywords.class).filter("keywords.keyword", Pattern.compile("blah")) .find(new FindOptions().limit(1)) .tryNext()); }
@Test public void testRegexQuery() { getDs().save(new PhotoWithKeywords(new Keyword("california"), new Keyword("nevada"), new Keyword("arizona"))); assertNotNull(getDs().find(PhotoWithKeywords.class) .disableValidation() .filter("keywords.keyword", Pattern.compile("california")) .find(new FindOptions().limit(1)) .next()); assertNull(getDs().find(PhotoWithKeywords.class).filter("keywords.keyword", Pattern.compile("blah")) .find(new FindOptions().limit(1)) .tryNext()); }
@Test public void embeddedIds() { final MyId id = new MyId("1", "2"); final EmbeddedId a = new EmbeddedId(id, "data"); final EmbeddedId b = new EmbeddedId(new MyId("2", "3"), "data, too"); getDs().save(a); getDs().save(b); assertEquals(a.data, getDs().get(EmbeddedId.class, id).data); final EmbeddedId embeddedId = getDs().find(EmbeddedId.class).field("_id").in(Arrays.asList(id)).find().next(); Assert.assertEquals(a.data, embeddedId.data); Assert.assertEquals(a.id, embeddedId.id); }
@Test public void testMapLike() { final ContainsMapLike ml = new ContainsMapLike(); ml.m.put("first", "test"); getDs().save(ml); final ContainsMapLike mlLoaded = getDs().find(ContainsMapLike.class) .find(new FindOptions().limit(1)) .next(); assertNotNull(mlLoaded); assertNotNull(mlLoaded.m); assertNotNull(mlLoaded.m.containsKey("first")); }
@Test public void testEmbeddedEntity() { getMorphia().map(ContainsEmbeddedEntity.class); getDs().save(new ContainsEmbeddedEntity()); final ContainsEmbeddedEntity ceeLoaded = getDs().find(ContainsEmbeddedEntity.class) .find(new FindOptions().limit(1)) .next(); assertNotNull(ceeLoaded); assertNotNull(ceeLoaded.id); assertNotNull(ceeLoaded.cil); assertNull(ceeLoaded.cil.id); }
@Test public void testStartsWithQuery() { getDs().save(new Photo()); assertNotNull(getDs().find(Photo.class).field("keywords").startsWith("amaz") .find(new FindOptions().limit(1)) .next()); assertNull(getDs().find(Photo.class).field("keywords").startsWith("notareal") .find(new FindOptions().limit(1)) .tryNext()); }