private boolean isRootFolder(File file) throws CatalogException { ParamUtils.checkObj(file, "File"); return file.getPath().isEmpty(); }
/** * Check if there is any file in the URI target. * * @throws org.opencb.opencga.catalog.exceptions.CatalogIOException */ private void checkTarget(File file, URI targetUri, CatalogIOManager targetIOManager, boolean overwrite) throws CatalogIOException { if (!overwrite && targetIOManager.exists(targetUri)) { throw new CatalogIOException("There is a file in the target!" + "file:{id:" + file.getPath() + ", targetUri: '" + targetUri + "' } " + "Needs 'overwrite = true' for continue."); } }
/** * Check status. To upload a fileObject, the catalog file entry must be in status "uploading". * Set "ignoreStatus = true" to ignore the actual file status and replace the content. * * @throws org.opencb.opencga.catalog.exceptions.CatalogIOException */ private void checkStatus(File file) throws CatalogIOException { if (!file.getStatus().getName().equals(File.FileStatus.STAGE)) { throw new CatalogIOException("File status is already uploaded and ready! " + "file:{ path: '" + file.getPath() + "', id:" + file.getPath() + ", status: '" + file.getStatus().getName() + "' } " + "Needs 'ignoreStatus = true' for continue."); } }
public QueryResult<File> getFilesFromFolder(String folderStr, String studyStr, QueryOptions options, String sessionId) throws CatalogException { ParamUtils.checkObj(folderStr, "folder"); MyResource<File> resource = getUid(folderStr, studyStr, sessionId); options = ParamUtils.defaultObject(options, QueryOptions::new); if (!resource.getResource().getType().equals(File.Type.DIRECTORY)) { throw new CatalogDBException("File {path:'" + resource.getResource().getPath() + "'} is not a folder."); } Query query = new Query(FileDBAdaptor.QueryParams.DIRECTORY.key(), resource.getResource().getPath()); return get(studyStr, query, options, sessionId); }
@Deprecated public QueryResult<File> getParents(long fileId, QueryOptions options, String sessionId) throws CatalogException { QueryResult<File> fileQueryResult = fileDBAdaptor.get(fileId, new QueryOptions(QueryOptions.INCLUDE, Arrays.asList(FileDBAdaptor.QueryParams.PATH.key(), FileDBAdaptor.QueryParams.STUDY_UID.key()))); if (fileQueryResult.getNumResults() == 0) { return fileQueryResult; } String userId = userManager.getUserId(sessionId); authorizationManager.checkFilePermission(fileQueryResult.first().getStudyUid(), fileId, userId, FileAclEntry.FilePermissions.VIEW); return getParents(true, options, fileQueryResult.first().getPath(), fileQueryResult.first().getStudyUid()); }
@Test public void testGetFileParents1() throws CatalogException { long fileId; QueryResult<File> fileParents; fileId = catalogManager.getFileManager().getUid("data/test/folder/", studyFqn, sessionIdUser).getResource().getUid(); fileParents = catalogManager.getFileManager().getParents(fileId, null, sessionIdUser); assertEquals(4, fileParents.getNumResults()); assertEquals("", fileParents.getResult().get(0).getPath()); assertEquals("data/", fileParents.getResult().get(1).getPath()); assertEquals("data/test/", fileParents.getResult().get(2).getPath()); assertEquals("data/test/folder/", fileParents.getResult().get(3).getPath()); }
@Test public void testIndexFromFolder() throws Exception { QueryOptions queryOptions = new QueryOptions(VariantStorageEngine.Options.ANNOTATE.key(), false) .append(VariantStorageEngine.Options.CALCULATE_STATS.key(), false); File file = getFile(0); Path pathParent = Paths.get(file.getPath()).getParent(); File parent = catalogManager.getFileManager().get(studyFqn, new Query(FileDBAdaptor.QueryParams.PATH.key(), pathParent.toString() + "/"), null, sessionId).first(); indexFiles(singletonList(parent), singletonList(file), queryOptions, outputId); }
@Test public void includeFields() throws CatalogDBException { QueryResult<File> fileQueryResult = catalogFileDBAdaptor.get(7, new QueryOptions(QueryOptions.INCLUDE, FileDBAdaptor.QueryParams.PATH.key())); List<File> files = fileQueryResult.getResult(); assertEquals("Include path does not work.", "data/file.vcf", files.get(0).getPath()); assertEquals("Include not working.", null, files.get(0).getName()); }
private WriteResult sendToTrash(long studyId, File file) throws CatalogDBException { // It doesn't really matter if file is a file or a directory. I can directly set the status of the file or the directory + // subfiles and subdirectories doing a single query as I don't need to rename anything // Obtain all files within the folder Query query = new Query() .append(FileDBAdaptor.QueryParams.STUDY_UID.key(), studyId) .append(FileDBAdaptor.QueryParams.PATH.key(), "~^" + file.getPath() + "*") .append(FileDBAdaptor.QueryParams.STATUS_NAME.key(), GET_NON_DELETED_FILES); ObjectMap params = new ObjectMap() .append(FileDBAdaptor.QueryParams.STATUS_NAME.key(), File.FileStatus.TRASHED); QueryResult<Long> update = fileDBAdaptor.update(query, params, QueryOptions.empty()); return new WriteResult("trash", update.getDbTime(), update.first(), update.first(), null, null, null); }
@Test public void testDeleteExisting() throws IOException, CatalogException { QueryResult<File> queryResult = catalogManager.getFileManager().upload(study.getFqn(), CatalogManagerTest.createDebugFile().toURI(), new File().setPath(folder.getPath() + "file1.txt"), false, false, sessionIdUser); File file = queryResult.first(); CatalogManagerTest.createDebugFile(directory.resolve("file1.txt").toString()); List<File> files = new FileScanner(catalogManager).scan(folder, directory.toUri(), FileScanner.FileScannerPolicy.DELETE, false, true, sessionIdUser); files.forEach((File f) -> assertFalse(f.getAttributes().containsKey("checksum"))); assertEquals(File.FileStatus.DELETED, getFile(file.getUid()).getStatus().getName()); }
@Test public void testUpdateIndexStatus() throws CatalogException { QueryResult<File> fileResult = fileManager.create(studyFqn, File.Type.FILE, File.Format.VCF, File.Bioformat.VARIANT, "data/test.vcf", "", "description", new File.FileStatus(File.FileStatus.STAGE), 0, -1, Collections.emptyList(), -1, Collections.emptyMap(), Collections.emptyMap(), true, null, new QueryOptions(), sessionIdUser); fileManager.updateFileIndexStatus(fileResult.first(), FileIndex.IndexStatus.TRANSFORMED, null, sessionIdUser); QueryResult<File> read = fileManager.get(studyFqn, fileResult.first().getPath(), new QueryOptions(), sessionIdUser); assertEquals(FileIndex.IndexStatus.TRANSFORMED, read.first().getIndex().getStatus().getName()); }
@Test public void testBySteps1() throws Exception { File transformFile = transformFile(inputFile1, new QueryOptions()); Assert.assertEquals(FileIndex.IndexStatus.TRANSFORMED, catalogManager.getFileManager().get(studyId, inputFile1.getPath(), null, sessionId).first().getIndex().getStatus().getName()); Assert.assertNull(catalogManager.getFileManager().get(studyId, inputFile2.getPath(), null, sessionId).first().getIndex().getStatus()); loadFile(transformFile, new QueryOptions(), outputId); Assert.assertEquals(FileIndex.IndexStatus.READY, catalogManager.getFileManager().get(studyId, inputFile1.getPath(), null, sessionId).first().getIndex().getStatus().getName()); Assert.assertNull(catalogManager.getFileManager().get(studyId, inputFile2.getPath(), null, sessionId).first().getIndex().getStatus()); }
@Test public void getFileIdByString() throws CatalogException { Study.StudyAclParams aclParams = new Study.StudyAclParams("", AclParams.Action.ADD, "analyst"); catalogManager.getStudyManager().updateAcl(Arrays.asList(studyFqn), "user2", aclParams, sessionIdUser).get(0); File file = catalogManager.getFileManager().create(studyFqn, File.Type.FILE, File.Format.UNKNOWN, File.Bioformat.NONE, "data/test/folder/file.txt", null, "My description", null, 0, -1, null, (long) -1, null, null, true, null, null, sessionIdUser2).first(); long fileId = catalogManager.getFileManager().getUid(file.getPath(), studyFqn, sessionIdUser).getResource().getUid(); assertEquals(file.getUid(), fileId); fileId = catalogManager.getFileManager().getUid(file.getPath(), studyFqn, sessionIdUser).getResource().getUid(); assertEquals(file.getUid(), fileId); fileId = catalogManager.getFileManager().getUid("/", studyFqn, sessionIdUser).getResource().getUid(); System.out.println(fileId); }
@Test public void testBySteps2() throws Exception { File transformFile = transformFile(inputFile2, new QueryOptions()); Assert.assertNull(catalogManager.getFileManager().get(studyId, inputFile1.getPath(), null, sessionId).first().getIndex().getStatus()); Assert.assertEquals(FileIndex.IndexStatus.TRANSFORMED, catalogManager.getFileManager().get(studyId, inputFile2.getPath(), null, sessionId).first().getIndex().getStatus().getName()); loadFile(transformFile, new QueryOptions(), outputId); Assert.assertNull(catalogManager.getFileManager().get(studyId, inputFile1.getPath(), null, sessionId).first().getIndex().getStatus()); Assert.assertEquals(FileIndex.IndexStatus.READY, catalogManager.getFileManager().get(studyId, inputFile2.getPath(), null, sessionId).first().getIndex().getStatus().getName()); } }
@Test public void testIndex2() throws Exception { indexFile(inputFile2, new QueryOptions(), outputId); Assert.assertNull(catalogManager.getFileManager().get(studyId, inputFile1.getPath(), null, sessionId).first().getIndex().getStatus()); Assert.assertEquals(FileIndex.IndexStatus.READY, catalogManager.getFileManager().get(studyId, inputFile2.getPath(), null, sessionId).first().getIndex().getStatus().getName()); }
@Test public void testIndex1() throws Exception { indexFile(inputFile1, new QueryOptions(), outputId); Assert.assertEquals(FileIndex.IndexStatus.READY, catalogManager.getFileManager().get(studyId, inputFile1.getPath(), null, sessionId).first().getIndex().getStatus().getName()); Assert.assertNull(catalogManager.getFileManager().get(studyId, inputFile2.getPath(), null, sessionId).first().getIndex().getStatus()); }
@Test public void unlinkNonExternalFile() throws CatalogException, IOException { File file = catalogManager.getFileManager().create(studyFqn, File.Type.FILE, File.Format.PLAIN, File.Bioformat.NONE, "item." + TimeUtils.getTimeMillis() + ".txt", null, "file at root", null, 0, -1, null, (long) -1, null, null, true, null, null, userSessionId).first(); // Now we try to unlink it thrown.expect(CatalogException.class); thrown.expectMessage("use delete instead"); catalogManager.getFileManager().unlink(studyFqn, file.getPath(), userSessionId); }
@Test public void testDeleteIndexedFile() throws Exception { QueryOptions queryOptions = new QueryOptions(VariantStorageEngine.Options.ANNOTATE.key(), false) .append(VariantStorageEngine.Options.CALCULATE_STATS.key(), false); File inputFile = getFile(0); indexFile(inputFile, queryOptions, outputId); Study study = catalogManager.getFileManager().getStudy(inputFile, sessionId); WriteResult result = catalogManager.getFileManager().delete( study.getFqn(), new Query(FileDBAdaptor.QueryParams.PATH.key(), inputFile.getPath()) , null, sessionId); assertEquals(0, result.getNumModified()); assertTrue(result.getFailed().get(0).getMessage().contains("index status")); }
@Test public void testDeleteRootFolder() throws CatalogException, IOException { File deletable = catalogManager.getFileManager().get(studyFqn2, "/", QueryOptions.empty(), sessionIdUser).first(); WriteResult result = catalogManager.getFileManager().delete(studyFqn2, new Query(FileDBAdaptor.QueryParams.PATH.key(), deletable.getPath()), null, sessionIdUser); assertEquals(1, result.getNumMatches()); assertEquals(0, result.getNumModified()); assertEquals("Root directories cannot be deleted", result.getFailed().get(0).getMessage()); }
@Test public void deleteFilesTest2() throws CatalogException, IOException { QueryResult<File> queryResult = catalogManager.getFileManager().create(studyFqn, File.Type.FILE, File.Format.PLAIN, File.Bioformat.NONE, "my.txt", null, "", new File.FileStatus(File.FileStatus.STAGE), 0, -1, null, -1, null, null, false, null, null, userSessionId); new FileUtils(catalogManager).upload(new ByteArrayInputStream(StringUtils.randomString(200) .getBytes()), queryResult.first(), userSessionId, false, false, true); File file = catalogManager.getFileManager().get(studyFqn, queryResult.first().getPath(), null, userSessionId).first(); CatalogIOManager ioManager = catalogManager.getCatalogIOManagerFactory().get(file.getUri()); assertTrue(ioManager.exists(file.getUri())); catalogManager.getFileManager().delete(studyFqn, new Query(FileDBAdaptor.QueryParams.UID.key(), file.getUid()), null, userSessionId); assertTrue(ioManager.exists(file.getUri())); // catalogFileUtils.delete(file.getId(), userSessionId); // assertTrue(!ioManager.exists(catalogManager.getFileUri(catalogManager.getFile(file.getId(), userSessionId).first()))); }