Efficient way to search a stream for a string

JavaAlgorithmStringSearchStream

Java Problem Overview


Let's suppose that have a stream of text (or Reader in Java) that I'd like to check for a particular string. The stream of text might be very large so as soon as the search string is found I'd like to return true and also try to avoid storing the entire input in memory.

Naively, I might try to do something like this (in Java):

public boolean streamContainsString(Reader reader, String searchString) throws IOException {
    char[] buffer = new char[1024];
    int numCharsRead;
    while((numCharsRead = reader.read(buffer)) > 0) {
        if ((new String(buffer, 0, numCharsRead)).indexOf(searchString) >= 0)
            return true;
    }
    return false;
}

Of course this fails to detect the given search string if it occurs on the boundary of the 1k buffer:

Search text: "stackoverflow"
Stream buffer 1: "abc.........stack"
Stream buffer 2: "overflow.......xyz"

How can I modify this code so that it correctly finds the given search string across the boundary of the buffer but without loading the entire stream into memory?

Edit: Note when searching a stream for a string, we're trying to minimise the number of reads from the stream (to avoid latency in a network/disk) and to keep memory usage constant regardless of the amount of data in the stream. Actual efficiency of the string matching algorithm is secondary but obviously, it would be nice to find a solution that used one of the more efficient of those algorithms.

Java Solutions


Solution 1 - Java

There are three good solutions here:

  1. If you want something that is easy and reasonably fast, go with no buffer, and instead implement a simple nondeterminstic finite-state machine. Your state will be a list of indices into the string you are searching, and your logic looks something like this (pseudocode):

    String needle; n = needle.length();

    for every input character c do add index 0 to the list for every index i in the list do if c == needle[i] then if i + 1 == n then return true else replace i in the list with i + 1 end else remove i from the list end end end

This will find the string if it exists and you will never need a
buffer.
  1. Slightly more work but also faster: do an NFA-to-DFA conversion that figures out in advance what lists of indices are possible, and assign each one to a small integer. (If you read about string search on Wikipedia, this is called the powerset construction.) Then you have a single state and you make a state-to-state transition on each incoming character. The NFA you want is just the DFA for the string preceded with a state that nondeterministically either drops a character or tries to consume the current character. You'll want an explicit error state as well.

  2. If you want something faster, create a buffer whose size is at least twice n, and user Boyer-Moore to compile a state machine from needle. You'll have a lot of extra hassle because Boyer-Moore is not trivial to implement (although you'll find code online) and because you'll have to arrange to slide the string through the buffer. You'll have to build or find a circular buffer that can 'slide' without copying; otherwise you're likely to give back any performance gains you might get from Boyer-Moore.

Solution 2 - Java

I did a few changes to the Knuth Morris Pratt algorithm for partial searches. Since the actual comparison position is always less or equal than the next one there is no need for extra memory. The code with a Makefile is also available on github and it is written in Haxe to target multiple programming languages at once, including Java.

I also wrote a related article: searching for substrings in streams: a slight modification of the Knuth-Morris-Pratt algorithm in Haxe. The article mentions the Jakarta RegExp, now retired and resting in the Apache Attic. The Jakarta Regexp library “match” method in the RE class uses a CharacterIterator as a parameter.

class StreamOrientedKnuthMorrisPratt {
	var m: Int;
	var i: Int;
	var ss:
	var table: Array<Int>;

	public function new(ss: String) {
		this.ss = ss;
		this.buildTable(this.ss);
	}

	public function begin() : Void {
		this.m = 0;
		this.i = 0;
	}

	public function partialSearch(s: String) : Int {
		var offset = this.m + this.i;

		while(this.m + this.i - offset < s.length) {
			if(this.ss.substr(this.i, 1) == s.substr(this.m + this.i - offset,1)) {
				if(this.i == this.ss.length - 1) {
					return this.m;
				}
				this.i += 1;
			} else {
				this.m += this.i - this.table[this.i];
				if(this.table[this.i] > -1)
					this.i = this.table[this.i];
				else
					this.i = 0;
			}
		}

		return -1;
	}

	private function buildTable(ss: String) : Void {
		var pos = 2;
		var cnd = 0;

		this.table = new Array<Int>();
		if(ss.length > 2)
			this.table.insert(ss.length, 0);
		else
			this.table.insert(2, 0);

		this.table[0] = -1;
		this.table[1] = 0;

		while(pos < ss.length) {
			if(ss.substr(pos-1,1) == ss.substr(cnd, 1))
			{
				cnd += 1;
				this.table[pos] = cnd;
				pos += 1;
			} else if(cnd > 0) {
				cnd = this.table[cnd];
			} else {
				this.table[pos] = 0;
				pos += 1;
			}
		}
	}

	public static function main() {
		var KMP = new StreamOrientedKnuthMorrisPratt("aa");
		KMP.begin();
		trace(KMP.partialSearch("ccaabb"));

		KMP.begin();
		trace(KMP.partialSearch("ccarbb"));
		trace(KMP.partialSearch("fgaabb"));

	}
}

Solution 3 - Java

The Knuth-Morris-Pratt search algorithm never backs up; this is just the property you want for your stream search. I've used it before for this problem, though there may be easier ways using available Java libraries. (When this came up for me I was working in C in the 90s.)

KMP in essence is a fast way to build a string-matching DFA, like Norman Ramsey's suggestion #2.

Solution 4 - Java

This answer applied to the initial version of the question where the key was to read the stream only as far as necessary to match on a String, if that String was present. This solution would not meet the requirement to guarantee fixed memory utilisation, but may be worth considering if you have found this question and are not bound by that constraint.

If you are bound by the constant memory usage constraint, Java stores arrays of any type on the heap, and as such nulling the reference does not deallocate memory in any way; I think any solution involving arrays in a loop will consume memory on the heap and require GC.


For simple implementation, maybe Java 5's Scanner which can accept an InputStream and use a java.util.regex.Pattern to search the input for might save you worrying about the implementation details.

Here's an example of a potential implementation:

public boolean streamContainsString(Reader reader, String searchString)
            throws IOException {
      Scanner streamScanner = new Scanner(reader);
      if (streamScanner.findWithinHorizon(searchString, 0) != null) {
		return true;
      } else {
		return false;
      }
}

I'm thinking regex because it sounds like a job for a Finite State Automaton, something that starts in an initial state, changing state character by character until it either rejects the string (no match) or gets to an accept state.

I think this is probably the most efficient matching logic you could use, and how you organize the reading of the information can be divorced from the matching logic for performance tuning.

It's also how regexes work.

Solution 5 - Java

Instead of having your buffer be an array, use an abstraction that implements a circular buffer. Your index calculation will be buf[(next+i) % sizeof(buf)], and you'll have to be careful to full the buffer one-half at a time. But as long as the search string fits in half the buffer, you'll find it.

Solution 6 - Java

I believe the best solution to this problem is to try to keep it simple. Remember, beacause I'm reading from a stream, I want to keep the number of reads from the stream to a minimum (as network or disk latency may be an issue) while keeping the amount of memory used constant (as the stream may be very large in size). Actual efficiency of the string matching is not the number one goal (as that has been studied to death already).

Based on AlbertoPL's suggestion, here's a simple solution that compares the buffer against the search string character by character. The key is that because the search is only done one character at a time, no back tracking is needed and therefore no circular buffers, or buffers of a particular size are needed.

Now, if someone can come up with a similar implementation based on Knuth-Morris-Pratt search algorithm then we'd have a nice efficient solution ;)

public boolean streamContainsString(Reader reader, String searchString) throws IOException {
    char[] buffer = new char[1024];
    int numCharsRead;
    int count = 0;
    while((numCharsRead = reader.read(buffer)) > 0) {
        for (int c = 0; c < numCharsRead; c++) {
            if (buffer[c] == searchString.charAt(count))
	            count++;
            else
                count = 0;
            if (count == searchString.length()) return true;
        }
    }
    return false;
}

Solution 7 - Java

If you're not tied to using a Reader, then you can use Java's NIO API to efficiently load the file. For example (untested, but should be close to working):

public boolean streamContainsString(File input, String searchString) throws IOException {
    Pattern pattern = Pattern.compile(Pattern.quote(searchString));
    
    FileInputStream fis = new FileInputStream(input);
    FileChannel fc = fis.getChannel();

    int sz = (int) fc.size();
    MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz);

    CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
    CharBuffer cb = decoder.decode(bb);

    Matcher matcher = pattern.matcher(cb);
    
    return matcher.matches();
}

This basically mmap()'s the file to search and relies on the operating system to do the right thing regarding cache and memory usage. Note however that map() is more expensive the just reading the file in to a large buffer for files less than around 10 KiB.

Solution 8 - Java

A very fast searching of a stream is implemented in the RingBuffer class from the Ujorm framework. See the sample:

 Reader reader = RingBuffer.createReader("xxx ${abc} ${def} zzz");

 String word1 = RingBuffer.findWord(reader, "${", "}");
 assertEquals("abc", word1);

 String word2 = RingBuffer.findWord(reader, "${", "}");
 assertEquals("def", word2);

 String word3 = RingBuffer.findWord(reader, "${", "}");
 assertEquals("", word3);

The single class implementation is available on the SourceForge: For more information see the link.

Solution 9 - Java

Implement a sliding window. Have your buffer around, move all elements in the buffer one forward and enter a single new character in the buffer at the end. If the buffer is equal to your searched word, it is contained.

Of course, if you want to make this more efficient, you can look at a way to prevent moving all elements in the buffer around, for example by having a cyclic buffer and a representation of the strings which 'cycles' the same way the buffer does, so you only need to check for content-equality. This saves moving all elements in the buffer.

Solution 10 - Java

I think you need to buffer a small amount at the boundary between buffers.

For example if your buffer size is 1024 and the length of the SearchString is 10, then as well as searching each 1024-byte buffer you also need to search each 18-byte transition between two buffers (9 bytes from the end of the previous buffer concatenated with 9 bytes from the start of the next buffer).

Solution 11 - Java

I'd say switch to a character by character solution, in which case you'd scan for the first character in your target text, then when you find that character increment a counter and look for the next character. Every time you don't find the next consecutive character restart the counter. It would work like this:

public boolean streamContainsString(Reader reader, String searchString) throws IOException {
char[] buffer = new char[1024];
int numCharsRead;
int count = 0;
while((numCharsRead = reader.read(buffer)) > 0) {
    if (buffer[numCharsRead -1] == searchString.charAt(count))
        count++;
    else
        count = 0;
    
    if (count == searchString.size())    
     return true;
}
return false; 
}

The only problem is when you're in the middle of looking through characters... in which case there needs to be a way of remembering your count variable. I don't see an easy way of doing so except as a private variable for the whole class. In which case you would not instantiate count inside this method.

Solution 12 - Java

You might be able to implement a very fast solution using Fast Fourier Transforms, which, if implemented properly, allow you to do string matching in times O(nlog(m)), where n is the length of the longer string to be matched, and m is the length of the shorter string. You could, for example, perform FFT as soon as you receive an stream input of length m, and if it matches, you can return, and if it doesn't match, you can throw away the first character in the stream input, wait for a new character to appear through the stream, and then perform FFT again.

Solution 13 - Java

You can increase the speed of search for very large strings by using some string search algorithm

Solution 14 - Java

If you're looking for a constant substring rather than a regex, I'd recommend Boyer-Moore. There's plenty of source code on the internet.

Also, use a circular buffer, to avoid think too hard about buffer boundaries.

Mike.

Solution 15 - Java

I also had a similar problem: skip bytes from the InputStream until specified string (or byte array). This is the simple code based on circular buffer. It is not very efficient but works for my needs:

  private static boolean matches(int[] buffer, int offset, byte[] search) {
    final int len = buffer.length;
    for (int i = 0; i < len; ++i) {
      if (search[i] != buffer[(offset + i) % len]) {
        return false;
      }
    }
    return true;
  }

  public static void skipBytes(InputStream stream, byte[] search) throws IOException {
    final int[] buffer = new int[search.length];
    for (int i = 0; i < search.length; ++i) {
      buffer[i] = stream.read();
    }

    int offset = 0;
    while (true) {
      if (matches(buffer, offset, search)) {
        break;
      }
      buffer[offset] = stream.read();
      offset = (offset + 1) % buffer.length;
    }
  }

Solution 16 - Java

Here is my implementation:

static boolean containsKeywordInStream( Reader ir, String keyword, int bufferSize ) throws IOException{
    SlidingContainsBuffer sb = new SlidingContainsBuffer( keyword );
    char[] buffer = new char[ bufferSize ];
    int read;
    while( ( read = ir.read( buffer ) ) != -1 ){
        if( sb.checkIfContains( buffer, read ) ){
            return true;
        }
    }
    return false;
}

SlidingContainsBuffer class:

class SlidingContainsBuffer{

    private final char[] keyword;
    private int keywordIndexToCheck = 0;
    private boolean keywordFound = false;

    SlidingContainsBuffer( String keyword ){
        this.keyword = keyword.toCharArray();
    }

    boolean checkIfContains( char[] buffer, int read ){
        for( int i = 0; i < read; i++ ){
            if( keywordFound == false ){
                if( keyword[ keywordIndexToCheck ] == buffer[ i ] ){
                    keywordIndexToCheck++;
                    if( keywordIndexToCheck == keyword.length ){
                        keywordFound = true;
                    }
                } else {
                    keywordIndexToCheck = 0;
                }
            } else {
                break;
            }
        }
        return keywordFound;
    }
}

This answer fully qualifies the task:

  1. The implementation is able to find the searched keyword even if it was split between buffers
  2. Minimum memory usage defined by the buffer size
  3. Number of reads will be minimized by using bigger buffer

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlex SpurlingView Question on Stackoverflow
Solution 1 - JavaNorman RamseyView Answer on Stackoverflow
Solution 2 - Javasw.View Answer on Stackoverflow
Solution 3 - JavaDarius BaconView Answer on Stackoverflow
Solution 4 - JavabrabsterView Answer on Stackoverflow
Solution 5 - JavaNorman RamseyView Answer on Stackoverflow
Solution 6 - JavaAlex SpurlingView Answer on Stackoverflow
Solution 7 - JavadevertonView Answer on Stackoverflow
Solution 8 - JavapopView Answer on Stackoverflow
Solution 9 - JavaTethaView Answer on Stackoverflow
Solution 10 - JavaChrisWView Answer on Stackoverflow
Solution 11 - JavaAlbertoPLView Answer on Stackoverflow
Solution 12 - JavarbolingView Answer on Stackoverflow
Solution 13 - JavaAlexView Answer on Stackoverflow
Solution 14 - JavaMikeView Answer on Stackoverflow
Solution 15 - JavadmitriykovalevView Answer on Stackoverflow
Solution 16 - JavaIgorView Answer on Stackoverflow