@Override public ContentClaim clone(final ContentClaim original, final boolean lossTolerant) throws IOException { final ContentClaim createdClaim = create(lossTolerant); try (final InputStream dataIn = read(original)) { final ContentRepository createdClaimRepo = lossTolerant ? this : getBackupRepository(); if (createdClaimRepo == null) { throw new IllegalStateException("Cannot create non-loss-tolerant ContentClaim because there is no persistent Content Repository configured"); } try (final OutputStream dataOut = createdClaimRepo.write(createdClaim)) { StreamUtils.copy(dataIn, dataOut); } } return createdClaim; }
@Override public ContentClaim create(boolean lossTolerant) throws IOException { if (lossTolerant) { return createLossTolerant(); } else { final ContentRepository backupRepo = getBackupRepository(); if (backupRepo == null) { // TODO: Loss Tolerance is not yet configurable. // Therefore, since this is an in-memory content repository, assume that this claim is loss-tolerant if there // is not a backup repository return createLossTolerant(); } final ContentClaim backupClaim = backupRepo.create(lossTolerant); backupRepoClaimMap.put(backupClaim, backupClaim); return backupClaim; } }
@Override public boolean isAccessible(final ContentClaim claim) throws IOException { if (claim == null) { return false; } final ContentClaim backupClaim = getBackupClaim(claim); if (backupClaim == null) { final ContentBlock contentBlock = claimMap.get(claim); return contentBlock != null; } return getBackupRepository().isAccessible(backupClaim); }
@Override public OutputStream write(final ContentClaim claim) throws IOException { final ContentClaim backupClaim = getBackupClaim(claim); return backupClaim == null ? getContent(claim).write() : getBackupRepository().write(backupClaim); }
@Override public int incrementClaimaintCount(final ContentClaim claim) { if (claim == null) { return 0; } final ContentClaim backupClaim = getBackupClaim(claim); if (backupClaim == null) { return claimManager.incrementClaimantCount(resolveClaim(claim).getResourceClaim()); } else { return getBackupRepository().incrementClaimaintCount(backupClaim); } }
@Override public long merge(final Collection<ContentClaim> claims, final ContentClaim destination, final byte[] header, final byte[] footer, final byte[] demarcator) throws IOException { long bytes = 0L; try (final OutputStream out = write(destination)) { if (header != null) { out.write(header); bytes += header.length; } final Iterator<ContentClaim> itr = claims.iterator(); while (itr.hasNext()) { final ContentClaim readClaim = itr.next(); try (final InputStream in = read(readClaim)) { bytes += StreamUtils.copy(in, out); } if (itr.hasNext() && demarcator != null) { bytes += demarcator.length; out.write(demarcator); } } if (footer != null) { bytes += footer.length; out.write(footer); } return bytes; } }
public long getSize() throws IOException { if (amos == null) { final ContentClaim backupClaim = getBackupClaim(claim); return getBackupRepository().size(backupClaim); } else { return amos.size(); } }
@Override public long exportTo(ContentClaim claim, OutputStream destination) throws IOException { final InputStream in = read(claim); try { return StreamUtils.copy(in, destination); } finally { IOUtils.closeQuietly(in); } }
@Override public long importFrom(final Path content, final ContentClaim claim) throws IOException { try (final InputStream in = new FileInputStream(content.toFile())) { return importFrom(in, claim); } }
private boolean remove(final ResourceClaim claim) { if (claim == null) { return false; } final Set<ContentClaim> contentClaims = new HashSet<>(); for (final Map.Entry<ContentClaim, ContentBlock> entry : claimMap.entrySet()) { final ContentClaim contentClaim = entry.getKey(); if (contentClaim.getResourceClaim().equals(claim)) { contentClaims.add(contentClaim); } } boolean removed = false; for (final ContentClaim contentClaim : contentClaims) { if (remove(contentClaim)) { removed = true; } } return removed; }
@Override public void purge() { for (final ContentClaim claim : claimMap.keySet()) { claimManager.decrementClaimantCount(resolveClaim(claim).getResourceClaim()); final ContentClaim backup = getBackupClaim(claim); if (backup != null) { getBackupRepository().remove(backup); } } }
@Override public long size(final ContentClaim claim) throws IOException { if (claim == null) { return 0L; } final ContentClaim backupClaim = getBackupClaim(claim); return backupClaim == null ? getContent(claim).getSize() : getBackupRepository().size(claim); }
@Override public boolean remove(final ContentClaim claim) { if (claim == null) { return false; } final ContentClaim backupClaim = getBackupClaim(claim); if (backupClaim == null) { final ContentBlock content = claimMap.remove(claim); if (content == null) { logger.debug("Removed {} from repo but it did not exist", claim); } else { logger.debug("Removed {} from repo; Content = {}", claim, content); content.destroy(); } } else { getBackupRepository().remove(backupClaim); } return true; }
public InputStream read() throws IOException { if (amos == null) { final ContentClaim backupClaim = getBackupClaim(claim); return getBackupRepository().read(backupClaim); } else { return amos.newInputStream(); } } }
@Override public long exportTo(ContentClaim claim, OutputStream destination, long offset, long length) throws IOException { final InputStream in = read(claim); try { StreamUtils.skip(in, offset); StreamUtils.copy(in, destination, length); } finally { IOUtils.closeQuietly(in); } return length; }
@Override public int decrementClaimantCount(final ContentClaim claim) { if (claim == null) { return 0; } final ContentClaim backupClaim = getBackupClaim(claim); if (backupClaim == null) { return claimManager.decrementClaimantCount(resolveClaim(claim).getResourceClaim()); } else { return getBackupRepository().decrementClaimantCount(backupClaim); } }
@Override public InputStream read(final ContentClaim claim) throws IOException { if (claim == null) { return new ByteArrayInputStream(new byte[0]); } final ContentClaim backupClaim = getBackupClaim(claim); return backupClaim == null ? getContent(claim).read() : getBackupRepository().read(backupClaim); }
private void redirect() throws IOException { final ContentRepository backupRepo = getBackupRepository(); if (backupRepo == null) { throw new IOException("Content Repository is out of space"); } final ContentClaim backupClaim = backupRepo.create(true); backupRepoClaimMap.put(claim, backupClaim); out = backupRepo.write(backupClaim); amos.writeTo(out); amos.destroy(); amos = null; }
@Override public long exportTo(final ContentClaim claim, final Path destination, final boolean append, final long offset, final long length) throws IOException { if (claim == null) { if (append) { return 0L; } Files.createFile(destination); return 0L; } final StandardOpenOption openOption = append ? StandardOpenOption.APPEND : StandardOpenOption.CREATE; try (final InputStream in = read(claim); final OutputStream destinationStream = Files.newOutputStream(destination, openOption)) { if (offset > 0) { StreamUtils.skip(in, offset); } StreamUtils.copy(in, destinationStream, length); return length; } }