Skip to content

Commit

Permalink
chore(spatial): add logging to the storage handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Elenterius committed Dec 7, 2023
1 parent 4ecc094 commit 9d71212
Showing 1 changed file with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.saveddata.SavedData;
import net.minecraft.world.level.storage.LevelResource;
import org.apache.logging.log4j.MarkerManager;
import org.h2.mvstore.MVMap;
import org.h2.mvstore.MVStore;
import org.h2.mvstore.rtree.MVRTreeMap;
Expand All @@ -19,6 +20,8 @@

public final class SpatialShapeStorage extends SavedData implements AutoCloseable {

public static final String LOG_MARKER = "SpatialStore";

public static final LevelResource DATA_DIR = new LevelResource("data");

private final MVStore store;
Expand All @@ -27,6 +30,7 @@ public final class SpatialShapeStorage extends SavedData implements AutoCloseabl

private SpatialShapeStorage(String filePath) {
store = new MVStore.Builder().fileName(filePath).cacheSize(8).open();
BiomancyMod.LOGGER.debug(MarkerManager.getMarker(LOG_MARKER), "initialized database using file {}", filePath);
}

@SuppressWarnings("unused")
Expand All @@ -41,15 +45,15 @@ public static SpatialShapeStorage getInstance(ServerLevel level) {
return level.getServer()
.overworld()
.getDataStorage()
.computeIfAbsent(tag -> load(tag, path), () -> new SpatialShapeStorage(path), "biomancy_spatial");
.computeIfAbsent(tag -> load(tag, path), () -> new SpatialShapeStorage(path), "biomancy.spatial");
}

@Override
public void close() {
levelTrees.clear();
levelShapes.clear();

BiomancyMod.LOGGER.debug("compacting & saving spatial database");
BiomancyMod.LOGGER.debug(MarkerManager.getMarker(LOG_MARKER), "compacting & saving database");
store.close(250);
}

Expand Down Expand Up @@ -95,7 +99,10 @@ public Shape getOrCreate(String levelKey, long shapeId, Supplier<Shape> factory)
shapes.put(shapeId, shape);
tree.add(new SpatialKey(shapeId, shape.getAABB()), shapeId);

BiomancyMod.LOGGER.debug(MarkerManager.getMarker(LOG_MARKER), "created id={} shape={}", shapeId, shape);

setDirty();

return shape;
}

Expand All @@ -106,8 +113,12 @@ public void remove(String levelKey, long shapeId) {
Shape shape = shapes.get(shapeId);
if (shape == null) return;

tree.remove(new SpatialKey(shapeId, shape.getAABB()));
Long removed = tree.remove(new SpatialKey(shapeId, shape.getAABB()));
shapes.remove(shapeId);

BiomancyMod.LOGGER.debug(MarkerManager.getMarker(LOG_MARKER), "removed id={} shape={}", removed, shape);

setDirty();
}

@Override
Expand All @@ -120,9 +131,19 @@ public void save(File file) {
super.save(file);

if (!store.isClosed()) {
store.commit(); //flush changes to file
// BiomancyMod.LOGGER.debug("compacting spatial database");
// store.compactFile(10);
if (store.hasUnsavedChanges()) {
long version = store.commit();

if (BiomancyMod.LOGGER.isDebugEnabled()) {
String message = version >= 0 ? "flushed changes to disk" : "failed to flush changes";
BiomancyMod.LOGGER.debug(MarkerManager.getMarker(LOG_MARKER), message);
}
}
else {
BiomancyMod.LOGGER.debug(MarkerManager.getMarker(LOG_MARKER), "has no unsaved changes");
//TODO: compact?
//store.compactFile(10);
}
}
}

Expand Down

0 comments on commit 9d71212

Please sign in to comment.