diff --git a/bosk-mongo/src/main/java/io/vena/bosk/drivers/mongo/AbstractFormatDriver.java b/bosk-mongo/src/main/java/io/vena/bosk/drivers/mongo/AbstractFormatDriver.java index 4629e384..a5c95ab5 100644 --- a/bosk-mongo/src/main/java/io/vena/bosk/drivers/mongo/AbstractFormatDriver.java +++ b/bosk-mongo/src/main/java/io/vena/bosk/drivers/mongo/AbstractFormatDriver.java @@ -3,11 +3,14 @@ import io.vena.bosk.MapValue; import io.vena.bosk.RootReference; import io.vena.bosk.StateTreeNode; +import io.vena.bosk.drivers.mongo.status.BsonComparator; import io.vena.bosk.drivers.mongo.status.MongoStatus; +import io.vena.bosk.drivers.mongo.status.StateStatus; import java.io.IOException; import lombok.RequiredArgsConstructor; import org.bson.BsonDocument; import org.bson.BsonInt64; +import org.bson.BsonValue; import static io.vena.bosk.drivers.mongo.Formatter.REVISION_ZERO; @@ -19,11 +22,18 @@ abstract class AbstractFormatDriver implements FormatDr @Override public MongoStatus readStatus() { try { - BsonState bsonState = loadBsonState(); + BsonState dbContents = loadBsonState(); + BsonDocument loadedBsonState = dbContents.state; + BsonValue inMemoryState = formatter.object2bsonValue(rootRef.value(), rootRef.targetType()); + BsonComparator comp = new BsonComparator(); return new MongoStatus( null, null, // MainDriver should fill this in - formatter.bsonValueBinarySize(bsonState.state) + new StateStatus( + dbContents.revision.longValue(), + formatter.bsonValueBinarySize(loadedBsonState), + comp.difference(inMemoryState, loadedBsonState) + ) ); } catch (UninitializedCollectionException e) { return new MongoStatus( @@ -64,4 +74,5 @@ record BsonState( BsonInt64 revision, BsonDocument diagnosticAttributes ){} + } diff --git a/bosk-mongo/src/main/java/io/vena/bosk/drivers/mongo/MainDriver.java b/bosk-mongo/src/main/java/io/vena/bosk/drivers/mongo/MainDriver.java index 5eee8965..22abe00b 100644 --- a/bosk-mongo/src/main/java/io/vena/bosk/drivers/mongo/MainDriver.java +++ b/bosk-mongo/src/main/java/io/vena/bosk/drivers/mongo/MainDriver.java @@ -335,16 +335,13 @@ public void refurbish() throws IOException { @Override public MongoStatus readStatus() throws Exception { - try (var __ = collection.newReadOnlySession()) { - FormatDriver detectedDriver = detectFormat(); - MongoStatus partialResult = detectedDriver.readStatus(); + try ( + var __1 = bosk.readContext(); + var __2 = collection.newReadOnlySession() + ) { + MongoStatus partialResult = detectFormat().readStatus(); Manifest manifest = loadManifest(); // TODO: Avoid loading the manifest again - - return new MongoStatus( - partialResult.error(), - manifest, - partialResult.stateBytes() - ); + return partialResult.with(driverSettings.preferredDatabaseFormat(), manifest); } } diff --git a/bosk-mongo/src/main/java/io/vena/bosk/drivers/mongo/Manifest.java b/bosk-mongo/src/main/java/io/vena/bosk/drivers/mongo/Manifest.java index c391a912..8a9d03a1 100644 --- a/bosk-mongo/src/main/java/io/vena/bosk/drivers/mongo/Manifest.java +++ b/bosk-mongo/src/main/java/io/vena/bosk/drivers/mongo/Manifest.java @@ -1,6 +1,7 @@ package io.vena.bosk.drivers.mongo; import io.vena.bosk.StateTreeNode; +import io.vena.bosk.drivers.mongo.MongoDriverSettings.DatabaseFormat; import java.util.Optional; public record Manifest( @@ -17,4 +18,13 @@ public static Manifest forSequoia() { public static Manifest forPando(PandoFormat settings) { return new Manifest(1, Optional.empty(), Optional.of(settings)); } + + public static Manifest forFormat(DatabaseFormat format) { + // TODO: Use a type switch in newer Java versions + if (format instanceof PandoFormat p) { + return forPando(p); + } else { + return forSequoia(); + } + } } diff --git a/bosk-mongo/src/main/java/io/vena/bosk/drivers/mongo/status/ManifestStatus.java b/bosk-mongo/src/main/java/io/vena/bosk/drivers/mongo/status/ManifestStatus.java new file mode 100644 index 00000000..13ea741c --- /dev/null +++ b/bosk-mongo/src/main/java/io/vena/bosk/drivers/mongo/status/ManifestStatus.java @@ -0,0 +1,12 @@ +package io.vena.bosk.drivers.mongo.status; + +import io.vena.bosk.drivers.mongo.Manifest; + +public record ManifestStatus( + Manifest expected, + Manifest actual +) { + public boolean isIdentical() { + return expected.equals(actual); + } +} diff --git a/bosk-mongo/src/main/java/io/vena/bosk/drivers/mongo/status/MongoStatus.java b/bosk-mongo/src/main/java/io/vena/bosk/drivers/mongo/status/MongoStatus.java index e0a211c9..6f16f579 100644 --- a/bosk-mongo/src/main/java/io/vena/bosk/drivers/mongo/status/MongoStatus.java +++ b/bosk-mongo/src/main/java/io/vena/bosk/drivers/mongo/status/MongoStatus.java @@ -10,6 +10,22 @@ */ public record MongoStatus( @JsonInclude(NON_NULL) String error, - Manifest manifest, - Long stateBytes -) { } + ManifestStatus manifest, + StateStatus state +) { + public MongoStatus with(MongoDriverSettings.DatabaseFormat preferredFormat, Manifest actualManifest) { + return new MongoStatus( + this.error, + new ManifestStatus( + Manifest.forFormat(preferredFormat), + actualManifest + ), + this.state + ); + } + + public boolean isAllClear() { + return manifest.isIdentical() + && state.difference() instanceof NoDifference; + } +} diff --git a/bosk-mongo/src/main/java/io/vena/bosk/drivers/mongo/status/StateStatus.java b/bosk-mongo/src/main/java/io/vena/bosk/drivers/mongo/status/StateStatus.java new file mode 100644 index 00000000..4075ac85 --- /dev/null +++ b/bosk-mongo/src/main/java/io/vena/bosk/drivers/mongo/status/StateStatus.java @@ -0,0 +1,7 @@ +package io.vena.bosk.drivers.mongo.status; + +public record StateStatus( + Long revision, + Long sizeInBytes, + Difference difference +) { }