Archive content from array to list.
| | |
| | | import java.nio.file.Files; |
| | | import java.nio.file.attribute.BasicFileAttributes; |
| | | import java.nio.file.attribute.FileTime; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.SortedMap; |
| | | import java.util.TreeMap; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * Cache for storing complete file lists of archives as gzipped files (using Java standard serialization for |
| | |
| | | * @param archive |
| | | * @return |
| | | */ |
| | | public BorgFilesystemItem[] load(BorgRepoConfig repoConfig, Archive archive) { |
| | | public List<BorgFilesystemItem> load(BorgRepoConfig repoConfig, Archive archive) { |
| | | File file = getFile(repoConfig, archive); |
| | | if (!file.exists()) { |
| | | return null; |
| | |
| | | return load(file); |
| | | } |
| | | |
| | | public BorgFilesystemItem[] load(File file) { |
| | | public List<BorgFilesystemItem> load(File file) { |
| | | if (file.exists() == false) { |
| | | log.error("File '" + file.getAbsolutePath() + "' doesn't exist. Can't get archive content files."); |
| | | return null; |
| | | } |
| | | log.info("Loading archive content as file list from: " + file.getAbsolutePath()); |
| | | BorgFilesystemItem[] list = null; |
| | | List<BorgFilesystemItem> list = null; |
| | | try { |
| | | // Set last modified time of file: |
| | | Files.setAttribute(file.toPath(), "lastModifiedTime", FileTime.fromMillis(System.currentTimeMillis())); |
| | |
| | | return null; |
| | | } |
| | | int size = (Integer) obj; |
| | | list = new BorgFilesystemItem[size]; |
| | | list = new ArrayList<>(); |
| | | for (int i = 0; i < size; i++) { |
| | | obj = inputStream.readObject(); |
| | | if (obj instanceof BorgFilesystemItem) { |
| | | list[i] = (BorgFilesystemItem) obj; |
| | | list.add((BorgFilesystemItem) obj); |
| | | } else { |
| | | log.error("Can't load archive content. FilesystemItem expected, but received: " |
| | | + (obj != null ? obj.getClass() : "null") |
| | |
| | | return archive; |
| | | } |
| | | |
| | | public BorgFilesystemItem[] getArchiveContent(BorgRepoConfig repoConfig, Archive archive) { |
| | | public List<BorgFilesystemItem> getArchiveContent(String archiveId) { |
| | | Archive archive = null; |
| | | outerLoop: |
| | | for (Repository repository : getAllRepositories()) { |
| | | if (repository.getArchives() != null) { |
| | | for (Archive arch : repository.getArchives()) { |
| | | if (StringUtils.equals(archiveId, arch.getId())) { |
| | | archive = arch; |
| | | break outerLoop; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | if (archive == null) { |
| | | log.error("Can't find archive with id '" + archiveId + "'. May-be it doesn't exist or the archives of the target repository aren't yet loaded."); |
| | | return null; |
| | | } |
| | | BorgRepoConfig repoConfig = ConfigurationHandler.getConfiguration().getRepoConfig(archive.getRepoId()); |
| | | return getArchiveContent(repoConfig, archive); |
| | | } |
| | | |
| | | public List<BorgFilesystemItem> getArchiveContent(BorgRepoConfig repoConfig, Archive archive) { |
| | | if (archive == null || StringUtils.isBlank(archive.getName())) { |
| | | return null; |
| | | } |
| | | BorgFilesystemItem[] items = archiveFilelistCache.load(repoConfig, archive); |
| | | List<BorgFilesystemItem> items = archiveFilelistCache.load(repoConfig, archive); |
| | | if (items == null) { |
| | | List<BorgFilesystemItem> list = BorgCommands.listArchiveContent(repoConfig, archive.getName()); |
| | | if (CollectionUtils.isNotEmpty(list)) { |
| | | archiveFilelistCache.save(repoConfig, archive, list); |
| | | items = list.toArray(new BorgFilesystemItem[0]); |
| | | items = new ArrayList<>(); |
| | | } |
| | | } |
| | | if (items == null) { |
| | |
| | | return items; |
| | | } |
| | | |
| | | public BorgFilesystemItem[] getArchiveContent(File file) { |
| | | public List<BorgFilesystemItem> getArchiveContent(File file) { |
| | | return archiveFilelistCache.load(file); |
| | | } |
| | | |
| | |
| | | cache.save(repoConfig, archive, list); |
| | | log.info("Saving done."); |
| | | log.info("Loading items from out dir."); |
| | | BorgFilesystemItem[] filesystemItems = cache.load(repoConfig, archive); |
| | | log.info("Loading " + filesystemItems.length + " items done."); |
| | | assertEquals(list.size(), filesystemItems.length); |
| | | for (int i = 0; i < filesystemItems.length; i++) { |
| | | assertEquals(list.get(i).getPath(), filesystemItems[i].getPath()); |
| | | List<BorgFilesystemItem> filesystemItems = cache.load(repoConfig, archive); |
| | | log.info("Loading " + filesystemItems.size() + " items done."); |
| | | assertEquals(list.size(), filesystemItems.size()); |
| | | for (int i = 0; i < filesystemItems.size(); i++) { |
| | | assertEquals(list.get(i).getPath(), filesystemItems.get(i).getPath()); |
| | | } |
| | | cache.removeAllCacheFiles(); |
| | | } |
| | |
| | | Archive archive = createArchive("2018-12-09"); |
| | | assertNull(cache.load(repoConfig, archive)); |
| | | cache.save(repoConfig, archive, list); |
| | | BorgFilesystemItem[] filesystemItems = cache.load(repoConfig, archive); |
| | | List<BorgFilesystemItem> filesystemItems = cache.load(repoConfig, archive); |
| | | assertNull(cache.load(repoConfig, archive)); |
| | | cache.removeAllCacheFiles(); |
| | | } |
| | |
| | | |
| | | private Archive createArchive(String time) throws Exception { |
| | | Archive archive = new Archive(); |
| | | set(archive, "archive", "archive-" + time); |
| | | set(archive, "name", "archive-" + time); |
| | | set(archive, "time", time); |
| | | return archive; |
| | | } |
| | |
| | | assertNotNull(archive2); |
| | | archive = ButlerCache.getInstance().getArchive(repoConfig.getRepo(), archive.getId()); |
| | | assertNotNull(archive2); |
| | | BorgFilesystemItem[] content = ButlerCache.getInstance().getArchiveContent(repoConfig, archive2); |
| | | log.info("Number of items (content) of archive: " + content.length); |
| | | List<BorgFilesystemItem> content = ButlerCache.getInstance().getArchiveContent(repoConfig, archive2); |
| | | log.info("Number of items (content) of archive: " + content.size()); |
| | | content = ButlerCache.getInstance().getArchiveContent(repoConfig, archive2); |
| | | log.info("Number of items (content) of archive: " + content.length); |
| | | log.info("Number of items (content) of archive: " + content.size()); |
| | | } |
| | | } |
| | | ButlerCache.getInstance().shutdown(); |