James Moger
2012-03-21 f1d2ada42c5e3640656d805155e1bcadb95fd126
src/com/gitblit/LuceneExecutor.java
@@ -25,7 +25,6 @@
import java.text.MessageFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
@@ -138,35 +137,24 @@
   private final Map<String, IndexSearcher> searchers = new ConcurrentHashMap<String, IndexSearcher>();
   private final Map<String, IndexWriter> writers = new ConcurrentHashMap<String, IndexWriter>();
   
   private final Set<String> excludedExtensions = new TreeSet<String>(Arrays.asList("7z", "arc",
         "arj", "bin", "bmp", "dll", "doc", "docx", "exe", "gif", "gz", "jar", "jpg", "lib",
         "lzh", "odg", "pdf", "ppt", "png", "so", "swf", "xcf", "xls", "xlsx", "zip"));
   private final String luceneIgnoreExtensions = "7z arc arj bin bmp dll doc docx exe gif gz jar jpg lib lzh odg odf odt pdf ppt png so swf xcf xls xlsx zip";
   private Set<String> excludedExtensions;
   public LuceneExecutor(IStoredSettings settings, File repositoriesFolder) {
      this.storedSettings = settings;
      this.repositoriesFolder = repositoriesFolder;
   }
   /**
    * Indicates if the Lucene executor can index repositories.
    *
    * @return true if the Lucene executor is ready to index repositories
    */
   public boolean isReady() {
      return storedSettings.getBoolean(Keys.lucene.enable, false);
   }
   /**
    * Run is executed by the gitblit executor service at whatever frequency
    * is specified in the settings.  Because this is called by an executor
    * service, calls will queue - i.e. there can never be concurrent execution
    * of repository index updates.
    * Run is executed by the Gitblit executor service.  Because this is called
    * by an executor service, calls will queue - i.e. there can never be
    * concurrent execution of repository index updates.
    */
   @Override
   public void run() {
      if (!isReady()) {
         return;
      }
      // reload the excluded extensions
      String exts = storedSettings.getString(Keys.web.luceneIgnoreExtensions, luceneIgnoreExtensions);
      excludedExtensions = new TreeSet<String>(StringUtils.getStringsFromValue(exts));
      for (String repositoryName: GitBlit.self().getRepositoryList()) {
         RepositoryModel model = GitBlit.self().getRepositoryModel(repositoryName);
@@ -228,7 +216,16 @@
    * 
    * @param repositoryName
    */
   public void close(String repositoryName) {
   public synchronized void close(String repositoryName) {
      try {
         IndexSearcher searcher = searchers.remove(repositoryName);
         if (searcher != null) {
            searcher.getIndexReader().close();
         }
      } catch (Exception e) {
         logger.error("Failed to close index searcher for " + repositoryName, e);
      }
      try {
         IndexWriter writer = writers.remove(repositoryName);
         if (writer != null) {
@@ -236,23 +233,14 @@
         }
      } catch (Exception e) {
         logger.error("Failed to close index writer for " + repositoryName, e);
      }
      try {
         IndexSearcher searcher = searchers.remove(repositoryName);
         if (searcher != null) {
            searcher.close();
         }
      } catch (Exception e) {
         logger.error("Failed to close index searcher for " + repositoryName, e);
      }
      }
   }
   /**
    * Close all Lucene indexers.
    * 
    */
   public void close() {
   public synchronized void close() {
      // close all writers
      for (String writer : writers.keySet()) {
         try {
@@ -266,7 +254,7 @@
      // close all searchers
      for (String searcher : searchers.keySet()) {
         try {
            searchers.get(searcher).close();
            searchers.get(searcher).getIndexReader().close();
         } catch (Throwable t) {
            logger.error("Failed to close Lucene searcher for " + searcher, t);
         }
@@ -283,18 +271,9 @@
    */
   public boolean deleteIndex(String repositoryName) {
      try {
         // remove the repository index writer from the cache and close it
         IndexWriter writer = writers.remove(repositoryName);
         if (writer != null) {
            writer.close();
            writer = null;
         }
         // remove the repository index searcher from the cache and close it
         IndexSearcher searcher = searchers.remove(repositoryName);
         if (searcher != null) {
            searcher.close();
            searcher = null;
         }
         // close any open writer/searcher
         close(repositoryName);
         // delete the index folder
         File repositoryFolder = new File(repositoriesFolder, repositoryName);
         File luceneIndex = new File(repositoryFolder, LUCENE_DIR);
@@ -420,7 +399,7 @@
    * @return IndexResult
    */
   public IndexResult reindex(RepositoryModel model, Repository repository) {
      IndexResult result = new IndexResult();
      IndexResult result = new IndexResult();
      if (!deleteIndex(model.name)) {
         return result;
      }
@@ -615,8 +594,8 @@
         // commit all changes and reset the searcher
         config.setInt(CONF_INDEX, null, CONF_VERSION, INDEX_VERSION);
         config.save();
         resetIndexSearcher(model.name);
         writer.commit();
         resetIndexSearcher(model.name);
         result.success();
      } catch (Exception e) {
         logger.error("Exception while reindexing " + model.name, e);
@@ -645,7 +624,7 @@
         IndexWriter writer = getIndexWriter(repositoryName);
         for (PathChangeModel path : changedPaths) {
            // delete the indexed blob
            deleteBlob(repositoryName, branch, path.path);
            deleteBlob(repositoryName, branch, path.name);
            // re-index the blob
            if (!ChangeType.DELETE.equals(path.changeType)) {
@@ -678,8 +657,17 @@
            }
         }
         writer.commit();
         Document doc = createDocument(commit, null);
         // get any annotated commit tags
         List<String> commitTags = new ArrayList<String>();
         for (RefModel ref : JGitUtils.getTags(repository, true, -1)) {
            if (ref.isAnnotatedTag() && ref.getReferencedObjectId().equals(commit.getId())) {
               commitTags.add(ref.displayName);
            }
         }
         // create and write the Lucene document
         Document doc = createDocument(commit, commitTags);
         doc.add(new Field(FIELD_BRANCH, branch, Store.YES, Index.ANALYZED));
         result.commitCount++;
         result.success = index(repositoryName, doc);
@@ -931,8 +919,8 @@
      try {         
         IndexWriter writer = getIndexWriter(repositoryName);
         writer.addDocument(doc);
         resetIndexSearcher(repositoryName);
         writer.commit();
         resetIndexSearcher(repositoryName);
         return true;
      } catch (Exception e) {
         logger.error(MessageFormat.format("Exception while incrementally updating {0} Lucene index", repositoryName), e);
@@ -940,8 +928,10 @@
      return false;
   }
   private SearchResult createSearchResult(Document doc, float score) throws ParseException {
   private SearchResult createSearchResult(Document doc, float score, int hitId, int totalHits) throws ParseException {
      SearchResult result = new SearchResult();
      result.hitId = hitId;
      result.totalHits = totalHits;
      result.score = score;
      result.date = DateTools.stringToDate(doc.get(FIELD_DATE));
      result.summary = doc.get(FIELD_SUMMARY);      
@@ -964,7 +954,7 @@
   private synchronized void resetIndexSearcher(String repository) throws IOException {
      IndexSearcher searcher = searchers.remove(repository);
      if (searcher != null) {
         searcher.close();
         searcher.getIndexReader().close();
      }
   }
@@ -1017,19 +1007,21 @@
    * 
    * @param text
    *            if the text is null or empty, null is returned
    * @param maximumHits
    *            the maximum number of hits to collect
    * @param page
    *            the page number to retrieve. page is 1-indexed.
    * @param pageSize
    *            the number of elements to return for this page
    * @param repositories
    *            a list of repositories to search. if no repositories are
    *            specified null is returned.
    * @return a list of SearchResults in order from highest to the lowest score
    * 
    */
   public List<SearchResult> search(String text, int maximumHits, List<String> repositories) {
   public List<SearchResult> search(String text, int page, int pageSize, List<String> repositories) {
      if (ArrayUtils.isEmpty(repositories)) {
         return null;
      }
      return search(text, maximumHits, repositories.toArray(new String[0]));
      return search(text, page, pageSize, repositories.toArray(new String[0]));
   }
   
   /**
@@ -1037,15 +1029,17 @@
    * 
    * @param text
    *            if the text is null or empty, null is returned
    * @param maximumHits
    *            the maximum number of hits to collect
    * @param page
    *            the page number to retrieve. page is 1-indexed.
    * @param pageSize
    *            the number of elements to return for this page
    * @param repositories
    *            a list of repositories to search. if no repositories are
    *            specified null is returned.
    * @return a list of SearchResults in order from highest to the lowest score
    * 
    */
   public List<SearchResult> search(String text, int maximumHits, String... repositories) {
    */
   public List<SearchResult> search(String text, int page, int pageSize, String... repositories) {
      if (StringUtils.isEmpty(text)) {
         return null;
      }
@@ -1082,14 +1076,15 @@
            searcher = new IndexSearcher(reader);
         }
         Query rewrittenQuery = searcher.rewrite(query);
         TopScoreDocCollector collector = TopScoreDocCollector.create(maximumHits, true);
         TopScoreDocCollector collector = TopScoreDocCollector.create(5000, true);
         searcher.search(rewrittenQuery, collector);
         ScoreDoc[] hits = collector.topDocs().scoreDocs;
         int offset = Math.max(0, (page - 1) * pageSize);
         ScoreDoc[] hits = collector.topDocs(offset, pageSize).scoreDocs;
         int totalHits = collector.getTotalHits();
         for (int i = 0; i < hits.length; i++) {
            int docId = hits[i].doc;
            Document doc = searcher.doc(docId);
            // TODO identify the source index for the doc, then eliminate FIELD_REPOSITORY
            SearchResult result = createSearchResult(doc, hits[i].score);
            SearchResult result = createSearchResult(doc, hits[i].score, offset + i + 1, totalHits);
            if (repositories.length == 1) {
               // single repository search
               result.repository = repositories[0];
@@ -1121,17 +1116,14 @@
    */
   private String getHighlightedFragment(Analyzer analyzer, Query query,
         String content, SearchResult result) throws IOException, InvalidTokenOffsetsException {
      content = content == null ? "":StringUtils.escapeForHtml(content, false);
      if (content == null) {
         content = "";
      }
      int fragmentLength = SearchObjectType.commit == result.type ? 512 : 150;
      QueryScorer scorer = new QueryScorer(query, "content");
      Fragmenter fragmenter;
      // TODO improve the fragmenter - hopefully on line breaks
      if (SearchObjectType.commit == result.type) {
         fragmenter = new SimpleSpanFragmenter(scorer, 1024);
      } else {
         fragmenter = new SimpleSpanFragmenter(scorer, 150);
      }
      Fragmenter fragmenter = new SimpleSpanFragmenter(scorer, fragmentLength);
      // use an artificial delimiter for the token
      String termTag = "<!--[";
@@ -1139,22 +1131,64 @@
      SimpleHTMLFormatter formatter = new SimpleHTMLFormatter(termTag, termTagEnd);
      Highlighter highlighter = new Highlighter(formatter, scorer);      
      highlighter.setTextFragmenter(fragmenter);
      String [] fragments = highlighter.getBestFragments(analyzer, "content", content, 5);
      String [] fragments = highlighter.getBestFragments(analyzer, "content", content, 3);
      if (ArrayUtils.isEmpty(fragments)) {
         if (SearchObjectType.blob  == result.type) {
            return "";
         }
         return "<pre class=\"text\">" + content + "</pre>";
         // clip commit message
         String fragment = content;
         if (fragment.length() > fragmentLength) {
            fragment = fragment.substring(0, fragmentLength) + "...";
         }
         return "<pre class=\"text\">" + StringUtils.escapeForHtml(fragment, true) + "</pre>";
      }
      int contentPos = 0;
      StringBuilder sb = new StringBuilder();
      for (int i = 0, len = fragments.length; i < len; i++) {
         String fragment = fragments[i];
         String tag = "<pre class=\"text\">";
         // resurrect the raw fragment from removing the artificial delimiters
         String raw = fragment.replace(termTag, "").replace(termTagEnd, "");
         sb.append(getPreTag(result, raw, content));
         String raw = fragment.replace(termTag, "").replace(termTagEnd, "");
         // determine position of the raw fragment in the content
         int pos = content.indexOf(raw, contentPos);
         // restore complete first line of fragment
         int c = pos;
         while (c > 0) {
            c--;
            if (content.charAt(c) == '\n') {
               break;
            }
         }
         if (c > 0) {
            // inject leading chunk of first fragment line
            fragment = content.substring(c + 1, pos) + fragment;
         }
         if (SearchObjectType.blob  == result.type) {
            // count lines as offset into the content for this fragment
            int line = StringUtils.countLines(content.substring(0, pos));
            // create fragment tag with line number and language
            String lang = "";
            String ext = StringUtils.getFileExtension(result.path).toLowerCase();
            if (!StringUtils.isEmpty(ext)) {
               // maintain leading space!
               lang = " lang-" + ext;
            }
            tag = MessageFormat.format("<pre class=\"prettyprint linenums:{0,number,0}{1}\">", line, lang);
            // update offset into content
            contentPos = pos + raw.length() + 1;
         }
         
         sb.append(tag);
         // replace the artificial delimiter with html tags
         String html = fragment.replace(termTag, "<span class=\"highlight\">").replace(termTagEnd, "</span>");
         sb.append(html);
@@ -1164,31 +1198,7 @@
         }
      }
      return sb.toString();
   }
   /**
    * Returns the appropriate tag for a fragment. Commit messages are visually
    * differentiated from blob fragments.
    *
    * @param result
    * @param fragment
    * @param content
    * @return an html tag appropriate for the fragment
    */
   private String getPreTag(SearchResult result, String fragment, String content) {
      String pre = "<pre class=\"text\">";
      if (SearchObjectType.blob  == result.type) {
         int line = StringUtils.countLines(content.substring(0, content.indexOf(fragment)));
         int lastDot = result.path.lastIndexOf('.');
         if (lastDot > -1) {
            String ext = result.path.substring(lastDot + 1).toLowerCase();
            pre = MessageFormat.format("<pre class=\"prettyprint linenums:{0,number,0} lang-{1}\">", line, ext);
         } else {
            pre = MessageFormat.format("<pre class=\"prettyprint linenums:{0,number,0}\">", line);
         }
      }
      return pre;
   }
   }
   
   /**
    * Simple class to track the results of an index update.