Provide Persistance for Persistance.Reference (#9326)

This commit is contained in:
Jaroslav Tulach 2024-03-08 18:23:17 +01:00 committed by GitHub
parent df72b38f60
commit 2330fdb8af
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 64 additions and 2 deletions

View File

@ -3,6 +3,7 @@ package org.enso.compiler.core;
import static org.junit.Assert.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Objects;
import java.util.UUID;
@ -68,7 +69,37 @@ public class IrPersistanceTest {
}
@Test
public void idHolderNoUUID() throws Exception {
public void refHolderWithUUID() throws Exception {
var id = UUID.randomUUID();
var il = new RefHolder(id);
var in = serde(RefHolder.class, il, -1, null);
assertEquals("UUID is the same", id, in.id().get(UUID.class));
}
@Test
public void twiceHolderWithUUID() throws Exception {
var id = UUID.randomUUID();
var il = new RefHolder(id);
var two = join(il, join(il, nil()));
;
var in = serde(List.class, two, -1, null);
assertEquals("Two elements", 2, in.size());
var both = new ArrayList<RefHolder>();
var it = in.iterator();
while (it.hasNext()) {
var elem = (RefHolder) it.next();
assertEquals("UUID is the same", id, elem.id().get(UUID.class));
both.add(elem);
}
var first = both.get(0);
var second = both.get(1);
assertSame("Holders are the same", first, second);
assertSame("Values are the same", first.id(), second.id());
}
@Test
public void refHolderNoUUID() throws Exception {
var il = new IdHolder(UUID.randomUUID());
Function<Object, Object> fn =
(obj) ->
@ -470,4 +501,11 @@ public class IrPersistanceTest {
@Persistable(clazz = IdHolder.class, id = 432876)
public record IdHolder(UUID id) {}
@Persistable(clazz = RefHolder.class, id = 436872)
public record RefHolder(Persistance.Reference<UUID> id) {
RefHolder(UUID id) {
this(Persistance.Reference.of(id));
}
}
}

View File

@ -1,9 +1,11 @@
package org.enso.persist;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.enso.persist.Persistance.Reference;
import org.openide.util.lookup.Lookups;
final class PerMap {
@ -14,7 +16,10 @@ final class PerMap {
static {
var loader = PerMap.class.getClassLoader();
var lookup = Lookups.metaInfServices(loader);
ALL = lookup.lookupAll(Persistance.class);
var all = new ArrayList<Persistance>();
all.add(ReferencePersitance.INSTANCE);
all.addAll(lookup.lookupAll(Persistance.class));
ALL = all;
}
private final Map<Integer, Persistance<?>> ids = new HashMap<>();
@ -95,4 +100,23 @@ final class PerMap {
}
return p;
}
private static class ReferencePersitance extends Persistance<Persistance.Reference> {
private static final ReferencePersitance INSTANCE = new ReferencePersitance();
private ReferencePersitance() {
super(Reference.class, true, 60941);
}
@Override
@SuppressWarnings("unchecked")
protected void writeObject(Reference obj, Output out) throws IOException {
out.writeObject(obj.get(Object.class));
}
@Override
protected Reference readObject(Input in) throws IOException, ClassNotFoundException {
return in.readReference(Object.class);
}
}
}