try
{
for (CacheKey key : keys)
{
Pair<String, String> path = key.getPathInfo();
SequentialWriter writer = writers.get(path);
if (writer == null)
{
writer = tempCacheFile(path);
writers.put(path, writer);
}
ByteBuffer bytes = key.serializeForStorage();
ByteBufferUtil.writeWithLength(bytes, writer.stream);
bytesWritten += bytes.remaining();
}
}
finally
{
for (SequentialWriter writer : writers.values())
FileUtils.closeQuietly(writer);
}
for (Map.Entry<Pair<String, String>, SequentialWriter> info : writers.entrySet())
{
Pair<String, String> path = info.getKey();
SequentialWriter writer = info.getValue();
File tmpFile = new File(writer.getPath());
File cacheFile = getCachePath(path.left, path.right);
cacheFile.delete(); // ignore error if it didn't exist
if (!tmpFile.renameTo(cacheFile))
logger.error("Unable to rename " + tmpFile + " to " + cacheFile);