Add LuceneTestCase.newBytesRef methods [LUCENE-10052]
- Dominant language
- Java
- Stars
- 3.6k
- Forks
- 1.4k
- Avg merge
- 2d 11h
- Merged PRs (30d)
- 88
Description
`BytesRef` is a super useful Lucene utility class, referencing a slice (offset + length) of an underlying possibly larger `byte[]`. We use it all over the place in our APIs.
But the `offset` is trappy – we programmers sometimes forget to add the `offset` when accessing the underlying bytes. Or sometimes we accidentally add it twice, as just happened in our (Amazon Product Search) Lucene usage. Such errors are devious because they often do not matter since typically `offset` will be zero, but then suddenly when the rare `BytesRef` arrives with non-zero `offset`, BOOM.
I think we should improve our testing here by making it simple to randomize a `BytesRef` creation to sometimes use non-zero offset and also to sometimes leave extra padding on the end of the underlying `byte[]` to catch another trappy case where we use `bytesRef.bytes.length` when we were supposed to use `bytesRef.length`.
---
Migrated from [LUCENE-10052](https://issues.apache.org/jira/browse/LUCENE-10052) by Michael McCandless (@mikemccand), updated Aug 25 2021
Pull requests: https://github.com/apache/lucene/pull/245, https://github.com/apache/lucene/pull/258
Contributor guide
Assessment
This issue has not been assessed yet.