2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.util;
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableMap;
12 import com.google.common.collect.Maps;
13 import com.romix.scala.collection.concurrent.TrieMap;
14 import java.util.Collections;
15 import java.util.HashMap;
17 import java.util.Map.Entry;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
22 * A simple layer on top of maps, which performs snapshot mediation and optimization of
23 * what the underlying implementation is.
25 public final class MapAdaptor {
26 public static final int DEFAULT_COPY_MAX_ITEMS = 100;
27 public static final String COPY_MAX_ITEMS_MAX_PROP = "org.opendaylight.yangtools.util.mapadaptor.maxcopy";
29 public static final int DEFAULT_PERSIST_MIN_ITEMS = 50;
30 public static final String PERSIST_MIN_ITEMS_PROP = "org.opendaylight.yangtools.util.mapadaptor.minpersist";
32 private static final Logger LOG = LoggerFactory.getLogger(MapAdaptor.class);
33 private static final MapAdaptor DEFAULT_INSTANCE;
35 private final boolean useSingleton;
36 private final int persistMinItems;
37 private final int copyMaxItems;
40 DEFAULT_INSTANCE = new MapAdaptor(true,
41 getProperty(COPY_MAX_ITEMS_MAX_PROP, DEFAULT_COPY_MAX_ITEMS),
42 getProperty(PERSIST_MIN_ITEMS_PROP, DEFAULT_PERSIST_MIN_ITEMS));
43 LOG.debug("Configured HashMap/TrieMap cutoff at {}/{} entries",
44 DEFAULT_INSTANCE.persistMinItems, DEFAULT_INSTANCE.copyMaxItems);
47 private static int getProperty(final String name, final int defaultValue) {
49 final String p = System.getProperty(name);
52 int pi = Integer.valueOf(p);
54 LOG.warn("Ignoring illegal value of {}: has to be a positive number", name);
58 } catch (NumberFormatException e) {
59 LOG.warn("Ignoring non-numerical value of {}", name, e);
62 } catch (Exception e) {
63 LOG.debug("Failed to get {}", name, e);
68 private MapAdaptor(final boolean useSingleton, final int copyMaxItems, final int persistMinItems) {
69 this.useSingleton = useSingleton;
70 this.copyMaxItems = copyMaxItems;
71 this.persistMinItems = persistMinItems;
75 * Return the default-configured instance.
77 * @return the singleton global instance
79 public static MapAdaptor getDefaultInstance() {
80 return DEFAULT_INSTANCE;
83 public static MapAdaptor getInstance(final boolean useSingleton, final int copyMaxItems,
84 final int persistMinItems) {
85 Preconditions.checkArgument(copyMaxItems >= 0, "copyMaxItems has to be a non-negative integer");
86 Preconditions.checkArgument(persistMinItems >= 0, "persistMinItems has to be a positive integer");
87 Preconditions.checkArgument(persistMinItems <= copyMaxItems,
88 "persistMinItems must be less than or equal to copyMaxItems");
89 return new MapAdaptor(useSingleton, copyMaxItems, persistMinItems);
93 * Creates an initial snapshot. The backing map is selected according to
96 * @param expectedSize Expected map size
97 * @return An empty mutable map.
99 public <K, V> Map<K, V> initialSnapshot(final int expectedSize) {
100 Preconditions.checkArgument(expectedSize >= 0);
101 if (expectedSize > persistMinItems) {
102 return new ReadWriteTrieMap<>();
105 if (expectedSize < 2) {
106 return new HashMap<>(1);
108 if (expectedSize == 2) {
109 return new HashMap<>(2);
111 return Maps.newHashMapWithExpectedSize(expectedSize);
115 * Input is treated is supposed to be left unmodified, result must be mutable.
117 @SuppressWarnings("static-method")
118 public <K, V> Map<K, V> takeSnapshot(final Map<K, V> input) {
119 if (input instanceof ReadOnlyTrieMap) {
120 return ((ReadOnlyTrieMap<K, V>)input).toReadWrite();
123 LOG.trace("Converting input {} to a HashMap", input);
126 * The default HashMap copy constructor performs a bad thing for small maps, using the default capacity of 16
127 * as the minimum sizing hint, which can lead to wasted memory. Since the HashMap grows in powers-of-two, we
128 * only kick this in if we are storing 6 entries or less, as that results in 8-entry map -- the next power is
129 * 16, which is the default.
132 final int size = input.size();
150 ret = new HashMap<>(target);
152 } else if (input instanceof HashMap) {
153 // HashMap supports cloning, but we want to make sure we trim it down if entries were removed, so we do
154 // this only after having checked for small sizes.
155 @SuppressWarnings("unchecked")
156 final Map<K, V> tmp = (Map<K, V>) ((HashMap<K, V>) input).clone();
159 ret = new HashMap<>(input);
162 LOG.trace("Read-write HashMap is {}", ret);
167 * Input will be thrown away, result will be retained for read-only access or
168 * {@link #takeSnapshot(Map)} purposes.
170 * @param input non-optimized (read-write) map
171 * @return optimized read-only map
173 public <K, V> Map<K, V> optimize(final Map<K, V> input) {
174 if (input instanceof ReadOnlyTrieMap) {
175 LOG.warn("Optimizing read-only map {}", input);
178 final int size = input.size();
184 LOG.trace("Reducing input {} to an empty map", input);
185 return ImmutableMap.of();
189 * We retain the persistent map as long as it holds at least
192 if (input instanceof ReadWriteTrieMap && size >= persistMinItems) {
193 return ((ReadWriteTrieMap<K, V>)input).toReadOnly();
197 * If the user opted to use singleton maps, use them. Except for the case
198 * when persistMinItems dictates we should not move off of the persistent
201 if (useSingleton && size == 1) {
202 final Entry<K, V> e = input.entrySet().iterator().next();
203 final Map<K, V> ret = Collections.singletonMap(e.getKey(), e.getValue());
204 LOG.trace("Reducing input {} to singleton map {}", input, ret);
208 if (size <= copyMaxItems) {
210 * Favor access speed: use a HashMap and copy it on modification.
212 if (input instanceof HashMap) {
216 LOG.trace("Copying input {} to a HashMap ({} entries)", input, size);
217 final Map<K, V> ret = new HashMap<>(input);
218 LOG.trace("Read-only HashMap is {}", ret);
223 * Favor isolation speed: use a TrieMap and perform snapshots
225 * This one is a bit tricky, as the TrieMap is concurrent and does not
226 * keep an uptodate size. Updating it requires a full walk -- which is
227 * O(N) and we want to avoid that. So we wrap it in an interceptor,
228 * which will maintain the size for us.
230 LOG.trace("Copying input {} to a TrieMap ({} entries)", input, size);
231 final TrieMap<K, V> map = new TrieMap<>();
233 final Map<K, V> ret = new ReadOnlyTrieMap<>(map, size);
234 LOG.trace("Read-only TrieMap is {}", ret);