try
{
for (K key : keys)
{
CacheKey.PathInfo path = key.getPathInfo();
SequentialWriter writer = writers.get(path);
if (writer == null)
{
writer = tempCacheFile(path);
writers.put(path, writer);
}
try
{
cacheLoader.serialize(key, writer.stream);
}
catch (IOException e)
{
throw new FSWriteError(e, writer.getPath());
}
keysWritten++;
}
}
finally
{
for (SequentialWriter writer : writers.values())
FileUtils.closeQuietly(writer);
}
for (Map.Entry<CacheKey.PathInfo, SequentialWriter> info : writers.entrySet())
{
CacheKey.PathInfo path = info.getKey();
SequentialWriter writer = info.getValue();
File tmpFile = new File(writer.getPath());
File cacheFile = getCachePath(path.keyspace, path.columnFamily, path.cfId, CURRENT_VERSION);
cacheFile.delete(); // ignore error if it didn't exist
if (!tmpFile.renameTo(cacheFile))
logger.error("Unable to rename {} to {}", tmpFile, cacheFile);