Changeset 12042 in josm


Ignore:
Timestamp:
2017-05-03T11:19:04+02:00 (7 years ago)
Author:
bastiK
Message:

applied #13956 - memory optimization with presets cache (patch by GerdP)

Location:
trunk/src/org/openstreetmap/josm
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/validation/tests/TagChecker.java

    r11942 r12042  
    6969    /** Normalized keys: the key should be substituted by the value if the key was not found in presets */
    7070    private static final Map<String, String> harmonizedKeys = new HashMap<>();
    71     /** The spell check preset values */
    72     private static volatile MultiMap<String, String> presetsValueData;
     71    /** The spell check preset values which are not stored in TaggingPresets */
     72    private static volatile MultiMap<String, String> additionalPresetsValueData;
    7373    /** The TagChecker data */
    7474    private static final List<CheckerData> checkerData = new ArrayList<>();
     
    259259        Collection<TaggingPreset> presets = TaggingPresets.getTaggingPresets();
    260260        if (!presets.isEmpty()) {
    261             presetsValueData = new MultiMap<>();
     261            additionalPresetsValueData = new MultiMap<>();
    262262            for (String a : OsmPrimitive.getUninterestingKeys()) {
    263                 presetsValueData.putVoid(a);
     263                additionalPresetsValueData.putVoid(a);
    264264            }
    265265            // TODO directionKeys are no longer in OsmPrimitive (search pattern is used instead)
    266266            for (String a : Main.pref.getCollection(ValidatorPreference.PREFIX + ".knownkeys",
    267267                    Arrays.asList(new String[]{"is_in", "int_ref", "fixme", "population"}))) {
    268                 presetsValueData.putVoid(a);
     268                additionalPresetsValueData.putVoid(a);
    269269            }
    270270            for (TaggingPreset p : presets) {
     
    285285        Collection<String> values = ky.getValues();
    286286        if (ky.key != null && values != null) {
    287             presetsValueData.putAll(ky.key, values);
    288287            harmonizedKeys.put(harmonizeKey(ky.key), ky.key);
    289288        }
     
    305304    }
    306305
     306    private static Set<String> getPresetValues(String key) {
     307        Set<String> res = TaggingPresets.getPresetValues(key);
     308        if (res != null)
     309            return res;
     310        return additionalPresetsValueData.get(key);
     311    }
     312
    307313    /**
    308314     * Determines if the given key is in internal presets.
     
    312318     */
    313319    public static boolean isKeyInPresets(String key) {
    314         return presetsValueData.get(key) != null;
     320        return getPresetValues(key) != null;
    315321    }
    316322
     
    323329     */
    324330    public static boolean isTagInPresets(String key, String value) {
    325         final Set<String> values = presetsValueData.get(key);
     331        final Set<String> values = getPresetValues(key);
    326332        return values != null && (values.isEmpty() || values.contains(value));
    327333    }
     
    462468                withErrors.put(p, "HTML");
    463469            }
    464             if (checkValues && key != null && value != null && !value.isEmpty() && presetsValueData != null && !isTagIgnored(key, value)) {
     470            if (checkValues && key != null && value != null && !value.isEmpty() && additionalPresetsValueData != null
     471                    && !isTagIgnored(key, value)) {
    465472                if (!isKeyInPresets(key)) {
    466473                    String prettifiedKey = harmonizeKey(key);
     
    487494                    // try to fix common typos and check again if value is still unknown
    488495                    String fixedValue = harmonizeValue(prop.getValue());
    489                     Map<String, String> possibleValues = getPossibleValues(presetsValueData.get(key));
     496                    Map<String, String> possibleValues = getPossibleValues(getPresetValues(key));
    490497                    if (possibleValues.containsKey(fixedValue)) {
    491498                        final String newKey = possibleValues.get(fixedValue);
  • trunk/src/org/openstreetmap/josm/gui/tagging/ac/AutoCompletionManager.java

    r11746 r12042  
    2929import org.openstreetmap.josm.data.osm.event.WayNodesChangedEvent;
    3030import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset;
    31 import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetItem;
    3231import org.openstreetmap.josm.gui.tagging.presets.TaggingPresets;
    33 import org.openstreetmap.josm.gui.tagging.presets.items.CheckGroup;
    34 import org.openstreetmap.josm.gui.tagging.presets.items.KeyedItem;
    35 import org.openstreetmap.josm.gui.tagging.presets.items.Roles;
    3632import org.openstreetmap.josm.gui.tagging.presets.items.Roles.Role;
    3733import org.openstreetmap.josm.tools.CheckParameterUtil;
     
    203199
    204200    /**
    205      * Initialize the cache for presets. This is done only once.
    206      * @param presets Tagging presets to cache
    207      */
    208     public static void cachePresets(Collection<TaggingPreset> presets) {
    209         for (final TaggingPreset p : presets) {
    210             for (TaggingPresetItem item : p.data) {
    211                 cachePresetItem(p, item);
    212             }
    213         }
    214     }
    215 
    216     protected static void cachePresetItem(TaggingPreset p, TaggingPresetItem item) {
    217         if (item instanceof KeyedItem) {
    218             KeyedItem ki = (KeyedItem) item;
    219             if (ki.key != null && ki.getValues() != null) {
    220                 PRESET_TAG_CACHE.putAll(ki.key, ki.getValues());
    221             }
    222         } else if (item instanceof Roles) {
    223             Roles r = (Roles) item;
    224             for (Role i : r.roles) {
    225                 if (i.key != null) {
    226                     PRESET_ROLE_CACHE.add(i.key);
    227                 }
    228             }
    229         } else if (item instanceof CheckGroup) {
    230             for (KeyedItem check : ((CheckGroup) item).checks) {
    231                 cachePresetItem(p, check);
    232             }
    233         }
    234     }
    235 
    236     /**
    237201     * Remembers user input for the given key/value.
    238202     * @param key Tag key
     
    253217    protected List<String> getDataKeys() {
    254218        return new ArrayList<>(getTagCache().keySet());
    255     }
    256 
    257     protected List<String> getPresetKeys() {
    258         return new ArrayList<>(PRESET_TAG_CACHE.keySet());
    259219    }
    260220
     
    281241    }
    282242
    283     protected static List<String> getPresetValues(String key) {
    284         return new ArrayList<>(PRESET_TAG_CACHE.getValues(key));
    285     }
    286 
    287243    protected static Collection<String> getUserInputValues(String key) {
    288244        List<String> values = new ArrayList<>();
     
    312268     */
    313269    public void populateWithMemberRoles(AutoCompletionList list) {
    314         list.add(PRESET_ROLE_CACHE, AutoCompletionItemPriority.IS_IN_STANDARD);
     270        list.add(TaggingPresets.getPresetRoles(), AutoCompletionItemPriority.IS_IN_STANDARD);
    315271        list.add(getRoleCache(), AutoCompletionItemPriority.IS_IN_DATASET);
    316272    }
     
    347303     */
    348304    public void populateWithKeys(AutoCompletionList list) {
    349         list.add(getPresetKeys(), AutoCompletionItemPriority.IS_IN_STANDARD);
     305        list.add(TaggingPresets.getPresetKeys(), AutoCompletionItemPriority.IS_IN_STANDARD);
    350306        list.add(new AutoCompletionListItem("source", AutoCompletionItemPriority.IS_IN_STANDARD));
    351307        list.add(getDataKeys(), AutoCompletionItemPriority.IS_IN_DATASET);
     
    373329    public void populateWithTagValues(AutoCompletionList list, List<String> keys) {
    374330        for (String key : keys) {
    375             list.add(getPresetValues(key), AutoCompletionItemPriority.IS_IN_STANDARD);
     331            list.add(TaggingPresets.getPresetValues(key), AutoCompletionItemPriority.IS_IN_STANDARD);
    376332            list.add(getDataValues(key), AutoCompletionItemPriority.IS_IN_DATASET);
    377333            list.addUserInput(getUserInputValues(key));
  • trunk/src/org/openstreetmap/josm/gui/tagging/presets/TaggingPresets.java

    r11386 r12042  
    44import java.util.ArrayList;
    55import java.util.Collection;
     6import java.util.Collections;
    67import java.util.HashMap;
     8import java.util.HashSet;
    79import java.util.Map;
     10import java.util.Set;
    811
    912import javax.swing.JMenu;
     
    1417import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1518import org.openstreetmap.josm.gui.MenuScroller;
    16 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
     19import org.openstreetmap.josm.gui.tagging.presets.items.CheckGroup;
     20import org.openstreetmap.josm.gui.tagging.presets.items.KeyedItem;
     21import org.openstreetmap.josm.gui.tagging.presets.items.Roles;
     22import org.openstreetmap.josm.gui.tagging.presets.items.Roles.Role;
     23import org.openstreetmap.josm.tools.MultiMap;
    1724import org.openstreetmap.josm.tools.SubclassFilteredCollection;
    1825
     
    2633    private static final Collection<TaggingPreset> taggingPresets = new ArrayList<>();
    2734
     35    /** cache for key/value pairs found in the preset */
     36    private static final MultiMap<String, String> PRESET_TAG_CACHE = new MultiMap<>();
     37    /** cache for roles found in the preset */
     38    private static final Set<String> PRESET_ROLE_CACHE = new HashSet<>();
     39
    2840    /** The collection of listeners */
    2941    private static final Collection<TaggingPresetListener> listeners = new ArrayList<>();
     
    3951        taggingPresets.clear();
    4052        taggingPresets.addAll(TaggingPresetReader.readFromPreferences(false, false));
     53        cachePresets(taggingPresets);
    4154    }
    4255
     
    5467            Main.main.menu.presetsMenu.setVisible(false);
    5568        } else {
    56             AutoCompletionManager.cachePresets(taggingPresets);
    5769            Map<TaggingPresetMenu, JMenu> submenus = new HashMap<>();
    5870            for (final TaggingPreset p : taggingPresets) {
     
    88100
    89101    /**
     102     * Initialize the cache for presets. This is done only once.
     103     * @param presets Tagging presets to cache
     104     */
     105    private static void cachePresets(Collection<TaggingPreset> presets) {
     106        for (final TaggingPreset p : presets) {
     107            for (TaggingPresetItem item : p.data) {
     108                cachePresetItem(p, item);
     109            }
     110        }
     111    }
     112
     113    private static void cachePresetItem(TaggingPreset p, TaggingPresetItem item) {
     114        if (item instanceof KeyedItem) {
     115            KeyedItem ki = (KeyedItem) item;
     116            if (ki.key != null && ki.getValues() != null) {
     117                try {
     118                    PRESET_TAG_CACHE.putAll(ki.key, ki.getValues());
     119                } catch (NullPointerException e) {
     120                    Main.error(e, p + ": Unable to cache " + ki);
     121                }
     122            }
     123        } else if (item instanceof Roles) {
     124            Roles r = (Roles) item;
     125            for (Role i : r.roles) {
     126                if (i.key != null) {
     127                    PRESET_ROLE_CACHE.add(i.key);
     128                }
     129            }
     130        } else if (item instanceof CheckGroup) {
     131            for (KeyedItem check : ((CheckGroup) item).checks) {
     132                cachePresetItem(p, check);
     133            }
     134        }
     135    }
     136
     137    /**
    90138     * Replies a new collection containing all tagging presets.
    91139     * @return a new collection containing all tagging presets. Empty if presets are not initialized (never null)
    92140     */
    93141    public static Collection<TaggingPreset> getTaggingPresets() {
    94         return new ArrayList<>(taggingPresets);
     142        return Collections.unmodifiableCollection(taggingPresets);
     143    }
     144
     145    /**
     146     * Replies a set of all roles in the tagging presets.
     147     * @return a set of all roles in the tagging presets.
     148     */
     149    public static Set<String> getPresetRoles() {
     150        return Collections.unmodifiableSet(PRESET_ROLE_CACHE);
     151    }
     152
     153    /**
     154     * Replies a set of all keys in the tagging presets.
     155     * @return a set of all keys in the tagging presets.
     156     */
     157    public static Set<String> getPresetKeys() {
     158        return Collections.unmodifiableSet(PRESET_TAG_CACHE.keySet());
     159    }
     160
     161    /**
     162     * Return set of values for a key in the tagging presets
     163     * @param key the key
     164     * @return set of values for a key in the tagging presets or null if none is found
     165     */
     166    public static Set<String> getPresetValues(String key) {
     167        Set<String> values = PRESET_TAG_CACHE.get(key);
     168        if (values != null)
     169            return Collections.unmodifiableSet(values);
     170        return null;
    95171    }
    96172
Note: See TracChangeset for help on using the changeset viewer.