@Override public void insertModel(Server model) { BasicDBObject dbServer = new BasicDBObject(); dbServer.put("_id", model.getId()); dbServer.put("created_at", model.getCreated_at()); dbServer.put("updated_at", model.getUpdated_at()); dbServer.put("network_id", model.getNetwork().getId().toString()); dbServer.put("node_id", null); dbServer.put("server_type_id", model.getServerType().getId().toString()); dbServer.put("port", 0); dbServer.put("container", "NULL"); dbServer.put("players", new BasicDBObject()); BasicDBList metaDataList = new BasicDBList(); for (Map.Entry<String, ServerMetaData> metaDataEntry : model.getMetaData().entrySet()) { DBObject dbMetaData = new BasicDBObject(); dbMetaData.put("_id", metaDataEntry.getValue().getId()); dbMetaData.put("created_at", metaDataEntry.getValue().getCreated_at()); dbMetaData.put("updated_at", metaDataEntry.getValue().getUpdated_at()); dbMetaData.put("key", metaDataEntry.getValue().getKey()); dbMetaData.put("value", metaDataEntry.getValue().getValue()); metaDataList.add(dbMetaData); } dbServer.put("metaData", metaDataList); dbServer.put("number", 0); getDatabase().insert("servers", dbServer); }
@Override public void saveModel(Server model) { BasicDBObject dbServer = new BasicDBObject(); dbServer.put("updated_at", model.getUpdated_at()); if (model.getNode() != null) { dbServer.put("node_id", model.getNode().getId().toString()); } dbServer.put("port", model.getPort()); dbServer.put("container", model.getContainerId()); dbServer.put("players", new BasicDBObject(model.getPlayers())); BasicDBList metaDataList = new BasicDBList(); for (Map.Entry<String, ServerMetaData> metaDataEntry : model.getMetaData().entrySet()) { DBObject dbMetaData = new BasicDBObject(); dbMetaData.put("_id", metaDataEntry.getValue().getId()); dbMetaData.put("created_at", metaDataEntry.getValue().getCreated_at()); dbMetaData.put("updated_at", metaDataEntry.getValue().getUpdated_at()); dbMetaData.put("key", metaDataEntry.getValue().getKey()); dbMetaData.put("value", metaDataEntry.getValue().getValue()); metaDataList.add(dbMetaData); } dbServer.put("metaData", metaDataList); dbServer.put("number", model.getNumber()); getDatabase() .updateDocument( "servers", new BasicDBObject("_id", model.getId()), new BasicDBObject("$set", dbServer)); }
@Override public DBObject toJSON() { BasicDBObjectBuilder builder = BasicDBObjectBuilder.start(); BasicDBList jsonList = new BasicDBList(); Iterator<VoronoiSite> sitesIt = this.sites.iterator(); while (sitesIt.hasNext()) { jsonList.add(sitesIt.next().toJSON()); } builder.add("sites", jsonList); jsonList = new BasicDBList(); Iterator<VoronoiCorner> cornersIt = this.corners.iterator(); while (cornersIt.hasNext()) { jsonList.add(cornersIt.next().toJSON()); } builder.add("corners", jsonList); jsonList = new BasicDBList(); Iterator<VoronoiEdge> edgesIt = this.edges.iterator(); while (edgesIt.hasNext()) { jsonList.add(edgesIt.next().toJSON()); } builder.add("edges", jsonList); return builder.get(); }
/* * (non-Javadoc) * * @see * com.sg.ui.model.input.BusinessObjectRelationListInput#doQuery(com.mongodb * .DBObject) */ @Override protected List<DBObject> doQuery(DBObject returnFields) { if (service == null) { service = getService(); } DBObject projection = returnFields == null ? service.getDefaultSearchColumns() : returnFields; try { BasicDBList list = new BasicDBList(); list.add(new BasicDBObject().append(Action.FIELD_OPPORTUNIY_ID, opportunityId)); list.add(new BasicDBObject().append(Action.FIELD_OPPORTUNIY_ID, null)); BasicDBObject query = new BasicDBObject().append(Action.FIELD_COMPANY_ID, companyId).append(Action.OR, list); DBCursor cursor = service.find(query, projection); DBObject sort = getSort(); cursor.sort(sort); return cursor.toArray(); } catch (Exception e) { e.printStackTrace(); } return null; }
public static BasicDBList encodeArray(JsonArray a) { BasicDBList dbl = new BasicDBList(); for (JsonElement el : a) { dbl.add(encodeUnknown(el)); } return dbl; } // TESTED
@Override public QueryResponse getAllByPositionList(List<Position> positionList, QueryOptions options) { // db.regulatory_region.find({"chunkIds": {$in:["1_200", "1_300"]}, "start": 601156}) String featureType = options.getString("featureType", null); String featureClass = options.getString("featureClass", null); List<DBObject> queries = new ArrayList<>(); for (Position position : positionList) { String chunkId = position.getChromosome() + "_" + getChunkId(position.getPosition(), CHUNKSIZE); BasicDBList chunksId = new BasicDBList(); chunksId.add(chunkId); QueryBuilder builder = QueryBuilder.start("chunkIds").in(chunksId).and("start").is(position.getPosition()); if (featureType != null) { builder.and("featureType").is(featureType); } if (featureClass != null) { builder.and("featureClass").is(featureClass); } // System.out.println("Query: " + builder.get()); queries.add(builder.get()); } System.out.println("Query: " + queries); options = addExcludeReturnFields("chunkIds", options); return executeQueryList(positionList, queries, options); }
@Override public void update(KeycloakSession session) { BasicDBList orArgs = new BasicDBList(); orArgs.add(new BasicDBObject("type", UserCredentialModel.PASSWORD)); orArgs.add(new BasicDBObject("type", UserCredentialModel.PASSWORD_HISTORY)); BasicDBObject elemMatch = new BasicDBObject("$or", orArgs); elemMatch.put("algorithm", new BasicDBObject("$exists", false)); BasicDBObject query = new BasicDBObject("credentials", new BasicDBObject("$elemMatch", elemMatch)); BasicDBObject update = new BasicDBObject( "$set", new BasicDBObject("credentials.$.algorithm", Pbkdf2PasswordHashProvider.ID)); DBCollection users = db.getCollection("users"); // Not sure how to do in single query int countModified = 1; while (countModified > 0) { WriteResult wr = users.update(query, update, false, true); countModified = wr.getN(); log.debugf( "%d credentials modified in current iteration during upgrade to 1.8", countModified); } }
@Test public void testBlacklistedWithPositiveResultAndNewline() { BasicDBObject mongoList = new BasicDBObject(); mongoList.put("_id", new ObjectId()); mongoList.put("title", "foo"); BasicDBObject mongoRule1 = new BasicDBObject(); mongoRule1.put("_id", new ObjectId()); mongoRule1.put("blacklist_id", mongoList.get("_id")); mongoRule1.put("term", "^ohai.+"); BasicDBObject mongoRule2 = new BasicDBObject(); mongoRule2.put("_id", new ObjectId()); mongoRule1.put("blacklist_id", mongoList.get("_id")); mongoRule2.put("term", ".+aarrghhhllll"); BasicDBList rules = new BasicDBList(); rules.add(mongoRule1); rules.add(mongoRule2); mongoList.put("blacklisted_terms", rules); Blacklist blacklist = new Blacklist(mongoList); GELFMessage msg = new GELFMessage(); msg.setShortMessage("ohai thar\nfoo"); List<Blacklist> blacklists = new ArrayList<Blacklist>(); blacklists.add(blacklist); assertTrue(msg.blacklisted(blacklists)); }
private DBObject marshallArray(Object[] parameters) { BasicDBList list = new BasicDBList(); for (int i = 0; i < parameters.length; i++) { list.add(marshallParameter(parameters[i])); } return list; }
protected BasicDBList list(Object... values) { BasicDBList list = new BasicDBList(); for (Object v : values) { list.add(v); } return list; }
private DBObject marshallCollection(Collection<?> parameters) { BasicDBList list = new BasicDBList(); for (Object param : parameters) { list.add(marshallParameter(param)); } return list; }
/** {@inheritDoc} */ @Override public DBObject toDBObject(Music music) { DBObject doc = new BasicDBObject(); doc.put("trackNumber", music.getTrackNumber()); doc.put("title", music.getTitle()); doc.put("artistName", music.getArtistName()); doc.put("albumName", music.getAlbumName()); if (music.getFileId() != null) { doc.put("fileId", new ObjectId(music.getFileId())); } BasicDBList tagsList = new BasicDBList(); for (String tag : music.getTags()) { tagsList.add(tag); } doc.put("tags", tagsList); BasicDBList comentariosList = new BasicDBList(); for (Comment comentario : music.getComments()) { comentariosList.add(commentConv.toDBObject(comentario)); } doc.put("comments", comentariosList); return doc; }
private String selectBooks( String schema, String collection, String title, String author, String isbn) throws UnknownHostException { System.out.println(schema); System.out.println(collection); System.out.println(title); System.out.println(author); List<String> l = new ArrayList<String>(); MongoDBConnectionManager mgr = MongoDBConnectionManager.getInstance(); Mongo mongo = mgr.getMongo(); DB db = mongo.getDB(schema); try { DBCollection c = db.getCollection(collection); BasicDBObject titleQuery = new BasicDBObject(); titleQuery.put("title", new BasicDBObject("$regex", title)); BasicDBObject authorQuery = new BasicDBObject(); authorQuery.put("author", new BasicDBObject("$regex", author)); BasicDBList or = new BasicDBList(); or.add(titleQuery); or.add(titleQuery); DBObject query = new BasicDBObject("$and", or); DBCursor cursor = c.find(titleQuery); while (cursor.hasNext()) l.add(cursor.next().toString()); } catch (Exception e) { System.out.println(e.getMessage()); } System.out.println(l); if (l.size() > 0) return l.toString(); else return null; }
/** * All headers except collection and database are non available for this operation. * * @param exchange * @throws Exception */ protected void doAggregate(Exchange exchange) throws Exception { DBCollection dbCol = calculateCollection(exchange); DBObject query = exchange.getIn().getMandatoryBody(DBObject.class); // Impossible with java driver to get the batch size and number to skip Iterable<DBObject> dbIterator = null; AggregationOutput aggregationResult = null; // Allow body to be a pipeline // @see http://docs.mongodb.org/manual/core/aggregation/ if (query instanceof BasicDBList) { BasicDBList queryList = (BasicDBList) query; aggregationResult = dbCol.aggregate( (DBObject) queryList.get(0), queryList .subList(1, queryList.size()) .toArray(new BasicDBObject[queryList.size() - 1])); } else { aggregationResult = dbCol.aggregate(query); } dbIterator = aggregationResult.results(); Message resultMessage = prepareResponseMessage(exchange, MongoDbOperation.aggregate); resultMessage.setBody(dbIterator); }
private void addNewAdminRolesToRealm(BasicDBObject currentRealm) { DBCollection applications = db.getCollection("applications"); DBCollection roles = db.getCollection("roles"); BasicDBObject adminApp = (BasicDBObject) applications.findOne( new BasicDBObject() .append("realmId", currentRealm.get("_id")) .append("name", "realm-management")); String viewIdProvidersRoleId = insertApplicationRole(roles, AdminRoles.VIEW_IDENTITY_PROVIDERS, adminApp.getString("_id")); String manageIdProvidersRoleId = insertApplicationRole( roles, AdminRoles.MANAGE_IDENTITY_PROVIDERS, adminApp.getString("_id")); BasicDBObject adminRole = (BasicDBObject) roles.findOne( new BasicDBObject() .append("applicationId", adminApp.get("_id")) .append("name", AdminRoles.REALM_ADMIN)); BasicDBList adminCompositeRoles = (BasicDBList) adminRole.get("compositeRoleIds"); adminCompositeRoles.add(viewIdProvidersRoleId); adminCompositeRoles.add(manageIdProvidersRoleId); roles.save(adminRole); log.debugv( "Added roles {0} and {1} to application realm-management of realm {2}", AdminRoles.VIEW_IDENTITY_PROVIDERS, AdminRoles.MANAGE_IDENTITY_PROVIDERS, currentRealm.get("name")); }
@Test public void testGetStreamRules() { BasicDBObject mongoList = this.buildMongoBlacklist(); BasicDBObject mongoRule1 = new BasicDBObject(); mongoRule1.put("_id", new ObjectId()); mongoRule1.put("blacklist_id", mongoList.get("_id")); mongoRule1.put("term", "^foo.+"); BasicDBObject mongoRule2 = new BasicDBObject(); mongoRule2.put("_id", new ObjectId()); mongoRule1.put("blacklist_id", mongoList.get("_id")); mongoRule2.put("term", ".+bar"); BasicDBList rules = new BasicDBList(); rules.add(mongoRule1); rules.add(mongoRule2); mongoList.put("blacklisted_terms", rules); Blacklist blacklist = new Blacklist(mongoList); assertEquals(2, blacklist.getRules().size()); assertEquals("^foo.+", blacklist.getRules().get(0).getTerm()); assertEquals(".+bar", blacklist.getRules().get(1).getTerm()); }
@Override public Object encode(final Object value, final MappedField optionalExtraInfo) { ArrayFoo complex = (ArrayFoo) value; BasicDBList dbObject = new BasicDBList(); dbObject.put(1, complex.first()); dbObject.put(2, complex.second()); return dbObject; }
public static void main(String[] args) throws UnknownHostException { MongoClient client = new MongoClient(); DB database = client.getDB("school"); DBCollection collection = database.getCollection("students"); /* Hint/spoiler: With the new schema, this problem is a lot harder and that is sort of the point. One way is to find the lowest homework in code and then update the scores array with the low homework pruned. If you are struggling with the Node.js side of this, look at the .slice() operator, which can remove an element from an array in-place. */ DBCursor cursor = collection.find(); try { while (cursor.hasNext()) { BasicDBObject student = (BasicDBObject) cursor.next(); int studentId = student.getInt("_id"); String name = student.getString("name"); BasicDBList scores = (BasicDBList) student.get("scores"); System.out.printf("_id[%d], name[%s], scores%s %n", studentId, name, scores); DBObject scoreToRemove = null; double minScoreValue = 100.0; for (Object obj : scores) { BasicDBObject score = (BasicDBObject) obj; String type = score.getString("type"); if (!"homework".equals(type)) { continue; } double curScoreValue = score.getDouble("score"); System.out.printf("type[%s], current score value[%f] %n", type, curScoreValue); if (curScoreValue < minScoreValue) { scoreToRemove = score; minScoreValue = curScoreValue; } } System.out.printf("score to remove[%s] %n", scoreToRemove); if (scoreToRemove != null) { scores.remove(scoreToRemove); BasicDBObject query = new BasicDBObject("_id", studentId); BasicDBObject scoresUpdate = new BasicDBObject("$set", new BasicDBObject("scores", scores)); WriteResult result = collection.update(query, scoresUpdate); System.out.printf("update count[%d] %n", result.getN()); } } } finally { cursor.close(); } }
public void ExportMongoCollection( String mongoCollectionName, String xmlCollectionName, String xmlOutputFileName, HashSet fields) throws Exception { BasicDBList mongoCollection = GetDBList(mongoCollectionName, null); try { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); // root elements Document doc = docBuilder.newDocument(); Element rootElement = doc.createElement(xmlCollectionName); doc.appendChild(rootElement); for (int i = 0; i < mongoCollection.size(); i++) { Element thisElement = doc.createElement( mongoCollectionName); // the xml nodes are named after the mongoColleciton BasicDBObject record = (BasicDBObject) mongoCollection.get(i); Object attributeArray[] = fields.toArray(); for (int j = 0; j < fields.size(); j++) { String attribName = (String) attributeArray[j]; String attribValue = record.getString(attribName); // Attr thisAttr = doc.createAttribute(attribName); // thisAttr.setValue(attribValue); thisElement.setAttribute(attribName, attribValue); } rootElement.appendChild(thisElement); } // write the content into xml file TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); String filePath = System.getProperty("user.home") + "/" + xmlOutputFileName; File f = new File(filePath); // StreamResult result = new StreamResult(new File("/" + xmlOutputFileName)); StreamResult result = new StreamResult(f.toURI().getPath()); // Output to console for testing // StreamResult result = new StreamResult(System.out); transformer.transform(source, result); System.out.println("File saved!"); } catch (ParserConfigurationException pce) { pce.printStackTrace(); } catch (TransformerException tfe) { tfe.printStackTrace(); } }
public static DBObject createUserDbObjectListMock() { BasicDBList users = mock(BasicDBList.class); Iterator<Object> mockIterator = mock(Iterator.class); when(mockIterator.hasNext()).thenReturn(true).thenReturn(false); when(mockIterator.next()).thenReturn(ObjectId.get().toString()); when(users.iterator()).thenReturn(mockIterator); return users; }
public void perform(BasicDBObjectBuilder builder) { BasicDBList list = new BasicDBList(); for (Operator operator : operators) { BasicDBObjectBuilder operatorBuilder = new BasicDBObjectBuilder(); operator.perform(operatorBuilder); list.add(operatorBuilder.get()); } builder.append("$or", list); }
public BasicDBList maybeConvertList(Iterable<?> source, TypeInformation<?> typeInformation) { BasicDBList newDbl = new BasicDBList(); for (Object element : source) { newDbl.add(convertToMongoType(element, typeInformation)); } return newDbl; }
public void run() throws UnknownHostException { final List<Integer> models = new ArrayList<Integer>(); final List<String> owners = new ArrayList<String>(); final MongoClient client = new MongoClient(); DB db = client.getDB("mongo_hadoop"); DBCollection devices = db.getCollection("devices"); DBCollection logs = db.getCollection("logs"); if ("true".equals(System.getenv("SENSOR_DROP"))) { LOG.info("Dropping sensor data"); devices.drop(); logs.drop(); devices.createIndex(new BasicDBObject("devices", 1)); } db.getCollection("logs_aggregate").createIndex(new BasicDBObject("devices", 1)); if (logs.count() == 0) { for (int i = 0; i < 10; i++) { owners.add(getRandomString(10)); } for (int i = 0; i < 10; i++) { models.add(getRandomInt(10, 20)); } List<ObjectId> deviceIds = new ArrayList<ObjectId>(); for (int i = 0; i < NUM_DEVICES; i++) { DBObject device = new BasicDBObject("_id", new ObjectId()) .append("name", getRandomString(5) + getRandomInt(3, 5)) .append("type", choose(TYPES)) .append("owner", choose(owners)) .append("model", choose(models)) .append("created_at", randomDate(new Date(2000, 1, 1, 16, 49, 29), new Date())); deviceIds.add((ObjectId) device.get("_id")); devices.insert(device); } for (int i = 0; i < NUM_LOGS; i++) { if (i % 50000 == 0) { LOG.info(format("Creating %d sensor log data entries: %d%n", NUM_LOGS, i)); } BasicDBList location = new BasicDBList(); location.add(getRandomInRange(-180, 180, 3)); location.add(getRandomInRange(-90, 90, 3)); DBObject log = new BasicDBObject("_id", new ObjectId()) .append("d_id", choose(deviceIds)) .append("v", getRandomInt(0, 10000)) .append("timestamp", randomDate(new Date(2013, 1, 1, 16, 49, 29), new Date())) .append("loc", location); logs.insert(log); } } }
@Override public QueryResult next(String chromosome, int position, QueryOptions options) { String featureType = options.getString("featureType", null); String featureClass = options.getString("featureClass", null); BasicDBList chunksId = new BasicDBList(); String chunkId = chromosome + "_" + getChunkId(position, regulatoryRegionChunkSize) + "_" + regulatoryRegionChunkSize / 1000 + "k"; chunksId.add(chunkId); // TODO: Add query to find next item considering next chunk // db.regulatory_region.find({ "chromosome" : "19" , "start" : { "$gt" : 62005} , "featureType" // : "TF_binding_site_motif"}).sort({start:1}).limit(1) QueryBuilder builder; if (options.getString("strand") == null || (options.getString("strand").equals("1") || options.getString("strand").equals("+"))) { // db.core.find({chromosome: "1", start: {$gt: 1000000}}).sort({start: 1}).limit(1) builder = QueryBuilder.start("_chunkIds") .in(chunksId) .and("chromosome") .is(chromosome) .and("start") .greaterThan(position); options.put("sort", new BasicDBObject("start", 1)); options.put("limit", 1); } else { builder = QueryBuilder.start("_chunkIds") .in(chunksId) .and("chromosome") .is(chromosome) .and("end") .lessThan(position); options.put("sort", new BasicDBObject("end", -1)); options.put("limit", 1); } if (featureType != null) { builder.and("featureType").is(featureType); } if (featureClass != null) { builder.and("featureClass").is(featureClass); } System.out.println(builder.get()); return executeQuery("result", builder.get(), options); }
public BasicDBList saveAll() { BasicDBList list = new BasicDBList(); for (PanelElementAbstract p : panelElements) { if (p instanceof PanelElementPlugin) { if (((PanelElementPlugin) p).getParameterPanel().getMethod() == null) ((PanelElementPlugin) p).setCurrentMethod(); } else { ((ParameterPanel) p.getParameterPanel()).checkValidity(); } list.add(p.getParameterPanel().save()); } return list; }
@Test public void bench2() throws Exception { System.out.println("bench2()"); int TEST_TO = 100000; BasicDBList expectedlist = new BasicDBList(); expectedlist.add(new BasicDBObject().append("first1", 1234.0).append("first2", "hello")); expectedlist.add(new BasicDBObject().append("first1", 2345.0).append("first2", "hello2")); BasicDBObject expected = new BasicDBObject().append("stuff", true).append("stuff4", expectedlist); doBench(TEST_TO, expected); }
public MongoCursorImpl<T> in(String field, Collection<?> values) throws Exception { if (where == null) where = new BasicDBObject(); BasicDBList in = new BasicDBList(); Field f = store.type.getField(field); // Verification that field name belongs to store type for (Object value : values) { // TODO need to consider collection fields ... in.add(store.mcnv.toMongo(value)); } where.put(f.getName(), new BasicDBObject("$in", in)); return this; }
public static DBObject createCategoryDbObjectMock() { BasicDBList dbFeedIds = mock(BasicDBList.class); Iterator<Object> mockIterator = mock(Iterator.class); when(mockIterator.hasNext()).thenReturn(true).thenReturn(false); when(mockIterator.next()).thenReturn("53bf159330044594e9be9f78"); when(dbFeedIds.iterator()).thenReturn(mockIterator); DBObject categoryDbObject = mock(DBObject.class); when(categoryDbObject.get("_id")).thenReturn("12345"); when(categoryDbObject.get("name")).thenReturn("Sport"); when(categoryDbObject.get("username")).thenReturn("billblake"); when(categoryDbObject.get("feedIds")).thenReturn(dbFeedIds); return categoryDbObject; }
private List<VarietyEntry> parse(final String stdOut) { final BasicDBList parse = (BasicDBList) JSON.parse(stdOut); final List<VarietyEntry> entries = new ArrayList<>(); for (final Object o : parse) { final BasicDBObject obj = (BasicDBObject) o; final String key = ((BasicDBObject) obj.get("_id")).getString("key"); final long totalOccurrences = obj.getLong("totalOccurrences"); final double percentContaining = obj.getDouble("percentContaining"); final BasicDBList typesList = (BasicDBList) ((BasicDBObject) obj.get("value")).get("types"); final HashSet<String> types = new HashSet<>(Arrays.asList(typesList.toArray(new String[typesList.size()]))); entries.add(new VarietyEntry(key, totalOccurrences, percentContaining, types)); } return entries; }
public String getLOVId(String LovType, String GlobalVal) throws Exception { String retVal = new ObjectId().toString(); QueryBuilder qm = new QueryBuilder() .and( new QueryBuilder().start().put("LovType").is(LovType).get(), new QueryBuilder().start().put("GlobalVal").is(GlobalVal).get()); BasicDBObject query = new BasicDBObject(); query.putAll(qm.get()); BasicDBList lovItem = (BasicDBList) GetDBList("rwLov", query); if (lovItem != null && lovItem.size() > 0) { BasicDBObject result = (BasicDBObject) lovItem.get(0); retVal = result.get("_id").toString(); } return retVal; }