use of com.mongodb.BasicDBList in project mongo-hadoop by mongodb.
the class MongoLoaderTest method testSimpleBag.
@Test
public void testSimpleBag() throws IOException {
String userSchema = "b:{t:tuple(t1:chararray, t2:chararray)}";
BasicDBList bag = new BasicDBList();
bag.add(new BasicDBObject().append("t1", "t11_value").append("t2", "t12_value"));
bag.add(new BasicDBObject().append("t1", "t21_value").append("t2", "t22_value"));
MongoLoader ml = new MongoLoader(userSchema);
Object result = BSONLoader.readField(bag, ml.getFields()[0]);
DataBag b = (DataBag) result;
Iterator<Tuple> bit = b.iterator();
Tuple firstInnerT = bit.next();
assertEquals(2, firstInnerT.size());
assertEquals("t11_value", firstInnerT.get(0));
assertEquals("t12_value", firstInnerT.get(1));
Tuple secondInnerT = bit.next();
assertEquals(2, secondInnerT.size());
assertEquals("t21_value", secondInnerT.get(0));
assertEquals("t22_value", secondInnerT.get(1));
assertFalse(bit.hasNext());
}
use of com.mongodb.BasicDBList in project morphia by mongodb.
the class Mapper method toMongoObject.
/**
* Converts a java object to a mongo-compatible object (possibly a DBObject for complex mappings). Very similar to {@link
* Mapper#toDBObject}. Used (mainly) by query/update operations.
*
* @param mf the MappedField for this value
* @param mc the MappedClass for this value
* @param value the value to convert
* @return the MongoDB compatible object
*/
@SuppressWarnings("deprecation")
public Object toMongoObject(final MappedField mf, final MappedClass mc, final Object value) {
if (value == null) {
return null;
}
Object mappedValue = value;
if (value instanceof Query) {
mappedValue = ((QueryImpl) value).getQueryObject();
} else if (isAssignable(mf, value) || isEntity(mc)) {
//convert the value to Key (DBRef) if the field is @Reference or type is Key/DBRef, or if the destination class is an @Entity
try {
if (value instanceof Iterable) {
MappedClass mapped = getMappedClass(mf.getSubClass());
if (mapped != null && (Key.class.isAssignableFrom(mapped.getClazz()) || mapped.getEntityAnnotation() != null)) {
mappedValue = getDBRefs(mf, (Iterable) value);
} else {
if (mf.hasAnnotation(Reference.class)) {
mappedValue = getDBRefs(mf, (Iterable) value);
} else {
mappedValue = toMongoObject(value, false);
}
}
} else {
if (mf != null) {
Reference refAnn = mf.getAnnotation(Reference.class);
Class<?> idType = null;
if (!mf.getType().equals(Key.class) && isMapped(mf.getType())) {
idType = getMappedClass(mf.getType()).getMappedIdField().getType();
}
boolean valueIsIdType = mappedValue.getClass().equals(idType);
if (refAnn != null) {
if (!valueIsIdType) {
Key<?> key = value instanceof Key ? (Key<?>) value : getKey(value);
if (key != null) {
mappedValue = refAnn.idOnly() ? keyToId(key) : keyToDBRef(key);
}
}
} else if (mf.getType().equals(Key.class)) {
mappedValue = keyToDBRef(valueIsIdType ? createKey(mf.getSubClass(), value) : value instanceof Key ? (Key<?>) value : getKey(value));
if (mappedValue == value) {
throw new ValidationException("cannot map to Key<T> field: " + value);
}
}
}
if (mappedValue == value) {
mappedValue = toMongoObject(value, false);
}
}
} catch (Exception e) {
LOG.error("Error converting value(" + value + ") to reference.", e);
mappedValue = toMongoObject(value, false);
}
} else if (mf != null && mf.hasAnnotation(Serialized.class)) {
//serialized
try {
mappedValue = Serializer.serialize(value, !mf.getAnnotation(Serialized.class).disableCompression());
} catch (IOException e) {
throw new RuntimeException(e);
}
} else if (value instanceof DBObject) {
//pass-through
mappedValue = value;
} else {
mappedValue = toMongoObject(value, EmbeddedMapper.shouldSaveClassName(value, mappedValue, mf));
if (mappedValue instanceof BasicDBList) {
final BasicDBList list = (BasicDBList) mappedValue;
if (list.size() != 0) {
if (!EmbeddedMapper.shouldSaveClassName(extractFirstElement(value), list.get(0), mf)) {
for (Object o : list) {
if (o instanceof DBObject) {
((DBObject) o).removeField(CLASS_NAME_FIELDNAME);
}
}
}
}
} else if (mappedValue instanceof DBObject && !EmbeddedMapper.shouldSaveClassName(value, mappedValue, mf)) {
((DBObject) mappedValue).removeField(CLASS_NAME_FIELDNAME);
}
}
return mappedValue;
}
use of com.mongodb.BasicDBList in project morphia by mongodb.
the class MappedFieldTest method nestedCollectionsMapping.
@Test
public void nestedCollectionsMapping() {
final MappedField field = new MappedField(getField(TestEntity.class, "listOfListOfString"), TestEntity.class, getMorphia().getMapper());
Assert.assertFalse(field.isSingleValue());
Assert.assertTrue(field.isMultipleValues());
Assert.assertFalse(field.isArray());
Assert.assertTrue(List.class == field.getType());
final List<MappedField> level1Types = field.getTypeParameters();
final MappedField typeParameter = level1Types.get(0);
Assert.assertTrue(List.class == typeParameter.getConcreteType());
final List<MappedField> level2Types = typeParameter.getTypeParameters();
final MappedField nested = level2Types.get(0);
Assert.assertTrue(String.class == nested.getConcreteType());
Assert.assertEquals("listOfListOfString", field.getJavaFieldName());
Assert.assertEquals("listOfListOfString", field.getNameToStore());
final BasicDBList list = new BasicDBList();
list.add(dbList("a", "b", "c"));
list.add(dbList("d", "e", "f"));
final TestEntity entity = getMorphia().getMapper().fromDb(getDs(), new BasicDBObject("listOfListOfString", list), new TestEntity(), new DefaultEntityCache());
final List<String> strings = asList("a", "b", "c");
final List<String> strings1 = asList("d", "e", "f");
final List<List<String>> expected = new ArrayList<List<String>>();
expected.add(strings);
expected.add(strings1);
Assert.assertEquals(expected, entity.listOfListOfString);
}
use of com.mongodb.BasicDBList in project spring-data-document-examples by spring-projects.
the class ChartController method getFavoritesData.
private DefaultCategoryDataset getFavoritesData() {
MongoTemplate mongoTemplate;
DefaultCategoryDataset ds = null;
try {
Mongo m = new Mongo();
mongoTemplate = new MongoTemplate(m, "mvc");
DBObject result = getTopRecommendedRestaurants(mongoTemplate);
/* Example data.
* [ { "parameters.p1" : "1" , "count" : 5.0} ,
* { "parameters.p1" : "2" , "count" : 6.0} ,
* { "parameters.p1" : "3" , "count" : 3.0} ,
* { "parameters.p1" : "4" , "count" : 8.0}]
*/
ds = new DefaultCategoryDataset();
if (result instanceof BasicDBList) {
BasicDBList dbList = (BasicDBList) result;
for (Iterator iterator = dbList.iterator(); iterator.hasNext(); ) {
DBObject dbo = (DBObject) iterator.next();
System.out.println(dbo);
Restaurant r = restaurantDao.findRestaurant(Long.parseLong(dbo.get("parameters.p1").toString()));
ds.addValue(Double.parseDouble(dbo.get("count").toString()), "recommended", r.getName());
}
}
return ds;
} catch (Exception e) {
e.printStackTrace();
}
return ds;
}
use of com.mongodb.BasicDBList in project graylog2-server by Graylog2.
the class InputImpl method getStaticFields.
@Override
public Map<String, String> getStaticFields() {
if (fields.get(EMBEDDED_STATIC_FIELDS) == null) {
return Collections.emptyMap();
}
final BasicDBList list = (BasicDBList) fields.get(EMBEDDED_STATIC_FIELDS);
final Map<String, String> staticFields = Maps.newHashMapWithExpectedSize(list.size());
for (final Object element : list) {
try {
final DBObject field = (DBObject) element;
staticFields.put((String) field.get(FIELD_STATIC_FIELD_KEY), (String) field.get(FIELD_STATIC_FIELD_VALUE));
} catch (Exception e) {
LOG.error("Cannot build static field from persisted data. Skipping.", e);
}
}
return staticFields;
}
Aggregations