solr dataimport 数据导入源码分析(四)

本文解析了EntityProcessorWrapper类的功能及其实现方式,该类作为SqlEntityProcessor的代理,增加了缓存处理和格式转换功能。

我们查看DocBuilder类的源码发现,并不是直接持有对SqlEntityProcessor类的引用,而是另外一个包装类EntityProcessorWrapper,EntityProcessorWrapper直接继承自抽象类EntityProcessor,而不是继承自中间的EntityProcessorBase类,简要类图如下

 包装类额外添加了缓存处理以及格式转换等功能,然后调用SqlEntityProcessor相应方法进行处理,相当于SqlEntityProcessor的代理类,共同继承自抽象类EntityProcessor(SqlEntityProcessor 是通过EntityProcessorBase 间接继承自EntityProcessor)

 EntityProcessorWrapper类的简要代码如下

public class EntityProcessorWrapper extends EntityProcessor {
  private static final Logger log = LoggerFactory.getLogger(EntityProcessorWrapper.class);

  EntityProcessor delegate;
  private DocBuilder docBuilder;

  String onError;
  Context context;
  protected VariableResolverImpl resolver;
  String entityName;

  protected List<Transformer> transformers;

  protected List<Map<String, Object>> rowcache;

  public EntityProcessorWrapper(EntityProcessor delegate, DocBuilder docBuilder) {
    this.delegate = delegate;
    this.docBuilder = docBuilder;
  }

  @Override
  public void init(Context context) {

    delegate.init(context);

  }
  
  protected Map<String, Object> getFromRowCache() {
    if (rowcache.isEmpty()){
      return null;
    }
    return rowcache.remove(0);
  }

  @Override
  public Map<String, Object> nextRow() {
    pullRow();
  }
  

  protected Map<String,Object> pullRow() {
    Map<String,Object> arow = null;
    try {
      arow = delegate.nextRow();
    } catch (Exception e) {
      if (ABORT.equals(onError)) {
        wrapAndThrow(SEVERE, e);
      } else {
        // SKIP is not really possible. If this calls the nextRow() again the
        
// Entityprocessor would be in an inconistent state
        log.error("Exception in entity : " + entityName, e);
        return null;
      }
    }
    log.debug("arow : {}", arow);
    return arow;
  }

  @Override
  public Map<String, Object> nextModifiedRowKey() {
    Map<String, Object> row = delegate.nextModifiedRowKey();
    row = applyTransformer(row);
    rowcache = null;
    return row;
  }

  @Override
  public Map<String, Object> nextDeletedRowKey() {
    Map<String, Object> row = delegate.nextDeletedRowKey();
    row = applyTransformer(row);
    rowcache = null;
    return row;
  }

  @Override
  public Map<String, Object> nextModifiedParentRowKey() {
    return delegate.nextModifiedParentRowKey();
  }

  @Override
  public void destroy() {
    delegate.destroy();
  }
  

  @Override
  public void close() {
    delegate.close();
  }

转载于:https://www.cnblogs.com/chenying99/archive/2012/09/09/2677764.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值