Skip to content

Commit

Permalink
Dump more details when two objects at the same locations are read (#8802
Browse files Browse the repository at this point in the history
)
  • Loading branch information
JaroslavTulach authored Jan 19, 2024
1 parent 457eb22 commit 5d877ab
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
import java.util.logging.Logger;

final class PerGenerator {
private static final Logger LOG = Logger.getLogger(Persistance.class.getPackageName());

static final byte[] HEADER = new byte[] {0x0a, 0x0d, 0x03, 0x0f};
private final OutputStream main;
private final Map<Object, Integer> knownObjects = new IdentityHashMap<>();
Expand All @@ -33,7 +31,7 @@ private PerGenerator(
}

static byte[] writeObject(Object obj, Function<Object, Object> writeReplace) throws IOException {
var histogram = LOG.isLoggable(Level.FINE) ? new Histogram() : null;
var histogram = PerUtils.LOG.isLoggable(Level.FINE) ? new Histogram() : null;

var out = new ByteArrayOutputStream();
var data = new DataOutputStream(out);
Expand All @@ -50,7 +48,7 @@ static byte[] writeObject(Object obj, Function<Object, Object> writeReplace) thr
arr[11] = (byte) (at & 0xff);

if (histogram != null) {
histogram.dump(LOG, arr.length);
histogram.dump(PerUtils.LOG, arr.length);
}
return arr;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.logging.Level;
import org.enso.persist.Persistance.Input;
import org.enso.persist.Persistance.Reference;

Expand Down Expand Up @@ -202,19 +204,29 @@ static Object readIndirect(InputCache cache, PerMap map, Input in) throws IOExce
res = cache.readResolve().apply(res);
var prev = cache.cache().put(at, res);
if (prev != null) {
throw raise(
RuntimeException.class,
new IOException(
"Adding at "
+ at
+ " object: "
+ res.getClass().getName()
+ " but there already is "
+ prev.getClass().getName()));
var bothObjectsAreTheSame = Objects.equals(res, prev);
var sb = new StringBuilder();
sb.append("Adding at ").append(at).append(" object:\n ");
dumpObject(sb, res);
sb.append("\nbut there already is:\n ");
dumpObject(sb, prev);
sb.append("\nare they equal: ").append(bothObjectsAreTheSame);
var ex = new IOException(sb.toString());
if (bothObjectsAreTheSame) {
PerUtils.LOG.log(Level.WARNING, sb.toString(), ex);
} else {
throw raise(RuntimeException.class, ex);
}
}
return res;
}

private static void dumpObject(StringBuilder sb, Object obj) {
sb.append(obj.getClass().getName())
.append("@")
.append(Integer.toHexString(System.identityHashCode(obj)));
}

@SuppressWarnings("unchecked")
static <T> Reference<T> readIndirectAsReference(
InputCache buffer, PerMap map, Input in, Class<T> clazz) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package org.enso.persist;

import java.util.logging.Logger;

final class PerUtils {
private PerUtils() {}

static final Logger LOG = Logger.getLogger(Persistance.class.getPackageName());

@SuppressWarnings("unchecked")
static <E extends Throwable> E raise(Class<E> clazz, Throwable t) throws E {
throw (E) t;
Expand Down

0 comments on commit 5d877ab

Please sign in to comment.