All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.browseengine.bobo.facets.impl.DynamicRangeFacetHandler Maven / Gradle / Ivy

There is a newer version: 3.1.2
Show newest version
/**
 * This software is licensed to you under the Apache License, Version 2.0 (the
 * "Apache License").
 *
 * LinkedIn's contributions are made under the Apache License. If you contribute
 * to the Software, the contributions will be deemed to have been made under the
 * Apache License, unless you expressly indicate otherwise. Please do not make any
 * contributions that would be inconsistent with the Apache License.
 *
 * You may obtain a copy of the Apache License at http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, this software
 * distributed under the Apache License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the Apache
 * License for the specific language governing permissions and limitations for the
 * software governed under the Apache License.
 *
 * © 2012 LinkedIn Corp. All Rights Reserved.  
 */

package com.browseengine.bobo.facets.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import com.browseengine.bobo.api.BoboIndexReader;
import com.browseengine.bobo.api.BrowseFacet;
import com.browseengine.bobo.api.BrowseSelection;
import com.browseengine.bobo.api.FacetIterator;
import com.browseengine.bobo.api.FacetSpec;
import com.browseengine.bobo.facets.FacetCountCollector;
import com.browseengine.bobo.facets.FacetCountCollectorSource;
import com.browseengine.bobo.facets.RuntimeFacetHandler;
import com.browseengine.bobo.facets.FacetHandler.FacetDataNone;
import com.browseengine.bobo.facets.data.FacetDataCache;
import com.browseengine.bobo.facets.filter.RandomAccessFilter;
import com.browseengine.bobo.sort.DocComparatorSource;
import com.browseengine.bobo.util.ListMerger;

/**
 * @author ymatsuda
 *
 */
public abstract class DynamicRangeFacetHandler extends RuntimeFacetHandler
{
  protected final String _dataFacetName;
  protected RangeFacetHandler _dataFacetHandler;
  
  public DynamicRangeFacetHandler(String name, String dataFacetName)
  {
    super(name,new HashSet(Arrays.asList(new String[]{dataFacetName})));
    _dataFacetName = dataFacetName;
  }
  
  protected abstract String buildRangeString(String val);
  protected abstract List buildAllRangeStrings();
  protected abstract String getValueFromRangeString(String rangeString);
  
  @Override
  public RandomAccessFilter buildRandomAccessFilter(String val, Properties props) throws IOException
  {
    return _dataFacetHandler.buildRandomAccessFilter(buildRangeString(val), props);
  }

  @Override
  public RandomAccessFilter buildRandomAccessAndFilter(String[] vals, Properties prop) throws IOException
  {
    List valList = new ArrayList(vals.length);
    for(String val : vals)
    {
      valList.add(buildRangeString(val));
    }
    
    return _dataFacetHandler.buildRandomAccessAndFilter(valList.toArray(new String[valList.size()]), prop);
  }

  @Override
  public RandomAccessFilter buildRandomAccessOrFilter(String[] vals,Properties prop,boolean isNot) throws IOException
  {
    List valList = new ArrayList(vals.length);
    for(String val : vals)
    {
      valList.add(buildRangeString(val));
    }
    return _dataFacetHandler.buildRandomAccessOrFilter(valList.toArray(new String[valList.size()]), prop, isNot);
  }

  @Override
  public FacetCountCollectorSource getFacetCountCollectorSource(final BrowseSelection sel, final FacetSpec fspec)
  {
    final List list = buildAllRangeStrings();
    
    return new FacetCountCollectorSource(){
		@Override
		public FacetCountCollector getFacetCountCollector(BoboIndexReader reader, int docBase) {
		    FacetDataCache dataCache = _dataFacetHandler.getFacetData(reader);
		    return new DynamicRangeFacetCountCollector(getName(), dataCache, docBase, fspec, list);
		}
    };
  }

  @Override
  public String[] getFieldValues(BoboIndexReader reader,int docid)
  {
    return _dataFacetHandler.getFieldValues(reader,docid);
  }
  
  @Override
  public Object[] getRawFieldValues(BoboIndexReader reader,int docid)
  {
    return _dataFacetHandler.getRawFieldValues(reader,docid);
  }

  @Override
  public DocComparatorSource getDocComparatorSource()
  {
    return _dataFacetHandler.getDocComparatorSource();
  }

  @Override
  public FacetDataNone load(BoboIndexReader reader) throws IOException
  {
    _dataFacetHandler = (RangeFacetHandler)getDependedFacetHandler(_dataFacetName);
    return FacetDataNone.instance;
  }
  
  private class DynamicRangeFacetCountCollector extends RangeFacetCountCollector
  {
    DynamicRangeFacetCountCollector(String name, FacetDataCache dataCache,int docBase, FacetSpec fspec, List predefinedList)
    {
      super(name,dataCache,docBase,fspec,predefinedList);
    }

    @Override
    public BrowseFacet getFacet(String value)
    {
      String rangeString = buildRangeString(value);
      BrowseFacet facet = super.getFacet(rangeString);
      if (facet!=null)
      {
        return new BrowseFacet(value,facet.getHitCount());
      }
      else
      {
        return null;
      }
    }

    @Override
    public int getFacetHitsCount(Object value) 
    {
      String rangeString = buildRangeString((String)value);
      return super.getFacetHitsCount(rangeString);
    }

    @Override
    public List getFacets()
    {
      List list = super.getFacets();      
      ArrayList retList = new ArrayList(list.size());
      Iterator iter = list.iterator();
      while(iter.hasNext())
      {
        BrowseFacet facet = iter.next();
        String val = facet.getValue();
        String rangeString = getValueFromRangeString(val);
        if (rangeString != null)
        {
          BrowseFacet convertedFacet = new BrowseFacet(rangeString, facet.getHitCount());
          retList.add(convertedFacet);
        }
      }
      return retList;
    }

    public FacetIterator iterator()
    {
      FacetIterator iter = super.iterator();

      List facets = new ArrayList();
      while(iter.hasNext())
      {
        Comparable facet = iter.next();
        int count = iter.count;
        facets.add(new BrowseFacet(getValueFromRangeString(String.valueOf(facet)), count));
      }
      Collections.sort(facets, ListMerger.FACET_VAL_COMPARATOR);
      return new PathFacetIterator(facets);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy