[vfs] throw exception if AppendOnlyLog.read(NULL_ID)

+ NULL_ID is incorrect, but an appropriate check was missed before -> added it

GitOrigin-RevId: ab543ac547d558794127b936372d6bb395f99001
This commit is contained in:
Ruslan Cheremin
2024-06-29 13:24:36 +02:00
committed by intellij-monorepo-bot
parent c5dfa1bff8
commit eb2e311653
2 changed files with 29 additions and 11 deletions

View File

@@ -49,7 +49,7 @@ public class AppendOnlyLogOverMMappedFileTest {
@Before
public void setUp() throws IOException {
assertEquals("THREADS_COUNT(" + THREADS_COUNT + ") must divide ENOUGH_RECORDS(" + ENOUGH_RECORDS + ")",
0,
0,
ENOUGH_RECORDS % THREADS_COUNT);
@@ -89,8 +89,8 @@ public class AppendOnlyLogOverMMappedFileTest {
return true;
});
assertEquals("Data written must be the data read back",
dataToWrite,
dataReadBackRef.get());
dataToWrite,
dataReadBackRef.get());
}
@Test
@@ -282,6 +282,21 @@ public class AppendOnlyLogOverMMappedFileTest {
//Special/edge cases, regressions:
@Test
public void readingRecordWithNULL_ID_leadsToException() throws IOException {
assertThrows(IllegalArgumentException.class, () -> {
appendOnlyLog.read(0, buffer -> null);
});
}
@Test
public void readingRecordWithNegative_ID_leadsToException() throws IOException {
assertThrows(IllegalArgumentException.class, () -> {
appendOnlyLog.read(-1, buffer -> null);
});
}
@Test
public void singleEmptyRecordWritten_CouldBeReadBackAsIs() throws Exception {
byte[] dataToWrite = new byte[0];
@@ -292,8 +307,8 @@ public class AppendOnlyLogOverMMappedFileTest {
return bytes;
});
assertArrayEquals("Data written must be the data read back",
dataToWrite,
dataReadBack);
dataToWrite,
dataReadBack);
}
@Test
@@ -308,8 +323,8 @@ public class AppendOnlyLogOverMMappedFileTest {
return true;
});
assertArrayEquals("Data written must be the data read back",
dataToWrite,
dataReadBackRef.get());
dataToWrite,
dataReadBackRef.get());
}
@Test
@@ -371,8 +386,8 @@ public class AppendOnlyLogOverMMappedFileTest {
Object stringWritten = idToStringWritten.get(recordId);
if (!stringReadBack.equals(stringWritten)) {
assertEquals("[" + i + "]: data written[recordId: " + recordId + "] must be the data read back[recordId: " + recordId + "]",
stringWritten,
stringReadBack);
stringWritten,
stringReadBack);
}
i.inc();
@@ -386,8 +401,8 @@ public class AppendOnlyLogOverMMappedFileTest {
long recordId = recordsIds[i];
String stringReadBack = appendOnlyLog.read(recordId, IOUtil::readString);
assertEquals("[" + i + "]: data written must be the data read back",
stringsWritten[i],
stringReadBack);
stringsWritten[i],
stringReadBack);
}
}

View File

@@ -1096,6 +1096,9 @@ public final class AppendOnlyLogOverMMappedFile implements AppendOnlyLog, Unmapp
@VisibleForTesting
static long recordIdToOffset(long recordId) {
if (recordId <= 0) {
throw new IllegalArgumentException("recordId(=" + recordId + ") is negative or NULL_ID -- can't be read");
}
long offset = recordIdToOffsetUnchecked(recordId);
if (!AlignmentUtils.is32bAligned(offset)) {
throw new IllegalArgumentException("recordId(=" + recordId + ") is invalid: recordOffsetInFile(=" + offset + ") is not 32b-aligned");