Removed references locks

master
Andrea Cavalli 3 years ago
parent 7fd7497464
commit f030a96608
  1. 13
      src/main/java/it/cavallium/strangedb/database/references/DatabaseReferencesMetadata.java
  2. 21
      src/main/java/it/cavallium/strangedb/database/references/DatabaseReferencesMetadataCache.java

@ -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();
}

@ -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();
}
}
}

Loading…
Cancel
Save