zoie-solr插件修改:ZoieUpdateHandler

  1. package proj.zoie.solr;  
  2.   
  3. import it.unimi.dsi.fastutil.longs.LongArrayList;  
  4. import it.unimi.dsi.fastutil.longs.LongList;  
  5.   
  6. import java.io.IOException;  
  7. import java.net.URL;  
  8. import java.util.ArrayList;  
  9. import java.util.Arrays;  
  10. import java.util.List;  
  11. import java.util.concurrent.Future;  
  12.   
  13. import org.apache.log4j.Logger;  
  14. import org.apache.lucene.document.Document;  
  15. import org.apache.lucene.index.IndexReader;  
  16. import org.apache.lucene.index.MultiReader;  
  17. import org.apache.lucene.search.Collector;  
  18. import org.apache.lucene.search.IndexSearcher;  
  19. import org.apache.lucene.search.Query;  
  20. import org.apache.lucene.search.Scorer;  
  21. import org.apache.lucene.store.Directory;  
  22. import org.apache.solr.common.SolrException;  
  23. import org.apache.solr.common.util.NamedList;  
  24. import org.apache.solr.common.util.SimpleOrderedMap;  
  25. import org.apache.solr.core.SolrConfig;  
  26. import org.apache.solr.core.SolrCore;  
  27. import org.apache.solr.core.SolrInfoMBean.Category;  
  28. import org.apache.solr.search.QueryParsing;  
  29. import org.apache.solr.update.AddUpdateCommand;  
  30. import org.apache.solr.update.CommitUpdateCommand;  
  31. import org.apache.solr.update.DeleteUpdateCommand;  
  32. import org.apache.solr.update.MergeIndexesCommand;  
  33. import org.apache.solr.update.RollbackUpdateCommand;  
  34. import org.apache.solr.update.UpdateHandler;  
  35.   
  36. import proj.zoie.api.DataConsumer.DataEvent;  
  37. import proj.zoie.api.DocIDMapper;  
  38. import proj.zoie.api.Zoie;  
  39. import proj.zoie.api.ZoieException;  
  40. import proj.zoie.api.ZoieIndexReader;  
  41. import proj.zoie.api.indexing.IndexingEventListener;  
  42. import proj.zoie.impl.indexing.ZoieSystem;  
  43.   
  44. public class ZoieUpdateHandler extends UpdateHandler {  
  45.     private static Logger log = Logger.getLogger(ZoieUpdateHandler.class);  
  46.   
  47.     private SolrCore _core;  
  48.   
  49.     /** 
  50.      * 间隔多长时间重新打开searcher,默认值是一分钟 
  51.      */  
  52.     private static long intervalTime = 1000 * 60;// 一分钟  
  53.   
  54. //  private Directory indexDir;  
  55.   
  56.     private Zoie<IndexReader, DocumentWithID> zoie;  
  57.     /** 
  58.      * 记录最后一个版本 
  59.      */  
  60.     private String lastVersion;  
  61.   
  62.     public ZoieUpdateHandler(SolrCore core) {  
  63.         super(core);  
  64.         _core = core;  
  65.         SolrConfig solrConfig = _core.getSolrConfig();  
  66. //      try {  
  67. //          indexDir = _core.getDirectoryFactory().open(solrConfig.getDataDir());  
  68. //      } catch (Exception e) {  
  69. //          log.error("", e);  
  70. //      }  
  71.         long interval = solrConfig.getInt("solrIndexSearcher.reopen.batchDelay", (int) intervalTime);  
  72.         intervalTime = interval;  
  73.         log.info("intervalTime:" + intervalTime + "    ,间隔时间重新打开SolrIndexSearcher");  
  74.         ZoieSystemHome zoieHome = ZoieSystemHome.getInstance(_core);  
  75.         if (zoieHome == null) {  
  76.             log.error("zoie home is not setup");  
  77.             throw new RuntimeException("zoie is not setup");  
  78.         }  
  79.         zoie = zoieHome.getZoieSystem();  
  80. //      zoie.start();  
  81.         if (zoie == null) {  
  82.             log.error("zoie is not setup");  
  83.             throw new RuntimeException("zoie is not setup");  
  84.         }  
  85.         lastVersion = zoie.getCurrentReaderVersion();  
  86.           
  87.         log.info("###########################################################################" + lastVersion);  
  88.         runOpenSolrIndexSearcher();  
  89.         log.info("初始化结束");  
  90.   
  91.         ZoieSystem _zoie = (ZoieSystem) zoie;  
  92.         _zoie.addIndexingEventListener(new IndexingEventListener() {  
  93.             @Override  
  94.             public void handleIndexingEvent(IndexingEvent evt) {  
  95.                 log.info(" ###########刷数据到硬盘事件 ###########");  
  96.             }  
  97.   
  98.             @Override  
  99.             public void handleUpdatedDiskVersion(String version) {  
  100.                 log.info("indexingEventListener update version###########");  
  101.             }  
  102.         });  
  103.           
  104.         log.info(" core getLogId:"+core.getLogId()+",core getStartTime"+core.getStartTime());  
  105.     }  
  106.   
  107.     /** 
  108.      * 监控是否重新打开searcher 
  109.      */  
  110.     private void runOpenSolrIndexSearcher() {  
  111.         log.info("准备启动监控重新打开solrIndexSearcher的线程。。");  
  112.         Thread thread = new Thread() {  
  113.             public void run() {  
  114.                 try {  
  115.                     log.info("休息10秒。。");  
  116.                     Thread.sleep(10000);  
  117.                 } catch (Exception e) {  
  118.                     log.error("", e);  
  119.                 }  
  120.                 try {  
  121.                     while (true) {  
  122.                         log.info("休息" + intervalTime + "  毫秒。。");  
  123.                         Thread.sleep(intervalTime);  
  124.                         boolean hasChange = readerHasChange();  
  125.                         log.info("reader是否有修改过############################:" + hasChange);  
  126.                         if (hasChange) {  
  127.                             log.info("重新打开solrIndexSearcher");  
  128.                             try {  
  129.                                 updateReader(false);  
  130.                             } catch (Exception e) {  
  131.                                 log.error("", e);  
  132.                             }  
  133.                         }  
  134.                         log.info("休息");  
  135.                     }  
  136.                 } catch (Exception e) {  
  137.                     log.error("", e);  
  138.                 }  
  139.             }  
  140.         };  
  141.         thread.start();  
  142.         log.info("启动监听线程");  
  143.     }  
  144.   
  145.     /** 
  146.      * 检测reader是否有修改,如果有修改重新new searcher 
  147.      *  
  148.      * @return 
  149.      * @throws Exception 
  150.      */  
  151.     private boolean readerHasChange() throws Exception {  
  152.         String version = zoie.getCurrentReaderVersion();  
  153.         log.info("###########################################version:" + version);  
  154.         if (version == null) {  
  155.             return false;  
  156.         }  
  157.         if ((version != null && lastVersion == null) || version.compareTo(lastVersion) > 0) {  
  158.             lastVersion = version;  
  159.             return true;  
  160.         } else {  
  161.             return false;  
  162.         }  
  163.     }  
  164.   
  165.     @Override  
  166.     public int addDoc(AddUpdateCommand cmd) throws IOException {  
  167.           
  168.         ZoieSystemHome zoieHome = ZoieSystemHome.getInstance(_core);  
  169.         if (zoieHome == null) {  
  170.             log.error("zoie home is not setup");  
  171.             throw new RuntimeException("zoie is not setup");  
  172.         }  
  173.         zoie = zoieHome.getZoieSystem();  
  174.         if (zoie == null) {  
  175.             log.error("zoie is not setup");  
  176.             throw new RuntimeException("zoie is not setup");  
  177.         }  
  178.         // String id = cmd.getIndexedId(_core.getSchema());  
  179.         String uid2 = (String) cmd.solrDoc.getFieldValue(idField.getName());  
  180.         long zoieUid;  
  181.         try {  
  182.             zoieUid = Long.parseLong(uid2);  
  183.         } catch (Exception e) {  
  184.             throw new IOException("index uid must exist and of type long: " + uid2);  
  185.         }  
  186.         Document doc = cmd.doc;  
  187.         long time = System.currentTimeMillis();  
  188.         String version = String.valueOf(time);  
  189.         DataEvent<DocumentWithID> event = new DataEvent<DocumentWithID>(new DocumentWithID(zoieUid, doc), version);  
  190.         try {  
  191.             zoie.consume(Arrays.asList(event));  
  192.             return 1;  
  193.         } catch (ZoieException e) {  
  194.             log.error("", e);  
  195.             throw new IOException(e.toString());  
  196.         }  
  197.     }  
  198.   
  199.     @Override  
  200.     public void close() throws IOException {  
  201.         log.info("关闭zoieUpdateHandler ....close and ZoieSystemHome shutdown");  
  202.         ZoieSystemHome zoieHome = ZoieSystemHome.getInstance(_core);  
  203.         if (zoieHome != null) {  
  204.             zoieHome.shutdown();  
  205.         }  
  206.     }  
  207.   
  208.     private void updateReader(boolean waitForSearcher) throws IOException {  
  209.         callPostCommitCallbacks();  
  210.         Future[] waitSearcher = null;  
  211.         if (waitForSearcher) {  
  212.             waitSearcher = new Future[1];  
  213.         }  
  214.         core.getSearcher(truefalse, waitSearcher);  
  215.     }  
  216.   
  217.     @Override  
  218.     public void commit(CommitUpdateCommand cmd) throws IOException {  
  219.         if (zoie != null) {  
  220.             try {  
  221.                 zoie.flushEvents(10000);  
  222.             } catch (ZoieException e) {  
  223.                 log.error("", e);  
  224.             }  
  225.         }  
  226.         updateReader(cmd.waitSearcher);  
  227.     }  
  228.   
  229.     @Override  
  230.     public void delete(DeleteUpdateCommand cmd) throws IOException {  
  231.         String id = cmd.id;  
  232.         long zoieUid;  
  233.         try {  
  234.             zoieUid = Long.parseLong(id);  
  235.         } catch (Exception e) {  
  236.             throw new IOException("index uid must exist and of type long: " + id);  
  237.         }  
  238.           
  239.         ZoieSystemHome zoieHome = ZoieSystemHome.getInstance(_core);  
  240.         if (zoieHome == null) {  
  241.             log.error("zoie home is not setup");  
  242.             throw new RuntimeException("zoie is not setup");  
  243.         }  
  244.         zoie = zoieHome.getZoieSystem();  
  245.         if (zoie == null) {  
  246.             log.error("zoie is not setup");  
  247.             throw new RuntimeException("zoie is not setup");  
  248.         }  
  249.         // String version = zoie.getVersion();  
  250.         long time = System.currentTimeMillis();  
  251.         String version = String.valueOf(time);  
  252.         DataEvent<DocumentWithID> event = new DataEvent<DocumentWithID>(new DocumentWithID(zoieUid, true), version);  
  253.         try {  
  254.             zoie.consume(Arrays.asList(event));  
  255.         } catch (ZoieException e) {  
  256.             log.error(e.getMessage(), e);  
  257.             throw new IOException(e.toString());  
  258.         }  
  259.     }  
  260.   
  261.     @Override  
  262.     public void deleteByQuery(DeleteUpdateCommand cmd) throws IOException {  
  263. //      log.info("删除操作");  
  264.         Query q = QueryParsing.parseQuery(cmd.query, schema);  
  265.         ZoieSystemHome zoieHome = ZoieSystemHome.getInstance(_core);  
  266.         if (zoieHome == null) {  
  267.             log.error("zoie home is not setup");  
  268.             throw new RuntimeException("zoie is not setup");  
  269.         }  
  270.         zoie = zoieHome.getZoieSystem();  
  271.         if (zoie == null) {  
  272.             log.error("zoie is not setup");  
  273.             throw new RuntimeException("zoie is not setup");  
  274.         }  
  275.         final LongList delList = new LongArrayList();  
  276.         final int[] count = new int[1];  
  277.         count[0] = 0;  
  278.         List<ZoieIndexReader<IndexReader>> readerList = null;  
  279.         IndexSearcher searcher = null;  
  280.         try {  
  281.             readerList = zoie.getIndexReaders();  
  282.             MultiReader reader = new MultiReader(readerList.toArray(new IndexReader[readerList.size()]), false);  
  283.             searcher = new IndexSearcher(reader);  
  284.             searcher.search(q, new Collector() {  
  285.                 ZoieIndexReader<IndexReader> zoieReader = null;  
  286.                 int base = 0;  
  287.   
  288.                 @Override  
  289.                 public boolean acceptsDocsOutOfOrder() {  
  290.                     return true;  
  291.                 }  
  292.   
  293.                 @Override  
  294.                 public void collect(int doc) throws IOException {  
  295.                     try {  
  296.                         // long uid = zoieReader.getUID(doc + base);  
  297.                         long uid = zoieReader.getUID(doc);  
  298.                         if (uid != DocIDMapper.NOT_FOUND) {  
  299.                             delList.add(uid);  
  300.                         }  
  301.                     } catch (Exception e) {  
  302.                         count[0] += 1;  
  303.                     }  
  304.                 }  
  305.   
  306.                 @Override  
  307.                 public void setNextReader(IndexReader reader, int base) throws IOException {  
  308.                     zoieReader = (ZoieIndexReader<IndexReader>) reader;  
  309.                     this.base = base;  
  310.                 }  
  311.   
  312.                 @Override  
  313.                 public void setScorer(Scorer scorer) throws IOException {  
  314.                 }  
  315.             });  
  316.   
  317.         } catch (Exception e) {  
  318.             log.error("", e);  
  319.         } finally {  
  320.             try {  
  321.                 if (searcher != null) {  
  322.                     searcher.close();  
  323.                 }  
  324.             } finally {  
  325.                 if (readerList != null) {  
  326.                     zoie.returnIndexReaders(readerList);  
  327.                 }  
  328.             }  
  329.         }  
  330.   
  331.         log.info("删除条目:" + delList.size() + ",获取失败:" + count[0]);  
  332.   
  333.         if (delList.size() > 0) {  
  334.             ArrayList<DataEvent<DocumentWithID>> eventList = new ArrayList<DataEvent<DocumentWithID>>(delList.size());  
  335.             for (long val : delList) {  
  336.                 long time = System.currentTimeMillis();  
  337.                 String version = String.valueOf(time);  
  338.                 eventList.add(new DataEvent<DocumentWithID>(new DocumentWithID(val, true), version));  
  339.             }  
  340.             try {  
  341.                 zoie.consume(eventList);  
  342.             } catch (ZoieException e) {  
  343.                 log.error(e.getMessage(), e);  
  344.                 throw new IOException(e.toString());  
  345.             }  
  346.   
  347.         }  
  348.     }  
  349.   
  350.     @Override  
  351.     public int mergeIndexes(MergeIndexesCommand cmd) throws IOException {  
  352.         throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, ZoieUpdateHandler.class + " doesn't support mergeIndexes.");  
  353.     }  
  354.   
  355.     @Override  
  356.     public void rollback(RollbackUpdateCommand cmd) throws IOException {  
  357.         throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, ZoieUpdateHandler.class + " doesn't support rollback.");  
  358.     }  
  359.   
  360.     // ///  
  361.     // SolrInfoMBean stuff: Statistics and Module Info  
  362.     // ///  
  363.   
  364.     public String getName() {  
  365.         return ZoieUpdateHandler.class.getName();  
  366.     }  
  367.   
  368.     public String getVersion() {  
  369.         return SolrCore.version;  
  370.     }  
  371.   
  372.     public String getDescription() {  
  373.         return "Update handler builds on Zoie system";  
  374.     }  
  375.   
  376.     public Category getCategory() {  
  377.         return Category.UPDATEHANDLER;  
  378.     }  
  379.   
  380.     public String getSourceId() {  
  381.         return "$Id{1}quot;;  
  382.     }  
  383.   
  384.     public String getSource() {  
  385.         return "$URL{1}quot;;  
  386.     }  
  387.   
  388.     public URL[] getDocs() {  
  389.         return null;  
  390.     }  
  391.   
  392.     public NamedList getStatistics() {  
  393.         NamedList lst = new SimpleOrderedMap();  
  394.         lst.add("interval.open.searcher", intervalTime);  
  395.         lst.add("lastversion",lastVersion);  
  396.         return lst;  
  397.     }  
  398.   
  399. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值