-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: How to find all Entities marked with @Indexed at runtime?
PostPosted: Fri Aug 14, 2009 9:55 am 
Newbie

Joined: Fri Aug 14, 2009 9:37 am
Posts: 3
Hi, I'm trying to find a list (List<Class<?>>) of all entities marked with @Indexed. I would like to have a "re-index" feature where all classes marked with @Indexed are re-indexed.
I found the SearchConfiguration.getClassMappings(), but haven't been able to get an instance of SearchConfiguration at runtime.
Could someone please post an example of how to get a list of indexed entities?

As a temporary solution I retrieve all entity names, an use reflection to check for @Indexed, but surly it must be a better way?

Thanks!

Erik


Top
 Profile  
 
 Post subject: Re: How to find all Entities marked with @Indexed at runtime?
PostPosted: Fri Aug 14, 2009 1:29 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
you could try the current trunk, I've implemented that and it uses some cool performance improvements,
Not yet released, but it works like this:

Code:
FullTextSession fs = ...
fs.createIndexer(Object.class)
  .batchSizeToLoadObjects( 25 )
  .threadsToLoadObjects( 12 )
  .threadsForSubsequentFetching( 30 )
  .startAndWait();


keep in mind I'm giving you a preview; AFAIK it is fully functional but I'll rename some methods soon.

the Object.class means "reindex all objects which do subclass Object"; you could use createIndexer(User.class) to limit the indexing task to some type.
BTW if you look into the code you'll see how I retrieve the list of indexed types.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: How to find all Entities marked with @Indexed at runtime?
PostPosted: Sat Aug 15, 2009 9:27 am 
Newbie

Joined: Sat Aug 15, 2009 9:14 am
Posts: 1
# One common way to get in trouble is to use absolute URLs in your unsigned Applet. The Applet will then work on one website only, or locally only. Instead, use a relative URL and convert it to an absolute one like this:

import java.net.URL;

// ...

URL url = new URL( absoluteOrRelativeUrlString );

if ( url.getProtocol().length() == 0 )
{
// convert relative URL to absolute
url = new URL ( getDocumentBase(), url.getPath() );
}

# Watch out for this one. Sometime when you test your Applets locally they work, but when you use them on the web they fail with AccessControlException. Remember, Applets are not supposed to read files, though you can do an AppletContext. showDocument. You can however read resources in the jar file.

Cellulite


Top
 Profile  
 
 Post subject: Re: How to find all Entities marked with @Indexed at runtime?
PostPosted: Sun Aug 16, 2009 1:41 pm 
Newbie

Joined: Fri Aug 14, 2009 9:37 am
Posts: 3
s.grinovero wrote:
you could try the current trunk, I've implemented that and it uses some cool performance improvements,
Not yet released, but it works like this:

Code:
FullTextSession fs = ...
fs.createIndexer(Object.class)
  .batchSizeToLoadObjects( 25 )
  .threadsToLoadObjects( 12 )
  .threadsForSubsequentFetching( 30 )
  .startAndWait();


keep in mind I'm giving you a preview; AFAIK it is fully functional but I'll rename some methods soon.

the Object.class means "reindex all objects which do subclass Object"; you could use createIndexer(User.class) to limit the indexing task to some type.
BTW if you look into the code you'll see how I retrieve the list of indexed types.


Thanks a lot, I will give it a try!


Top
 Profile  
 
 Post subject: Re: How to find all Entities marked with @Indexed at runtime?
PostPosted: Mon Aug 17, 2009 8:55 am 
Newbie

Joined: Fri Aug 14, 2009 9:37 am
Posts: 3
ebaxt wrote:
s.grinovero wrote:
you could try the current trunk, I've implemented that and it uses some cool performance improvements,
Not yet released, but it works like this:

Code:
FullTextSession fs = ...
fs.createIndexer(Object.class)
  .batchSizeToLoadObjects( 25 )
  .threadsToLoadObjects( 12 )
  .threadsForSubsequentFetching( 30 )
  .startAndWait();


keep in mind I'm giving you a preview; AFAIK it is fully functional but I'll rename some methods soon.

the Object.class means "reindex all objects which do subclass Object"; you could use createIndexer(User.class) to limit the indexing task to some type.
BTW if you look into the code you'll see how I retrieve the list of indexed types.


Thanks a lot, I will give it a try!


In case anyone else is looking for a way to extract all indexed classes, here is what you need from the current trunk:

Code:
   public void findAllMarkedWithIndexed() {
        SearchFactoryImplementor sfi = getSearchFactory((Session) entityManager.getDelegate());
        Set<Class<?>> indexedClazzes = toRootEntities(sfi, Object.class);
        for (Class<?> jalla : indexedClazzes) {
            System.out.println(jalla.getSimpleName());
        }
    }

    private Set<Class<?>> toRootEntities(SearchFactoryImplementor searchFactoryImplementor, Class<?>... selection) {
        Set<Class<?>> entities = new HashSet<Class<?>>();
        //first build the "entities" set containing all indexed subtypes of "selection".
        for (Class<?> entityType : selection) {
            Set<Class<?>> targetedClasses = searchFactoryImplementor.getIndexedTypesPolymorphic(new Class[]{entityType});
            if (targetedClasses.isEmpty()) {
                String msg = entityType.getName() + " is not an indexed entity or a subclass of an indexed entity";
                throw new IllegalArgumentException(msg);
            }
            entities.addAll(targetedClasses);
        }
        Set<Class<?>> cleaned = new HashSet<Class<?>>();
        Set<Class<?>> toRemove = new HashSet<Class<?>>();
        //now remove all repeated types to avoid duplicate loading by polymorphic query loading
        for (Class<?> type : entities) {
            boolean typeIsOk = true;
            for (Class<?> existing : cleaned) {
                if (existing.isAssignableFrom(type)) {
                    typeIsOk = false;
                    break;
                }
                if (type.isAssignableFrom(existing)) {
                    toRemove.add(existing);
                }
            }
            if (typeIsOk) {
                cleaned.add(type);
            }
        }
        cleaned.removeAll(toRemove);
        log.debug("Targets for indexing job: {}", cleaned);
        return cleaned;
    }

    public static SearchFactoryImplementor getSearchFactory(Session session) {
        return getSearchFactoryBySFI((SessionImplementor) session);
    }


    public static SearchFactoryImplementor getSearchFactoryBySFI(SessionImplementor session) {
        PostInsertEventListener[] listeners = session.getListeners().getPostInsertEventListeners();
        FullTextIndexEventListener listener = null;
        //FIXME this sucks since we mandante the event listener use
        for (PostInsertEventListener candidate : listeners) {
            if (candidate instanceof FullTextIndexEventListener) {
                listener = (FullTextIndexEventListener) candidate;
                break;
            }
        }
        if (listener == null) throw new HibernateException(
                "Hibernate Search Event listeners not configured, please check the reference documentation and the " +
                        "application's hibernate.cfg.xml");
        return listener.getSearchFactoryImplementor();
    }


Just to clarify, this is code from trunk written by s.grinovero, I just copied what I needed so that it works with HS-3.1.1 GA.


Top
 Profile  
 
 Post subject: Re: How to find all Entities marked with @Indexed at runtime?
PostPosted: Wed Aug 26, 2009 6:41 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
No need to copy the whole class, this should be enough unless you need all that polymorphysm handling ;-)
Code:
Set<Class<?>> targetedClasses = searchFactoryImplementor.getIndexedTypesPolymorphic(new Class[]{Object,class});

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.