View Javadoc
1   package org.kuali.ole.docstore.process;
2   
3   import org.apache.solr.client.solrj.SolrServerException;
4   import org.kuali.ole.docstore.common.document.BibTrees;
5   import org.kuali.ole.docstore.common.util.BatchBibTreeDBUtil;
6   import org.kuali.ole.docstore.common.util.ReindexBatchStatistics;
7   import org.kuali.ole.docstore.engine.service.index.solr.BibMarcIndexer;
8   import org.kuali.ole.docstore.engine.service.index.solr.HoldingsOlemlIndexer;
9   import org.slf4j.Logger;
10  import org.slf4j.LoggerFactory;
11  
12  import java.io.IOException;
13  import java.sql.SQLException;
14  import java.util.*;
15  
16  /**
17   * Created with IntelliJ IDEA.
18   * User: sambasivam
19   * Date: 5/30/14
20   * Time: 2:51 PM
21   * To change this template use File | Settings | File Templates.
22   */
23  public class BibHoldingItemReindexer {
24  
25  //    int batchSize = 5000;
26      private String filePath =  System.getProperty("solr.solr.home");
27      private static BibHoldingItemReindexer bibHoldingItemReindexer = null;
28      private static BibMarcIndexer bibMarcIndexer = null;
29      private ReindexBatchStatistics totalBatchStatistics = new ReindexBatchStatistics();
30      private static final Logger LOG = LoggerFactory.getLogger(BibHoldingItemReindexer.class);
31      private ReindexBatchStatistics currentBatchStatistics = new ReindexBatchStatistics();
32      BatchBibTreeDBUtil bibTreeUtil = new BatchBibTreeDBUtil();
33  
34      public ReindexBatchStatistics getTotalBatchStatistics() {
35          return totalBatchStatistics;
36      }
37  
38      public void setTotalBatchStatistics(ReindexBatchStatistics totalBatchStatistics) {
39          this.totalBatchStatistics = totalBatchStatistics;
40      }
41  
42      public void index(int batchSize, int startIndex, int endIndex,String updateDate) throws Exception {
43  
44          totalBatchStatistics.setStartTime(new Date());
45  
46          BibMarcIndexer bibMarcIndexer = BibMarcIndexer.getInstance();
47  
48          int count = 1;
49  
50          bibTreeUtil.init(startIndex, endIndex,updateDate);
51  
52          BibTrees bibTrees = bibTreeUtil.fetchNextBatch(batchSize, totalBatchStatistics);
53  
54          while (bibTrees.getBibTrees().size() > 0) {
55              org.springframework.util.StopWatch stopWatch = new org.springframework.util.StopWatch();
56              try {
57                  stopWatch.start();
58                  bibMarcIndexer.createTrees(bibTrees, totalBatchStatistics);
59                  stopWatch.stop();
60                  totalBatchStatistics.addIndexTime(stopWatch.getTotalTimeMillis());
61  
62                  if(count * batchSize % 20000 == 0) {
63                      currentBatchStatistics.getBatchStatus(totalBatchStatistics);
64                      BatchBibTreeDBUtil.writeStatusToFile(filePath,  RebuildIndexesHandler.STATUS_FILE_NAME, currentBatchStatistics.toString());
65                      currentBatchStatistics.setData(totalBatchStatistics);
66  
67                  }
68  
69              } catch (Exception e) {
70                  BatchBibTreeDBUtil.writeStatusToFile(filePath, RebuildIndexesHandler.EXCEPION_FILE_NAME, "batch " + count + " failed due to *************\n" + e, bibTrees.getBibTrees().get(0).getBib().getId(),bibTrees.getBibTrees().get(bibTrees.getBibTrees().size()-1).getBib().getId());
71                  LOG.error("Exception while reindexing  : ", e);
72              }
73              count++;
74              bibTrees = bibTreeUtil.fetchNextBatch(batchSize, totalBatchStatistics);
75          }
76  
77          totalBatchStatistics.setEndTime(new Date());
78          BatchBibTreeDBUtil.writeStatusToFile(filePath,  RebuildIndexesHandler.STATUS_FILE_NAME, totalBatchStatistics.toString());
79  
80  
81          bibTreeUtil.fetchBibHoldings();
82          bibTreeUtil.fetchHoldingItems();
83  
84          Thread.sleep(3000);
85  
86          fetchHoldings();
87          fetchItems();
88          bibTreeUtil.closeConnections();
89      }
90  
91  
92      public String showStats() {
93  
94          return totalBatchStatistics.toString();
95      }
96  
97      public static BibHoldingItemReindexer getInstance() {
98          if(bibHoldingItemReindexer == null) {
99              bibHoldingItemReindexer = new BibHoldingItemReindexer();
100         }
101         return bibHoldingItemReindexer;
102     }
103 
104     public void fetchHoldings() throws SQLException, IOException, SolrServerException {
105         boolean cursor = true;
106         Map<String, List> map = bibTreeUtil.fetchBibHolding(cursor);
107         while (map != null) {
108             for (Map.Entry<String, List> stringListEntry : map.entrySet()) {
109                     String key = stringListEntry.getKey();
110                     List value = stringListEntry.getValue();
111                     BibMarcIndexer.getInstance().bind(key, value);
112             }
113             cursor = false;
114             map = bibTreeUtil.fetchBibHolding(cursor);
115 
116         }
117     }
118     public void fetchItems() throws SQLException, IOException, SolrServerException {
119         boolean cursor = true;
120         Map<String, List> map = bibTreeUtil.fetchHoldingItem(cursor);
121         while (map != null) {
122             for (Map.Entry<String, List> stringListEntry : map.entrySet()) {
123                     String key = stringListEntry.getKey();
124                     List value = stringListEntry.getValue();
125                     BibMarcIndexer.getInstance().bindAnalytics(key, value, "CREATE");
126             }
127             cursor = false;
128             map = bibTreeUtil.fetchHoldingItem(cursor);
129 
130         }
131     }
132 
133 }