Skip to content

Commit

Permalink
Provide Persistance for Persistance.Reference (#9326)
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach authored Mar 8, 2024
1 parent df72b38 commit 2330fdb
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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;
Expand Down Expand Up @@ -68,7 +69,37 @@ public void identifiedLocationNoUUID() throws Exception {
}

@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) ->
Expand Down Expand Up @@ -470,4 +501,11 @@ public record ServiceSupply(Service supply) {}

@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));
}
}
}
26 changes: 25 additions & 1 deletion lib/java/persistance/src/main/java/org/enso/persist/PerMap.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<>();
Expand Down Expand Up @@ -95,4 +100,23 @@ final Persistance<?> forId(int id) {
}
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);
}
}
}

0 comments on commit 2330fdb

Please sign in to comment.