Skip to content

Commit

Permalink
HSEARCH-823 Make it possible to plug in custom IndexManager implement…
Browse files Browse the repository at this point in the history
…ations
  • Loading branch information
Sanne authored and emmanuelbernard committed Aug 30, 2011
1 parent 1e52171 commit 17c7db7
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package org.hibernate.search.indexes.impl;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -29,6 +30,7 @@
import org.hibernate.annotations.common.reflection.ReflectionManager;
import org.hibernate.annotations.common.reflection.XClass;
import org.hibernate.annotations.common.reflection.java.JavaReflectionManager;
import org.hibernate.annotations.common.util.StringHelper;
import org.hibernate.search.Environment;
import org.hibernate.search.SearchException;
import org.hibernate.search.annotations.Indexed;
Expand All @@ -44,6 +46,8 @@
import org.hibernate.search.util.configuration.impl.ConfigurationParseHelper;
import org.hibernate.search.util.configuration.impl.MaskedProperty;
import org.hibernate.search.util.impl.ClassLoaderHelper;
import org.hibernate.search.util.logging.impl.Log;
import org.hibernate.search.util.logging.impl.LoggerFactory;

/**
* Stores references to IndexManager instances, and starts/stops them.
Expand All @@ -60,11 +64,24 @@
*/
public class IndexManagerHolder {

public static final String INDEXMANAGER_IMPL_NAME = "indexmanager";

private static final Log log = LoggerFactory.make();

private static final String SHARDING_STRATEGY = "sharding_strategy";
private static final String NBR_OF_SHARDS = SHARDING_STRATEGY + ".nbr_of_shards";

private final Map<String, IndexManager> indexManagersRegistry= new ConcurrentHashMap<String, IndexManager>();

private static final Map<String, String> defaultIndexManagerClasses;

static {
defaultIndexManagerClasses = new HashMap<String, String>( 3 );
defaultIndexManagerClasses.put( "", DirectoryBasedIndexManager.class.getName() );
defaultIndexManagerClasses.put( "transactional", DirectoryBasedIndexManager.class.getName() );
defaultIndexManagerClasses.put( "NRT", NRTIndexManager.class.getName() );
}

/**
* Multiple IndexManager might be built for the same entity to implement Sharding.
* @return a map of created IndexManagers, having as key the names of each index.
Expand Down Expand Up @@ -162,12 +179,26 @@ private void setSimilarity(Similarity newSimilarity, IndexManager manager) {
manager.setSimilarity( newSimilarity );
}

//FIXME for now we only build "legacy" DirectoryBasedIndexManager
// we should support replacing the IndexManager type with other types: HSEARCH-823
private IndexManager createDirectoryManager(String indexName, Properties indexProps, Class<?> entity, WorkerBuildContext context) {
DirectoryBasedIndexManager manager = new DirectoryBasedIndexManager();
manager.initialize( indexName, indexProps, context );
return manager;
String indexManagerName = indexProps.getProperty( INDEXMANAGER_IMPL_NAME, "transactional" );
final IndexManager manager;
if ( StringHelper.isEmpty( indexManagerName ) ) {
manager = new DirectoryBasedIndexManager();
}
else {
String longName = defaultIndexManagerClasses.get( indexManagerName );
if ( longName == null ) {
longName = indexManagerName;
}
manager = ClassLoaderHelper.instanceFromName( IndexManager.class, longName,
IndexManagerHolder.class, "index manager" );
}
try {
manager.initialize( indexName, indexProps, context );
return manager;
} catch (Exception e) {
throw log.unableToInitializeIndexManager( indexName, e );
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* JBoss, Home of Professional Open Source
* Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @authors tag. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License, v. 2.1.
* This program is distributed in the hope that it will be useful, but WITHOUT A
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License,
* v.2.1 along with this distribution; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
package org.hibernate.search.indexes.impl;

/**
* IndexManager implementation taking advantage of the Near-Real-Time
* features of Lucene.
* When using this work mode the IndexWriter does not need a full
* flush at the end of each operation: new IndexReaders are able to
* inspect the unflushed changes still pending in the IndexWriter buffers.
*
* This improves write performance as the IndexWriter doesn't need
* to commit as often, but has two limitations:
* <ul>
* <li>unsaved index data might be lost in case of crashes</li>
* <li>is not useful for non-local (clustered) backends</li>
* </ul>
*
* @since 4.0
* @author Sanne Grinovero <sanne@hibernate.org> (C) 2011 Red Hat Inc.
*/
public class NRTIndexManager extends DirectoryBasedIndexManager {

}
Original file line number Diff line number Diff line change
Expand Up @@ -450,4 +450,7 @@ public interface Log extends BasicLogger {

@Message(id = 102, value = "Unable to serialize Lucene works in Avro")
SearchException unableToSerializeInAvro(@Cause Throwable e);

@Message(id = 103, value = "Unable to initialize IndexManager %1$s")
SearchException unableToInitializeIndexManager(String indexName, @Cause Throwable e);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* JBoss, Home of Professional Open Source
* Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @authors tag. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License, v. 2.1.
* This program is distributed in the hope that it will be useful, but WITHOUT A
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License,
* v.2.1 along with this distribution; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
package org.hibernate.search.test.configuration;

import org.hibernate.search.FullTextSession;
import org.hibernate.search.engine.spi.SearchFactoryImplementor;
import org.hibernate.search.indexes.impl.IndexManagerHolder;
import org.hibernate.search.indexes.spi.IndexManager;
import org.hibernate.search.test.util.FullTextSessionBuilder;
import org.junit.Assert;
import org.junit.Test;

/**
* Verifies the configured IndexManager implementation is used for each index
*
* @author Sanne Grinovero <sanne@hibernate.org> (C) 2011 Red Hat Inc.
*/
public class IndexManagerOverrideTest {

@Test
public void verifyIndexExclusivity() {
FullTextSessionBuilder builder = new FullTextSessionBuilder();
FullTextSession ftSession = builder
.setProperty( "hibernate.search.Book.indexmanager", "NRT" )
.setProperty( "hibernate.search.org.hibernate.search.test.perf.Boat.indexmanager",
"org.hibernate.search.test.util.RamIndexManager" )
.addAnnotatedClass( BlogEntry.class )
.addAnnotatedClass( org.hibernate.search.test.perf.Boat.class )
.addAnnotatedClass( org.hibernate.search.test.query.Book.class )
.addAnnotatedClass( org.hibernate.search.test.query.Author.class )
.openFullTextSession();
SearchFactoryImplementor searchFactory = ( SearchFactoryImplementor ) ftSession.getSearchFactory();
ftSession.close();
IndexManagerHolder allIndexesManager = searchFactory.getAllIndexesManager();

//checks for the default implementation
checkIndexManagerType( allIndexesManager, "org.hibernate.search.test.configuration.BlogEntry",
org.hibernate.search.indexes.impl.DirectoryBasedIndexManager.class );

//Uses "NRT" taken from shortcut names
checkIndexManagerType( allIndexesManager, "Book",
org.hibernate.search.indexes.impl.NRTIndexManager.class );

//Uses a fully qualified name to load an implementation
checkIndexManagerType( allIndexesManager, "org.hibernate.search.test.perf.Boat",
org.hibernate.search.test.util.RamIndexManager.class );

builder.close();
}

private void checkIndexManagerType(IndexManagerHolder allIndexesManager, String name, Class expectedType) {
IndexManager indexManager = allIndexesManager.getIndexManager( name );
Assert.assertEquals( expectedType, indexManager.getClass() );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void testFailOnInexistentLockingFactory() {
fail();
}
catch (SearchException e) {
assertEquals( "Unable to find locking_strategy implementation class: org.hibernate.NotExistingFactory", e.getMessage() );
assertEquals( "Unable to find locking_strategy implementation class: org.hibernate.NotExistingFactory", e.getCause().getMessage() );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@
public class RamIndexManager extends DirectoryBasedIndexManager {

private static final LogErrorHandler logErrorHadler = new LogErrorHandler();

private RamIndexManager() {
//make sure tests use the helper below:
}

public static RamIndexManager makeRamDirectory() {
RamIndexManager ramIndexManager = new RamIndexManager();
Expand Down

0 comments on commit 17c7db7

Please sign in to comment.