t) {
- return aquire(() -> {
- try {
- return t.get();
- } catch (Throwable e) {
- IrisLogging.reportError("Atomic cache supplier failed.", e);
- return null;
- }
- });
- }
-
- /**
- * Like {@link #aquire(Supplier)} but propagates a supplier failure to the caller instead
- * of swallowing it into a null return. For values that are mandatory: a caller of a
- * "@NotNull" accessor should see the supplier's real exception, not a downstream NPE.
- *
- * The supplier is retried on every call until it produces a value, which is what a caller that can
- * repair the cause between attempts needs. Use {@link #aquireOnceOrThrow(Supplier)} when re-running a
- * failed supplier is itself the problem.
- */
- public T aquireOrThrow(Supplier t) {
- Object v = value;
-
- if (v != null) {
- return unwrap(v);
- }
-
- synchronized (initLock) {
- v = value;
-
- if (v != null) {
- return unwrap(v);
- }
-
- T computed = t.get();
- if (computed == null) {
- throw new IllegalStateException("Atomic cache supplier produced null");
- }
- value = computed;
- return computed;
- }
- }
-
- /**
- * Like {@link #aquireOrThrow(Supplier)} but memoizes the failure as well as the value, so a supplier
- * that cannot produce one is run exactly once and every later caller is told the same reason. For
- * values whose supplier rebuilds shared state, where re-running it per call repeats that work and
- * hides the original cause behind whatever the retry happens to fail on. {@link #reset()} clears it.
- */
- public T aquireOnceOrThrow(Supplier t) {
- Object v = value;
-
- if (v != null) {
- return unwrap(v);
- }
- rethrowFailure(failure);
-
- synchronized (initLock) {
- v = value;
-
- if (v != null) {
- return unwrap(v);
- }
- rethrowFailure(failure);
-
- T computed;
- try {
- computed = t.get();
- if (computed == null) {
- throw new IllegalStateException("Atomic cache supplier produced null");
- }
- } catch (Throwable e) {
- failure = e;
- throw e;
- }
- value = computed;
- return computed;
- }
- }
-
- private static void rethrowFailure(Throwable memoized) {
- if (memoized == null) {
- return;
- }
- if (memoized instanceof RuntimeException runtimeFailure) {
- throw runtimeFailure;
- }
- if (memoized instanceof Error error) {
- throw error;
- }
- throw new IllegalStateException("Atomic cache supplier failed", memoized);
- }
-
- public T aquire(Supplier t) {
- Object v = value;
-
- if (v != null) {
- return unwrap(v);
- }
-
- synchronized (initLock) {
- v = value;
-
- if (v != null) {
- return unwrap(v);
- }
-
- try {
- T computed = t.get();
-
- if (computed != null) {
- value = computed;
- return computed;
- }
-
- if (nullSupport) {
- value = NULL_VALUE;
- }
- } catch (Throwable e) {
- // aquire retries on every call, so a supplier that stays broken is reached per sample.
- // The first statement of it carries the stack; the rest are traces of the same cause.
- if (IrisLogging.warnOnce("atomic-cache:" + e.getClass().getName() + ":" + e.getMessage(),
- "Atomic cache supplier failed: %s: %s", e.getClass().getSimpleName(), e.getMessage())) {
- IrisLogging.reportError(e);
- }
- }
-
- return null;
- }
- }
-
- @SuppressWarnings("unchecked")
- private T unwrap(Object v) {
- return v == null || v == NULL_VALUE ? null : (T) v;
- }
-}
diff --git a/core/src/main/java/art/arcane/iris/engine/data/cache/AtomicCacheFailureReporting.java b/core/src/main/java/art/arcane/iris/engine/data/cache/AtomicCacheFailureReporting.java
new file mode 100644
index 000000000..40b18fb9f
--- /dev/null
+++ b/core/src/main/java/art/arcane/iris/engine/data/cache/AtomicCacheFailureReporting.java
@@ -0,0 +1,51 @@
+/*
+ * Iris is a World Generator for Minecraft Bukkit Servers
+ * Copyright (c) 2022 Arcane Arts (Volmit Software)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+
+package art.arcane.iris.engine.data.cache;
+
+import art.arcane.iris.spi.IrisLogging;
+import art.arcane.volmlib.util.cache.AtomicCache;
+import art.arcane.volmlib.util.cache.AtomicCacheFailureHandler;
+
+/**
+ * Sends the failures of {@link AtomicCache} suppliers to Iris's logging and error reporting. Bound once at startup;
+ * it needs no platform of its own because {@link IrisLogging} falls back to the process streams when none is bound.
+ */
+public final class AtomicCacheFailureReporting implements AtomicCacheFailureHandler {
+ private AtomicCacheFailureReporting() {
+ }
+
+ public static void bind() {
+ AtomicCache.setFailureHandler(new AtomicCacheFailureReporting());
+ }
+
+ @Override
+ public void supplierFailed(Throwable failure) {
+ IrisLogging.reportError("Atomic cache supplier failed.", failure);
+ }
+
+ @Override
+ public void optionalSupplierFailed(Throwable failure) {
+ // aquire retries on every call, so a supplier that stays broken is reached per sample.
+ // The first statement of it carries the stack; the rest are traces of the same cause.
+ if (IrisLogging.warnOnce("atomic-cache:" + failure.getClass().getName() + ":" + failure.getMessage(),
+ "Atomic cache supplier failed: %s: %s", failure.getClass().getSimpleName(), failure.getMessage())) {
+ IrisLogging.reportError(failure);
+ }
+ }
+}
diff --git a/core/src/main/java/art/arcane/iris/engine/data/cache/Cache.java b/core/src/main/java/art/arcane/iris/engine/data/cache/Cache.java
deleted file mode 100644
index acc1879e7..000000000
--- a/core/src/main/java/art/arcane/iris/engine/data/cache/Cache.java
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * Iris is a World Generator for Minecraft Bukkit Servers
- * Copyright (c) 2022 Arcane Arts (Volmit Software)
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see .
- */
-
-package art.arcane.iris.engine.data.cache;
-
-import art.arcane.volmlib.util.cache.CacheKey;
-
-public interface Cache {
- static long key(int x, int z) {
- return CacheKey.key(x, z);
- }
-
- static int keyX(long key) {
- return CacheKey.keyX(key);
- }
-
- static int keyZ(long key) {
- return CacheKey.keyZ(key);
- }
-
- static int to1D(int x, int y, int z, int w, int h) {
- return CacheKey.to1D(x, y, z, w, h);
- }
-
- static int[] to3D(int idx, int w, int h) {
- return CacheKey.to3D(idx, w, h);
- }
-
- int getId();
-
- V get(int x, int z);
-}
diff --git a/core/src/main/java/art/arcane/iris/util/common/data/VectorMap.java b/core/src/main/java/art/arcane/iris/util/common/data/VectorMap.java
deleted file mode 100644
index 770f8753a..000000000
--- a/core/src/main/java/art/arcane/iris/util/common/data/VectorMap.java
+++ /dev/null
@@ -1,331 +0,0 @@
-package art.arcane.iris.util.common.data;
-
-import art.arcane.volmlib.util.collection.KMap;
-import lombok.NonNull;
-import art.arcane.iris.util.common.math.IrisBlockVector;
-import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
-
-import java.util.Iterator;
-import java.util.Map;
-import java.util.NoSuchElementException;
-import java.util.function.BiConsumer;
-import java.util.function.Function;
-
-public class VectorMap implements Iterable> {
- private final Map> map = new KMap<>();
- private transient volatile long modificationRevision;
-
- public long modificationRevision() {
- return modificationRevision;
- }
-
- public int size() {
- return map.values().stream().mapToInt(Map::size).sum();
- }
-
- public boolean isEmpty() {
- return map.values().stream().allMatch(Map::isEmpty);
- }
-
- public boolean containsKey(@NonNull IrisBlockVector vector) {
- if (map.isEmpty()) return false;
- var chunk = map.get(chunk(vector));
- return chunk != null && chunk.containsKey(relative(vector));
- }
-
- public boolean containsValue(@NonNull T value) {
- return map.values().stream().anyMatch(m -> m.containsValue(value));
- }
-
- public @Nullable T get(@NonNull IrisBlockVector vector) {
- if (map.isEmpty()) return null;
- var chunk = map.get(chunk(vector));
- return chunk == null ? null : chunk.get(relative(vector));
- }
-
- public @Nullable T put(@NonNull IrisBlockVector vector, @NonNull T value) {
- T previous = map.computeIfAbsent(chunk(vector), k -> new KMap<>())
- .put(relative(vector), value);
- modificationRevision++;
- return previous;
- }
-
- public @Nullable T computeIfAbsent(@NonNull IrisBlockVector vector, @NonNull Function<@NonNull IrisBlockVector, @NonNull T> mappingFunction) {
- boolean[] inserted = new boolean[1];
- T value = map.computeIfAbsent(chunk(vector), key -> new KMap<>())
- .computeIfAbsent(relative(vector), key -> {
- inserted[0] = true;
- return mappingFunction.apply(vector);
- });
- if (inserted[0]) {
- modificationRevision++;
- }
- return value;
- }
-
- @SuppressWarnings("unchecked")
- public @Nullable T remove(@NonNull IrisBlockVector vector) {
- if (map.isEmpty()) return null;
- Key relative = relative(vector);
- Object[] removed = new Object[1];
-
- // computeIfPresent so the emptied bucket is pruned atomically against a concurrent put.
- map.computeIfPresent(chunk(vector), (key, chunk) -> {
- removed[0] = chunk.remove(relative);
- return chunk.isEmpty() ? null : chunk;
- });
-
- if (removed[0] != null) {
- modificationRevision++;
- }
-
- return (T) removed[0];
- }
-
- public void putAll(@NonNull VectorMap map) {
- map.forEach(this::put);
- }
-
- public void clear() {
- if (!map.isEmpty()) {
- modificationRevision++;
- }
- map.clear();
- }
-
- public void forEach(@NonNull BiConsumer<@NonNull IrisBlockVector, @NonNull T> consumer) {
- map.forEach((chunk, values) -> {
- int rX = chunk.x << 10;
- int rY = chunk.y << 10;
- int rZ = chunk.z << 10;
-
- values.forEach((relative, value) -> consumer.accept(
- relative.resolve(rX, rY, rZ),
- value
- ));
- });
- }
-
- private static Key chunk(IrisBlockVector vector) {
- return new Key(vector.getBlockX() >> 10, vector.getBlockY() >> 10, vector.getBlockZ() >> 10);
- }
-
- private static Key relative(IrisBlockVector vector) {
- return new Key(vector.getBlockX() & 0x3FF, vector.getBlockY() & 0x3FF, vector.getBlockZ() & 0x3FF);
- }
-
- @Override
- public @NotNull EntryIterator iterator() {
- return new EntryIterator();
- }
-
- /**
- * Allocation-free entry walk. {@link EntryIterator} allocates a resolved vector plus a Map.Entry per element;
- * the cursor resolves into one reused vector instead. Only valid for callers that do not retain the vector
- * returned by {@link Cursor#key()} beyond the current step - clone it if it must outlive the next
- * {@link Cursor#next()}.
- */
- public @NotNull Cursor cursor() {
- return new Cursor();
- }
-
- public @NotNull KeyIterator keys() {
- return new KeyIterator();
- }
-
- public @NotNull ValueIterator values() {
- return new ValueIterator();
- }
-
- public final class Cursor {
- private final Iterator>> chunkIterator = map.entrySet().iterator();
- private final IrisBlockVector position = new IrisBlockVector(0, 0, 0);
- private Iterator> relativeIterator;
- private int rX, rY, rZ;
- private T value;
-
- public boolean next() {
- while (relativeIterator == null || !relativeIterator.hasNext()) {
- if (!chunkIterator.hasNext()) {
- value = null;
- return false;
- }
-
- Map.Entry> chunk = chunkIterator.next();
- rX = chunk.getKey().x << 10;
- rY = chunk.getKey().y << 10;
- rZ = chunk.getKey().z << 10;
- relativeIterator = chunk.getValue().entrySet().iterator();
- }
-
- Map.Entry entry = relativeIterator.next();
- Key relative = entry.getKey();
- position.setX(rX + relative.x);
- position.setY(rY + relative.y);
- position.setZ(rZ + relative.z);
- value = entry.getValue();
- return true;
- }
-
- /**
- * The position of the current element. The same instance is returned every step.
- */
- public @NotNull IrisBlockVector key() {
- return position;
- }
-
- public T value() {
- return value;
- }
- }
-
- public class EntryIterator implements Iterator> {
- private final Iterator>> chunkIterator = map.entrySet().iterator();
- private Iterator> relativeIterator;
- private int rX, rY, rZ;
-
- @Override
- public boolean hasNext() {
- return advance();
- }
-
- @Override
- public Map.Entry next() {
- if (!advance()) throw new NoSuchElementException();
-
- var entry = relativeIterator.next();
- return Map.entry(entry.getKey().resolve(rX, rY, rZ), entry.getValue());
- }
-
- private boolean advance() {
- while (relativeIterator == null || !relativeIterator.hasNext()) {
- if (!chunkIterator.hasNext()) return false;
- var chunk = chunkIterator.next();
- rX = chunk.getKey().x << 10;
- rY = chunk.getKey().y << 10;
- rZ = chunk.getKey().z << 10;
- relativeIterator = chunk.getValue().entrySet().iterator();
- }
-
- return true;
- }
-
- @Override
- public void remove() {
- if (relativeIterator == null) throw new IllegalStateException("No element to remove");
- relativeIterator.remove();
- modificationRevision++;
- }
- }
-
- public class KeyIterator implements Iterator, Iterable {
- private final Iterator>> chunkIterator = map.entrySet().iterator();
- private Iterator relativeIterator;
- private int rX, rY, rZ;
-
- @Override
- public boolean hasNext() {
- return advance();
- }
-
- @Override
- public IrisBlockVector next() {
- if (!advance()) throw new NoSuchElementException();
-
- return relativeIterator.next().resolve(rX, rY, rZ);
- }
-
- private boolean advance() {
- while (relativeIterator == null || !relativeIterator.hasNext()) {
- if (!chunkIterator.hasNext()) return false;
- var chunk = chunkIterator.next();
- rX = chunk.getKey().x << 10;
- rY = chunk.getKey().y << 10;
- rZ = chunk.getKey().z << 10;
- relativeIterator = chunk.getValue().keySet().iterator();
- }
-
- return true;
- }
-
- @Override
- public void remove() {
- if (relativeIterator == null) throw new IllegalStateException("No element to remove");
- relativeIterator.remove();
- modificationRevision++;
- }
-
- @Override
- public @NotNull Iterator iterator() {
- return this;
- }
- }
-
- public class ValueIterator implements Iterator, Iterable {
- private final Iterator