@Test public void testStore() { HasExpiryField hasExpiryField = new HasExpiryField(); hasExpiryField.setOfferIs("Good"); Calendar c = Calendar.getInstance(); hasExpiryField.setOfferExpiresAt(c.getTime()); ds.getMapper().addMappedClass(HasExpiryField.class); ds.ensureIndexes(); ds.save(hasExpiryField); DB db = ds.getDB(); DBCollection dbCollection = db.getCollection("HasExpiryField"); List<DBObject> indexes = dbCollection.getIndexInfo(); Assert.assertNotNull(indexes); Assert.assertEquals(2, indexes.size()); DBObject index = null; for (DBObject candidateIndex : indexes) { if (candidateIndex.containsField("expireAfterSeconds")) { index = candidateIndex; } } Assert.assertNotNull(index); Assert.assertTrue(index.containsField("expireAfterSeconds")); Assert.assertEquals(60, index.get("expireAfterSeconds")); }
/* * Extract "_id" from "o" if it fails try to extract from "o2" */ private String getObjectIdFromOplogEntry(DBObject entry) { if (entry.containsField(MongoDBRiver.OPLOG_OBJECT)) { DBObject object = (DBObject) entry.get(MongoDBRiver.OPLOG_OBJECT); if (object.containsField(MongoDBRiver.MONGODB_ID_FIELD)) { return object.get(MongoDBRiver.MONGODB_ID_FIELD).toString(); } } if (entry.containsField(MongoDBRiver.OPLOG_UPDATE)) { DBObject object = (DBObject) entry.get(MongoDBRiver.OPLOG_UPDATE); if (object.containsField(MongoDBRiver.MONGODB_ID_FIELD)) { return object.get(MongoDBRiver.MONGODB_ID_FIELD).toString(); } } return null; }
@Test public void integrationTestInsertCall() throws Exception { missedCallsDAO = new MissedCallsDAO(Resources.getDatabaseActivity()); DBObject testCall = new BasicDBObject("who", 9581300084L); try { assertTrue(missedCallsDAO.insertCall(testCall)); assertTrue(testCall.containsField("arrival")); assertEquals("Pending", testCall.get("state")); assertTrue(testCall.containsField("_id")); } finally { deleteCall(testCall); } }
public void ensureCaps() { for (MappedClass mc : mapr.getMappedClasses()) if (mc.getEntityAnnotation() != null && mc.getEntityAnnotation().cap().value() > 0) { CappedAt cap = mc.getEntityAnnotation().cap(); String collName = mapr.getCollectionName(mc.getClazz()); BasicDBObjectBuilder dbCapOpts = BasicDBObjectBuilder.start("capped", true); if (cap.value() > 0) dbCapOpts.add("size", cap.value()); if (cap.count() > 0) dbCapOpts.add("max", cap.count()); DB db = getDB(); if (db.getCollectionNames().contains(collName)) { DBObject dbResult = db.command(BasicDBObjectBuilder.start("collstats", collName).get()); if (dbResult.containsField("capped")) { // TODO: check the cap options. log.warning("DBCollection already exists is cap'd already; doing nothing. " + dbResult); } else { log.warning( "DBCollection already exists with same name(" + collName + ") and is not cap'd; not creating cap'd version!"); } } else { getDB().createCollection(collName, dbCapOpts.get()); log.debug("Created cap'd DBCollection (" + collName + ") with opts " + dbCapOpts); } } }
private void fixThumbnailImages() { DBObject query = new BasicDBObject("card_image_url", new BasicDBObject("$exists", false)); for (Iterator<DBObject> it = mPhotoCollection.find(query); it.hasNext(); ) { DBObject photo = it.next(); if (!photo.containsField("portal_image_url")) { System.err.println("No portal_image_url in " + photo); continue; } String portal_image_url = photo.get("portal_image_url").toString(); if (!portal_image_url.matches("^https?://.*")) { portal_image_url = Config.ALBUM_ROOT + portal_image_url; } try { URL url = new URL(portal_image_url); System.err.println("Missing thumbnail images for " + url); BufferedImage[] images = ImageUtil.convertImage(url); if (images != null) { DBObject updates = new BasicDBObject(); saveThumbnailImages(updates, images, url.getPath().replace(".", "_") + ".png"); mPhotoCollection.update(photo, new BasicDBObject("$set", updates)); } else { System.err.println(url + " corrupted"); } } catch (MalformedURLException e) { System.err.println("Unknown image URL" + portal_image_url); continue; } } }
private void deleteCall(DBObject call) throws UnknownHostException { if (call.containsField("_id")) { Resources.getDatabaseActivity() .getCollection("MissedCalls") .remove(new BasicDBObject("_id", call.get("_id"))); } }
@Override public Node getModel(ObjectId id) { DBObject dbNode = getDatabase().findOne("nodes", new BasicDBObject("_id", id)); if (dbNode == null) { return null; } Node node = new Node((ObjectId) dbNode.get("_id"), (Date) dbNode.get("created_at")); node.setUpdated_at((Date) dbNode.get("updated_at")); node.setName((String) dbNode.get("name")); node.setPrivateAddress((String) dbNode.get("privateAddress")); node.setRam(Integer.parseInt((String) dbNode.get("ram"))); if (dbNode.containsField("publicaddresses")) { BasicDBList publicAddressList = (BasicDBList) dbNode.get("publicaddresses"); for (Object objPublicAddress : publicAddressList) { DBObject dbPublicAddress = (DBObject) objPublicAddress; NodePublicAddress nodePublicAddress = new NodePublicAddress( (ObjectId) dbPublicAddress.get("_id"), (Date) dbPublicAddress.get("created_at")); nodePublicAddress.setUpdated_at((Date) dbPublicAddress.get("updated_at")); nodePublicAddress.setPublicAddress((String) dbPublicAddress.get("publicAddress")); nodePublicAddress.setNode(node); node.getPublicAddresses().put(nodePublicAddress.getId(), nodePublicAddress); } } return node; }
@PostPersist void postPersistWithParam(final DBObject dbObj) { postPersistWithParam = true; if (!dbObj.containsField(Mapper.ID_KEY)) { throw new RuntimeException("missing " + Mapper.ID_KEY); } }
protected void writeInternal(Object obj, final DBObject dbo, MongoPersistentEntity<?> entity) { if (obj == null) { return; } if (null == entity) { throw new MappingException( "No mapping metadata found for entity of type " + obj.getClass().getName()); } final PersistentPropertyAccessor accessor = entity.getPropertyAccessor(obj); final MongoPersistentProperty idProperty = entity.getIdProperty(); if (!dbo.containsField("_id") && null != idProperty) { try { Object id = accessor.getProperty(idProperty); dbo.put("_id", idMapper.convertId(id)); } catch (ConversionException ignored) { } } // Write the properties entity.doWithProperties( new PropertyHandler<MongoPersistentProperty>() { public void doWithPersistentProperty(MongoPersistentProperty prop) { if (prop.equals(idProperty) || !prop.isWritable()) { return; } Object propertyObj = accessor.getProperty(prop); if (null != propertyObj) { if (!conversions.isSimpleType(propertyObj.getClass())) { writePropertyInternal(propertyObj, dbo, prop); } else { writeSimpleInternal(propertyObj, dbo, prop); } } } }); entity.doWithAssociations( new AssociationHandler<MongoPersistentProperty>() { public void doWithAssociation(Association<MongoPersistentProperty> association) { MongoPersistentProperty inverseProp = association.getInverse(); Object propertyObj = accessor.getProperty(inverseProp); if (null != propertyObj) { writePropertyInternal(propertyObj, dbo, inverseProp); } } }); }
private String addInsertToStream( final Timestamp<?> currentTimestamp, final DBObject data, final String collection) throws InterruptedException { totalDocuments.incrementAndGet(); addToStream(Operation.INSERT, currentTimestamp, data, collection); return data.containsField(MongoDBRiver.MONGODB_ID_FIELD) ? data.get(MongoDBRiver.MONGODB_ID_FIELD).toString() : null; }
@Test public void testInsertCall() throws Exception { DBObject callObj = new BasicDBObject(); assertTrue(missedCallsDAO.insertCall(callObj)); assertTrue(callObj.containsField("arrival")); assertEquals("Pending", callObj.get("state")); verify(missedCalls, times(1)).insert(any(DBObject.class)); }
private void processAdminCommandOplogEntry( final DBObject entry, final Timestamp<?> startTimestamp) throws InterruptedException { if (logger.isTraceEnabled()) { logger.trace("processAdminCommandOplogEntry - [{}]", entry); } DBObject object = (DBObject) entry.get(MongoDBRiver.OPLOG_OBJECT); if (definition.isImportAllCollections()) { if (object.containsField(MongoDBRiver.OPLOG_RENAME_COLLECTION_COMMAND_OPERATION) && object.containsField(MongoDBRiver.OPLOG_TO)) { String to = object.get(MongoDBRiver.OPLOG_TO).toString(); if (to.startsWith(definition.getMongoDb())) { String newCollection = getCollectionFromNamespace(to); DBCollection coll = slurpedDb.getCollection(newCollection); doInitialImport(coll); } } } }
@Override public Object createObject(byte[] data, int offset) { DBObject dbo = new LazyBsonByte(data, offset, this); Iterator it = dbo.keySet().iterator(); if (it.hasNext() && it.next().equals("$ref") && dbo.containsField("$id")) { return new DBRef(db, dbo); } return dbo; }
@Override public Node byNodeId(String nodeId) throws NodeNotFoundException { DBObject query = new BasicDBObject("node_id", nodeId); DBObject o = findOne(NodeImpl.class, query); if (o == null || !o.containsField("node_id")) { throw new NodeNotFoundException("Unable to find node " + nodeId); } return new NodeImpl((ObjectId) o.get("_id"), o.toMap()); }
/** @see DATAMONGO-404 */ @Test public void rendersNestedDbRefCorrectly() { Update update = new Update().pull("nested.dbRefAnnotatedList.id", "2"); DBObject mappedObject = mapper.getMappedObject( update.getUpdateObject(), context.getPersistentEntity(Wrapper.class)); DBObject pullClause = getAsDBObject(mappedObject, "$pull"); assertThat(pullClause.containsField("mapped.dbRefAnnotatedList"), is(true)); }
public String getAccountConnected(String msisdn, DB userDB) { DBCollection collection = userDB.getCollection(HikeConstants.MONGO_USERS_COLLECTION); BasicDBObject query = new BasicDBObject(); query.put(HikeConstants.MSISDN, msisdn); DBObject result = collection.findOne(query, new BasicDBObject().append("accounts", 1)); if ((result == null) || !result.containsField("accounts")) { return null; } return result.get("accounts").toString(); }
private boolean filterMatch(DBObject filter, DBObject object) { for (String key : filter.keySet()) { if (!object.containsField(key)) { return false; } if (!filter.get(key).equals(object.get(key))) { return false; } } return true; }
/** @see DATAMONGO-862 */ @Test public void rendersUpdateAndPreservesKeyForPathsNotPointingToProperty() { Update update = new Update().set("listOfInterface.$.value", "expected-value"); DBObject mappedObject = mapper.getMappedObject( update.getUpdateObject(), context.getPersistentEntity(ParentClass.class)); DBObject setClause = getAsDBObject(mappedObject, "$set"); assertThat(setClause.containsField("listOfInterface.$.value"), is(true)); }
public String getDetailsFromMsisdn(String msisdn, String field) { DBCollection collection = userDB.getCollection(HikeConstants.MONGO_USERS_COLLECTION); BasicDBObject query = new BasicDBObject(); query.put(HikeConstants.MSISDN, msisdn); DBObject result = collection.findOne(query, new BasicDBObject().append(field, 1)); if ((result == null) || !result.containsField(field)) { return null; } return result.get(field).toString(); }
/** * @param user * @return gender if exists, else UNKNOWN */ public static String getGender(DBObject user) { if (user != null) { if (!user.containsField(User.ATTR_GENDER) || user.get(User.ATTR_GENDER) == null) { return Gender.UNKNOWN.getValue(); } return (String) user.get(User.ATTR_GENDER); } return null; }
/** * Find and mask property values. * * <p>Properties will be searched for deeply. * * @param obj the object to modify */ public static DBObject maskValues(DBObject dbObject) { if (dbObject instanceof BasicDBList) { BasicDBList objListOrig = (BasicDBList) dbObject; // Copy entries to a new list BasicDBList newObjList = new BasicDBList(); for (Object origListObjT : objListOrig) { DBObject origListObj = (DBObject) origListObjT; // Mask any values DBObject newListObj = maskValues(origListObj); newObjList.add(newListObj); } // Done return newObjList; } else if (dbObject.containsField(FIELD_MASK)) { boolean mask = Boolean.parseBoolean((String) dbObject.get(FIELD_MASK)); if (mask) { DBObject newObj = copyDBObject(dbObject); // We have a copy to play with newObj.put(FIELD_DEFAULT, MASK); if (dbObject.get(FIELD_VALUE) != null) { newObj.put(FIELD_VALUE, MASK); } return newObj; } else { return dbObject; } } else if (dbObject.containsField(FIELD_PROPERTIES)) { // There are properties BasicDBList propsObj = (BasicDBList) dbObject.get(FIELD_PROPERTIES); BasicDBList newPropsObj = (BasicDBList) maskValues(propsObj); // Copy DBObject newObj = copyDBObject(dbObject); newObj.put(FIELD_PROPERTIES, newPropsObj); // Done return newObj; } else { // Not a list and does not contain the mask field return dbObject; } }
public String getBlockedUserList(String msisdnBlocker, DB userDB) { DBCollection collection = userDB.getCollection(HikeConstants.COMMON_BLOCKLIST); BasicDBObject query = new BasicDBObject(); query.put(HikeConstants.MSISDN, msisdnBlocker); DBObject result = collection.findOne(query); if ((result == null) || !result.containsField("blocklist") || result.get("blocklist").toString().equals("[ ]")) { return null; } return result.get("blocklist").toString(); }
@Test public void writesTypeDiscriminatorIntoRootObject() { Person person = new Person(); person.birthDate = new LocalDate(); DBObject result = new BasicDBObject(); converter.write(person, result); assertThat(result.containsField(MappingMongoConverter.CUSTOM_TYPE_KEY), is(true)); assertThat( result.get(MappingMongoConverter.CUSTOM_TYPE_KEY).toString(), is(Person.class.getName())); }
@Override public List<Favorite> addToFavorites(String email, List<Favorite> localFavorites) throws UserNotFound { if (email == null) { throw new IllegalArgumentException("Email cannot be null"); } DBCollection col = db.getCollection(MongoDBConstants.COLLECTION_USERS); // Search user by email DBObject userDb = col.findOne(new BasicDBObject(MongoDBConstants.USER_PROP_EMAIL, email)); if (userDb == null) { throw new UserNotFound("Unable to find user with email '" + email + "'"); } DBObject favoritesDb = (DBObject) userDb.get(MongoDBConstants.USER_PROP_FAVORITES); if (favoritesDb == null) { favoritesDb = new BasicDBObject(); } if (localFavorites != null) { for (Favorite localFavorite : localFavorites) { DBObject favoriteItemsDb = (DBObject) favoritesDb.get(localFavorite.getStationId()); long lastMessageIdDb = -1; if (favoriteItemsDb == null) { favoriteItemsDb = new BasicDBObject(); } else { if (favoriteItemsDb.containsField(MongoDBConstants.USER_PROP_FAVORITE_LASTMESSAGEID)) { lastMessageIdDb = (Long) favoriteItemsDb.get(MongoDBConstants.USER_PROP_FAVORITE_LASTMESSAGEID); } } favoriteItemsDb.put( MongoDBConstants.USER_PROP_FAVORITE_LASTMESSAGEID, Math.max(lastMessageIdDb, localFavorite.getLastMessageId())); favoritesDb.put(localFavorite.getStationId(), favoriteItemsDb); } } userDb.put(MongoDBConstants.USER_PROP_FAVORITES, favoritesDb); col.save(userDb); List<Favorite> returnValue = new ArrayList<Favorite>(favoritesDb.keySet().size()); for (String stationId : favoritesDb.keySet()) { Favorite favorite = new Favorite(); favorite.setStationId(stationId); DBObject favoriteItemDb = (DBObject) favoritesDb.get(stationId); favorite.setLastMessageId( (Long) favoriteItemDb.get(MongoDBConstants.USER_PROP_FAVORITE_LASTMESSAGEID)); returnValue.add(favorite); } return returnValue; }
public DBObject findByPermalink(String permalink) { DBObject post = postsCollection.findOne(new BasicDBObject("permalink", permalink)); // fix up if a post has no likes if (post != null) { List<DBObject> comments = (List<DBObject>) post.get("comments"); for (DBObject comment : comments) { if (!comment.containsField("num_likes")) { comment.put("num_likes", 0); } } } return post; }
private Filter buildExpressionFilter(final List<String> path, Object expression) { if (OR.equals(path.get(0))) { @SuppressWarnings("unchecked") Collection<DBObject> queryList = typecast(path + " operator", expression, Collection.class); OrFilter orFilter = new OrFilter(); for (DBObject query : queryList) { orFilter.addFilter(buildFilter(query)); } return orFilter; } else if (AND.equals(path.get(0))) { Collection<DBObject> queryList = typecast(path + " operator", expression, Collection.class); AndFilter andFilter = new AndFilter(); for (DBObject query : queryList) { andFilter.addFilter(buildFilter(query)); } return andFilter; } else if (WHERE.equals(path.get(0))) { return new WhereFilter((String) expression); } else if (expression instanceof DBObject || expression instanceof Map) { DBObject ref = expression instanceof DBObject ? (DBObject) expression : new BasicDBObject((Map) expression); if (ref.containsField(NOT)) { return new NotFilter(buildExpressionFilter(path, ref.get(NOT))); } else { AndFilter andFilter = new AndFilter(); int matchCount = 0; for (FilterFactory filterFactory : filterFactories) { if (filterFactory.matchesCommand(ref)) { matchCount++; andFilter.addFilter(filterFactory.createFilter(path, ref)); } } if (matchCount == 0) { return simpleFilter(path, expression); } if (matchCount > 2) { throw new FongoException("Invalid expression for key " + path + ": " + expression); } return andFilter; } } else if (expression instanceof Pattern) { return createPatternFilter(path, (Pattern) expression); } else { return simpleFilter(path, expression); } }
/** * @param source * @param target */ public static void copyUser(DBObject source, DBObject target) { for (String property : new String[] { "email", "name", "gender", "yearOfBirth", "state", "settings", EntityUtils.ID }) { if (source.containsField(property)) { if (Map.class.isInstance(source.get(property))) { target.put(property, new BasicDBObject(Map.class.cast(source.get(property)))); } else { target.put(property, source.get(property)); } } } }
@Test public void integrationTestFetchNextPendingCall() throws Exception { missedCallsDAO = new MissedCallsDAO(Resources.getDatabaseActivity()); DBObject testCallFirst = new BasicDBObject("who", 9581300084L); DBObject testCallSecond = new BasicDBObject("who", 9581300084L); try { assertTrue(missedCallsDAO.insertCall(testCallFirst)); Thread.sleep(1000); assertTrue(missedCallsDAO.insertCall(testCallSecond)); Date tsFirst = (Date) testCallFirst.get("arrival"); Date tsSecond = (Date) testCallSecond.get("arrival"); assertTrue(tsFirst.before(tsSecond)); DBObject fetchedCallFirst = missedCallsDAO.fetchNextPendingCall(); assertNotNull(fetchedCallFirst); assertTrue(fetchedCallFirst.containsField("who")); assertEquals("Processing", fetchedCallFirst.get("state")); assertEquals(testCallSecond.get("_id"), fetchedCallFirst.get("_id")); DBObject fetchedCallSecond = missedCallsDAO.fetchNextPendingCall(); assertNotNull(fetchedCallSecond); assertTrue(fetchedCallSecond.containsField("who")); assertEquals("Processing", fetchedCallSecond.get("state")); assertEquals(testCallFirst.get("_id"), fetchedCallSecond.get("_id")); tsFirst = (Date) fetchedCallFirst.get("arrival"); tsSecond = (Date) fetchedCallSecond.get("arrival"); assertTrue(tsFirst.after(tsSecond)); } finally { deleteCall(testCallFirst); deleteCall(testCallSecond); } }
@Override public Server getModel(ObjectId id) { DBObject dbServer = getDatabase().findOne("servers", new BasicDBObject("_id", id)); if (dbServer == null) { return null; } Server server = new Server((ObjectId) dbServer.get("_id"), (Date) dbServer.get("created_at")); server.setUpdated_at((Date) dbServer.get("updated_at")); server.setNetwork( getDatabase() .getNetworkRepository() .getModel(new ObjectId((String) dbServer.get("network_id")))); String node = (String) dbServer.get("node_id"); if (node != null) { server.setNode(getDatabase().getNodeRepository().getModel(new ObjectId(node))); } server.setServerType( getDatabase() .getServerTypeRepository() .getModel(new ObjectId((String) dbServer.get("server_type_id")))); server.setContainerId((String) dbServer.get("container")); server.setPort((int) dbServer.get("port")); BasicDBObject dbPlayers = (BasicDBObject) dbServer.get("players"); for (String name : dbPlayers.keySet()) { server.getPlayers().put(name, (UUID) dbPlayers.get(name)); } if (dbServer.containsField("metaData")) { BasicDBList metaDataList = (BasicDBList) dbServer.get("metaData"); for (Object metaDataObj : metaDataList) { DBObject dbMetaData = (DBObject) metaDataObj; ServerMetaData metaData = new ServerMetaData( (ObjectId) dbMetaData.get("_id"), (Date) dbMetaData.get("created_at")); metaData.setUpdated_at((Date) dbMetaData.get("updated_at")); metaData.setKey((String) dbMetaData.get("key")); metaData.setValue((String) dbMetaData.get("value")); server.getMetaData().put(metaData.getKey(), metaData); } } server.setNumber((int) dbServer.get("number")); return server; }
/** @see DATAMONGO-943 */ @Test public void updatePushEachAtPositionWorksCorrectlyWhenGivenPositionNull() { Update update = new Update().push("key").atPosition(null).each(Arrays.asList("Arya", "Arry", "Weasel")); DBObject mappedObject = mapper.getMappedObject(update.getUpdateObject(), context.getPersistentEntity(Object.class)); DBObject push = getAsDBObject(mappedObject, "$push"); DBObject key = getAsDBObject(push, "key"); assertThat(key.containsField("$position"), is(false)); assertThat(getAsDBObject(push, "key").containsField("$each"), is(true)); }