ConfigSnapshot.java

  1. /*
  2.  * Copyright (C) 2010, Mathias Kinzler <mathias.kinzler@sap.com>
  3.  * Copyright (C) 2009, Constantine Plotnikov <constantine.plotnikov@gmail.com>
  4.  * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
  5.  * Copyright (C) 2008-2012, Google Inc.
  6.  * Copyright (C) 2009, JetBrains s.r.o.
  7.  * Copyright (C) 2007-2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  8.  * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
  9.  * Copyright (C) 2008, Thad Hughes <thadh@thad.corp.google.com> and others
  10.  *
  11.  * This program and the accompanying materials are made available under the
  12.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  13.  * https://www.eclipse.org/org/documents/edl-v10.php.
  14.  *
  15.  * SPDX-License-Identifier: BSD-3-Clause
  16.  */

  17. package org.eclipse.jgit.lib;

  18. import static org.eclipse.jgit.util.StringUtils.compareIgnoreCase;
  19. import static org.eclipse.jgit.util.StringUtils.compareWithCase;
  20. import static org.eclipse.jgit.util.StringUtils.toLowerCase;

  21. import java.util.AbstractSet;
  22. import java.util.ArrayList;
  23. import java.util.Collections;
  24. import java.util.Comparator;
  25. import java.util.HashMap;
  26. import java.util.Iterator;
  27. import java.util.LinkedHashMap;
  28. import java.util.LinkedHashSet;
  29. import java.util.List;
  30. import java.util.Map;
  31. import java.util.Set;
  32. import java.util.concurrent.ConcurrentHashMap;

  33. import org.eclipse.jgit.util.StringUtils;

  34. class ConfigSnapshot {
  35.     final List<ConfigLine> entryList;
  36.     final Map<Object, Object> cache;
  37.     final ConfigSnapshot baseState;
  38.     volatile List<ConfigLine> sorted;
  39.     volatile SectionNames names;

  40.     ConfigSnapshot(List<ConfigLine> entries, ConfigSnapshot base) {
  41.         entryList = entries;
  42.         cache = new ConcurrentHashMap<>(16, 0.75f, 1);
  43.         baseState = base;
  44.     }

  45.     Set<String> getSections() {
  46.         return names().sections;
  47.     }

  48.     Set<String> getSubsections(String section) {
  49.         Map<String, Set<String>> m = names().subsections;
  50.         Set<String> r = m.get(section);
  51.         if (r == null)
  52.             r = m.get(toLowerCase(section));
  53.         if (r == null)
  54.             return Collections.emptySet();
  55.         return Collections.unmodifiableSet(r);
  56.     }

  57.     Set<String> getNames(String section, String subsection) {
  58.         return getNames(section, subsection, false);
  59.     }

  60.     Set<String> getNames(String section, String subsection, boolean recursive) {
  61.         Map<String, String> m = getNamesInternal(section, subsection, recursive);
  62.         return new CaseFoldingSet(m);
  63.     }

  64.     private Map<String, String> getNamesInternal(String section,
  65.             String subsection, boolean recursive) {
  66.         List<ConfigLine> s = sorted();
  67.         int idx = find(s, section, subsection, ""); //$NON-NLS-1$
  68.         if (idx < 0)
  69.             idx = -(idx + 1);

  70.         Map<String, String> m = new LinkedHashMap<>();
  71.         while (idx < s.size()) {
  72.             ConfigLine e = s.get(idx++);
  73.             if (!e.match(section, subsection))
  74.                 break;
  75.             if (e.name == null)
  76.                 continue;
  77.             String l = toLowerCase(e.name);
  78.             if (!m.containsKey(l))
  79.                 m.put(l, e.name);
  80.         }
  81.         if (recursive && baseState != null)
  82.             m.putAll(baseState.getNamesInternal(section, subsection, recursive));
  83.         return m;
  84.     }

  85.     String[] get(String section, String subsection, String name) {
  86.         List<ConfigLine> s = sorted();
  87.         int idx = find(s, section, subsection, name);
  88.         if (idx < 0)
  89.             return null;
  90.         int end = end(s, idx, section, subsection, name);
  91.         String[] r = new String[end - idx];
  92.         for (int i = 0; idx < end;)
  93.             r[i++] = s.get(idx++).value;
  94.         return r;
  95.     }

  96.     private int find(List<ConfigLine> s, String s1, String s2, String name) {
  97.         int low = 0;
  98.         int high = s.size();
  99.         while (low < high) {
  100.             int mid = (low + high) >>> 1;
  101.             ConfigLine e = s.get(mid);
  102.             int cmp = compare2(
  103.                     s1, s2, name,
  104.                     e.section, e.subsection, e.name);
  105.             if (cmp < 0)
  106.                 high = mid;
  107.             else if (cmp == 0)
  108.                 return first(s, mid, s1, s2, name);
  109.             else
  110.                 low = mid + 1;
  111.         }
  112.         return -(low + 1);
  113.     }

  114.     private int first(List<ConfigLine> s, int i, String s1, String s2, String n) {
  115.         while (0 < i) {
  116.             if (s.get(i - 1).match(s1, s2, n))
  117.                 i--;
  118.             else
  119.                 return i;
  120.         }
  121.         return i;
  122.     }

  123.     private int end(List<ConfigLine> s, int i, String s1, String s2, String n) {
  124.         while (i < s.size()) {
  125.             if (s.get(i).match(s1, s2, n))
  126.                 i++;
  127.             else
  128.                 return i;
  129.         }
  130.         return i;
  131.     }

  132.     private List<ConfigLine> sorted() {
  133.         List<ConfigLine> r = sorted;
  134.         if (r == null)
  135.             sorted = r = sort(entryList);
  136.         return r;
  137.     }

  138.     private static List<ConfigLine> sort(List<ConfigLine> in) {
  139.         List<ConfigLine> sorted = new ArrayList<>(in.size());
  140.         for (ConfigLine line : in) {
  141.             if (line.section != null && line.name != null)
  142.                 sorted.add(line);
  143.         }
  144.         Collections.sort(sorted, new LineComparator());
  145.         return sorted;
  146.     }

  147.     private static int compare2(
  148.             String aSection, String aSubsection, String aName,
  149.             String bSection, String bSubsection, String bName) {
  150.         int c = compareIgnoreCase(aSection, bSection);
  151.         if (c != 0)
  152.             return c;

  153.         if (aSubsection == null && bSubsection != null)
  154.             return -1;
  155.         if (aSubsection != null && bSubsection == null)
  156.             return 1;
  157.         if (aSubsection != null) {
  158.             c = compareWithCase(aSubsection, bSubsection);
  159.             if (c != 0)
  160.                 return c;
  161.         }

  162.         return compareIgnoreCase(aName, bName);
  163.     }

  164.     private static class LineComparator implements Comparator<ConfigLine> {
  165.         @Override
  166.         public int compare(ConfigLine a, ConfigLine b) {
  167.             return compare2(
  168.                     a.section, a.subsection, a.name,
  169.                     b.section, b.subsection, b.name);
  170.         }
  171.     }

  172.     private SectionNames names() {
  173.         SectionNames n = names;
  174.         if (n == null)
  175.             names = n = new SectionNames(this);
  176.         return n;
  177.     }

  178.     private static class SectionNames {
  179.         final CaseFoldingSet sections;
  180.         final Map<String, Set<String>> subsections;

  181.         SectionNames(ConfigSnapshot cfg) {
  182.             Map<String, String> sec = new LinkedHashMap<>();
  183.             Map<String, Set<String>> sub = new HashMap<>();
  184.             while (cfg != null) {
  185.                 for (ConfigLine e : cfg.entryList) {
  186.                     if (e.section == null)
  187.                         continue;

  188.                     String l1 = toLowerCase(e.section);
  189.                     if (!sec.containsKey(l1))
  190.                         sec.put(l1, e.section);

  191.                     if (e.subsection == null)
  192.                         continue;

  193.                     Set<String> m = sub.get(l1);
  194.                     if (m == null) {
  195.                         m = new LinkedHashSet<>();
  196.                         sub.put(l1, m);
  197.                     }
  198.                     m.add(e.subsection);
  199.                 }
  200.                 cfg = cfg.baseState;
  201.             }

  202.             sections = new CaseFoldingSet(sec);
  203.             subsections = sub;
  204.         }
  205.     }

  206.     private static class CaseFoldingSet extends AbstractSet<String> {
  207.         private final Map<String, String> names;

  208.         CaseFoldingSet(Map<String, String> names) {
  209.             this.names = names;
  210.         }

  211.         @Override
  212.         public boolean contains(Object needle) {
  213.             if (needle instanceof String) {
  214.                 String n = (String) needle;
  215.                 return names.containsKey(n)
  216.                         || names.containsKey(StringUtils.toLowerCase(n));
  217.             }
  218.             return false;
  219.         }

  220.         @Override
  221.         public Iterator<String> iterator() {
  222.             final Iterator<String> i = names.values().iterator();
  223.             return new Iterator<>() {

  224.                 @Override
  225.                 public boolean hasNext() {
  226.                     return i.hasNext();
  227.                 }

  228.                 @Override
  229.                 public String next() {
  230.                     return i.next();
  231.                 }

  232.                 @Override
  233.                 public void remove() {
  234.                     throw new UnsupportedOperationException();
  235.                 }
  236.             };
  237.         }

  238.         @Override
  239.         public int size() {
  240.             return names.size();
  241.         }
  242.     }
  243. }