simplify transaction method names

This commit is contained in:
2024-12-29 11:47:21 +01:00
parent 9273dc818e
commit 097929260b
6 changed files with 54 additions and 54 deletions

View File

@@ -20,17 +20,17 @@ public class CurrentTransaction implements Transaction {
}
@Override
public <T extends JData> Optional<T> getObject(Class<T> type, JObjectKey key, LockingStrategy strategy) {
return transactionManager.current().getObject(type, key, strategy);
public <T extends JData> Optional<T> get(Class<T> type, JObjectKey key, LockingStrategy strategy) {
return transactionManager.current().get(type, key, strategy);
}
@Override
public void deleteObject(JObjectKey key) {
transactionManager.current().deleteObject(key);
public void delete(JObjectKey key) {
transactionManager.current().delete(key);
}
@Override
public <T extends JData> void putObject(JData obj) {
transactionManager.current().putObject(obj);
public <T extends JData> void put(JData obj) {
transactionManager.current().put(obj);
}
}

View File

@@ -9,13 +9,13 @@ import java.util.Optional;
public interface Transaction {
long getId();
<T extends JData> Optional<T> getObject(Class<T> type, JObjectKey key, LockingStrategy strategy);
<T extends JData> Optional<T> get(Class<T> type, JObjectKey key, LockingStrategy strategy);
<T extends JData> void putObject(JData obj);
<T extends JData> void put(JData obj);
void deleteObject(JObjectKey key);
void delete(JObjectKey key);
default <T extends JData> Optional<T> getObject(Class<T> type, JObjectKey key) {
return getObject(type, key, LockingStrategy.OPTIMISTIC);
default <T extends JData> Optional<T> get(Class<T> type, JObjectKey key) {
return get(type, key, LockingStrategy.OPTIMISTIC);
}
}

View File

@@ -28,7 +28,7 @@ public class TransactionFactoryImpl implements TransactionFactory {
}
@Override
public <T extends JData> Optional<T> getObject(Class<T> type, JObjectKey key, LockingStrategy strategy) {
public <T extends JData> Optional<T> get(Class<T> type, JObjectKey key, LockingStrategy strategy) {
var got = _objects.get(key);
if (got != null) {
var compatible = got.getIfStrategyCompatible(key, strategy);
@@ -66,12 +66,12 @@ public class TransactionFactoryImpl implements TransactionFactory {
}
@Override
public void deleteObject(JObjectKey key) {
public void delete(JObjectKey key) {
_objects.put(key, new TxRecord.TxObjectRecordDeleted(key));
}
@Override
public void putObject(JData obj) {
public void put(JData obj) {
if (_objects.containsKey(obj.getKey())) {
throw new IllegalArgumentException("Object already exists in transaction");
}

View File

@@ -34,13 +34,13 @@ public class ObjectsTest {
txm.begin();
var newParent = alloc.create(Parent.class, new JObjectKey("Parent"));
newParent.setLastName("John");
curTx.putObject(newParent);
curTx.put(newParent);
txm.commit();
}
{
txm.begin();
var parent = curTx.getObject(Parent.class, new JObjectKey("Parent")).orElse(null);
var parent = curTx.get(Parent.class, new JObjectKey("Parent")).orElse(null);
Assertions.assertEquals("John", parent.getLastName());
txm.commit();
}
@@ -52,26 +52,26 @@ public class ObjectsTest {
txm.begin();
var newParent = alloc.create(Parent.class, new JObjectKey("Parent"));
newParent.setLastName("John");
curTx.putObject(newParent);
curTx.put(newParent);
txm.commit();
}
{
txm.begin();
var parent = curTx.getObject(Parent.class, new JObjectKey("Parent")).orElse(null);
var parent = curTx.get(Parent.class, new JObjectKey("Parent")).orElse(null);
Assertions.assertEquals("John", parent.getLastName());
txm.commit();
}
{
txm.begin();
curTx.deleteObject(new JObjectKey("Parent"));
curTx.delete(new JObjectKey("Parent"));
txm.commit();
}
{
txm.begin();
var parent = curTx.getObject(Parent.class, new JObjectKey("Parent")).orElse(null);
var parent = curTx.get(Parent.class, new JObjectKey("Parent")).orElse(null);
Assertions.assertNull(parent);
txm.commit();
}
@@ -83,17 +83,17 @@ public class ObjectsTest {
txm.begin();
var newParent = alloc.create(Parent.class, new JObjectKey("Parent7"));
newParent.setLastName("John");
curTx.putObject(newParent);
curTx.put(newParent);
txm.commit();
}
Assertions.assertThrows(Exception.class, () -> txm.run(() -> {
var newParent = alloc.create(Parent.class, new JObjectKey("Parent7"));
newParent.setLastName("John2");
curTx.putObject(newParent);
curTx.put(newParent);
}));
{
txm.begin();
var parent = curTx.getObject(Parent.class, new JObjectKey("Parent7")).orElse(null);
var parent = curTx.get(Parent.class, new JObjectKey("Parent7")).orElse(null);
Assertions.assertEquals("John", parent.getLastName());
txm.commit();
}
@@ -105,13 +105,13 @@ public class ObjectsTest {
txm.begin();
var newParent = alloc.create(Parent.class, new JObjectKey("Parent3"));
newParent.setLastName("John");
curTx.putObject(newParent);
curTx.put(newParent);
txm.commit();
}
{
txm.begin();
var parent = curTx.getObject(Parent.class, new JObjectKey("Parent3"), LockingStrategy.OPTIMISTIC).orElse(null);
var parent = curTx.get(Parent.class, new JObjectKey("Parent3"), LockingStrategy.OPTIMISTIC).orElse(null);
Assertions.assertEquals("John", parent.getLastName());
parent.setLastName("John2");
txm.commit();
@@ -119,7 +119,7 @@ public class ObjectsTest {
{
txm.begin();
var parent = curTx.getObject(Parent.class, new JObjectKey("Parent3"), LockingStrategy.WRITE).orElse(null);
var parent = curTx.get(Parent.class, new JObjectKey("Parent3"), LockingStrategy.WRITE).orElse(null);
Assertions.assertEquals("John2", parent.getLastName());
parent.setLastName("John3");
txm.commit();
@@ -127,7 +127,7 @@ public class ObjectsTest {
{
txm.begin();
var parent = curTx.getObject(Parent.class, new JObjectKey("Parent3")).orElse(null);
var parent = curTx.get(Parent.class, new JObjectKey("Parent3")).orElse(null);
Assertions.assertEquals("John3", parent.getLastName());
txm.commit();
}
@@ -148,7 +148,7 @@ public class ObjectsTest {
barrier.await();
var newParent = alloc.create(Parent.class, new JObjectKey("Parent2"));
newParent.setLastName("John");
curTx.putObject(newParent);
curTx.put(newParent);
Log.warn("Thread 1 commit");
txm.commit();
thread1Failed.set(false);
@@ -164,7 +164,7 @@ public class ObjectsTest {
barrier.await();
var newParent = alloc.create(Parent.class, new JObjectKey("Parent2"));
newParent.setLastName("John2");
curTx.putObject(newParent);
curTx.put(newParent);
Log.warn("Thread 2 commit");
txm.commit();
thread2Failed.set(false);
@@ -177,7 +177,7 @@ public class ObjectsTest {
latch.await();
txm.begin();
var got = curTx.getObject(Parent.class, new JObjectKey("Parent2")).orElse(null);
var got = curTx.get(Parent.class, new JObjectKey("Parent2")).orElse(null);
txm.commit();
if (!thread1Failed.get()) {
@@ -198,7 +198,7 @@ public class ObjectsTest {
txm.begin();
var newParent = alloc.create(Parent.class, new JObjectKey(key));
newParent.setLastName("John3");
curTx.putObject(newParent);
curTx.put(newParent);
txm.commit();
}
@@ -213,7 +213,7 @@ public class ObjectsTest {
Log.warn("Thread 1");
txm.begin();
barrier.await();
var parent = curTx.getObject(Parent.class, new JObjectKey(key), strategy).orElse(null);
var parent = curTx.get(Parent.class, new JObjectKey(key), strategy).orElse(null);
parent.setLastName("John");
Log.warn("Thread 1 commit");
txm.commit();
@@ -228,7 +228,7 @@ public class ObjectsTest {
Log.warn("Thread 2");
txm.begin();
barrier.await();
var parent = curTx.getObject(Parent.class, new JObjectKey(key), strategy).orElse(null);
var parent = curTx.get(Parent.class, new JObjectKey(key), strategy).orElse(null);
parent.setLastName("John2");
Log.warn("Thread 2 commit");
txm.commit();
@@ -242,7 +242,7 @@ public class ObjectsTest {
latchEnd.await();
txm.begin();
var got = curTx.getObject(Parent.class, new JObjectKey(key)).orElse(null);
var got = curTx.get(Parent.class, new JObjectKey(key)).orElse(null);
txm.commit();
if (!thread1Failed.get()) {

View File

@@ -77,7 +77,7 @@ public class DhfsFileServiceImpl implements DhfsFileService {
private ChunkData createChunk(ByteString bytes) {
var newChunk = objectAllocator.create(ChunkData.class, new JObjectKey(UUID.randomUUID().toString()));
newChunk.setData(bytes);
curTx.putObject(newChunk);
curTx.put(newChunk);
return newChunk;
}
@@ -89,21 +89,21 @@ public class DhfsFileServiceImpl implements DhfsFileService {
private JKleppmannTreeNode getDirEntry(String name) {
var res = getTree().traverse(StreamSupport.stream(Path.of(name).spliterator(), false).map(p -> p.toString()).toList());
if (res == null) throw new StatusRuntimeExceptionNoStacktrace(Status.NOT_FOUND);
var ret = curTx.getObject(JKleppmannTreeNode.class, res).orElseThrow(() -> new StatusRuntimeException(Status.NOT_FOUND.withDescription("Tree node exists but not found as jObject: " + name)));
var ret = curTx.get(JKleppmannTreeNode.class, res).orElseThrow(() -> new StatusRuntimeException(Status.NOT_FOUND.withDescription("Tree node exists but not found as jObject: " + name)));
return ret;
}
private Optional<JKleppmannTreeNode> getDirEntryOpt(String name) {
var res = getTree().traverse(StreamSupport.stream(Path.of(name).spliterator(), false).map(p -> p.toString()).toList());
if (res == null) return Optional.empty();
var ret = curTx.getObject(JKleppmannTreeNode.class, res);
var ret = curTx.get(JKleppmannTreeNode.class, res);
return ret;
}
@Override
public Optional<GetattrRes> getattr(JObjectKey uuid) {
return jObjectTxManager.executeTx(() -> {
var ref = curTx.getObject(JData.class, uuid).orElse(null);
var ref = curTx.get(JData.class, uuid).orElse(null);
if (ref == null) return Optional.empty();
GetattrRes ret;
if (ref instanceof File f) {
@@ -159,7 +159,7 @@ public class DhfsFileServiceImpl implements DhfsFileService {
f.setCtime(f.getMtime());
f.setSymlink(false);
f.setChunks(new TreeMap<>());
curTx.putObject(f);
curTx.put(f);
try {
getTree().move(parent.getKey(), new JKleppmannTreeNodeMetaFile(fname, f.getKey()), getTree().getNewNodeId());
@@ -231,7 +231,7 @@ public class DhfsFileServiceImpl implements DhfsFileService {
@Override
public Boolean chmod(JObjectKey uuid, long mode) {
return jObjectTxManager.executeTx(() -> {
var dent = curTx.getObject(JData.class, uuid).orElseThrow(() -> new StatusRuntimeExceptionNoStacktrace(Status.NOT_FOUND));
var dent = curTx.get(JData.class, uuid).orElseThrow(() -> new StatusRuntimeExceptionNoStacktrace(Status.NOT_FOUND));
if (dent instanceof JKleppmannTreeNode) {
return true;
@@ -265,7 +265,7 @@ public class DhfsFileServiceImpl implements DhfsFileService {
if (offset < 0)
throw new StatusRuntimeException(Status.INVALID_ARGUMENT.withDescription("Offset should be more than zero: " + offset));
var file = curTx.getObject(File.class, fileUuid).orElse(null);
var file = curTx.get(File.class, fileUuid).orElse(null);
if (file == null) {
Log.error("File not found when trying to read: " + fileUuid);
return Optional.empty();
@@ -325,7 +325,7 @@ public class DhfsFileServiceImpl implements DhfsFileService {
}
private ByteString readChunk(JObjectKey uuid) {
var chunkRead = curTx.getObject(ChunkData.class, uuid).orElse(null);
var chunkRead = curTx.get(ChunkData.class, uuid).orElse(null);
if (chunkRead == null) {
Log.error("Chunk requested not found: " + uuid);
@@ -364,7 +364,7 @@ public class DhfsFileServiceImpl implements DhfsFileService {
throw new StatusRuntimeException(Status.INVALID_ARGUMENT.withDescription("Offset should be more than zero: " + offset));
// FIXME:
var file = curTx.getObject(File.class, fileUuid).orElse(null);
var file = curTx.get(File.class, fileUuid).orElse(null);
if (file == null) {
Log.error("File not found when trying to write: " + fileUuid);
return -1L;
@@ -515,7 +515,7 @@ public class DhfsFileServiceImpl implements DhfsFileService {
if (length < 0)
throw new StatusRuntimeException(Status.INVALID_ARGUMENT.withDescription("Length should be more than zero: " + length));
var file = curTx.getObject(File.class, fileUuid).orElse(null);
var file = curTx.get(File.class, fileUuid).orElse(null);
if (file == null) {
Log.error("File not found when trying to write: " + fileUuid);
return false;
@@ -602,7 +602,7 @@ public class DhfsFileServiceImpl implements DhfsFileService {
@Override
public ByteString readlinkBS(JObjectKey uuid) {
return jObjectTxManager.executeTx(() -> {
var fileOpt = curTx.getObject(File.class, uuid).orElseThrow(() -> new StatusRuntimeException(Status.NOT_FOUND.withDescription("File not found when trying to readlink: " + uuid)));
var fileOpt = curTx.get(File.class, uuid).orElseThrow(() -> new StatusRuntimeException(Status.NOT_FOUND.withDescription("File not found when trying to readlink: " + uuid)));
return read(uuid, 0, Math.toIntExact(size(uuid))).get();
});
}
@@ -635,7 +635,7 @@ public class DhfsFileServiceImpl implements DhfsFileService {
@Override
public Boolean setTimes(JObjectKey fileUuid, long atimeMs, long mtimeMs) {
return jObjectTxManager.executeTx(() -> {
var file = curTx.getObject(File.class, fileUuid).orElseThrow(
var file = curTx.get(File.class, fileUuid).orElseThrow(
() -> new StatusRuntimeException(Status.NOT_FOUND.withDescription(
"File not found for setTimes: " + fileUuid))
);
@@ -665,7 +665,7 @@ public class DhfsFileServiceImpl implements DhfsFileService {
@Override
public Long size(JObjectKey uuid) {
return jObjectTxManager.executeTx(() -> {
var read = curTx.getObject(File.class, uuid)
var read = curTx.get(File.class, uuid)
.orElseThrow(() -> new StatusRuntimeException(Status.NOT_FOUND));
return read.getSize();

View File

@@ -35,14 +35,14 @@ public class JKleppmannTreeManager {
public JKleppmannTree getTree(JObjectKey name) {
return txManager.executeTx(() -> {
var data = curTx.getObject(JKleppmannTreePersistentData.class, name).orElse(null);
var data = curTx.get(JKleppmannTreePersistentData.class, name).orElse(null);
if (data == null) {
data = objectAllocator.create(JKleppmannTreePersistentData.class, name);
data.setClock(new AtomicClock(1L));
data.setQueues(new HashMap<>());
data.setLog(new TreeMap<>());
data.setPeerTimestampLog(new HashMap<>());
curTx.putObject(data);
curTx.put(data);
}
return new JKleppmannTree(data);
// opObjectRegistry.registerObject(tree);
@@ -291,13 +291,13 @@ public class JKleppmannTreeManager {
private final PeerLogWrapper _peerLogWrapper = new PeerLogWrapper();
public JKleppmannTreeStorageInterface() {
if (curTx.getObject(JKleppmannTreeNode.class, getRootId()).isEmpty()) {
if (curTx.get(JKleppmannTreeNode.class, getRootId()).isEmpty()) {
var rootNode = objectAllocator.create(JKleppmannTreeNode.class, getRootId());
rootNode.setNode(new TreeNode<>(getRootId(), null, new JKleppmannTreeNodeMetaDirectory("")));
curTx.putObject(rootNode);
curTx.put(rootNode);
var trashNode = objectAllocator.create(JKleppmannTreeNode.class, getTrashId());
trashNode.setNode(new TreeNode<>(getTrashId(), null, new JKleppmannTreeNodeMetaDirectory("")));
curTx.putObject(trashNode);
curTx.put(trashNode);
}
}
@@ -318,7 +318,7 @@ public class JKleppmannTreeManager {
@Override
public JKleppmannTreeNodeWrapper getById(JObjectKey id) {
var got = curTx.getObject(JKleppmannTreeNode.class, id);
var got = curTx.get(JKleppmannTreeNode.class, id);
if (got.isEmpty()) return null;
return new JKleppmannTreeNodeWrapper(got.get());
}
@@ -327,7 +327,7 @@ public class JKleppmannTreeManager {
public JKleppmannTreeNodeWrapper createNewNode(TreeNode<Long, UUID, JKleppmannTreeNodeMeta, JObjectKey> node) {
var created = objectAllocator.create(JKleppmannTreeNode.class, node.getId());
created.setNode(node);
curTx.putObject(created);
curTx.put(created);
return new JKleppmannTreeNodeWrapper(created);
}