import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
import org.opendaylight.yangtools.yang.parser.spi.SchemaTreeNamespace;
-import org.opendaylight.yangtools.yang.parser.spi.meta.CopyHistory;
import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.OnDemandSchemaTreeStorageNode;
private final @NonNull StatementContextBase<A, D, E> prototype;
private final @NonNull StatementContextBase<?, ?, ?> parent;
private final @NonNull StmtContext<A, D, E> originalCtx;
+ // TODO: consider encoding this in StatementContextBase fields, there should be plenty of room
private final @NonNull CopyType childCopyType;
private final QNameModule targetModule;
private final A argument;
InferredStatementContext(final StatementContextBase<?, ?, ?> parent, final StatementContextBase<A, D, E> prototype,
final CopyType myCopyType, final CopyType childCopyType, final QNameModule targetModule) {
- super(prototype.definition(), CopyHistory.of(myCopyType, prototype.history()));
+ super(prototype, myCopyType);
this.parent = requireNonNull(parent);
this.prototype = requireNonNull(prototype);
this.argument = targetModule == null ? prototype.argument()
// First check if we can reuse the entire prototype
if (!factory.canReuseCurrent(this, prototype, origSubstatements)) {
+ // FIXME: YANGTOOLS-1214: deduplicate this return
return tryToReuseSubstatements(factory, origEffective);
}
- // No substatements to deal with, we can freely reuse the original
+ // We can reuse this statement let's see if all statements agree...
+ // ... no substatements to deal with, we can freely reuse the original
if (origSubstatements.isEmpty()) {
LOG.debug("Reusing empty: {}", origEffective);
substatements = ImmutableList.of();
return origEffective;
}
- // We can reuse this statement let's see if all the statements agree
+ // ... all are context independent, reuse the original
+ if (allSubstatementsContextIndependent()) {
+ LOG.debug("Reusing context-independent: {}", origEffective);
+ substatements = noRefs() ? REUSED_SUBSTATEMENTS : reusePrototypeReplicas();
+ prototype.decRef();
+ return origEffective;
+ }
+
+ // ... copy-sensitive check
final List<EffectiveCopy> declCopy = prototype.streamDeclared()
.map(sub -> effectiveCopy((ReactorStmtCtx<?, ?, ?>) sub))
.filter(Objects::nonNull)
.filter(Objects::nonNull)
.collect(Collectors.toUnmodifiableList());
+ // ... are any copy-sensitive?
if (allReused(declCopy) && allReused(effCopy)) {
LOG.debug("Reusing after substatement check: {}", origEffective);
substatements = noRefs() ? REUSED_SUBSTATEMENTS
return origEffective;
}
+ // *sigh*, ok, heavy lifting through a shallow copy
final List<ReactorStmtCtx<?, ?, ?>> declared = declCopy.stream()
.map(copy -> copy.toChildContext(this))
.collect(ImmutableList.toImmutableList());
prototype.decRef();
// Values are the effective copies, hence this efficiently deals with recursion.
+ // FIXME: YANGTOOLS-1214: deduplicate this return
return factory.createEffective(this, declared.stream(), effective.stream());
}
// Fall back to full instantiation, which populates our substatements. Then check if we should be reusing
// the substatement list, as this operation turned out to not affect them.
final E effective = super.createEffective(factory);
+ // Since we have forced instantiation to deal with this case, we also need to reset the 'modified' flag
+ setUnmodified();
+
if (sameSubstatements(original.effectiveSubstatements(), effective)) {
LOG.debug("Reusing unchanged substatements of: {}", prototype);
return factory.copyEffective(this, original);
}
private List<ReactorStmtCtx<?, ?, ?>> reusePrototypeReplicas() {
- return reusePrototypeReplicas(Streams.concat(
- prototype.streamDeclared(), prototype.streamEffective()));
+ return reusePrototypeReplicas(Streams.concat(prototype.streamDeclared(), prototype.streamEffective()));
}
private List<ReactorStmtCtx<?, ?, ?>> reusePrototypeReplicas(final Stream<StmtContext<?, ?, ?>> stream) {
return entries.stream().allMatch(EffectiveCopy::isReused);
}
+ @Override
+ ReactorStmtCtx<A, D, E> unmodifiedEffectiveSource() {
+ return isModified() ? this : prototype.unmodifiedEffectiveSource();
+ }
+
@Override
boolean hasEmptySubstatements() {
if (substatements == null) {
final List<ReactorStmtCtx<?, ?, ?>> ret = beforeAddEffectiveStatementUnsafe(ImmutableList.of(), buffer.size());
ret.addAll((Collection) buffer);
substatements = ret;
+ setModified();
prototype.decRef();
return ret;
// resizing operation.
materializedSchemaTree = new HashMap<>(4);
substatements = materializedSchemaTree;
+ setModified();
} else {
verify(substatements instanceof HashMap, "Unexpected substatements %s", substatements);
materializedSchemaTree = castMaterialized(substatements);