JBoss Cache Searchable Edition (JBCS) Designs
The current (v1.0) release of JBCS uses a listener approach.
- The SearchableCacheFactory takes a running Cache, and creates a delegate
- This delegate implements SearchableCache, which is a sub-interface of Cache
- Most calls delegated to the underlying Cache, except the query related methods
- A listener is created and attached to the cache
- Used to listen for cache modifying events which originate both locally and remotely
- Updates indexes based on events
The plan is from v1.1 onwards, to drop the listener based approach as this can lead to complications with Hibernate Search (the interface to Lucene for indexing and searching).
Instead, we should create an interceptor and register this interceptor with the cache.
- IndexingInterceptor should extend CommandInterceptor.
- Methods we are interested in overriding are any visitXXXCommand() that change the cache (such as CommandInterceptor.visitPutKeyValueCommand()).
- On interception, we should first check if there is a transaction in progress (InvocationContext.getTransaction() will tell you this)
- If there is no transaction in progress, update indexes.
- We should also override visitPrepareCommand() (and visitOptimisticPrepareCommand() as well, for backward compatibility). Here, we should make a note of all modifications made to the cache
- If this is a one-phase prepare command, update indexes immediately. Otherwise, store the changes somewhere for now
- We should also override visitCommitCommand(). Here, we should update indexes for all modifications stored in 6.
- For visitRollbackCommand(), we should not do anything except free up resources by removing anything we store in 6 pertaining to this transaction.
That's it. :-)
The IndexingInterceptor should be registered with the cache by using Cache.addInterceptor(). It would be a good idea to put this just before the CallInterceptor, which is the last interceptor in the chain.