Removed references locks

This commit is contained in:
Andrea Cavalli 2019-04-24 01:10:43 +02:00
parent 7fd7497464
commit f030a96608
2 changed files with 20 additions and 14 deletions

View File

@ -110,14 +110,9 @@ public class DatabaseReferencesMetadata implements IReferencesMetadata {
@Override
public long newReference(long index, int size) throws IOException {
lock.writeLock().lock();
long newReference;
try {
newReference = firstFreeReference.getAndIncrement();
cache.put(newReference, new ReferenceInfo(index, size, BLANK_DATA_CLEANER), true);
} finally {
lock.writeLock().unlock();
}
newReference = firstFreeReference.getAndIncrement();
cache.put(newReference, new ReferenceInfo(index, size, BLANK_DATA_CLEANER), true, true);
return newReference;
}
@ -125,7 +120,7 @@ public class DatabaseReferencesMetadata implements IReferencesMetadata {
public void editReference(long reference, ReferenceInfo info) throws IOException {
lock.writeLock().lock();
try {
cache.put(reference, info, true);
cache.put(reference, info, true, false);
} finally {
lock.writeLock().unlock();
}
@ -135,7 +130,7 @@ public class DatabaseReferencesMetadata implements IReferencesMetadata {
public void deleteReference(long reference) throws IOException {
lock.writeLock().lock();
try {
cache.put(reference, NONEXISTENT_REFERENCE_INFO, true);
cache.put(reference, NONEXISTENT_REFERENCE_INFO, true, false);
} finally {
lock.writeLock().unlock();
}

View File

@ -44,20 +44,31 @@ public class DatabaseReferencesMetadataCache {
}
}
public void put(long reference, ReferenceInfo info, boolean modified) throws IOException {
public void put(long reference, ReferenceInfo info, boolean isModified, boolean isNew) throws IOException {
if (closed) throw new IOException("Cache already closed!");
lock.writeLock().lock();
if (isNew) {
lock.readLock().lock();
} else {
lock.writeLock().lock();
}
try {
if (info.getCleanerId() == 0) {
throw new IOException("Null cleaner id");
}
referencesInfos.put(reference, info);
if (modified) {
if (isModified) {
modifiedInfos.put(reference, info);
}
flush();
// If it's new you can't flush because you have only the read permission
if (!isNew) {
flush();
}
} finally {
lock.writeLock().unlock();
if (isNew) {
lock.readLock().unlock();
} else {
lock.writeLock().unlock();
}
}
}