in commons-jcs3-core/src/main/java/org/apache/commons/jcs3/auxiliary/disk/indexed/IndexedDiskCache.java [1057:1121]
protected void optimizeFile()
{
final ElapsedTimer timer = new ElapsedTimer();
timesOptimized++;
log.info("{0}: Beginning Optimization #{1}", logCacheName, timesOptimized);
// CREATE SNAPSHOT
Collection<IndexedDiskElementDescriptor> defragList = null;
storageLock.writeLock().lock();
try
{
queueInput = true;
// shut off recycle while we're optimizing,
doRecycle = false;
defragList = createPositionSortedDescriptorList();
}
finally
{
storageLock.writeLock().unlock();
}
// Defrag the file outside of the write lock. This allows a move to be made,
// and yet have the element still accessible for reading or writing.
long expectedNextPos = defragFile(defragList, 0);
// ADD THE QUEUED ITEMS to the end and then truncate
storageLock.writeLock().lock();
try
{
try
{
if (!queuedPutList.isEmpty())
{
// pack them at the end
expectedNextPos = defragFile(queuedPutList, expectedNextPos);
}
// TRUNCATE THE FILE
dataFile.truncate(expectedNextPos);
}
catch (final IOException e)
{
log.error("{0}: Error optimizing queued puts.", logCacheName, e);
}
// RESTORE NORMAL OPERATION
removeCount = 0;
resetBytesFree();
this.recycle.clear();
queuedPutList.clear();
queueInput = false;
// turn recycle back on.
doRecycle = true;
isOptimizing = false;
}
finally
{
storageLock.writeLock().unlock();
}
log.info("{0}: Finished #{1}, Optimization took {2}",
logCacheName, timesOptimized, timer.getElapsedTimeString());
}