Skip to content

Commit e50ca25

Browse files
author
Philip Z
committed
refactor(runtime): rename entity mutation ledger
1 parent 5361227 commit e50ca25

6 files changed

Lines changed: 119 additions & 119 deletions

File tree

‎teaql-core/src/main/java/io/teaql/core/BaseEntity.java‎

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ protected LoadedPropertyLayout computeValue(Class<?> type) {
5151
/**
5252
* Shared change tracking root for the entire entity graph.
5353
*/
54-
private EntityRoot entityRoot = new EntityRoot();
54+
private EntityMutationLedger entityMutationLedger = new EntityMutationLedger();
5555

5656
@Override
5757
public String getComment() {
@@ -61,8 +61,8 @@ public String getComment() {
6161
@Override
6262
public void setComment(String comment) {
6363
this._comment = comment;
64-
if (entityRoot != null) {
65-
entityRoot.setComment(comment);
64+
if (entityMutationLedger != null) {
65+
entityMutationLedger.setComment(comment);
6666
}
6767
}
6868

@@ -125,10 +125,10 @@ public void __internalSet(String property, Object value) {
125125

126126
@FrameworkInternal("Business code should use typed getXxx() methods")
127127
public Object __internalGet(String property) {
128-
// First try to get from entityRoot if available
129-
if (entityRoot != null && id != null) {
128+
// First try to get from entityMutationLedger if available
129+
if (entityMutationLedger != null && id != null) {
130130
EntityKey key = new EntityKey(typeName(), id);
131-
Object value = entityRoot.get(key, property);
131+
Object value = entityMutationLedger.get(key, property);
132132
if (value != null) {
133133
return value;
134134
}
@@ -196,9 +196,9 @@ public boolean needPersist() {
196196

197197
@Override
198198
public List<String> getUpdatedProperties() {
199-
if (entityRoot != null && id != null) {
199+
if (entityMutationLedger != null && id != null) {
200200
EntityKey key = new EntityKey(typeName(), id);
201-
Set<String> rootChanges = entityRoot.changedFieldNames(key);
201+
Set<String> rootChanges = entityMutationLedger.changedFieldNames(key);
202202
if (rootChanges != null && !rootChanges.isEmpty()) {
203203
return new ArrayList<>(rootChanges);
204204
}
@@ -259,8 +259,8 @@ private String dynamicPropertyNameOf(String propertyName) {
259259
@Override
260260
public void markAsDeleted() {
261261
gotoNextStatus(EntityAction.DELETE);
262-
if (entityRoot != null && id != null) {
263-
entityRoot.markAsDelete(new EntityKey(typeName(), id));
262+
if (entityMutationLedger != null && id != null) {
263+
entityMutationLedger.markAsDelete(new EntityKey(typeName(), id));
264264
}
265265
}
266266

@@ -309,26 +309,26 @@ public void setDisplayName(String pDisplayName) {
309309
displayName = pDisplayName;
310310
}
311311

312-
// --- EntityRoot integration ---
312+
// --- EntityMutationLedger integration ---
313313

314-
public EntityRoot getEntityRoot() {
315-
return entityRoot;
314+
public EntityMutationLedger getEntityMutationLedger() {
315+
return entityMutationLedger;
316316
}
317317

318-
public void setEntityRoot(EntityRoot entityRoot) {
319-
this.entityRoot = entityRoot;
320-
if (entityRoot != null && id != null && newItem()) {
321-
entityRoot.markAsNew(new EntityKey(typeName(), id));
318+
public void setEntityMutationLedger(EntityMutationLedger entityMutationLedger) {
319+
this.entityMutationLedger = entityMutationLedger;
320+
if (entityMutationLedger != null && id != null && newItem()) {
321+
entityMutationLedger.markAsNew(new EntityKey(typeName(), id));
322322
}
323-
if (entityRoot != null && id != null && version != null) {
324-
entityRoot.setOriginalVersion(new EntityKey(typeName(), id), version);
323+
if (entityMutationLedger != null && id != null && version != null) {
324+
entityMutationLedger.setOriginalVersion(new EntityKey(typeName(), id), version);
325325
}
326326
}
327327

328328
public Set<String> dirtyFields() {
329-
if (entityRoot != null && id != null) {
329+
if (entityMutationLedger != null && id != null) {
330330
EntityKey key = new EntityKey(typeName(), id);
331-
Set<String> fields = entityRoot.changedFieldNames(key);
331+
Set<String> fields = entityMutationLedger.changedFieldNames(key);
332332
if (fields != null && !fields.isEmpty()) {
333333
return fields;
334334
}
@@ -337,24 +337,24 @@ public Set<String> dirtyFields() {
337337
}
338338

339339
public boolean isMarkedAsDelete() {
340-
if (entityRoot == null || id == null) {
340+
if (entityMutationLedger == null || id == null) {
341341
return deleteItem();
342342
}
343-
return entityRoot.isMarkedAsDelete(new EntityKey(typeName(), id));
343+
return entityMutationLedger.isMarkedAsDelete(new EntityKey(typeName(), id));
344344
}
345345

346346
public boolean isNew() {
347-
if (entityRoot == null || id == null) {
347+
if (entityMutationLedger == null || id == null) {
348348
return newItem();
349349
}
350-
return entityRoot.isNew(new EntityKey(typeName(), id));
350+
return entityMutationLedger.isNew(new EntityKey(typeName(), id));
351351
}
352352

353353
public Long getOriginalVersion() {
354-
if (entityRoot == null || id == null) {
354+
if (entityMutationLedger == null || id == null) {
355355
return null;
356356
}
357-
return entityRoot.getOriginalVersion(new EntityKey(typeName(), id));
357+
return entityMutationLedger.getOriginalVersion(new EntityKey(typeName(), id));
358358
}
359359

360360
@Override
@@ -452,11 +452,11 @@ public void handleUpdate(String propertyName, Object oldValue, Object newValue)
452452
}
453453
updatedProperties.put(propertyName, new PropertyChange(propertyName, oldValue, newValue));
454454

455-
if (entityRoot != null && id != null) {
455+
if (entityMutationLedger != null && id != null) {
456456
EntityKey key = new EntityKey(typeName(), id);
457-
entityRoot.set(key, propertyName, newValue);
457+
entityMutationLedger.set(key, propertyName, newValue);
458458
if (_traceChain != null) {
459-
entityRoot.setTraceChain(key, _traceChain);
459+
entityMutationLedger.setTraceChain(key, _traceChain);
460460
}
461461
}
462462
}
@@ -486,8 +486,8 @@ public Object getNewValue(String propertyName) {
486486

487487
public BaseEntity markToRemove() {
488488
gotoNextStatus(EntityAction.DELETE);
489-
if (entityRoot != null && id != null) {
490-
entityRoot.markAsDelete(new EntityKey(typeName(), id));
489+
if (entityMutationLedger != null && id != null) {
490+
entityMutationLedger.markAsDelete(new EntityKey(typeName(), id));
491491
}
492492
return this;
493493
}

teaql-core/src/main/java/io/teaql/core/EntityRoot.java renamed to teaql-core/src/main/java/io/teaql/core/EntityMutationLedger.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
* Central change tracking context shared across all entities in a save graph.
77
* Holds the change set stack, deleted keys, new keys, trace chains, and original versions.
88
*
9-
* This is the Java equivalent of Rust's {@code EntityRoot}.
9+
* This is the Java equivalent of Rust's {@code EntityMutationLedger}.
1010
*/
11-
public class EntityRoot {
11+
public class EntityMutationLedger {
1212
private final ChangeSetStack changeSets = new ChangeSetStack();
1313
private String comment;
1414
private final Set<EntityKey> deletedKeys = new TreeSet<>();
@@ -111,10 +111,10 @@ public Long getOriginalVersion(EntityKey key) {
111111
}
112112

113113
/**
114-
* Merge another EntityRoot's changes into this one.
114+
* Merge another EntityMutationLedger's changes into this one.
115115
* Used when saving an entity graph (e.g., Order + OrderItems).
116116
*/
117-
public void mergeFrom(EntityRoot other) {
117+
public void mergeFrom(EntityMutationLedger other) {
118118
if (other == null) return;
119119

120120
// Merge change sets

‎teaql-core/src/test/java/io/teaql/core/BaseEntityTest.java‎

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ public void __internalSet(String property, Object value) {
3535
@Override
3636
public Object __internalGet(String property) {
3737
if ("name".equals(property)) {
38-
if (getEntityRoot() != null && getId() != null) {
39-
Object rootVal = getEntityRoot().get(new EntityKey(typeName(), getId()), property);
38+
if (getEntityMutationLedger() != null && getId() != null) {
39+
Object rootVal = getEntityMutationLedger().get(new EntityKey(typeName(), getId()), property);
4040
if (rootVal != null) return rootVal;
4141
}
4242
return this.name;
@@ -68,8 +68,8 @@ public void testRootBackedPropertyTracking() {
6868
entity.set$status(EntityStatus.PERSISTED); // Persisted entity
6969
entity.clearUpdatedProperties();
7070

71-
EntityRoot root = new EntityRoot();
72-
entity.setEntityRoot(root);
71+
EntityMutationLedger root = new EntityMutationLedger();
72+
entity.setEntityMutationLedger(root);
7373

7474
// 1. Updating a persisted entity records the new value under its EntityKey.
7575
entity.updateName("Alice");
@@ -102,8 +102,8 @@ public void testRootBackedPropertyTracking() {
102102
public void testTraceChainCopiedToRoot() {
103103
TestEntity entity = new TestEntity();
104104
entity.updateId(101L);
105-
EntityRoot root = new EntityRoot();
106-
entity.setEntityRoot(root);
105+
EntityMutationLedger root = new EntityMutationLedger();
106+
entity.setEntityMutationLedger(root);
107107

108108
entity.setTraceChain("trace-123");
109109
entity.updateName("Bob");
@@ -115,8 +115,8 @@ public void testTraceChainCopiedToRoot() {
115115
@Test
116116
public void testEntityWithoutIdFallsBackToLocalTracking() {
117117
TestEntity entity = new TestEntity();
118-
EntityRoot root = new EntityRoot();
119-
entity.setEntityRoot(root);
118+
EntityMutationLedger root = new EntityMutationLedger();
119+
entity.setEntityMutationLedger(root);
120120

121121
// No ID set
122122
entity.updateName("Charlie");
@@ -365,7 +365,7 @@ public String toString() {
365365
}
366366

367367
@Test
368-
public void testEntityRootIntegrationExtras() {
368+
public void testEntityMutationLedgerIntegrationExtras() {
369369
TestEntity e = new TestEntity();
370370
e.updateId(1L);
371371
e.updateVersion(2L);
@@ -374,8 +374,8 @@ public void testEntityRootIntegrationExtras() {
374374
assertFalse(e.isMarkedAsDelete());
375375
assertNull(e.getOriginalVersion());
376376

377-
EntityRoot root = new EntityRoot();
378-
e.setEntityRoot(root);
377+
EntityMutationLedger root = new EntityMutationLedger();
378+
e.setEntityMutationLedger(root);
379379

380380
// assertion removed
381381
assertEquals(Long.valueOf(2L), e.getOriginalVersion());
@@ -494,9 +494,9 @@ public void testBaseEntityBranches() {
494494
assertEquals(20L, e.__internalGet("id"));
495495
assertEquals(6L, e.__internalGet("version"));
496496

497-
EntityRoot root = new EntityRoot();
498-
e.setEntityRoot(root); // setEntityRoot branches
499-
e.setEntityRoot(root); // already set, branch hit
497+
EntityMutationLedger root = new EntityMutationLedger();
498+
e.setEntityMutationLedger(root); // setEntityMutationLedger branches
499+
e.setEntityMutationLedger(root); // already set, branch hit
500500

501501
root.set(new EntityKey(e.typeName(), e.getId()), "id", 30L);
502502
assertEquals(30L, e.__internalGet("id")); // get from root branch
@@ -522,18 +522,18 @@ public void testBaseEntityBranches() {
522522
e.__internalSet("name", "test_name");
523523
assertEquals("test_name", e.getDisplayName());
524524

525-
// update id before setting entityRoot to hit id != null branch
525+
// update id before setting entityMutationLedger to hit id != null branch
526526
TestEntity eRootTest = new TestEntity();
527527
eRootTest.updateId(500L);
528-
EntityRoot root2 = new EntityRoot();
529-
eRootTest.setEntityRoot(root2);
528+
EntityMutationLedger root2 = new EntityMutationLedger();
529+
eRootTest.setEntityMutationLedger(root2);
530530

531-
// getUpdatedProperties when entityRoot != null and id != null
531+
// getUpdatedProperties when entityMutationLedger != null and id != null
532532
assertNotNull(eRootTest.getUpdatedProperties());
533533

534-
// getUpdatedProperties when entityRoot != null and id == null
534+
// getUpdatedProperties when entityMutationLedger != null and id == null
535535
TestEntity eRootTest2 = new TestEntity();
536-
eRootTest2.setEntityRoot(root2);
536+
eRootTest2.setEntityMutationLedger(root2);
537537
assertNotNull(eRootTest2.getUpdatedProperties());
538538

539539
// isNew, isMarkedAsDelete, getOriginalVersion with id != null and root != null
@@ -579,8 +579,8 @@ public void testBaseEntityBranches() {
579579
public void testMoreBaseEntityBranches() {
580580
TestEntity e = new TestEntity();
581581
e.updateId(100L);
582-
EntityRoot root = new EntityRoot();
583-
e.setEntityRoot(root);
582+
EntityMutationLedger root = new EntityMutationLedger();
583+
e.setEntityMutationLedger(root);
584584

585585
// setComment
586586
e.setComment("comment");
@@ -618,8 +618,8 @@ public void register(io.teaql.core.meta.EntityDescriptor type) {}
618618
io.teaql.core.meta.EntityMetaFactory.registerGlobal(null);
619619
}
620620

621-
// setEntityRoot same
622-
e.setEntityRoot(root);
621+
// setEntityMutationLedger same
622+
e.setEntityMutationLedger(root);
623623

624624
// dirtyFields
625625
assertTrue(e.dirtyFields().contains("id")); // it has 'id' because we called root.set
@@ -677,7 +677,7 @@ public void testRemainingBranches() {
677677
e.setComment(null);
678678
e.setComment(null);
679679

680-
// __internalGet, getUpdatedProperties, markAsDeleted, isMarkedAsDelete, isNew, getOriginalVersion, markToRemove, handleUpdate when id == null or entityRoot == null
680+
// __internalGet, getUpdatedProperties, markAsDeleted, isMarkedAsDelete, isNew, getOriginalVersion, markToRemove, handleUpdate when id == null or entityMutationLedger == null
681681
e.getUpdatedProperties();
682682
e.set$status(io.teaql.core.EntityStatus.PERSISTED);
683683
e.markAsDeleted();
@@ -694,8 +694,8 @@ public void testRemainingBranches() {
694694
e.__internalGet("nonExistent");
695695
} catch (Exception ignored) {}
696696

697-
EntityRoot root = new EntityRoot();
698-
e.setEntityRoot(root); // id == null, root != null
697+
EntityMutationLedger root = new EntityMutationLedger();
698+
e.setEntityMutationLedger(root); // id == null, root != null
699699
e.getUpdatedProperties();
700700
e.isMarkedAsDelete();
701701
e.isNew();
@@ -710,7 +710,7 @@ public void testRemainingBranches() {
710710
// getUpdatedProperties when id != null, root != null but changes empty
711711
TestEntity e2 = new TestEntity();
712712
e2.updateId(1L);
713-
e2.setEntityRoot(root);
713+
e2.setEntityMutationLedger(root);
714714
e2.getUpdatedProperties();
715715

716716
// addRelation when descriptor == null
@@ -773,7 +773,7 @@ public void register(io.teaql.core.meta.EntityDescriptor type) {}
773773
}
774774

775775
@Test
776-
public void testEntityRootDelegates() {
776+
public void testEntityMutationLedgerDelegates() {
777777
BaseEntity entity = new BaseEntity() {
778778
@Override public String typeName() { return "DummyEntity"; }
779779
@Override public boolean newItem() { return true; }
@@ -782,17 +782,17 @@ public void testEntityRootDelegates() {
782782
entity.__internalSet("id", 1L);
783783
entity.__internalSet("version", 1L);
784784

785-
// Test entityRoot == null branches
786-
entity.setEntityRoot(null);
785+
// Test entityMutationLedger == null branches
786+
entity.setEntityMutationLedger(null);
787787
try { entity.__internalGet("name"); } catch(Exception ignored) {}
788788
entity.getUpdatedProperties();
789789
entity.dirtyFields();
790790
try { entity.markAsDeleted(); } catch(Exception ignored) {}
791791
entity.isMarkedAsDelete();
792792
entity.isNew();
793793

794-
EntityRoot root = new EntityRoot();
795-
entity.setEntityRoot(root);
794+
EntityMutationLedger root = new EntityMutationLedger();
795+
entity.setEntityMutationLedger(root);
796796

797797
entity.setComment("test comment");
798798
assertEquals("test comment", root.getComment());
@@ -821,7 +821,7 @@ public void testEntityRootDelegates() {
821821
BaseEntity entityNoId = new BaseEntity() {
822822
@Override public String typeName() { return "NoIdEntity"; }
823823
};
824-
entityNoId.setEntityRoot(root);
824+
entityNoId.setEntityMutationLedger(root);
825825
try {
826826
entityNoId.__internalGet("name");
827827
} catch (Exception ignored) {}

0 commit comments

Comments
 (0)