Forum

Forum Navigation
You need to log in to create posts and topics.

How to make searches as fast as possible using the cache?

How can I realize it?

For this purpose it is necessary to load the classes of the paths that we want scan in the cache and then perform a search on it. This can be accomplished in 2 different ways:

  • way one: call the findBy method by passing to it a SearchConfig with a ClassCriteria that contains no condition and subsequently call it again by passing to it another SearchConfig with a ClassCriteria that contains all conditions we are interested. E.g .:

    import java.util.Collection;
    import java.util.Date;
    
    import org.burningwave.core.assembler.ComponentContainer;
    import org.burningwave.core.assembler.ComponentSupplier;
    import org.burningwave.core.classes.CacheableSearchConfig;
    import org.burningwave.core.classes.ClassCriteria;
    import org.burningwave.core.classes.ClassHunter;
    import org.burningwave.core.classes.MethodCriteria;
    import org.burningwave.core.classes.SearchConfig;
    import org.burningwave.core.classes.ClassHunter.SearchResult;
    import org.burningwave.core.io.PathHelper;
    
    public class Finder {	   
    
        public Collection<Class<?>> find() {
            ComponentSupplier componentSupplier = ComponentContainer.getInstance();
            PathHelper pathHelper = componentSupplier.getPathHelper();
            ClassHunter classHunter = componentSupplier.getClassHunter();
            
            CacheableSearchConfig searchConfig = SearchConfig.forPaths(
                pathHelper.getMainClassPaths()
            );
            
            //With this we load the cache
            try(SearchResult result = classHunter.findBy(searchConfig)) {}
            
            MethodCriteria methodCriteria = MethodCriteria.forName(
                (methodName) -> methodName.startsWith("set")
            ).and().parameterType(
                (uploadedClasses, array, idx) ->
                    idx == 0 && array[idx].equals(uploadedClasses.get(Date.class))
            ).skip((classes, initialClass, examinedClass) -> 
                classes.get(Object.class) == examinedClass
            );			
    
            searchConfig = SearchConfig.forPaths(
                pathHelper.getMainClassPaths()
            ).by(
                ClassCriteria.create().byMembers(
                    methodCriteria
                ).useClasses(
                    Date.class,
                    Object.class
                )
            );
    
            SearchResult searchResult = classHunter.findBy(searchConfig);
            return searchResult.getClasses();
        }
    
    }

     

  • way two: call the method chain loadInCache(CacheableSearchConfig).find(). E.g.:

    import java.util.Collection;
    import java.util.Date;
    
    import org.burningwave.core.assembler.ComponentContainer;
    import org.burningwave.core.assembler.ComponentSupplier;
    import org.burningwave.core.classes.CacheableSearchConfig;
    import org.burningwave.core.classes.ClassCriteria;
    import org.burningwave.core.classes.ClassHunter;
    import org.burningwave.core.classes.MethodCriteria;
    import org.burningwave.core.classes.SearchConfig;
    import org.burningwave.core.classes.ClassHunter.SearchResult;
    import org.burningwave.core.io.PathHelper;
    
    public class Finder {	   
    
        public Collection<Class<?>> find() {
            ComponentSupplier componentSupplier = ComponentContainer.getInstance();
            PathHelper pathHelper = componentSupplier.getPathHelper();
            ClassHunter classHunter = componentSupplier.getClassHunter();
            
            MethodCriteria methodCriteria = MethodCriteria.forName(
                (methodName) -> methodName.startsWith("set")
            ).and().parameterType(
                (uploadedClasses, array, idx) ->
                    idx == 0 && array[idx].equals(uploadedClasses.get(Date.class))
            ).skip((classes, initialClass, examinedClass) -> 
                classes.get(Object.class) == examinedClass
            );			
    
            CacheableSearchConfig searchConfig = SearchConfig.forPaths(
                pathHelper.getMainClassPaths()
            ).by(
                ClassCriteria.create().byMembers(
                    methodCriteria
                ).useClasses(
                    Date.class,
                    Object.class
                )
            );
            
            SearchResult searchResult = classHunter.loadInCache(searchConfig).find();
            return searchResult.getClasses();
        }
    
    }