--- /dev/null
+/*
+ * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.mdsal.binding.dom.codec.impl;
+
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.yangtools.yang.binding.BindingObject;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+
+/**
+ * An abstract cache mapping BindingObject instances to their NormalizedNode counterparts. Note that this mapping is
+ * not one-to-one, as NormalizedNodes work on instantiated trees while BindingObjects are generally reused across
+ * instantiations.
+ *
+ * @param <T> BindingObject subtype
+ * @param <C> Root codec context type
+ */
+abstract class AbstractBindingNormalizedNodeCache<T extends BindingObject, C extends NodeCodecContext>
+ extends CacheLoader<T, NormalizedNode<?, ?>> {
+ private final LoadingCache<T, NormalizedNode<?, ?>> cache = CacheBuilder.newBuilder().weakValues().build(this);
+
+ private final @NonNull C rootContext;
+
+ AbstractBindingNormalizedNodeCache(final C rootContext) {
+ this.rootContext = requireNonNull(rootContext);
+ }
+
+ /**
+ * Returns the root codec context associated with this cache.
+ *
+ * @return Root codec context
+ */
+ final @NonNull C rootContext() {
+ return rootContext;
+ }
+
+ /**
+ * Returns cached NormalizedNode representation of DataObject. If the representation is not cached, serializes
+ * DataObject and updates cache with representation.
+ *
+ * @param obj Binding object to be deserialized
+ * @return NormalizedNode representation of binding object.
+ */
+ final NormalizedNode<?, ?> get(final @NonNull T obj) {
+ return cache.getUnchecked(obj);
+ }
+}
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableSet;
+import org.opendaylight.yangtools.yang.binding.BindingObject;
import org.opendaylight.yangtools.yang.binding.DataObject;
/**
* associated.
*/
abstract class AbstractBindingNormalizedNodeCacheHolder {
-
- private final ImmutableSet<Class<? extends DataObject>> cachedValues;
- private final LoadingCache<DataContainerCodecContext<?, ?>, BindingNormalizedNodeCache> caches = CacheBuilder
- .newBuilder().build(new CacheLoader<DataContainerCodecContext<?, ?>, BindingNormalizedNodeCache>() {
+ private final LoadingCache<DataContainerCodecContext<?, ?>, DataObjectNormalizedNodeCache> caches = CacheBuilder
+ .newBuilder().build(new CacheLoader<DataContainerCodecContext<?, ?>, DataObjectNormalizedNodeCache>() {
@Override
- public BindingNormalizedNodeCache load(final DataContainerCodecContext<?, ?> key) {
- return new BindingNormalizedNodeCache(AbstractBindingNormalizedNodeCacheHolder.this, key);
+ public DataObjectNormalizedNodeCache load(final DataContainerCodecContext<?, ?> key) {
+ return new DataObjectNormalizedNodeCache(AbstractBindingNormalizedNodeCacheHolder.this, key);
}
});
+ private final ImmutableSet<Class<? extends DataObject>> cacheSpec;
AbstractBindingNormalizedNodeCacheHolder(final ImmutableSet<Class<? extends DataObject>> cacheSpec) {
- cachedValues = requireNonNull(cacheSpec);
+ this.cacheSpec = requireNonNull(cacheSpec);
}
- BindingNormalizedNodeCache getCachingSerializer(final DataContainerCodecContext<?, ?> childCtx) {
+ DataObjectNormalizedNodeCache getCachingSerializer(final DataContainerCodecContext<?, ?> childCtx) {
if (isCached(childCtx.getBindingClass())) {
return caches.getUnchecked(childCtx);
}
return null;
}
- boolean isCached(final Class<?> type) {
- return cachedValues.contains(type);
+ final boolean isCached(final Class<? extends BindingObject> type) {
+ return cacheSpec.contains(type);
}
}
+++ /dev/null
-/*
- * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.mdsal.binding.dom.codec.impl;
-
-import static java.util.Objects.requireNonNull;
-
-import com.google.common.cache.CacheBuilder;
-import com.google.common.cache.CacheLoader;
-import com.google.common.cache.LoadingCache;
-import org.opendaylight.yangtools.yang.binding.DataObject;
-import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-
-final class BindingNormalizedNodeCache extends CacheLoader<DataObject, NormalizedNode<?, ?>> {
-
- private final LoadingCache<DataObject, NormalizedNode<?, ?>> cache = CacheBuilder.newBuilder().weakValues()
- .build(this);
- final DataContainerCodecContext<?, ?> subtreeRoot;
- final AbstractBindingNormalizedNodeCacheHolder cacheHolder;
-
- BindingNormalizedNodeCache(final AbstractBindingNormalizedNodeCacheHolder cacheHolder,
- final DataContainerCodecContext<?, ?> subtreeRoot) {
- this.cacheHolder = requireNonNull(cacheHolder, "cacheHolder");
- this.subtreeRoot = requireNonNull(subtreeRoot, "subtreeRoot");
- }
-
- @Override
- public NormalizedNode<?, ?> load(final DataObject key) throws Exception {
- return CachingNormalizedNodeSerializer.serializeUsingStreamWriter(cacheHolder, subtreeRoot, key);
- }
-
- /**
- * Returns cached NormalizedNode representation of DataObject. If the representation is not cached, serializes
- * DataObject and updates cache with representation.
- *
- * @param obj Binding object to be deserialized
- * @return NormalizedNode representation of binding object.
- */
- NormalizedNode<?, ?> get(final DataObject obj) {
- return cache.getUnchecked(obj);
- }
-}
class CachingNormalizedNodeCodec<D extends DataObject> extends AbstractBindingNormalizedNodeCacheHolder implements
BindingNormalizedNodeCachingCodec<D> {
-
private final DataContainerCodecContext<D, ?> context;
CachingNormalizedNodeCodec(final DataContainerCodecContext<D, ?> subtreeRoot,
@Override
public NormalizedNode<?, ?> serialize(final D data) {
- return CachingNormalizedNodeSerializer.serialize(this, context, data);
+ // Serialize data using stream writer with child cache enable or using the cache if it is available
+ final DataObjectNormalizedNodeCache cache = getCachingSerializer(context);
+ return cache == null ? CachingNormalizedNodeSerializer.serializeUsingStreamWriter(this, context, data)
+ : cache.get(data);
}
@Override
public void close() {
// NOOP as of now.
}
-
}
import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
/**
- * Serializer of Binding objects to Normalized Node which uses {@link BindingNormalizedNodeCache} to
+ * Serializer of Binding objects to Normalized Node which uses {@link DataObjectNormalizedNodeCache} to
* cache already serialized values.
*
* <p>
*/
@Override
public NormalizedNode<?, ?> serialize(final DataObject input) {
- final BindingNormalizedNodeCache cachingSerializer = getCacheSerializer(input.implementedInterface());
+ final DataObjectNormalizedNodeCache cachingSerializer = getCacheSerializer(input.implementedInterface());
if (cachingSerializer != null) {
final NormalizedNode<?, ?> domData = cachingSerializer.get(input);
domWriter.addChild(domData);
return null;
}
- /**
- * Serializes supplied data using stream writer with child cache enabled or using cache directly
- * if cache is avalaible also for supplied Codec node.
- *
- * @param cacheHolder Binding to Normalized Node Cache holder
- * @param subtreeRoot Codec Node for provided data object
- * @param data Data to be serialized
- * @return Normalized Node representation of data.
- */
- static NormalizedNode<?, ?> serialize(final AbstractBindingNormalizedNodeCacheHolder cacheHolder,
- final DataContainerCodecContext<?, ?> subtreeRoot, final DataObject data) {
- final BindingNormalizedNodeCache cache = cacheHolder.getCachingSerializer(subtreeRoot);
- if (cache != null) {
- return cache.get(data);
- }
- return serializeUsingStreamWriter(cacheHolder, subtreeRoot, data);
- }
-
- private BindingNormalizedNodeCache getCacheSerializer(final Class<? extends DataObject> type) {
+ private DataObjectNormalizedNodeCache getCacheSerializer(final Class<? extends DataObject> type) {
if (cacheHolder.isCached(type)) {
final DataContainerCodecContext<?, ?> currentCtx = (DataContainerCodecContext<?, ?>) delegate.current();
if (type.equals(currentCtx.getBindingClass())) {
--- /dev/null
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.mdsal.binding.dom.codec.impl;
+
+import static java.util.Objects.requireNonNull;
+
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+
+/**
+ * A cache of NormalizedNodes corresponding to a particular DataObject instantiation.
+ */
+final class DataObjectNormalizedNodeCache
+ extends AbstractBindingNormalizedNodeCache<DataObject, DataContainerCodecContext<?, ?>> {
+ private final AbstractBindingNormalizedNodeCacheHolder cacheHolder;
+
+ DataObjectNormalizedNodeCache(final AbstractBindingNormalizedNodeCacheHolder cacheHolder,
+ final DataContainerCodecContext<?, ?> rootContext) {
+ super(rootContext);
+ this.cacheHolder = requireNonNull(cacheHolder, "cacheHolder");
+ }
+
+ @Override
+ public NormalizedNode<?, ?> load(final DataObject key) {
+ return CachingNormalizedNodeSerializer.serializeUsingStreamWriter(cacheHolder, rootContext(), key);
+ }
+}