Skip to content

Commit

Permalink
Refactor utility functions from convex.core.data.Format => Cells
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Nov 27, 2024
1 parent 8758719 commit b4263b0
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public void validateCell() throws InvalidDataException {
// OK?
((AOp<?>) command).validateCell();
} else {
if (!Format.isCanonical(command)) throw new InvalidDataException("Non-canonical object as command?", this);
if (!Cells.isCanonical(command)) throw new InvalidDataException("Non-canonical object as command?", this);
}
}

Expand Down
24 changes: 24 additions & 0 deletions convex-core/src/main/java/convex/core/data/Cells.java
Original file line number Diff line number Diff line change
Expand Up @@ -321,4 +321,28 @@ public static Hash cachedHash(ACell k) {
return k.cachedHash();
}

/**
* Returns true if the object is a canonical data object. Canonical data objects
* can be used as first class decentralised data objects.
*
* @param o Value to test
* @return true if object is canonical, false otherwise.
*/
public static boolean isCanonical(ACell o) {
if (o==null) return true;
return o.isCanonical();
}

/**
* Determines if an object should be embedded directly in the encoding rather
* than referenced with a Ref / hash. Defined to be true for most small objects.
*
* @param cell Value to test
* @return true if object is embedded, false otherwise
*/
public static boolean isEmbedded(ACell cell) {
if (cell == null) return true;
return cell.isEmbedded();
}

}
24 changes: 0 additions & 24 deletions convex-core/src/main/java/convex/core/data/Format.java
Original file line number Diff line number Diff line change
Expand Up @@ -714,30 +714,6 @@ private static <T extends ACell> T readTransaction(byte tag, Blob b, int pos) th
return (T) dr;
}

/**
* Returns true if the object is a canonical data object. Canonical data objects
* can be used as first class decentralised data objects.
*
* @param o Value to test
* @return true if object is canonical, false otherwise.
*/
public static boolean isCanonical(ACell o) {
if (o==null) return true;
return o.isCanonical();
}

/**
* Determines if an object should be embedded directly in the encoding rather
* than referenced with a Ref / hash. Defined to be true for most small objects.
*
* @param cell Value to test
* @return true if object is embedded, false otherwise
*/
public static boolean isEmbedded(ACell cell) {
if (cell == null) return true;
return cell.isEmbedded();
}

/**
* Gets the encoded Blob for an object in canonical message format
*
Expand Down
2 changes: 1 addition & 1 deletion convex-core/src/main/java/convex/core/data/RefDirect.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public boolean equals(Ref<T> a) {
@Override
public void validate() throws InvalidDataException {
super.validate();
if (isEmbedded() != Format.isEmbedded(value)) throw new InvalidDataException("Embedded flag is wrong!", this);
if (isEmbedded() != Cells.isEmbedded(value)) throw new InvalidDataException("Embedded flag is wrong!", this);
if (value == null) {
if (this != Ref.NULL_VALUE) throw new InvalidDataException("Null Ref not singleton!", this);
}
Expand Down
2 changes: 1 addition & 1 deletion convex-core/src/main/java/convex/core/data/RefSoft.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public void validate() throws InvalidDataException {
if (hash == null) throw new InvalidDataException("Hash should never be null in soft ref", this);
ACell val = softRef.get();
boolean embedded=isEmbedded();
if (embedded!=Format.isEmbedded(val)) {
if (embedded!=Cells.isEmbedded(val)) {
throw new InvalidDataException("Embedded flag ["+embedded+"] inconsistent with value", this);
}
}
Expand Down
2 changes: 1 addition & 1 deletion convex-core/src/test/java/convex/comms/GenTestFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void messageRoundTrip(String str) throws BadFormatException {
@Property
public void primitiveRoundTrip(@From(PrimitiveGen.class) ACell prim) throws BadFormatException, IOException {
Blob b = Format.encodedBlob(prim);
if (!Format.isEmbedded(prim)) {
if (!Cells.isEmbedded(prim)) {
// persist in case large
Cells.persist(prim);
}
Expand Down
10 changes: 5 additions & 5 deletions convex-core/src/test/java/convex/core/data/EncodingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private void doVLQLongTest(long x) throws BadFormatException {
Blob b=Format.encodedBlob(k);
ACell o=Format.read(b);
assertEquals(k,o);
assertTrue(Format.isEmbedded(k));
assertTrue(Cells.isEmbedded(k));
Ref<?> r=Ref.get(o);
assertTrue(r.isDirect());
}
Expand Down Expand Up @@ -178,10 +178,10 @@ private void doVLQLongTest(long x) throws BadFormatException {
}

@Test public void testCanonical() {
assertTrue(Format.isCanonical(Vectors.empty()));
assertTrue(Format.isCanonical(null));
assertTrue(Format.isCanonical(RT.cvm(1)));
assertTrue(Format.isCanonical(Blob.create(new byte[1000]))); // should be OK
assertTrue(Cells.isCanonical(Vectors.empty()));
assertTrue(Cells.isCanonical(null));
assertTrue(Cells.isCanonical(RT.cvm(1)));
assertTrue(Cells.isCanonical(Blob.create(new byte[1000]))); // should be OK
assertFalse(Blob.create(new byte[10000]).isCanonical()); // too big to be canonical
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private static void doFuzzTest(Blob b) throws BadFormatException {

// if we manage to read the object and it is not a Ref, it must be in canonical
// format!
assertTrue(Format.isCanonical(v),()->"Not canonical: "+Utils.getClassName(v));
assertTrue(Cells.isCanonical(v),()->"Not canonical: "+Utils.getClassName(v));

Blob b2 = Format.encodedBlob(v);
assertEquals(v, Format.read(b2),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void testFuzzing(@From(ValueGen.class) ACell o) throws InvalidDataExcepti

@Property
public void validEmbedded(@From(ValueGen.class) ACell o) throws InvalidDataException, BadFormatException, IOException {
if (Format.isEmbedded(o)) {
if (Cells.isEmbedded(o)) {
Cells.persist(o); // NOTE: may have child refs to persist

Blob data=Format.encodedBlob(o);
Expand All @@ -84,7 +84,7 @@ public void validEmbedded(@From(ValueGen.class) ACell o) throws InvalidDataExcep
assertEquals(o,o2);
AArrayBlob data2=Format.encodedBlob(o2);
assertEquals(data,data2);
assertTrue(Format.isEmbedded(o2));
assertTrue(Cells.isEmbedded(o2));

// when we persist a ref to an embedded object, should be the object itself
Ref<ACell> ref=Ref.get(o);
Expand Down Expand Up @@ -113,7 +113,7 @@ public void dataRoundTrip(@From(ValueGen.class) ACell o) throws BadFormatExcepti

// re-read data, should be canonical
ACell o2=Format.read(data);
assertTrue(Format.isCanonical(o2));
assertTrue(Cells.isCanonical(o2));

// equality checks
assertEquals(o,o2);
Expand Down
4 changes: 2 additions & 2 deletions convex-core/src/test/java/convex/core/data/ObjectsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ private static void doAnyEncodingTests(ACell a) {

// If length exceeds MAX_EMBEDDED_LENGTH, cannot be an embedded value
if (encoding.count > Format.MAX_EMBEDDED_LENGTH) {
assertFalse(Format.isEmbedded(a),()->"Should not be embedded: "+Utils.getClassName(a)+ " = "+Utils.toString(a));
assertFalse(Cells.isEmbedded(a),()->"Should not be embedded: "+Utils.getClassName(a)+ " = "+Utils.toString(a));
}

try {
Expand Down Expand Up @@ -315,7 +315,7 @@ private static void doCanonicalTests(ACell a) {
long cms=childRef.getMemorySize();
childMem+=cms;
}
boolean embedded=Format.isEmbedded(a);
boolean embedded=Cells.isEmbedded(a);
if (embedded) {
assertEquals(memorySize,childMem);
} else {
Expand Down

0 comments on commit b4263b0

Please sign in to comment.