0 Replies Latest reply on Feb 25, 2013 7:46 PM by Mathieu Lachance

    how to search and return the first non null result in a distributed cache

    Mathieu Lachance Novice

      Hello everyone,

       

      I was wondering if there was any efficient way to search and return the first non null result in a distributed cache. For now I have used the Map-Reduce framework to perform a basic search but I do not think this is quite optimal (well it is not !).

      Here is my example :

       

          public class Session {
              private String userId;
          }
      
          public static class GetSessionByUserIdMapper implements Mapper<String, Session, String, Session>{
              private static final long serialVersionUID = 1L;
      
              private String userId;
              
              public GetSessionByUserIdMapper(String userId){
                  this.userId = userId;
              }
              
              @Override
              public void map(String key, Session value, Collector<String, Session> collector) {
                  if (value.getUserId().equals(userId)){
                      collector.emit(value.getUserId(), value);
                      // at that moment we would be done
                      // is it possible to stop the map reduce operation and return this value ?
                  }
              }
          }
          
          public static class GetSessionByUserIdReducer implements Reducer<String, Session>{
              private static final long serialVersionUID = 1L;
      
              @Override
              public Session reduce(String reducedKey, Iterator<Session> iter) {
                  if (! iter.hasNext()){
                      return null;
                  }
                  return iter.next();
              }
          }
      
          public static class GetSessionByUserIdCollator implements Collator<String, Session, Session>{
              @Override
              public Session collate(Map<String, Session> reducedResults)
              {
                  Iterator<Session> iterator = reducedResults.values().iterator();
                  if (iterator.hasNext()){
                      return iterator.next();
                  }
                  return null;
              }
          }
      
      
      

       

      That is a lot of operations to perform this quite simple task.

      I'm aware of the existence of the Infinispan-HibernateSearch-Lucene bridge and I know it would technically work, though I would really like to far from that approach.

       

      Is there any other way to perform this task ?

       

      Big thanks,