SearchProcessors.
AbstractSearchProcessorFactory is built for searching for a concrete sequence of bytes
(the needle), it contains precomputed data needed to perform the search, and is meant to be reused
whenever searching for the same needleNote
implementations of SearchProcessor scan the io. sequentially,
one byte after another, without doing any random access. As a result, when using SearchProcessor
with such methods as io., these methods return the index of the last byte
of the found byte sequence within the io. (which might feel counterintuitive,
and different from io. which returns the index of the first byte
of found sequence).
A SearchProcessor is implemented as a
Finite State Automaton that contains a
small internal state which is updated with every byte processed. As a result, an instance of SearchProcessor
should not be reused across independent search sessions (eg. for searching in different
io.s). A new instance should be created with AbstractSearchProcessorFactory for
every search session. However, a SearchProcessor can (and should) be reused within the search session,
eg. when searching for all occurrences of the needle within the same haystack. That way, it can
also detect overlapping occurrences of the needle (eg. a string "ABABAB" contains two occurrences of "BAB"
that overlap by one character "B"). For this to work correctly, after an occurrence of the needle is
found ending at index idx, the search should continue starting from the index idx + 1.
Example (given that the haystack is a io. containing "ABABAB" and
the needle is "BAB"):
SearchProcessorFactory factory =
SearchProcessorFactory.newKmpSearchProcessorFactory(needle.getBytes(CharsetUtil.UTF_8));
SearchProcessor processor = factory.newSearchProcessor();
int idx1 = haystack.forEachByte(processor);
// idx1 is 3 (index of the last character of the first occurrence of the needle in the haystack)
int continueFrom1 = idx1 + 1;
// continue the search starting from the next character
int idx2 = haystack.forEachByte(continueFrom1, haystack.readableBytes() - continueFrom1, processor);
// idx2 is 5 (index of the last character of the second occurrence of the needle in the haystack)
int continueFrom2 = idx2 + 1;
// continue the search starting from the next character
int idx3 = haystack.forEachByte(continueFrom2, haystack.readableBytes() - continueFrom2, processor);
// idx3 is -1 (no more occurrences of the needle)
// After this search session is complete, processor should be discarded.
// To search for the same needle again, reuse the same factory to get a new SearchProcessor.
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public static BitapSearchProcessorFactory | newBitapSearchProcessorFactory(byte[]
an array of no more than 64 bytes to search for needle)Creates a |
| public static KmpSearchProcessorFactory | newKmpSearchProcessorFactory(byte[]
an array of bytes to search for needle)Creates a |
| AbstractSearchProcessorFactory | back to summary |
|---|---|
| public AbstractSearchProcessorFactory() | |
| newBitapSearchProcessorFactory | back to summary |
|---|---|
| public static BitapSearchProcessorFactory newBitapSearchProcessorFactory(byte[] needle) Creates a
| |
| newKmpSearchProcessorFactory | back to summary |
|---|---|
| public static KmpSearchProcessorFactory newKmpSearchProcessorFactory(byte[] needle) Creates a
| |