solr索引操作(包括新增 更新 删除 提交 合并等)相关UML图如下

从上面的类图我们可以发现,其中体现了工厂方法模式及责任链模式的运用

UpdateRequestProcessor相当于责任链模式中的处理器角色,我们通过如下的对象图也许更能反映多个UpdateRequestProcessor类型的处理器的活动行为

UpdateRequestProcessorChain为请求处理器链,供客户端调用(内部依赖处理器工厂数组生成不同的处理器)

public final class UpdateRequestProcessorChain implements PluginInfoInitialized
{private UpdateRequestProcessorFactory[] chain;private final SolrCore solrCore;public UpdateRequestProcessorChain(SolrCore solrCore) {this.solrCore = solrCore;}public void init(PluginInfo info) {List<UpdateRequestProcessorFactory> list = solrCore.initPlugins(info.getChildren("processor"),UpdateRequestProcessorFactory.class,null);if(list.isEmpty()){throw new RuntimeException( "updateRequestProcessorChain require at least one processor");}chain = list.toArray(new UpdateRequestProcessorFactory[list.size()]); }public UpdateRequestProcessorChain( UpdateRequestProcessorFactory[] chain , SolrCore solrCore) {this.chain = chain;this.solrCore =  solrCore;}public UpdateRequestProcessor createProcessor(SolrQueryRequest req, SolrQueryResponse rsp) {UpdateRequestProcessor processor = null;UpdateRequestProcessor last = null;for (int i = chain.length-1; i>=0; i--) {processor = chain[i].getInstance(req, rsp, last);last = processor == null ? last : processor;}return last;}public UpdateRequestProcessorFactory[] getFactories() {return chain;}
}

UpdateRequestProcessorFactory为请求处理器抽象工厂类,用于实例化请求处理器(UpdateRequestProcessor),而具体的实例化过程延迟到子类实现

/*** A factory to generate an UpdateRequestProcessor for each request.  * * If the factory needs access to {@link SolrCore} in initialization, it could * implement {@link SolrCoreAware}* * @since solr 1.3*/
public abstract class UpdateRequestProcessorFactory implements NamedListInitializedPlugin
{    public void init( NamedList args ){// could process the Node
  }abstract public UpdateRequestProcessor getInstance( SolrQueryRequest req, SolrQueryResponse rsp, UpdateRequestProcessor next );
}

请求处理器抽象类UpdateRequestProcessor,持有对自身类型的引用(责任链模式的体现)

/*** This is a good place for subclassed update handlers to process the document before it is * indexed.  You may wish to add/remove fields or check if the requested user is allowed to * update the given document...* * Perhaps you continue adding an error message (without indexing the document)...* perhaps you throw an error and halt indexing (remove anything already indexed??)* * By default, this just passes the request to the next processor in the chain.* * @since solr 1.3*/
public abstract class UpdateRequestProcessor {protected final Logger log = LoggerFactory.getLogger(getClass());protected final UpdateRequestProcessor next;public UpdateRequestProcessor( UpdateRequestProcessor next) {this.next = next;}public void processAdd(AddUpdateCommand cmd) throws IOException {if (next != null) next.processAdd(cmd);}public void processDelete(DeleteUpdateCommand cmd) throws IOException {if (next != null) next.processDelete(cmd);}public void processMergeIndexes(MergeIndexesCommand cmd) throws IOException {if (next != null) next.processMergeIndexes(cmd);}public void processCommit(CommitUpdateCommand cmd) throws IOException{if (next != null) next.processCommit(cmd);}/*** @since Solr 1.4*/public void processRollback(RollbackUpdateCommand cmd) throws IOException{if (next != null) next.processRollback(cmd);}public void finish() throws IOException {if (next != null) next.finish();    }
}

具体工厂类RunUpdateProcessorFactory及具体请求处理器RunUpdateProcessor

/*** Pass the command to the UpdateHandler without any modifications* * @since solr 1.3*/
public class RunUpdateProcessorFactory extends UpdateRequestProcessorFactory
{@Overridepublic UpdateRequestProcessor getInstance(SolrQueryRequest req, SolrQueryResponse rsp, UpdateRequestProcessor next) {return new RunUpdateProcessor(req, next);}
}class RunUpdateProcessor extends UpdateRequestProcessor
{private final SolrQueryRequest req;private final UpdateHandler updateHandler;public RunUpdateProcessor(SolrQueryRequest req, UpdateRequestProcessor next) {super( next );this.req = req;this.updateHandler = req.getCore().getUpdateHandler();}@Overridepublic void processAdd(AddUpdateCommand cmd) throws IOException {cmd.doc = DocumentBuilder.toDocument(cmd.getSolrInputDocument(), req.getSchema());updateHandler.addDoc(cmd);super.processAdd(cmd);}@Overridepublic void processDelete(DeleteUpdateCommand cmd) throws IOException {if( cmd.id != null ) {updateHandler.delete(cmd);}else {updateHandler.deleteByQuery(cmd);}super.processDelete(cmd);}@Overridepublic void processMergeIndexes(MergeIndexesCommand cmd) throws IOException {updateHandler.mergeIndexes(cmd);super.processMergeIndexes(cmd);}@Overridepublic void processCommit(CommitUpdateCommand cmd) throws IOException{updateHandler.commit(cmd);super.processCommit(cmd);}/*** @since Solr 1.4*/@Overridepublic void processRollback(RollbackUpdateCommand cmd) throws IOException{updateHandler.rollback(cmd);super.processRollback(cmd);}
}

命令参数

/** An index update command encapsulated in an object (Command pattern)** @version $Id: UpdateCommand.java 1065312 2011-01-30 16:08:25Z rmuir $*/public class UpdateCommand {protected String commandName;public UpdateCommand(String commandName) {this.commandName = commandName;}@Overridepublic String toString() {return commandName;}}

AddUpdateCommand命令

/*** @version $Id: AddUpdateCommand.java 1145201 2011-07-11 15:18:47Z yonik $*/
public class AddUpdateCommand extends UpdateCommand {// optional id in "internal" indexed form... if it is needed and not supplied,// it will be obtained from the doc.public String indexedId;// The Lucene document to be indexedpublic Document doc;// Higher level SolrInputDocument, normally used to construct the Lucene Document// to index.public SolrInputDocument solrDoc;public boolean allowDups;public boolean overwritePending;public boolean overwriteCommitted;public Term updateTerm;public int commitWithin = -1;/** Reset state to reuse this object with a different document in the same request */public void clear() {doc = null;solrDoc = null;indexedId = null;}public SolrInputDocument getSolrInputDocument() {return solrDoc;}public Document getLuceneDocument(IndexSchema schema) {if (doc == null && solrDoc != null) {// TODO??  build the doc from the SolrDocument?
     }return doc;    }public String getIndexedId(IndexSchema schema) {if (indexedId == null) {SchemaField sf = schema.getUniqueKeyField();if (sf != null) {if (doc != null) {schema.getUniqueKeyField();Fieldable storedId = doc.getFieldable(sf.getName());indexedId = sf.getType().storedToIndexed(storedId);}if (solrDoc != null) {SolrInputField field = solrDoc.getField(sf.getName());if (field != null) {indexedId = sf.getType().toInternal( field.getFirstValue().toString() );}}}}return indexedId;}public String getPrintableId(IndexSchema schema) {SchemaField sf = schema.getUniqueKeyField();if (indexedId != null && sf != null) {return sf.getType().indexedToReadable(indexedId);}if (doc != null) {return schema.printableUniqueKey(doc);}if (solrDoc != null && sf != null) {SolrInputField field = solrDoc.getField(sf.getName());if (field != null) {return field.getFirstValue().toString();}}return "(null)";}public AddUpdateCommand() {super("add");}@Overridepublic String toString() {StringBuilder sb = new StringBuilder(commandName);sb.append(':');if (indexedId !=null) sb.append("id=").append(indexedId);sb.append(",allowDups=").append(allowDups);sb.append(",overwritePending=").append(overwritePending);sb.append(",overwriteCommitted=").append(overwriteCommitted);return sb.toString();}}

DeleteUpdateCommand命令

/*** @version $Id: DeleteUpdateCommand.java 1235304 2012-01-24 15:39:17Z janhoy $*/
public class DeleteUpdateCommand extends UpdateCommand {public String id;    // external (printable) id, for delete-by-idpublic String query; // query string for delete-by-querypublic boolean fromPending;public boolean fromCommitted;public int commitWithin = -1;public DeleteUpdateCommand() {super("delete");}@Overridepublic String toString() {StringBuilder sb = new StringBuilder(commandName);sb.append(':');if (id!=null) sb.append("id=").append(id);else sb.append("query=`").append(query).append('`');sb.append(",fromPending=").append(fromPending);sb.append(",fromCommitted=").append(fromCommitted);sb.append(",commitWithin=").append(commitWithin);return sb.toString();}
}

---------------------------------------------------------------------------

本系列solr&lucene3.6.0源码解析系本人原创

转载请注明出处 博客园 刺猬的温驯

本人邮箱: chenying998179#163.com (#改为@)

本文链接http://www.cnblogs.com/chenying99/p/3501172.html

solrlucene3.6.0源码解析(三)相关推荐

  1. AFNetworking2.0源码解析三

    本篇说说安全相关的AFSecurityPolicy模块,AFSecurityPolicy用于验证HTTPS请求的证书,先来看看HTTPS的原理和证书相关的几个问题. HTTPS HTTPS连接建立过程 ...

  2. Heritrix 3.1.0 源码解析(三十四)

    本文主要分析FetchFTP处理器,该处理器用于ftp文件的下载,该处理器的实现是通过封装commons-net-2.0.jar组件来实现ftp文件下载 在FetchFTP处理器里面定义了内部类Soc ...

  3. 锚框、交并比和非极大值抑制(tf2.0源码解析)

    锚框.交并比和非极大值抑制(tf2.0源码解析) 文章目录 锚框.交并比和非极大值抑制(tf2.0源码解析) 一.锚框生成 1.锚框的宽高 2.锚框的个数 3.注意点(★★★) 4.tf2.0代码 二 ...

  4. Android Glide 3.7.0 源码解析(八) , RecyclableBufferedInputStream 的 mark/reset 实现

    个人博客传送门 一.mark / reset 的作用 Android Glide 3.7.0 源码解析(七) , 细说图形变换和解码有提到过RecyclableBufferedInputStream ...

  5. Heritrix 3.1.0 源码解析(八)

    本文接着分析存储CrawlURI curi的队列容器,最重要的是BdbWorkQueue类及BdbMultipleWorkQueues类 BdbWorkQueue类继承自抽象类WorkQueue,抽象 ...

  6. Heritrix 3.1.0 源码解析(六)

    本文分析BdbFrontier对象的相关状态和方法 BdbFrontier类继承自WorkQueueFrontier类   WorkQueueFrontier类继承自AbstractFrontier类 ...

  7. Heritrix 3.1.0 源码解析(十一)

    上文分析了Heritrix3.1.0系统是怎么添加CrawlURI curi对象的,那么在系统初始化的时候,是怎么载入CrawlURI curi种子的呢? 我们回顾前面的文章,在我们执行采集任务的la ...

  8. Heritrix 3.1.0 源码解析(十四)

    我在分析BdbFrontier对象的void schedule(CrawlURI caURI).CrawlURI next() .void finished(CrawlURI cURI)方法是,其实还 ...

  9. Disruptor源码解析三 RingBuffer解析

    目录 系列索引 前言 主要内容 RingBuffer的要点 源码解析 系列索引 Disruptor源码解析一 Disruptor高性能之道 Disruptor源码解析二 Sequence相关类解析 D ...

最新文章

  1. hexo博客系统安装
  2. Vishay将MCW 0406 AT系列精密宽端子薄膜片式电阻欧姆值降至业内最低
  3. 《贝叶斯思维:统计建模的Python学习法》——1.8 讨论
  4. 分段二次插值例题_分段三次插值
  5. [转] Java中的容器
  6. 搞AI的产品经理该怎么写PRD?谷歌的导师教你
  7. Shell 基本语法
  8. 《大话操作系统——扎实project实践派》(8.2)(除了指令集.完)
  9. 交换机cad图例_各种弱电系统的CAD图纸,包含图例、大样图、系统图及原理图等...
  10. php连接sql server
  11. 如何从零学习游戏开发
  12. python执行外部方法_python执行外部程序的常用方法小结
  13. lgv50进入工程模式_LG手机工程模式进入方法及菜单指令翻译(适用G6、G7、V20、V30等)...
  14. Java-用类描述人之间的血缘关系
  15. 6.PMAC下位机-下位机编程
  16. 保留两位小数的四舍五入
  17. 三参数坐标转换matlab,Coordinate-conversion
  18. 30个物联网产业动向 芯片商也来抢占IoT
  19. Linux 之十七 Ubuntu 22.04 配置内核版本、GRUB 引导、远程桌面
  20. 火山PC文件目录的创建复制移动等操作

热门文章

  1. 搜索Idiot就出现特朗普图片,算法无罪!
  2. 干货丨一份不可多得的深度学习技巧指南
  3. 认知AI的兴起:2025年AI将会发生质的飞跃
  4. 无线网络未来十年十大产业趋势
  5. 人工智能皇冠上的明珠:自然语言处理简介、最新进展、未来趋势
  6. 神经科学中的数学之美
  7. 蝙蝠为啥这么厉害?地球人整明白了没有?
  8. 科大讯飞:让世界听见AI的声音
  9. 4位数学家获得2018年菲尔兹奖
  10. 昆虫大脑帮助AI解决导航难题