While index updates are fast on average,
worst-case update performance may be significantly slower.
To make index updates consistently quick,
we must manually intervene to control the process of index segment consolidation.

Ordinarily,
modifying an index is cheap.
New data is added to new segments,
and the time to write a new segment scales more or less linearly with the number of documents added during the indexing session.

Deletions are also cheap most of the time,
because we don't remove documents immediately but instead mark them as deleted,
and adding the deletion mark is cheap.

However,
as new segments are added and the deletion rate for existing segments increases,
search-time performance slowly begins to degrade.
At some point,
it becomes necessary to consolidate existing segments,
rewriting their data into a new segment.

If the recycled segments are small,
the time it takes to rewrite them may not be significant.
Every once in a while,
though,
a large amount of data must be rewritten.

The simplest way to force fast index updates is to avoid rewriting anything.

Indexer relies upon IndexManager's recycle() method to tell it which segments should be consolidated.
If we subclass IndexManager and override recycle() so that it always returns an empty array,
we get consistently quick performance:

Never merging anything at all in the main indexing process is probably overkill. Small segments are relatively cheap to merge; we just need to guard against the big rewrites.

Setting a ceiling on the number of documents in the segments to be recycled allows us to avoid a mass proliferation of tiny, single-document segments, while still offering decent worst-case update speed:

The exception handling code becomes useful once you have more than one index modification process happening simultaneously. By default, Indexer tries several times to acquire a write lock over the span of one second, then holds it until commit() completes. BackgroundMerger handles most of its work without the write lock, but it does need it briefly once at the beginning and once again near the end. Under normal loads, the internal retry logic will resolve conflicts, but if it's not acceptable to miss an insert, you probably want to catch LockErr exceptions thrown by Indexer. In contrast, a LockErr from BackgroundMerger probably just needs to be logged.