@Override public Memory unconvert(Environment env, TraceInfo trace, Document arg) throws Throwable { if (arg == null) return Memory.NULL; ArrayMemory result = ArrayMemory.createHashed(arg.size()); for (Entry<String, Object> entry : arg.entrySet()) { if (entry.getValue() instanceof Document) { result.put(entry.getKey(), unconvert(env, trace, (Document) entry.getValue())); } else { result.put(entry.getKey(), Memory.wrap(env, entry.getValue())); } } return result; }
private Object negate(String key, Document value) { if (value.size() == 1) { return asDocument(key, asDocument("$not", value)); } else { List<Object> list2 = new ArrayList<>(); for (Map.Entry<String, Object> entry2 : value.entrySet()) { list2.add(asDocument(key, asDocument("$not", asDocument(entry2.getKey(), entry2.getValue())))); } return asDocument("$or", list2); } }
private void createLocksIndexIfNecessary() { String databaseName = CommonUtils.getApplication(this.endpoint).replaceAll("\\W", "_"); MongoDatabase database = this.mongoClient.getDatabase(databaseName); MongoCollection<Document> locks = database.getCollection(CONSTANTS_TB_LOCKS); ListIndexesIterable<Document> lockIndexList = locks.listIndexes(); boolean transactionIndexExists = false; MongoCursor<Document> lockCursor = null; try { lockCursor = lockIndexList.iterator(); while (transactionIndexExists == false && lockCursor.hasNext()) { Document document = lockCursor.next(); Boolean unique = document.getBoolean("unique"); Document key = (Document) document.get("key"); boolean globalExists = key.containsKey(CONSTANTS_FD_GLOBAL); boolean lengthEquals = key.size() == 1; transactionIndexExists = lengthEquals && globalExists; if (transactionIndexExists && (unique == null || unique == false)) { throw new IllegalStateException(); } } } finally { IOUtils.closeQuietly(lockCursor); } if (transactionIndexExists == false) { Document index = new Document(CONSTANTS_FD_GLOBAL, 1); locks.createIndex(index, new IndexOptions().unique(true)); } }
private void createTransactionsGlobalTxKeyIndexIfNecessary() { String databaseName = CommonUtils.getApplication(this.endpoint).replaceAll("\\W", "_"); MongoDatabase database = this.mongoClient.getDatabase(databaseName); MongoCollection<Document> transactions = database.getCollection(CONSTANTS_TB_TRANSACTIONS); ListIndexesIterable<Document> transactionIndexList = transactions.listIndexes(); boolean transactionIndexExists = false; MongoCursor<Document> transactionCursor = null; try { transactionCursor = transactionIndexList.iterator(); while (transactionIndexExists == false && transactionCursor.hasNext()) { Document document = transactionCursor.next(); Boolean unique = document.getBoolean("unique"); Document key = (Document) document.get("key"); boolean globalExists = key.containsKey(CONSTANTS_FD_GLOBAL); boolean lengthEquals = key.size() == 1; transactionIndexExists = lengthEquals && globalExists; if (transactionIndexExists && (unique == null || unique == false)) { throw new IllegalStateException(); } } } finally { IOUtils.closeQuietly(transactionCursor); } if (transactionIndexExists == false) { Document index = new Document(CONSTANTS_FD_GLOBAL, 1); transactions.createIndex(index, new IndexOptions().unique(true)); } }
protected List<Document> loadTestDocuments(String pathOnClasspath) { List<Document> results = new ArrayList<>(); try (InputStream stream = Testing.Files.readResourceAsStream(pathOnClasspath);) { assertThat(stream).isNotNull(); IoUtil.readLines(stream, line -> { Document doc = Document.parse(line); assertThat(doc.size()).isGreaterThan(0); results.add(doc); }); } catch (IOException e) { fail("Unable to find or read file '" + pathOnClasspath + "': " + e.getMessage()); } return results; }
protected void storeDocuments(MongoCollection<Document> collection, String pathOnClasspath) { InsertOneOptions insertOptions = new InsertOneOptions().bypassDocumentValidation(true); loadTestDocuments(pathOnClasspath).forEach(doc -> { assertThat(doc).isNotNull(); assertThat(doc.size()).isGreaterThan(0); collection.insertOne(doc, insertOptions); }); }
private Object negate(String key, Document value) { if (value.size() == 1) { return asDocument(key, asDocument("$not", value)); } else { List<Object> list2 = new ArrayList<>(); for (Map.Entry<String, Object> entry2 : value.entrySet()) { list2.add(asDocument(key, asDocument("$not", asDocument(entry2.getKey(), entry2.getValue())))); } return asDocument("$or", list2); } }
private void addParentToMap(Map<Long, List<Document>> individualMap, Document parent) { if (parent != null && parent.size() > 0) { Long uid = parent.getLong("uid"); if (uid != null && uid > 0) { if (!individualMap.containsKey(uid)) { individualMap.put(uid, new ArrayList<>()); } individualMap.get(uid).add(parent); } } }
private static boolean isNewVariant(Document document, boolean newStudy) { // If the document has only the study, _id, end, ref and alt fields. if (!newStudy || document.size() != 6) { for (Map.Entry<String, Object> entry : document.entrySet()) { if (!entry.getKey().equals(StageDocumentToVariantConverter.ID_FIELD) && !entry.getKey().equals(StageDocumentToVariantConverter.END_FIELD) && !entry.getKey().equals(StageDocumentToVariantConverter.REF_FIELD) && !entry.getKey().equals(StageDocumentToVariantConverter.ALT_FIELD) && !entry.getKey().equals(StageDocumentToVariantConverter.STUDY_FILE_FIELD)) { if (!isNewStudy((Document) entry.getValue())) { return false; } } } } return true; }
@Override public QueryResult updateStudyConfiguration(StudyConfiguration studyConfiguration, QueryOptions options) { Document studyMongo = new DocumentToStudyConfigurationConverter().convertToStorageType(studyConfiguration); // Update field by field, instead of replacing the whole object to preserve existing fields like "_lock" Document query = new Document("_id", studyConfiguration.getStudyId()); List<Bson> updates = new ArrayList<>(studyMongo.size()); studyMongo.forEach((s, o) -> updates.add(new Document("$set", new Document(s, o)))); QueryResult<UpdateResult> queryResult = collection.update(query, Updates.combine(updates), new QueryOptions(UPSERT, true)); // studyConfigurationMap.put(studyConfiguration.getStudyId(), studyConfiguration); return queryResult; }
@Override public QueryResult updateProjectMetadata(ProjectMetadata projectMetadata, boolean updateCounters) { Document mongo = new GenericDocumentComplexConverter<>(ProjectMetadata.class).convertToStorageType(projectMetadata); // Update field by field, instead of replacing the whole object to preserve existing fields like "_lock" List<Bson> updates = new ArrayList<>(mongo.size()); mongo.forEach((s, o) -> { // Do not update counters if (updateCounters || !s.equals(COUNTERS_FIELD)) { updates.add(new Document("$set", new Document(s, o))); } }); return collection.update(QUERY, Updates.combine(updates), new QueryOptions(UPSERT, true)); }
private List<Tuple> extractTuples(String storedProcedureName, Object retvalObj) { if ( retvalObj instanceof Document ) { Document retval = (Document) retvalObj; if ( retval.size() > 1 ) { throw log.multipleDocumentReturnedByStoredProcedure( storedProcedureName, retval.size() ); } String firstRetValTag = retval.keySet().iterator().next(); Object firstRetVal = retval.get( firstRetValTag ); @SuppressWarnings("unchecked") Iterable<Document> documents = (Iterable<Document>) firstRetVal; List<Tuple> resultTuples = new ArrayList<>(); for ( Document doc : documents ) { resultTuples.add( convert( doc ) ); } return resultTuples; } else { Tuple tuple = new Tuple(); tuple.put( "result", retvalObj ); return Collections.singletonList( tuple ); } }
private List<Tuple> extractTuples(String storedProcedureName, Object retvalObj) { if ( retvalObj instanceof Document ) { Document retval = (Document) retvalObj; if ( retval.size() > 1 ) { throw log.multipleDocumentReturnedByStoredProcedure( storedProcedureName, retval.size() ); } String firstRetValTag = retval.keySet().iterator().next(); Object firstRetVal = retval.get( firstRetValTag ); @SuppressWarnings("unchecked") Iterable<Document> documents = (Iterable<Document>) firstRetVal; List<Tuple> resultTuples = new ArrayList<>(); for ( Document doc : documents ) { resultTuples.add( convert( doc ) ); } return resultTuples; } else { Tuple tuple = new Tuple(); tuple.put( "result", retvalObj ); return Collections.singletonList( tuple ); } }
Map<String, Set<Integer>> gtsMap = new HashMap<>(gt.size()); for (Map.Entry<String, Object> entry : gt.entrySet()) { gtsMap.put(entry.getKey(), new HashSet<>((Collection) entry.getValue()));
@Override public void updateJWTParameters(ObjectMap params) throws CatalogDBException { Bson query = METADATA_QUERY; Document adminDocument = new Document(); if (StringUtils.isNotEmpty(params.getString(SECRET_KEY))) { adminDocument.append("admin.secretKey", params.getString(SECRET_KEY)); } // if (StringUtils.isNotEmpty(params.getString(ALGORITHM))) { // String signature = params.getString(ALGORITHM); // try { // SignatureAlgorithm.valueOf(signature); // } catch (Exception e) { // logger.error("{}", e.getMessage(), e); // throw new CatalogDBException("Unknown signature algorithm " + signature); // } // adminDocument.append("admin.algorithm", params.getString(ALGORITHM)); // } if (adminDocument.size() > 0) { Bson update = new Document("$set", adminDocument); this.metaCollection.update(query, update, QueryOptions.empty()); } } }
protected void walkOrderBy() { sortOnFulltextScore = false; if (orderByClause == null) { orderBy = null; } else { orderBy = new Document(); for (OrderByExpr ob : orderByClause.elements) { Reference ref = ob.reference; boolean desc = ob.isDescending; String field = walkReference(ref).queryField; if (!orderBy.containsKey(field)) { Object value; if (KEY_FULLTEXT_SCORE.equals(field)) { if (!desc) { throw new QueryParseException("Cannot sort by " + NXQL.ECM_FULLTEXT_SCORE + " ascending"); } sortOnFulltextScore = true; value = new Document(MONGODB_META, MONGODB_TEXT_SCORE); } else { value = desc ? MINUS_ONE : ONE; } orderBy.put(field, value); } } if (sortOnFulltextScore && orderBy.size() > 1) { throw new QueryParseException("Cannot sort by " + NXQL.ECM_FULLTEXT_SCORE + " and other criteria"); } } }
@Test public void testGroupBy1() throws Exception { List<Long> pfurioStudies = Arrays.asList(9L, 14L); List<Document> groupByBioformat = catalogFileDBAdaptor.groupBy( new Query(FileDBAdaptor.QueryParams.STUDY_UID.key(), pfurioStudies), Arrays.asList(FileDBAdaptor.QueryParams.BIOFORMAT.key(), FileDBAdaptor.QueryParams.TYPE.key()), new QueryOptions()).getResult(); assertEquals(3, groupByBioformat.size()); assertEquals(2, ((Document) groupByBioformat.get(0).get("_id")).size()); // Alignment - File assertEquals(Arrays.asList("m_alignment.bam", "alignment.bam"), groupByBioformat.get(0).get("items")); assertEquals(2, ((Document) groupByBioformat.get(1).get("_id")).size()); // None - File assertEquals(Arrays.asList("m_file1.txt", "file2.txt", "file1.txt"), groupByBioformat.get(1).get("items")); assertEquals(2, ((Document) groupByBioformat.get(2).get("_id")).size()); // None - Folder assertEquals(Arrays.asList("data/"), groupByBioformat.get(2).get("items")); }
@Test public void annotationToDB1() throws Exception { Variable v = new Variable().setId("a").setType(Variable.VariableType.BOOLEAN); Set<Variable> setOfVariables = new HashSet<>(); setOfVariables.add(v); VariableSet variableSet = new VariableSet().setVariables(setOfVariables).setUid(1); Map<String, Object> myMap = new HashMap<>(); myMap.put("b", "nothing"); AnnotationConverter annotableConverter = new AnnotationConverter(); List<Document> document1 = annotableConverter.annotationToDB(variableSet, "annotName", myMap); assertTrue(document1.isEmpty()); myMap.put("a", "hello"); List<Document> document2 = annotableConverter.annotationToDB(variableSet, "annotName", myMap); assertEquals(1, document2.size()); assertEquals(5, document2.get(0).size()); assertEquals("a", document2.get(0).get(AnnotationConverter.ID)); assertEquals("annotName", document2.get(0).get(AnnotationConverter.ANNOTATION_SET_NAME)); assertEquals(1L, document2.get(0).get(AnnotationConverter.VARIABLE_SET)); assertEquals("hello", document2.get(0).get(AnnotationConverter.VALUE)); }
private IdentityProvider convert(IdentityProviderMongo identityProviderMongo) { if (identityProviderMongo == null) { return null; } IdentityProvider identityProvider = new IdentityProvider(); identityProvider.setId(identityProviderMongo.getId()); identityProvider.setName(identityProviderMongo.getName()); identityProvider.setType(identityProviderMongo.getType()); identityProvider.setConfiguration(identityProviderMongo.getConfiguration()); identityProvider.setMappers((Map) identityProviderMongo.getMappers()); if (identityProviderMongo.getRoleMapper() != null) { Map<String, String[]> roleMapper = new HashMap<>(identityProviderMongo.getRoleMapper().size()); identityProviderMongo.getRoleMapper().forEach((key, value) -> { List lstValue = (List) value; String[] arr = new String[lstValue.size()]; lstValue.toArray(arr); roleMapper.put(key, arr); }); identityProvider.setRoleMapper(roleMapper); } identityProvider.setDomain(identityProviderMongo.getDomain()); identityProvider.setExternal(identityProviderMongo.isExternal()); identityProvider.setCreatedAt(identityProviderMongo.getCreatedAt()); identityProvider.setUpdatedAt(identityProviderMongo.getUpdatedAt()); return identityProvider; }
assertEquals(7, document.get(0).size()); assertEquals(5, ((List) document.get(0).get(AnnotationConverter.VALUE)).size()); assertEquals(Arrays.asList(true, false, false, false, true), document.get(0).get(AnnotationConverter.VALUE));