|
| 1 | +package convex.lattice.generic; |
| 2 | + |
| 3 | +import convex.core.data.ACell; |
| 4 | +import convex.core.data.AHashMap; |
| 5 | +import convex.core.data.Maps; |
| 6 | +import convex.core.data.SignedData; |
| 7 | +import convex.core.util.MergeFunction; |
| 8 | +import convex.lattice.ALattice; |
| 9 | + |
| 10 | +/** |
| 11 | + * Lattice implementation for owner-based signed data. |
| 12 | + * |
| 13 | + * The value is a Map from owner (ACell) to SignedData<V>, where each owner |
| 14 | + * has their own signed lattice value. The merge operation merges the maps, |
| 15 | + * and for each owner key, merges the SignedData values using SignedLattice |
| 16 | + * semantics. |
| 17 | + * |
| 18 | + * Signature validation for owners will be looked up later, so checkForeign |
| 19 | + * is lenient and only checks the structure. |
| 20 | + * |
| 21 | + * @param <V> Type of the underlying signed value |
| 22 | + */ |
| 23 | +public class OwnerLattice<V extends ACell> extends ALattice<AHashMap<ACell, SignedData<V>>> { |
| 24 | + |
| 25 | + /** |
| 26 | + * The child lattice for signed values |
| 27 | + */ |
| 28 | + protected final SignedLattice<V> signedLattice; |
| 29 | + |
| 30 | + /** |
| 31 | + * Merge function for SignedData values using SignedLattice |
| 32 | + */ |
| 33 | + protected final MergeFunction<SignedData<V>> mergeFunction; |
| 34 | + |
| 35 | + /** |
| 36 | + * Creates an OwnerLattice with the given child lattice for signed values |
| 37 | + * |
| 38 | + * @param valueLattice Lattice for the underlying value type V |
| 39 | + */ |
| 40 | + public OwnerLattice(ALattice<V> valueLattice) { |
| 41 | + this.signedLattice = SignedLattice.create(valueLattice); |
| 42 | + this.mergeFunction = (a, b) -> { |
| 43 | + return signedLattice.merge(a, b); |
| 44 | + }; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Creates an OwnerLattice with the given child lattice for signed values |
| 49 | + * |
| 50 | + * @param <V> Type of the underlying signed value |
| 51 | + * @param valueLattice Lattice for the underlying value type V |
| 52 | + * @return New OwnerLattice instance |
| 53 | + */ |
| 54 | + public static <V extends ACell> OwnerLattice<V> create(ALattice<V> valueLattice) { |
| 55 | + return new OwnerLattice<>(valueLattice); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public AHashMap<ACell, SignedData<V>> merge( |
| 60 | + AHashMap<ACell, SignedData<V>> ownValue, |
| 61 | + AHashMap<ACell, SignedData<V>> otherValue) { |
| 62 | + if (otherValue == null) { |
| 63 | + return ownValue; |
| 64 | + } |
| 65 | + if (ownValue == null) { |
| 66 | + if (checkForeign(otherValue)) { |
| 67 | + return otherValue; |
| 68 | + } |
| 69 | + return zero(); |
| 70 | + } |
| 71 | + |
| 72 | + // Merge the maps using the SignedLattice merge function for each owner |
| 73 | + return ownValue.mergeDifferences(otherValue, mergeFunction); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public AHashMap<ACell, SignedData<V>> zero() { |
| 78 | + return Maps.empty(); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public boolean checkForeign(AHashMap<ACell, SignedData<V>> value) { |
| 83 | + if (value == null) { |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + // Check that it's a valid HashMap |
| 88 | + // TODO: Signature validation for specific owners will be looked up later, |
| 89 | + // so we only check the structure here |
| 90 | + return (value instanceof AHashMap); |
| 91 | + } |
| 92 | + |
| 93 | + @SuppressWarnings("unchecked") |
| 94 | + @Override |
| 95 | + public <T extends ACell> ALattice<T> path(ACell childKey) { |
| 96 | + // For OwnerLattice, child paths are owner keys |
| 97 | + // Each owner's value is a SignedData<V>, which uses SignedLattice |
| 98 | + // Return the SignedLattice for child nodes |
| 99 | + return (ALattice<T>) signedLattice; |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments