[JGIT PATCH 15/19] Cache Config subsection names when requested by application code

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Using the SectionParser based cache allows us to read through the
configuration once to produce the set of available subsections,
but then reuse that set in the future.  This makes lookups like
"find all remotes" more efficient.

Signed-off-by: Shawn O. Pearce <spearce@xxxxxxxxxxx>
---
 .../src/org/spearce/jgit/lib/Config.java           |   42 +++++++++++++++----
 1 files changed, 33 insertions(+), 9 deletions(-)

diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/Config.java b/org.spearce.jgit/src/org/spearce/jgit/lib/Config.java
index 6036c4c..1668a3f 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/Config.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/Config.java
@@ -366,15 +366,7 @@ public String getString(final String section, String subsection,
 	 *         subsection exists.
 	 */
 	public Set<String> getSubsections(final String section) {
-		final Set<String> result = new HashSet<String>();
-		for (final Entry e : state.get().entryList) {
-			if (StringUtils.equalsIgnoreCase(section, e.section)
-					&& e.subsection != null)
-				result.add(e.subsection);
-		}
-		if (baseConfig != null)
-			result.addAll(baseConfig.getSubsections(section));
-		return result;
+		return get(new SubsectionNames(section));
 	}
 
 	/**
@@ -1013,6 +1005,38 @@ private static String readValue(final StringReader in, boolean quote,
 		T parse(Config cfg);
 	}
 
+	private static class SubsectionNames implements SectionParser<Set<String>> {
+		private final String section;
+
+		SubsectionNames(final String sectionName) {
+			section = sectionName;
+		}
+
+		public int hashCode() {
+			return section.hashCode();
+		}
+
+		public boolean equals(Object other) {
+			if (other instanceof SubsectionNames) {
+				return section.equals(((SubsectionNames) other).section);
+			}
+			return false;
+		}
+
+		public Set<String> parse(Config cfg) {
+			final Set<String> result = new HashSet<String>();
+			while (cfg != null) {
+				for (final Entry e : cfg.state.get().entryList) {
+					if (e.subsection != null && e.name == null
+							&& StringUtils.equalsIgnoreCase(section, e.section))
+						result.add(e.subsection);
+				}
+				cfg = cfg.baseConfig;
+			}
+			return Collections.unmodifiableSet(result);
+		}
+	}
+
 	private static class State {
 		final List<Entry> entryList;
 
-- 
1.6.4.rc2.216.g769fa

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]