package org.opendaylight.controller.cluster.datastore.node.utils;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
import org.opendaylight.yangtools.yang.common.QName;
-import java.util.HashMap;
-import java.util.Map;
-
public class QNameFactory {
- private static final Map<String, QName> cache = new HashMap<>();
- public static QName create(String name){
- QName value = cache.get(name);
- if(value == null){
- synchronized (cache){
- value = cache.get(name);
- if(value == null) {
- value = QName.create(name);
- cache.put(name, value);
+ private static final int MAX_QNAME_CACHE_SIZE = 10000;
+
+ private static LoadingCache<String, QName> cache = CacheBuilder.newBuilder()
+ .maximumSize(MAX_QNAME_CACHE_SIZE)
+ .softValues()
+ .build(
+ new CacheLoader<String, QName>() {
+ public QName load(String key) {
+ return QName.create(key);
}
}
- }
- return value;
+ );
+
+
+ public static QName create(String name){
+ return cache.getUnchecked(name);
}
}
package org.opendaylight.controller.cluster.datastore.util;
import org.opendaylight.controller.cluster.datastore.node.utils.NodeIdentifierFactory;
+import org.opendaylight.controller.cluster.datastore.node.utils.QNameFactory;
import org.opendaylight.controller.protobuff.messages.common.NormalizedNodeMessages;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
YangInstanceIdentifier.NodeWithValue nodeWithValue =
new YangInstanceIdentifier.NodeWithValue(
- QName.create(pathArgument.getNodeType().getValue()),
+ QNameFactory.create(pathArgument.getNodeType().getValue()),
parseAttribute(pathArgument.getAttributes(0)));
return nodeWithValue;
YangInstanceIdentifier.NodeIdentifierWithPredicates
nodeIdentifierWithPredicates =
new YangInstanceIdentifier.NodeIdentifierWithPredicates(
- QName.create(pathArgument.getNodeType().getValue()), toAttributesMap(pathArgument.getAttributesList()));
+ QNameFactory.create(pathArgument.getNodeType().getValue()), toAttributesMap(pathArgument.getAttributesList()));
return nodeIdentifierWithPredicates;
Set<QName> qNameSet = new HashSet<>();
for(NormalizedNodeMessages.Attribute attribute : pathArgument.getAttributesList()){
- qNameSet.add(QName.create(attribute.getValue()));
+ qNameSet.add(QNameFactory.create(attribute.getValue()));
}
return new YangInstanceIdentifier.AugmentationIdentifier(qNameSet);
String name = attribute.getName();
Object value = parseAttribute(attribute);
- map.put(QName.create(name), value);
+ map.put(QNameFactory.create(name), value);
}
return map;
--- /dev/null
+package org.opendaylight.controller.cluster.datastore.node.utils;
+
+import org.junit.Test;
+import org.opendaylight.controller.cluster.datastore.util.TestModel;
+import org.opendaylight.yangtools.yang.common.QName;
+
+import static junit.framework.Assert.assertTrue;
+import static junit.framework.TestCase.assertEquals;
+import static org.junit.Assert.assertFalse;
+
+public class QNameFactoryTest {
+
+ @Test
+ public void testBasic(){
+ QName expected = TestModel.AUG_NAME_QNAME;
+ QName created = QNameFactory.create(expected.toString());
+
+ assertFalse( expected == created);
+
+ assertEquals(expected, created);
+
+ QName cached = QNameFactory.create(expected.toString());
+
+ assertEquals(expected, cached);
+
+ assertTrue( cached == created );
+ }
+
+}