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  
35      public void index(int batchSize, int startIndex, int endIndex) throws Exception {
36  
37          totalBatchStatistics.setStartTime(new Date());
38  
39          BibMarcIndexer bibMarcIndexer = BibMarcIndexer.getInstance();
40  
41          int count = 1;
42  
43          bibTreeUtil.init(startIndex, endIndex);
44  
45          BibTrees bibTrees = bibTreeUtil.fetchNextBatch(batchSize, totalBatchStatistics);
46  
47          while (bibTrees.getBibTrees().size() > 0) {
48              org.springframework.util.StopWatch stopWatch = new org.springframework.util.StopWatch();
49              try {
50                  stopWatch.start();
51                  bibMarcIndexer.createTrees(bibTrees, totalBatchStatistics);
52                  stopWatch.stop();
53                  totalBatchStatistics.addIndexTime(stopWatch.getTotalTimeMillis());
54  
55                  if(count * batchSize % 20000 == 0) {
56                      currentBatchStatistics.getBatchStatus(totalBatchStatistics);
57                      BatchBibTreeDBUtil.writeStatusToFile(filePath,  RebuildIndexesHandler.STATUS_FILE_NAME, currentBatchStatistics.toString());
58                      currentBatchStatistics.setData(totalBatchStatistics);
59  
60                  }
61  
62              } catch (Exception e) {
63                  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());
64                  LOG.error("Exception while reindexing  : ", e);
65              }
66              count++;
67              bibTrees = bibTreeUtil.fetchNextBatch(batchSize, totalBatchStatistics);
68          }
69  
70          totalBatchStatistics.setEndTime(new Date());
71          BatchBibTreeDBUtil.writeStatusToFile(filePath,  RebuildIndexesHandler.STATUS_FILE_NAME, totalBatchStatistics.toString());
72  
73  
74          bibTreeUtil.fetchBibHoldings();
75          bibTreeUtil.fetchHoldingItems();
76  
77          Thread.sleep(3000);
78  
79          fetchHoldings();
80          fetchItems();
81          bibTreeUtil.closeConnections();
82      }
83  
84  
85      public String showStats() {
86  
87          return totalBatchStatistics.toString();
88      }
89  
90      public static BibHoldingItemReindexer getInstance() {
91          if(bibHoldingItemReindexer == null) {
92              bibHoldingItemReindexer = new BibHoldingItemReindexer();
93          }
94          return bibHoldingItemReindexer;
95      }
96  
97      public void fetchHoldings() throws SQLException, IOException, SolrServerException {
98          boolean cursor = true;
99          Map<String, List> map = bibTreeUtil.fetchBibHolding(cursor);
100         while (map != null) {
101             for (Map.Entry<String, List> stringListEntry : map.entrySet()) {
102                     String key = stringListEntry.getKey();
103                     List value = stringListEntry.getValue();
104                     BibMarcIndexer.getInstance().bind(key, value);
105             }
106             cursor = false;
107             map = bibTreeUtil.fetchBibHolding(cursor);
108 
109         }
110     }
111     public void fetchItems() throws SQLException, IOException, SolrServerException {
112         boolean cursor = true;
113         Map<String, List> map = bibTreeUtil.fetchHoldingItem(cursor);
114         while (map != null) {
115             for (Map.Entry<String, List> stringListEntry : map.entrySet()) {
116                     String key = stringListEntry.getKey();
117                     List value = stringListEntry.getValue();
118                     BibMarcIndexer.getInstance().bindAnalytics(key, value, "CREATE");
119             }
120             cursor = false;
121             map = bibTreeUtil.fetchHoldingItem(cursor);
122 
123         }
124     }
125 
126 }