Skip to content

Commit

Permalink
Change reference count logic for temp files
Browse files Browse the repository at this point in the history
  • Loading branch information
Brock Noland committed Nov 20, 2012
1 parent 0ba71f8 commit 7621afb
Showing 1 changed file with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ public void destroy(PendingWrite write) {
File file = ((FileBackedWrite)write).getFileBackedByteArray().getFile();
boolean delete = false;
synchronized (mTempFileReferenceCounts) {
long count = mTempFileReferenceCounts.get(file).decrementAndGet();
if(count <= 0) {
if(getCounter(file).decrementAndGet() <= 0) {
mTempFileReferenceCounts.remove(file);
delete = true;
}
Expand All @@ -86,9 +85,7 @@ private FileBackedByteArray writeToTemp(long offset, byte[] buffer, int start, i
FileBackedByteArray result = FileBackedByteArray.create(fileHandle.file,
fileHandle.randomAccessFile, buffer, start, length);
mTempFileQueue.add(fileHandle);
synchronized (mTempFileReferenceCounts) {
mTempFileReferenceCounts.get(fileHandle.file).incrementAndGet();
}
getCounter(fileHandle.file).incrementAndGet();
return result;
}
private FileHandle nextTempFile(long offset) throws IOException {
Expand All @@ -97,11 +94,19 @@ private FileHandle nextTempFile(long offset) throws IOException {
File file = new File(base, UUID.randomUUID().toString());
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");
FileHandle fileHandle = new FileHandle(file, randomAccessFile);
return fileHandle;
}
private AtomicLong getCounter(File file) {
synchronized (mTempFileReferenceCounts) {
mTempFileReferenceCounts.put(fileHandle.file, new AtomicLong(0));
AtomicLong counter = mTempFileReferenceCounts.get(file);
if(counter == null) {
counter = new AtomicLong(0L);
mTempFileReferenceCounts.put(file, counter);
}
return counter;
}
return fileHandle;
}

private static class FileHandle {
private final File file;
private final RandomAccessFile randomAccessFile;
Expand Down

0 comments on commit 7621afb

Please sign in to comment.