Hi,
in our application we have a special folder because of licences, database connection, etc for each user. Now we are implementing fulltext and query search using Hibernate Search technology, so we want multiple folders, where to store indeces, according to the user who uses the application. So we are trying to implement our own DirectoryProvider.
Our vision was that everytime somebody is searching for a text, the DirectoryProvider gives him a correct directory (the getDirectory method) where to search and also when he tryies to save updates to searched entites or save new entites. In simple words, the application should just operate with index files owned by this user in his folder.
Some code for better understanding:
Code:
public class MyDirectoryProvider implements DirectoryProvider<NIOFSDirectory> {
private NIOFSDirectory tempFolder;
@Override
public void initialize(String s, Properties properties, BuildContext buildContext) {
// temp directory needs to be initialized
this.tempFolder = createTempDirectory(); // this method is not important
this.initIndexFolder(this.tempFolder.getDirectory(), properties, buildContext);
// all home directories need to be initialized
Collection<Directory> directories = directoryService.getAll();
directories.parallelStream().forEach(
d -> this.initIndexFolder(
d.getLuceneDirectory().getDirectory(), properties, buildContext));
}
@Override
public void start(DirectoryBasedIndexManager directoryBasedIndexManager) {
}
@Override
public void stop() {
this.getDirectory().close();
}
@Override
public NIOFSDirectory getDirectory() {
Directory directory = ContextHolder.getContext().getDirectory(); // not important, a way how to get the directory of current user
// if the app just started (user context is not available)
if (directory == null) {
return this.tempFolder;
}
else {
return directory.getLuceneDirectory(); // returns the directory with lucene files with indeces
}
}
// inits the folders with indeces
private void initIndexFolder(File directory, Properties properties, BuildContext buildContext) {
try {
DirectoryProviderHelper.createFSIndex(directory, properties, buildContext.getServiceManager());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
The problem is with the temp folder which is present because there is no one logged when the application starts. So we need some temp directory for Hibernate search to initialize properly. I correctly get the user's folder while searching the indeces for the fulltext query, but when I try to save my entity, the method getDirectory is not even called and the updates are saved in the temp directory. Is this a right way to do it and we just need more time to develop it, or should we rather implement some special sharding strategy, because this DirectoryProvider implementation has no sence?
Hibernate and Lucene versions are the newest available.
Thanks a lot for your replies