use of org.apache.nifi.stream.io.SynchronizedByteCountingOutputStream in project nifi by apache.
the class FileSystemRepository method create.
@Override
public ContentClaim create(final boolean lossTolerant) throws IOException {
ResourceClaim resourceClaim;
final long resourceOffset;
final ClaimLengthPair pair = writableClaimQueue.poll();
if (pair == null) {
final long currentIndex = index.incrementAndGet();
String containerName = null;
boolean waitRequired = true;
ContainerState containerState = null;
for (long containerIndex = currentIndex; containerIndex < currentIndex + containers.size(); containerIndex++) {
final long modulatedContainerIndex = containerIndex % containers.size();
containerName = containerNames.get((int) modulatedContainerIndex);
containerState = containerStateMap.get(containerName);
if (!containerState.isWaitRequired()) {
waitRequired = false;
break;
}
}
if (waitRequired) {
containerState.waitForArchiveExpiration();
}
final long modulatedSectionIndex = currentIndex % SECTIONS_PER_CONTAINER;
final String section = String.valueOf(modulatedSectionIndex);
final String claimId = System.currentTimeMillis() + "-" + currentIndex;
resourceClaim = resourceClaimManager.newResourceClaim(containerName, section, claimId, lossTolerant, true);
resourceOffset = 0L;
LOG.debug("Creating new Resource Claim {}", resourceClaim);
// we always append because there may be another ContentClaim using the same resource claim.
// However, we know that we will never write to the same claim from two different threads
// at the same time because we will call create() to get the claim before we write to it,
// and when we call create(), it will remove it from the Queue, which means that no other
// thread will get the same Claim until we've finished writing to it.
final File file = getPath(resourceClaim).toFile();
ByteCountingOutputStream claimStream = new SynchronizedByteCountingOutputStream(new FileOutputStream(file, true), file.length());
writableClaimStreams.put(resourceClaim, claimStream);
incrementClaimantCount(resourceClaim, true);
} else {
resourceClaim = pair.getClaim();
resourceOffset = pair.getLength();
LOG.debug("Reusing Resource Claim {}", resourceClaim);
incrementClaimantCount(resourceClaim, false);
}
final StandardContentClaim scc = new StandardContentClaim(resourceClaim, resourceOffset);
return scc;
}
Aggregations