Skip to content

Commit

Permalink
Merge pull request #409 from Zelldon/ck-fix-error-handling-key-format…
Browse files Browse the repository at this point in the history
…ting

fix: err fallback on key formatting
  • Loading branch information
Zelldon authored Jun 17, 2024
2 parents 42cb5b8 + a844938 commit e5f9db3
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions backend/src/main/kotlin/io/zell/zdb/state/KeyFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,29 @@ public String formatKey(final byte[] key) {
final var formatted = new StringBuilder();
final var keyBuffer = new UnsafeBuffer(key);
int offset = 8;
for (final var dbValue : this.values) {
dbValue.wrap(keyBuffer, offset, key.length - offset);
offset += dbValue.getLength();
if (!formatted.isEmpty()) {
formatted.append(":");
}
switch (dbValue) {
case final DbString dbString -> formatted.append(dbString);
case final DbLong dbLong -> formatted.append(dbLong.getValue());
case final DbInt dbInt -> formatted.append(dbInt.getValue());
case final DbByte dbByte -> formatted.append(dbByte.getValue());
case final DbBytes dbBytes -> {
final var buf = dbBytes.getDirectBuffer();
final var bytes = new byte[dbBytes.getLength()];
buf.getBytes(0, bytes);
formatted.append(KeyFormatters.HEX_FORMATTER.formatKey(bytes));
try {
for (final var dbValue : this.values) {
dbValue.wrap(keyBuffer, offset, key.length - offset);
offset += dbValue.getLength();
if (!formatted.isEmpty()) {
formatted.append(":");
}
switch (dbValue) {
case final DbString dbString -> formatted.append(dbString);
case final DbLong dbLong -> formatted.append(dbLong.getValue());
case final DbInt dbInt -> formatted.append(dbInt.getValue());
case final DbByte dbByte -> formatted.append(dbByte.getValue());
case final DbBytes dbBytes -> {
final var buf = dbBytes.getDirectBuffer();
final var bytes = new byte[dbBytes.getLength()];
buf.getBytes(0, bytes);
formatted.append(KeyFormatters.HEX_FORMATTER.formatKey(bytes));
}
default -> formatted.append(dbValue);
}
default -> formatted.append(dbValue);
}
} catch (final IndexOutOfBoundsException indexOutOfBoundsException) {
return KeyFormatters.HEX_FORMATTER.formatKey(key);
}
return formatted.toString();
}
Expand Down

0 comments on commit e5f9db3

Please sign in to comment.