Skip to content

Commit

Permalink
Report manifest and state differences
Browse files Browse the repository at this point in the history
  • Loading branch information
prdoyle committed Feb 10, 2024
1 parent e3d2669 commit f1b9d83
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -19,11 +22,18 @@ abstract class AbstractFormatDriver<R extends StateTreeNode> 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(
Expand Down Expand Up @@ -64,4 +74,5 @@ record BsonState(
BsonInt64 revision,
BsonDocument diagnosticAttributes
){}

}
Original file line number Diff line number Diff line change
Expand Up @@ -335,16 +335,13 @@ public void refurbish() throws IOException {

@Override
public MongoStatus readStatus() throws Exception {
try (var __ = collection.newReadOnlySession()) {
FormatDriver<R> 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);
}
}

Expand Down
10 changes: 10 additions & 0 deletions bosk-mongo/src/main/java/io/vena/bosk/drivers/mongo/Manifest.java
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.vena.bosk.drivers.mongo.status;

public record StateStatus(
Long revision,
Long sizeInBytes,
Difference difference
) { }

0 comments on commit f1b9d83

Please sign in to comment.