From a844938ed5907ccb6d59f87eb82b213695a45a54 Mon Sep 17 00:00:00 2001 From: Christopher Zell Date: Mon, 17 Jun 2024 08:19:55 +0200 Subject: [PATCH] fix: err fallback on key formatting If an keyformatter is wrong previously zdb failed with IndexOutOfBounce exceptions. This commit allows to still print the key in hex format, to handle errors more gracefully --- .../io/zell/zdb/state/KeyFormatter.java | 38 ++++++++++--------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/backend/src/main/kotlin/io/zell/zdb/state/KeyFormatter.java b/backend/src/main/kotlin/io/zell/zdb/state/KeyFormatter.java index d1cb712..40285f5 100644 --- a/backend/src/main/kotlin/io/zell/zdb/state/KeyFormatter.java +++ b/backend/src/main/kotlin/io/zell/zdb/state/KeyFormatter.java @@ -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(); }