From 7db0929ad58804ebc235730b9cfc83dc38835eb9 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Sun, 25 Mar 2012 13:15:06 -0400
Subject: [PATCH] Added web.allowLuceneIndexing to enable/disable Lucene integration

---
 src/com/gitblit/wicket/pages/LuceneSearchPage.java   |   16 ++++++++++------
 docs/01_setup.mkd                                    |    8 ++++++--
 src/com/gitblit/wicket/GitBlitWebApp.properties      |    3 ++-
 src/com/gitblit/wicket/pages/EditRepositoryPage.java |    5 ++---
 docs/04_releases.mkd                                 |    4 +++-
 src/com/gitblit/wicket/pages/RootPage.java           |    4 +++-
 distrib/gitblit.properties                           |    9 +++++++++
 src/com/gitblit/LuceneExecutor.java                  |    4 ++++
 resources/gitblit.css                                |    3 ++-
 src/com/gitblit/wicket/pages/RepositoryPage.java     |    3 ++-
 10 files changed, 43 insertions(+), 16 deletions(-)

diff --git a/distrib/gitblit.properties b/distrib/gitblit.properties
index ad82280..2846496 100644
--- a/distrib/gitblit.properties
+++ b/distrib/gitblit.properties
@@ -184,6 +184,15 @@
 # SINCE 0.5.0   
 web.allowZipDownloads = true
 
+# Allow optional Lucene integration. Lucene indexing is an opt-in feature.
+# A repository may specify branches to index with Lucene instead of using Git
+# commit traversal. There are scenarios where you may want to completely disable
+# Lucene indexing despite a repository specifying indexed branches.  One such
+# scenario is on a resource-constrained federated Gitblit mirror.
+#
+# SINCE 0.9.0
+web.allowLuceneIndexing = true
+
 # Use Clippy (Flash solution) to provide a copy-to-clipboard button.
 # If false, a button with a more primitive JavaScript-based prompt box will
 # offer a 3-step (click, ctrl+c, enter) copy-to-clipboard alternative.
diff --git a/docs/01_setup.mkd b/docs/01_setup.mkd
index 09ddabb..c5d78de 100644
--- a/docs/01_setup.mkd
+++ b/docs/01_setup.mkd
@@ -385,11 +385,11 @@
 
 ### How do I use it?
 
-Lucene indexing is an opt-in feature which means that no repositories are automatically indexed.  
+First you must ensure that *web.allowLuceneIndexing* is set *true* in `gitblit.properties` or `web.xml`.  Then you must understand that Lucene indexing is an opt-in feature which means that no repositories are automatically indexed.  
 Like anything else, this design has pros and cons.
 
 #### Pros
-1. no wasted cycles on repositories you will never search
+1. no wasted cycles indexing repositories you will never search
 2. you specify exactly what branches are indexed; experimental/dead/personal branches can be ignored
 
 #### Cons
@@ -408,6 +408,10 @@
 **NOTE:**  
 After specifying branches, only the content from those branches can be searched via Gitblit.  Gitblit will automatically redirect any queries entered on a repository's search box to the Lucene search page. Repositories that do not specify any indexed branches will use the traditional commit-traversal search.
 
+#### Adequate Heap
+
+The initial indexing of an existing repository can potentially exhaust the memory allocated to your Java instance and may throw OutOfMemory exceptions.  Be sure to provide your Gitblit server adequate heap space to index your repositories.  The heap is set using the *-Xmx* JVM parameter in your Gitblit launch command (e.g. -Xmx1024M).
+
 ## Client Setup and Configuration
 ### Https with Self-Signed Certificates
 You must tell Git/JGit not to verify the self-signed certificate in order to perform any remote Git operations.
diff --git a/docs/04_releases.mkd b/docs/04_releases.mkd
index 6e48210..5fec6e9 100644
--- a/docs/04_releases.mkd
+++ b/docs/04_releases.mkd
@@ -17,10 +17,12 @@
 #### additions
 
 - Added optional Lucene branch indexing (issue 16)  
+    **New:** *web.allowLuceneIndexing = true*  
     **New:** *web.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*  
 Repository branches may be optionally indexed by Lucene for improved searching.  To use this feature you must specify which branches to index within the *Edit Repository* page; _no repositories are automatically indexed_.  Gitblit will build or incrementally update enrolled repositories on a 2 minute cycle. (i.e you will have to wait 2-3 minutes after respecifying indexed branches or pushing new commits before Gitblit will build/update the repository's Lucene index.)  
 If a repository has Lucene-indexed branches the *search* form on the repository pages will redirect to the root-level Lucene search page and only the content of those branches can be searched.  
-If the repository does not specify any indexed branches then repository commit-traversal search is used.
+If the repository does not specify any indexed branches then repository commit-traversal search is used.  
+**Note:** Initial indexing of an existing repository can be memory-exhaustive. Be sure to provide your Gitblit server adequate heap space to index your repositories (e.g. -Xmx1024M).
 - Allow specifying timezone to use for Gitblit which is independent of both the JVM and the system timezone (issue 54)  
     **New:** *web.timezone =*  
 - Added a built-in AJP connector for integrating Gitblit GO into an Apache mod_proxy setup (issue 59)  
diff --git a/resources/gitblit.css b/resources/gitblit.css
index 3c265a4..8934cb2 100644
--- a/resources/gitblit.css
+++ b/resources/gitblit.css
@@ -310,7 +310,8 @@
 }
 
 div.searchResult .text {
-	border-left: 5px solid #EEEEEE;
+	border-left: 2px solid #ccc;
+	border-radius: 0px;
 	
     padding: 0 0 0 15px;
 }
diff --git a/src/com/gitblit/LuceneExecutor.java b/src/com/gitblit/LuceneExecutor.java
index 70d666f..cd7074b 100644
--- a/src/com/gitblit/LuceneExecutor.java
+++ b/src/com/gitblit/LuceneExecutor.java
@@ -157,6 +157,10 @@
 	 */
 	@Override
 	public void run() {
+		if (!storedSettings.getBoolean(Keys.web.allowLuceneIndexing, true)) {
+			// Lucene indexing is disabled
+			return;
+		}
 		// reload the excluded extensions
 		String exts = storedSettings.getString(Keys.web.luceneIgnoreExtensions, luceneIgnoreExtensions);
 		excludedExtensions = new TreeSet<String>(StringUtils.getStringsFromValue(exts));
diff --git a/src/com/gitblit/wicket/GitBlitWebApp.properties b/src/com/gitblit/wicket/GitBlitWebApp.properties
index 3b78b43..4300c04 100644
--- a/src/com/gitblit/wicket/GitBlitWebApp.properties
+++ b/src/com/gitblit/wicket/GitBlitWebApp.properties
@@ -222,4 +222,5 @@
 gb.indexedBranchesDescription = select the branches to include in your Lucene index
 gb.noIndexedRepositoriesWarning = none of your repositories are configured for Lucene indexing
 gb.undefinedQueryWarning = query is undefined!
-gb.noSelectedRepositoriesWarning = please select one or more repositories!
\ No newline at end of file
+gb.noSelectedRepositoriesWarning = please select one or more repositories!
+gb.luceneDisabled = Lucene indexing is disabled
\ No newline at end of file
diff --git a/src/com/gitblit/wicket/pages/EditRepositoryPage.java b/src/com/gitblit/wicket/pages/EditRepositoryPage.java
index 5b2124a..c2fc9f5 100644
--- a/src/com/gitblit/wicket/pages/EditRepositoryPage.java
+++ b/src/com/gitblit/wicket/pages/EditRepositoryPage.java
@@ -117,12 +117,11 @@
 
 		// indexed local branches palette
 		List<String> allLocalBranches = repositoryModel.getLocalBranches();
-
+		boolean luceneEnabled = GitBlit.getBoolean(Keys.web.allowLuceneIndexing, true);
 		final Palette<String> indexedBranchesPalette = new Palette<String>("indexedBranches", new ListModel<String>(
 				indexedBranches), new CollectionModel<String>(allLocalBranches),
 				new StringChoiceRenderer(), 8, false);
-		indexedBranchesPalette.setEnabled(allLocalBranches.size() > 0);
-
+		indexedBranchesPalette.setEnabled(luceneEnabled && (allLocalBranches.size() > 0));
 		
 		// federation sets palette
 		List<String> sets = GitBlit.getStrings(Keys.federation.sets);
diff --git a/src/com/gitblit/wicket/pages/LuceneSearchPage.java b/src/com/gitblit/wicket/pages/LuceneSearchPage.java
index 997ef21..ea94fa4 100644
--- a/src/com/gitblit/wicket/pages/LuceneSearchPage.java
+++ b/src/com/gitblit/wicket/pages/LuceneSearchPage.java
@@ -105,9 +105,13 @@
 				availableRepositories.add(model.name);
 			}
 		}
-		
-		if (availableRepositories.size() == 0) {
-			info(getString("gb.noIndexedRepositoriesWarning"));
+		boolean luceneEnabled = GitBlit.getBoolean(Keys.web.allowLuceneIndexing, true);
+		if (luceneEnabled) {
+			if (availableRepositories.size() == 0) {
+				info(getString("gb.noIndexedRepositoriesWarning"));
+			}
+		} else {
+			error(getString("gb.luceneDisabled"));
 		}
 
 		// enforce user-accessible repository selections
@@ -146,9 +150,9 @@
 		ListMultipleChoice<String> selections = new ListMultipleChoice<String>("repositories", 
 				repositoriesModel, availableRepositories, new StringChoiceRenderer());
 		selections.setMaxRows(8);
-		form.add(selections);
-		form.add(new TextField<String>("query", queryModel));
-		add(form);
+		form.add(selections.setEnabled(luceneEnabled));
+		form.add(new TextField<String>("query", queryModel).setEnabled(luceneEnabled));
+		add(form.setEnabled(luceneEnabled));
 				
 		// execute search
 		final List<SearchResult> results = new ArrayList<SearchResult>();
diff --git a/src/com/gitblit/wicket/pages/RepositoryPage.java b/src/com/gitblit/wicket/pages/RepositoryPage.java
index 132b1f7..d90d627 100644
--- a/src/com/gitblit/wicket/pages/RepositoryPage.java
+++ b/src/com/gitblit/wicket/pages/RepositoryPage.java
@@ -360,7 +360,8 @@
 			}
 			Class<? extends BasePage> searchPageClass = GitSearchPage.class;
 			RepositoryModel model = GitBlit.self().getRepositoryModel(repositoryName);
-			if (!ArrayUtils.isEmpty(model.indexedBranches)) {
+			if (GitBlit.getBoolean(Keys.web.allowLuceneIndexing, true)
+					&& !ArrayUtils.isEmpty(model.indexedBranches)) {
 				// this repository is Lucene-indexed
 				searchPageClass = LuceneSearchPage.class;
 			}
diff --git a/src/com/gitblit/wicket/pages/RootPage.java b/src/com/gitblit/wicket/pages/RootPage.java
index 57678df..d7b3ab7 100644
--- a/src/com/gitblit/wicket/pages/RootPage.java
+++ b/src/com/gitblit/wicket/pages/RootPage.java
@@ -101,7 +101,9 @@
 		pages.add(new PageRegistration("gb.repositories", RepositoriesPage.class,
 				getRootPageParameters()));
 		pages.add(new PageRegistration("gb.activity", ActivityPage.class, getRootPageParameters()));
-		pages.add(new PageRegistration("gb.search", LuceneSearchPage.class));
+		if (GitBlit.getBoolean(Keys.web.allowLuceneIndexing, true)) {
+			pages.add(new PageRegistration("gb.search", LuceneSearchPage.class));
+		}
 		if (showAdmin) {
 			pages.add(new PageRegistration("gb.users", UsersPage.class));
 		}

--
Gitblit v1.9.1