Skip to content

Commit

Permalink
Emergency-fix of #7088 (#7117)
Browse files Browse the repository at this point in the history
In #7088 the newly added attribute for the reference created-at-timestamp causes issues with MongoDB + JDBC.

This PR fixes those issues.
  • Loading branch information
snazy authored Jun 23, 2023
1 parent 6ec4551 commit 44f4b1b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import static org.projectnessie.versioned.storage.jdbc.SqlConstants.MARK_REFERENCE_AS_DELETED;
import static org.projectnessie.versioned.storage.jdbc.SqlConstants.MAX_BATCH_SIZE;
import static org.projectnessie.versioned.storage.jdbc.SqlConstants.PURGE_REFERENCE;
import static org.projectnessie.versioned.storage.jdbc.SqlConstants.REFS_CREATED_AT_COND;
import static org.projectnessie.versioned.storage.jdbc.SqlConstants.REFS_EXTENDED_INFO_COND;
import static org.projectnessie.versioned.storage.jdbc.SqlConstants.SCAN_OBJS;
import static org.projectnessie.versioned.storage.jdbc.SqlConstants.STORE_OBJ;
Expand Down Expand Up @@ -213,7 +214,11 @@ protected final Reference addReference(
ps.setString(2, reference.name());
serializeObjId(ps, 3, reference.pointer());
ps.setBoolean(4, reference.deleted());
ps.setLong(5, reference.createdAtMicros());
if (reference.createdAtMicros() != 0L) {
ps.setLong(5, reference.createdAtMicros());
} else {
ps.setNull(5, Types.BIGINT);
}
serializeObjId(ps, 6, reference.extendedInfoObj());

if (ps.executeUpdate() != 1) {
Expand All @@ -237,15 +242,19 @@ protected final Reference markReferenceAsDeleted(
throws RefNotFoundException, RefConditionFailedException {
try (PreparedStatement ps =
conn.prepareStatement(referencesDml(MARK_REFERENCE_AS_DELETED, reference))) {
ps.setBoolean(1, true);
ps.setString(2, config().repositoryId());
ps.setString(3, reference.name());
serializeObjId(ps, 4, reference.pointer());
ps.setBoolean(5, false);
ps.setLong(6, reference.createdAtMicros());
int idx = 1;
ps.setBoolean(idx++, true);
ps.setString(idx++, config().repositoryId());
ps.setString(idx++, reference.name());
serializeObjId(ps, idx++, reference.pointer());
ps.setBoolean(idx++, false);
long createdAtMicros = reference.createdAtMicros();
if (createdAtMicros != 0L) {
ps.setLong(idx++, createdAtMicros);
}
ObjId extendedInfoObj = reference.extendedInfoObj();
if (extendedInfoObj != null) {
serializeObjId(ps, 7, extendedInfoObj);
serializeObjId(ps, idx, extendedInfoObj);
}

if (ps.executeUpdate() != 1) {
Expand All @@ -267,14 +276,18 @@ protected final void purgeReference(
@Nonnull @jakarta.annotation.Nonnull Reference reference)
throws RefNotFoundException, RefConditionFailedException {
try (PreparedStatement ps = conn.prepareStatement(referencesDml(PURGE_REFERENCE, reference))) {
ps.setString(1, config().repositoryId());
ps.setString(2, reference.name());
serializeObjId(ps, 3, reference.pointer());
ps.setBoolean(4, true);
ps.setLong(5, reference.createdAtMicros());
int idx = 1;
ps.setString(idx++, config().repositoryId());
ps.setString(idx++, reference.name());
serializeObjId(ps, idx++, reference.pointer());
ps.setBoolean(idx++, true);
long createdAtMicros = reference.createdAtMicros();
if (createdAtMicros != 0L) {
ps.setLong(idx++, createdAtMicros);
}
ObjId extendedInfoObj = reference.extendedInfoObj();
if (extendedInfoObj != null) {
serializeObjId(ps, 6, extendedInfoObj);
serializeObjId(ps, idx, extendedInfoObj);
}

if (ps.executeUpdate() != 1) {
Expand All @@ -298,15 +311,19 @@ protected final Reference updateReferencePointer(
throws RefNotFoundException, RefConditionFailedException {
try (PreparedStatement ps =
conn.prepareStatement(referencesDml(UPDATE_REFERENCE_POINTER, reference))) {
serializeObjId(ps, 1, newPointer);
ps.setString(2, config().repositoryId());
ps.setString(3, reference.name());
serializeObjId(ps, 4, reference.pointer());
ps.setBoolean(5, false);
ps.setLong(6, reference.createdAtMicros());
int idx = 1;
serializeObjId(ps, idx++, newPointer);
ps.setString(idx++, config().repositoryId());
ps.setString(idx++, reference.name());
serializeObjId(ps, idx++, reference.pointer());
ps.setBoolean(idx++, false);
long createdAtMicros = reference.createdAtMicros();
if (createdAtMicros != 0L) {
ps.setLong(idx++, createdAtMicros);
}
ObjId extendedInfoObj = reference.extendedInfoObj();
if (extendedInfoObj != null) {
serializeObjId(ps, 7, extendedInfoObj);
serializeObjId(ps, idx, extendedInfoObj);
}

if (ps.executeUpdate() != 1) {
Expand All @@ -324,8 +341,10 @@ protected final Reference updateReferencePointer(
}

private String referencesDml(String sql, Reference reference) {
String createdAtCond = reference.createdAtMicros() != 0L ? "=?" : " IS NULL";
String extendedInfoCond = reference.extendedInfoObj() != null ? "=?" : " IS NULL";
return sql.replace(REFS_EXTENDED_INFO_COND, extendedInfoCond);
return sql.replace(REFS_CREATED_AT_COND, createdAtCond)
.replace(REFS_EXTENDED_INFO_COND, extendedInfoCond);
}

@SuppressWarnings("unused")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ final class SqlConstants {
static final String COL_REFS_DELETED = "deleted";
static final String COL_REFS_CREATED_AT = "created_at";
static final String COL_REFS_EXTENDED_INFO = "ext_info";
static final String REFS_CREATED_AT_COND = "_REFS_CREATED_AT_";
static final String REFS_EXTENDED_INFO_COND = "_REFS_EXTENDED_INFO_";
static final String UPDATE_REFERENCE_POINTER =
"UPDATE "
Expand All @@ -118,7 +119,8 @@ final class SqlConstants {
+ COL_REFS_DELETED
+ "=? AND "
+ COL_REFS_CREATED_AT
+ "=? AND "
+ REFS_CREATED_AT_COND
+ " AND "
+ COL_REFS_EXTENDED_INFO
+ REFS_EXTENDED_INFO_COND;
static final String PURGE_REFERENCE =
Expand All @@ -134,7 +136,8 @@ final class SqlConstants {
+ COL_REFS_DELETED
+ "=? AND "
+ COL_REFS_CREATED_AT
+ "=? AND "
+ REFS_CREATED_AT_COND
+ " AND "
+ COL_REFS_EXTENDED_INFO
+ REFS_EXTENDED_INFO_COND;
static final String MARK_REFERENCE_AS_DELETED =
Expand All @@ -152,7 +155,8 @@ final class SqlConstants {
+ COL_REFS_DELETED
+ "=? AND "
+ COL_REFS_CREATED_AT
+ "=? AND "
+ REFS_CREATED_AT_COND
+ " AND "
+ COL_REFS_EXTENDED_INFO
+ REFS_EXTENDED_INFO_COND;
static final String ADD_REFERENCE =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,10 +356,14 @@ public Reference fetchReference(@Nonnull @jakarta.annotation.Nonnull String name
name,
binaryToObjId(doc.get(COL_REFERENCES_POINTER, Binary.class)),
doc.getBoolean(COL_REFERENCES_DELETED),
doc.getLong(COL_REFERENCES_CREATED_AT),
refCreatedAt(doc),
binaryToObjId(doc.get(COL_REFERENCES_EXTENDED_INFO, Binary.class)));
}

private static Long refCreatedAt(Document doc) {
return doc.containsKey(COL_REFERENCES_CREATED_AT) ? doc.getLong(COL_REFERENCES_CREATED_AT) : 0L;
}

@Nonnull
@jakarta.annotation.Nonnull
@Override
Expand All @@ -376,7 +380,7 @@ public Reference[] fetchReferences(@Nonnull @jakarta.annotation.Nonnull String[]
name,
binaryToObjId(doc.get(COL_REFERENCES_POINTER, Binary.class)),
doc.getBoolean(COL_REFERENCES_DELETED),
doc.getLong(COL_REFERENCES_CREATED_AT),
refCreatedAt(doc),
binaryToObjId(doc.get(COL_REFERENCES_EXTENDED_INFO, Binary.class)));
for (int i = 0; i < names.length; i++) {
if (name.equals(names[i])) {
Expand Down

0 comments on commit 44f4b1b

Please sign in to comment.