@SuppressWarnings("unchecked") @Override public O2MSyncEventLog decode(BsonReader arg0, DecoderContext arg1) { Document document = documentCodec.decode(arg0, arg1); O2MSyncEventLog log = new O2MSyncEventLog(); log.setLogId(document.getObjectId(_ID)); log.setEventId(document.getString(EVENT_ID)); log.setCrOn(document.getDate(CREATED_ON)); log.setSyOn(document.getDate(SYNCED_ON)); log.setStatus(document.getString(STATUS)); log.setOperation(document.getString(OPERATION)); List<Document> filterDocList = (List<Document>) document.get(EVENT_FILTERS); if(filterDocList!=null){ List<O2MSyncEventInfo> filters = new ArrayList<O2MSyncEventInfo>(filterDocList.size()); O2MSyncEventInfo filter = null; for(Document filterDoc : filterDocList){ filter= new O2MSyncEventInfo(); filter.setTableName(filterDoc.getString(TABLE_NAME)); filter.setColumnName(filterDoc.getString(COLUMN_NAME)); filter.setColumnValue(filterDoc.get(COLUMN_VALUE)); filters.add(filter); } log.setEventFilters(filters); } return log; }
public String listPlants(Map<String, String[]> queryParams, String uploadId) { Document filterDoc = new Document(); filterDoc.append("uploadId", uploadId); if (queryParams.containsKey("gardenLocation")) { String location = (queryParams.get("gardenLocation")[0]); filterDoc = filterDoc.append("gardenLocation", location); } if (queryParams.containsKey("commonName")) { String commonName = (queryParams.get("commonName")[0]); filterDoc = filterDoc.append("commonName", commonName); } FindIterable<Document> matchingPlants = plantCollection.find(filterDoc); return JSON.serialize(matchingPlants); }
/** * This method insert the document using Document object */ @Override public void insertUsingDocument() { MongoDatabase db = null; MongoCollection collection = null; try { db = client.getDatabase(mongo.getDataBase()); collection = db.getCollection(mongo.getSampleCollection()); Document obj1 = new Document(); obj1.put("name", "Sivaraman"); obj1.put("age", 23); obj1.put("gender", "male"); collection.insertOne(obj1); log.info("Document Insert Successfully using Document Obj..."); } catch (MongoException | ClassCastException e) { log.error("Exception occurred while insert Value using **Document** : " + e, e); } }
/** * 查找指定条数的数据 */ public List<Map<String, Object>> find(String collectionName, Integer pageNumber, Integer pageSize) { MongoCollection collection = mongoDatabase.getCollection(collectionName); List<Map<String, Object>> list = new ArrayList<>(); if (collection == null) { return list; } FindIterable findIterable = collection.find(); if (pageSize != null && pageSize >= 0) { if (pageNumber != null && pageNumber >= 1) { findIterable = findIterable.skip((pageNumber - 1) * pageSize); } findIterable = findIterable.limit(pageSize); } Iterator<Document> iterator = findIterable.iterator(); while (iterator.hasNext()) { Document document = iterator.next(); document.remove("_id"); Map<String, Object> map = new HashMap<>(document); list.add(map); } return list; }
/** * Returns an array of Strings of all garden locations for the current uploadId * sorted according to the BedComparator * @param uploadID * @return */ public String[] getGardenLocations(String uploadID){ if (!ExcelParser.isValidUploadId(db, uploadID)) return null; //Get distinct gardenLocations for this uploadId Document filter = new Document(); filter.append("uploadId", uploadID); DistinctIterable<String> bedIterator = plantCollection.distinct("gardenLocation", filter, String.class); List<String> beds = new ArrayList<String>(); for(String s : bedIterator) { beds.add(s); } //Then sort the gardenLocations as according to BedComparator beds.sort(new BedComparator()); return beds.toArray(new String[beds.size()]); }
public SyncEvent retryEvent(ObjectId eventId, boolean retryFailed, boolean retryEntire, boolean dropCollection) { Document updateQuery = new Document(); updateQuery.append("$set", new Document(SyncAttrs.STATUS, SyncStatus.PENDING).append(SyncAttrs.IS_RETRY, true)) .append("$unset", new Document(SyncAttrs.ERRORS, true).append(SyncAttrs.MARKER, true)); if (retryFailed) { syncEvents.updateMany( Filters.and(Filters.eq(SyncAttrs.PARENT_EVENT_ID, eventId), Filters.eq(SyncAttrs.STATUS, SyncStatus.FAILED), Filters.ne(SyncAttrs.ID, eventId)), updateQuery); syncEvents.updateOne(Filters.eq(SyncAttrs.ID, eventId), Updates.set(SyncAttrs.STATUS, SyncStatus.IN_PROGRESS)); } else { if (retryEntire) { syncEvents.updateMany(Filters.eq(SyncAttrs.PARENT_EVENT_ID, eventId), Updates.set(SyncAttrs.STATUS, SyncStatus.CANCELLED)); syncEvents.updateOne(Filters.eq(SyncAttrs.ID, eventId), updateQuery); } } return getEvent(eventId); }
private Document encodeJoinedTable(JoinedTable joinedTable) { Document document = new Document(); document.append(SyncAttrs.JOIN_TYPE, String.valueOf(joinedTable.getJoinType())); document.append(SyncAttrs.FILTERS, encodeFilters(joinedTable.getFilters())); OracleTable innerJoinedTable = joinedTable.getTable(); document.append(SyncAttrs.TABLE_NAME, innerJoinedTable.getTableName()); document.append(SyncAttrs.TABLE_ALIAS, innerJoinedTable.getTableAlias()); if (innerJoinedTable.getJoinedTables() != null) { List<Document> nestedJoinedTableList = new ArrayList<Document>(); for (JoinedTable nestedJoinedTable : innerJoinedTable.getJoinedTables()) { nestedJoinedTableList.add(encodeJoinedTable(nestedJoinedTable)); } document.append(SyncAttrs.JOINED_TABLES, nestedJoinedTableList); } return document; }
/** * Appends an operator with simple pairs * * @param operator The operator * @param pairs The pairs of key/value for this operator * @return This */ public MongoQuery append(Operator operator, Keyable... pairs) { String key = "$" + operator.getName(); Document document = documentMap.containsKey(key) ? documentMap.get(key) : new Document(); for(Keyable p : pairs) { String pKey = p.getKey(); if(p instanceof KeyMultiValue) { for(Object o : ((KeyMultiValue) p).getValues()) { document.append(pKey, o); } } else if(p instanceof KeyValue) { document.append(pKey, ((KeyValue) p).getVal()); } } documentMap.put(key, document); return this; }
/** * Converts the list of messages into a list of elements of given class * This method is only for complex classes like {@link PlayerData}<br> * <p> * A really common pitfall is using the class for the same index of the respond list for this method, * because this method counts different. * e.g.: Respond list is: PlayerData, Group, PlayerData * Means the index is: 0 0 1 * * @param eClass The element's class * @param <E> The element type * @return The list */ public <E> List<E> toComplexes(Class<E> eClass) throws MooInputException { this.checkState(); if(complexElementMap.containsKey(eClass)) return complexElementMap.get(eClass); List<E> l = new ArrayList<>(); DataArchitecture architecture = DataArchitecture.fromClass(eClass); DataResolver dataResolver = new DataResolver(architecture); for(String msg : getMessageAsList()) { E e; if(DESERIALIZED_PATTERN.matcher(msg).matches()) { e = ReflectionUtil.deserialize(msg, eClass); } else { // list the document from the message // create object from this Document document = Document.parse(msg); e = dataResolver.doc(document).complete(eClass); } if(e != null) l.add(e); } complexElementMap.put(eClass, l); return l; }
@Test public void withDateTimeLiteral() { String expression = format("createdAt < '%s'", DateTimeFormatter.ISO_DATE_TIME.format(LocalDateTime.now())); List<Document> documents = toList(repository.find(dataset, "", expression, "", -1)); assertThat(documents.size(), is(2)); assertThat(documents, hasItem(bill)); assertThat(documents, hasItem(martin)); expression = format("createdAt > '%s'", DateTimeFormatter.ISO_DATE_TIME.format(LocalDateTime.now())); documents = toList(repository.find(dataset, "", expression, "", -1)); assertThat(documents.size(), is(0)); }
private void configure() { MongoDatabase db = this.mongoClient.getDatabase(dbName); MongoCollection<Document> metadataColl = db.getCollection(METADATA_COLL_NAME); if (metadataColl.count() > 1) { throw new IndraError("Model metadata must have only one entry!"); } if (metadataColl.count() == 1) { logger.debug("Using stored metadata of {}", dbName); Document storedMetadata = metadataColl.find().first(); metadata = ModelMetadata.createFromMap(storedMetadata); } else { logger.debug("No metadata found in {}, using defaults.", dbName); metadata = ModelMetadata.createDefault(); } logger.info("Model metadata: {}", metadata); }
/** * Get the current ClientState of a client, * connected or not. * @param uuid The UUID of the client. This must be a valid * UUID which belongs to a client. If the UUID is * not found in the database or connected clients, * then a RuntimeException is thrown. * @return The ClientState of the specified client with the UUID. */ public ClientState queryClientState(String uuid) { for(ClientSession session : this.sessions.values()) { if(session.getToken().getUuid().equals(uuid)) { return session.getState(); } } // The session is not currently connected, so we need to check the database MongoCollection<Document> clients = NectarServerApplication.getDb().getCollection("clients"); Document doc = clients.find(Filters.eq("uuid", uuid)).first(); if(doc != null) { return ClientState.fromInt(doc.getInteger("state", ClientState.UNKNOWN.toInt())); } // We couldn't find the client in the database, so throw an exception throw new RuntimeException("Failed to find UUID " + uuid + "in connected sessions or in database. Is it invalid?"); }
@Override public List<Migration> findAll() { List<Migration> migrations = new ArrayList<>(); collection.find() .forEach((Consumer<Document>) d -> migrations.add( new Migration( d.getString("version"), d.getString("description"), d.getString("author"), Optional.ofNullable(d.getDate("started")).map(DateTime::new).orElse(null), Optional.ofNullable(d.getDate("finished")).map(DateTime::new).orElse(null), MigrationStatus.valueOf(d.getString("status")), d.getString("failureMessage") ) ) ); return migrations; }
@SuppressWarnings("unchecked") @Override public void saveconnectionInfo(String jsonStr) { Document document = parsefromJson(jsonStr); SyncConnectionInfo connectionInfo = new SyncConnectionInfo(); if(document.getObjectId(ID)!=null){ connectionInfo.setConnectionId(document.getObjectId(ID)); } String dbType = document.getString(DBTYPE); connectionInfo.setDbType(dbType); connectionInfo.setDbName(document.getString(DBNAME)); connectionInfo.setUserName(document.getString(USERNAME)); connectionInfo.setPassword(document.getString(PASSWORD)); if(dbType!=null && dbType.equalsIgnoreCase("Mongo")){ Map<String,Double> hostToPortMap = new HashMap<String,Double>(); List<Document> hostToPort = (List<Document>) document.get(HOSTTOPORTMAP); for(Document doc : hostToPort){ hostToPortMap.put(doc.getString(HOST), doc.getDouble(PORT)); } connectionInfo.setHostToPortMap(hostToPortMap); } connectionDao.updateConnection(connectionInfo); }
public void encodeOracleToMongoGridFsMap(OracleToMongoGridFsMap map, Document document) { logger.debug("Encode called for MongoObject"); if (null != map.getCollectionName() && !map.getCollectionName().isEmpty()) { document.append(SyncAttrs.ATTRIBUTE_NAME, map.getCollectionName()); } Map<String, ColumnAttrMapper> mapperListMap = map.getMetaAttributes(); if (mapperListMap != null && !mapperListMap.isEmpty()) { List<Document> mapperListDoc = new ArrayList<Document>(); for (Map.Entry<String, ColumnAttrMapper> mapperEntry : mapperListMap.entrySet()) { mapperListDoc.add(encodeColumnAttrMapper(mapperEntry.getValue())); } document.append(SyncAttrs.META_ATTRIBUTES, new Document(SyncAttrs.ATTRIBUTES, mapperListDoc)); } document.append(SyncAttrs.FILE_NAME_COLUMN, encodeColumn(map.getFileNameColumn())); document.append(SyncAttrs.INPUT_STREAM_COLUMN, encodeColumn(map.getInputStreamColumn())); if(map.getStreamTable() != null) document.append(SyncAttrs.STREAM_TABLE, encodeTable(map.getStreamTable())); if (map.getFilters() != null) { document.append(SyncAttrs.FILTERS, encodeFilters(map.getFilters())); } logger.debug("Encoded Document : " + document); }
private Bson getFilter(Document doc){ Document filter = new Document(); //TODO : for nested identifier , logic needs to be built for(MongoAttribute keyAttribute : keyAttributes){ if(keyAttribute.isIdentifier()){ filter.append(SyncAttrs.ID, doc.get(SyncAttrs.ID)); }else{ filter.append(keyAttribute.getAttributeName(), doc.get(keyAttribute.getAttributeName())); } } return filter; }
public boolean incrementVisits(String body){ Document filter = new Document(); Document parsed = Document.parse(body); filter.append("id", parsed.getString("plantID")); flowerCollection.updateOne(filter, new Document("$inc", new Document("flowerVisits", 1))); return true; }
/** * 批量插入 * * @param collectionName * @param dataList * @return */ public <T> boolean insertModelList(String collectionName, List<T> dataList) { MongoCollection collection = sMongoDatabase.getCollection(collectionName); List<Document> documentList = dataList.stream() .map(data -> Document.parse(JSON.toJSONString(data))) .collect(Collectors.toList()); collection.insertMany(documentList); return true; }
public List<String> getSchedulesForGuild(String gId) { List<String> list = new ArrayList<>(); for (Document document : Main.getDBDriver().getScheduleCollection().find(eq("guildId", gId))) { list.add(document.getString("_id")); } return list; }
private MatchOperation decodeMatchOperation(Document document) { String sqlOperation = document.getString(SyncAttrs.SQL_OPERATION); MatchAble leftHandExpr = decodeExpression((Document) document.get(SyncAttrs.LEFT_HAND_EXPRESSION)); MatchAble rightHandExpr =null; if(document.get(SyncAttrs.RIGHT_HAND_EXPRESSION)!=null){ rightHandExpr = decodeExpression((Document) document.get(SyncAttrs.RIGHT_HAND_EXPRESSION)); } return OperationsFactory.getMatchExpression(leftHandExpr, rightHandExpr, sqlOperation); }
/** * Get the loader for (re)loading objects into the cache * * @return The cacheLoader object */ public CacheLoader<K, V> getLoader() { return new CacheLoader<K, V>() { @Override public V load(K k) throws Exception { DbFilter filter = DbFilter.fromPrimKey(DatabaseCache.this.getDatabaseCollection().getWrappedClass(), k); FindIterable<Document> result = DatabaseCache.this.getDatabaseCollection().fetch(filter, 1); return DatabaseCache.this.getDatabaseCollection().convert(result.first()); } }; }
@POST @Path("{_id}") @Produces(MediaType.APPLICATION_JSON) public Response active(@PathParam("_id") String id, @NotEmpty @QueryParam("device_id") String deviceId) { if (!ObjectId.isValid(id)) { return Responses.notFound("激活码无效"); } Document license = MongoDBs.licenses().find(eq(License._ID, objectId(id))).first(); RebaseAsserts.notNull(license, "license"); if (!Objects.equals(deviceId, license.getString(License.DEVICE_ID))) { final Bson target = eq(License._ID, objectId(id)); MongoDBs.licenses().updateOne(target, set(License.DEVICE_ID, deviceId)); license.put(License.DEVICE_ID, deviceId); } return Response.ok(license).build(); }
public List<Link> getAllLinks(LinkFilter filter, int skip, int first) { Optional<Bson> mongoFilter = Optional.ofNullable(filter).map(this::buildFilter); List<Link> allLinks = new ArrayList<>(); FindIterable<Document> documents = mongoFilter.map(links::find).orElseGet(links::find); for (Document doc : documents.skip(skip).limit(first)) { allLinks.add(link(doc)); } return allLinks; }
private void load(NetworkConfigCategory category, Document document) { for(NetworkConfigType configType : NetworkConfigType.getConfigTypes(category)) { Object val = document != null ? document.get(configType.getKey()) : configType.getDefaultValue(); if(val == null) return; MooCache.getInstance().getConfigMap().fastPutAsync(configType.getKey(), val); } }
@Override public SipMessage decode(BsonReader reader, DecoderContext decoderContext) { Document document = codec.decode(reader, decoderContext); SipMessage message = new SipMessage(); message.setId(document.getObjectId("_id").toHexString()); message.setMillis(document.getLong("millis")); Object nanos = document.get("nanos"); if (nanos != null) { message.setNanos((Integer) nanos); } String srcIp = document.getString("src_ip"); message.setSrcIp(srcIp); message.setSrcPort(document.getInteger("src_port")); message.setSrcHost(document.getString("src_host")); String dstIp = document.getString("dst_ip"); message.setDstIp(dstIp); message.setDstPort(document.getInteger("dst_port")); message.setDstHost(document.getString("dst_host")); message.setMethod(document.getString("method")); message.setCallId(document.getString("call_id")); message.setCaller(document.getString("caller")); message.setCallee(document.getString("callee")); Object payload = document.get("payload"); if (payload != null) { message.setPayload((String) payload); } return message; }
/** * Converts the type to a document * * @param e The element * @return The document */ public Document convert(E e) { if(e == null) return null; try { return new DataResolver(getArchitecture()).fullResolve(e); } catch(Exception ex) { return null; } }
public boolean addVisit(String plantID, String uploadID) { Document filterDoc = new Document(); filterDoc.append("id", plantID); filterDoc.append("uploadID", uploadID); Document visit = new Document(); visit.append("visit", new ObjectId()); return null != plantCollection.findOneAndUpdate(filterDoc, push("metadata.visits", visit)); }
@SuppressWarnings("rawtypes") @Override public OracleToMongoEvent decode(BsonReader reader, DecoderContext decoderContext) { Document document = documentCodec.decode(reader, decoderContext); SyncMapAndEventDecoder decoder = new SyncMapAndEventDecoder(); SyncEvent event = decoder.decodeSyncEvent(document); return (OracleToMongoEvent) event; }
public List<ObjectId> checkCancelledEvents(final Set<ObjectId> activeEventList) { final List<ObjectId> cancelledEvents = new ArrayList<ObjectId>(); syncEvents .find(Filters.and(Filters.in(SyncAttrs.ID, activeEventList), Filters.eq(SyncAttrs.STATUS, SyncStatus.CANCELLED)), Document.class) .projection(Projections.include(SyncAttrs.ID)).forEach(new Block<Document>() { @Override public void apply(Document arg0) { cancelledEvents.add(arg0.getObjectId(SyncAttrs.ID)); } }); return cancelledEvents; }
/** * @param plantID The plant to get feedback of * @param uploadID Dataset to find the plant * @return JSON for the number of comments, likes, and dislikes * Of the form:All * { * commentCount: number * likeCount: number * dislikeCount: number * } */ public String getFeedbackForPlantByPlantID(String plantID, String uploadID) { Document out = new Document(); Document filter = new Document(); filter.put("commentOnPlant", plantID); filter.put("uploadId", uploadID); long comments = commentCollection.count(filter); long likes = 0; long dislikes = 0; //Get a plant by plantID FindIterable doc = plantCollection.find(new Document().append("id", plantID).append("uploadId", uploadID)); Iterator iterator = doc.iterator(); if (iterator.hasNext()) { Document result = (Document) iterator.next(); //Get metadata.rating array List<Document> ratings = (List<Document>) ((Document) result.get("metadata")).get("ratings"); //Loop through all of the entries within the array, counting like=true(like) and like=false(dislike) for (Document rating : ratings) { if (rating.get("like").equals(true)) likes++; else if (rating.get("like").equals(false)) dislikes++; } } out.put("commentCount", comments); out.put("likeCount", likes); out.put("dislikeCount", dislikes); return JSON.serialize(out); }
@Override public MongoToOracleEvent decode(BsonReader reader, DecoderContext decoderContext) { Document document = documentCodec.decode(reader, decoderContext); SyncMapAndEventDecoder decoder = new SyncMapAndEventDecoder(); MongoToOracleEvent event = (MongoToOracleEvent) decoder.decodeSyncEvent(document); return event; }
/** * 插入一条记录 * * @param collectionName * 表名 * @param mongoObj * 记录 * @return */ public static boolean insertOne(String collectionName, MongoObj mongoObj) { MongoCollection<Document> collection = getCollection(collectionName); try { Document document = objectToDocument(mongoObj); collection.insertOne(document); return true; } catch (Exception e) { if (log != null) { log.error("插入document失败", e); } return false; } }
@Override public MongoObject processCollection(String sourceDbName, String sourceSchemaName, String collectionName) { MongoClient mongoClient = DBCacheManager.INSTANCE.getCachedMongoPool(sourceDbName, sourceSchemaName); Document document = mongoClient.getDatabase(sourceDbName).getCollection(collectionName).find().first(); MongoObject mongoObject = processDocument(document,collectionName,"Collection"/*,mongoObjects*/); return mongoObject; }
@Then("^Lead with id (\\d+) should be in the database") public void Lead_with_id_should_be_in_intellead_data_mongodb_database(int leadId) { MongoDatabase database = mongoClientData.getDatabase("local"); MongoCollection<Document> collection = database.getCollection("leads"); long count = collection.count(parse("{_id: {$eq: \"" + leadId + "\"}}")); assertEquals(1, count); }
public String getReminderFormat(String cId) { Document settings = Main.getDBDriver().getScheduleCollection().find(eq("_id",cId)).first(); if( settings == null ) { return Main.getBotSettingsManager().getAnnounceFormat(); } String format = (String) settings.get("reminder_format"); if(format == null ) { return (String) settings.get("announcement_format"); } return format; }
public <T> T transform(Class<T> clazz, Document document) { try { document.remove("_id"); return objectMapper.readValue(document.toJson(), clazz); } catch (Exception e) { throw new TransformerException("Failed to deserialise document!", e); } }
public void encodeMongoToOracleMap(MongoToOracleMap map, Document mapDocument) { if (map.getCollectionName() != null && !map.getCollectionName().isEmpty()) { mapDocument.append(SyncAttrs.COLLECTION_NAME, map.getCollectionName()); } if (map.getRootNode() != null && !map.getRootNode().isEmpty()) { List<Document> nodeTableGrpListDoc= new ArrayList<Document>(); for (NodeGroup rootNode : map.getRootNode()) { nodeTableGrpListDoc.add(encodeNodeGroup(rootNode)); } mapDocument.append(SyncAttrs.NODE_TABLE_GROUP, nodeTableGrpListDoc); } }
@Override public void ensureIndex(MongoCollection<Document> collection, String name, Document index, IndexOptions opts, boolean replace) { if(replace){ collection.dropIndex(name); } collection.createIndex(index, opts); }
@Test public void test_emptyCollections() { final String collName = "test"; final Document document = new Document("name", "unknown"); _mongoStore.insert(collName, document); // Empty collections _mongoStore.emptyCollections(); // find List<Document> documents = _mongoStore.findAll(collName, new Document("name", "unknown")); Assert.assertNotNull(documents); Assert.assertEquals(0, documents.size()); }
public static void addCollection(OutputMessage mo){ Document document = new Document("title", "MIS_Webchat") .append("from", mo.getFrom()) .append("text",mo.getText()) .append("time", Instant.now().toEpochMilli()); ms.stashMessage(document); }