cleanup object index a little

This commit is contained in:
2024-06-16 20:48:43 +02:00
parent 739122999f
commit 4b3f29f6f6
3 changed files with 33 additions and 27 deletions

View File

@@ -1,53 +1,48 @@
package com.usatiuk.dhfs.storage.objects.repository.distributed;
import lombok.Getter;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class ObjectIndex implements Serializable {
@Getter
final Map<String, ObjectMeta> _objectMetaMap = new HashMap<>();
private final ObjectIndexData _data = new ObjectIndexData();
private final ReadWriteLock _lock = new ReentrantReadWriteLock();
public <R> R runReadLocked(Callable<R> fn) {
@FunctionalInterface
public interface ObjectIndexFn<R> {
R apply(ObjectIndexData indexData);
}
public <R> R runReadLocked(ObjectIndexFn<R> fn) {
_lock.readLock().lock();
try {
return fn.call();
} catch (Exception e) {
throw new RuntimeException(e);
return fn.apply(_data);
} finally {
_lock.readLock().unlock();
}
}
public <R> R runWriteLocked(Callable<R> fn) {
public <R> R runWriteLocked(ObjectIndexFn<R> fn) {
_lock.writeLock().lock();
try {
return fn.call();
} catch (Exception e) {
throw new RuntimeException(e);
return fn.apply(_data);
} finally {
_lock.writeLock().unlock();
}
}
public boolean exists(String name) {
return runReadLocked(() -> {
return _objectMetaMap.containsKey(name);
return runReadLocked((data) -> {
return data.getObjectMetaMap().containsKey(name);
});
}
public Optional<ObjectMeta> get(String name) {
return runReadLocked(() -> {
if (_objectMetaMap.containsKey(name)) {
return Optional.of(_objectMetaMap.get(name));
return runReadLocked((data) -> {
if (data.getObjectMetaMap().containsKey(name)) {
return Optional.of(data.getObjectMetaMap().get(name));
} else {
return Optional.empty();
}
@@ -55,15 +50,15 @@ public class ObjectIndex implements Serializable {
}
public ObjectMeta getOrCreate(String name, boolean assumeUnique) {
return runWriteLocked(() -> {
if (_objectMetaMap.containsKey(name)) {
var got = _objectMetaMap.get(name);
return runWriteLocked((data) -> {
if (data.getObjectMetaMap().containsKey(name)) {
var got = data.getObjectMetaMap().get(name);
if (got.getAssumeUnique() != assumeUnique)
throw new IllegalArgumentException("assumeUnique mismatch for " + name);
return got;
} else {
var newObjectMeta = new ObjectMeta(name, assumeUnique);
_objectMetaMap.put(name, newObjectMeta);
data.getObjectMetaMap().put(name, newObjectMeta);
return newObjectMeta;
}
});

View File

@@ -0,0 +1,12 @@
package com.usatiuk.dhfs.storage.objects.repository.distributed;
import lombok.Getter;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
public class ObjectIndexData implements Serializable {
@Getter
private final Map<String, ObjectMeta> _objectMetaMap = new HashMap<>();
}

View File

@@ -56,9 +56,8 @@ public class ObjectIndexService {
}
public void forAllRead(ForAllFn fn) {
_index.runReadLocked(() -> {
// FIXME:
for (var entry : _index._objectMetaMap.entrySet()) {
_index.runReadLocked((data) -> {
for (var entry : data.getObjectMetaMap().entrySet()) {
fn.apply(entry.getKey(), entry.getValue());
}
return null;