extends StatementContextBase<A, D, E> implements OnDemandSchemaTreeStorageNode {
private static final Logger LOG = LoggerFactory.getLogger(InferredStatementContext.class);
+ // Sentinel object for 'substatements'
+ private static final Object SWEPT_SUBSTATEMENTS = new Object();
+
private final @NonNull StatementContextBase<A, D, E> prototype;
private final @NonNull StatementContextBase<?, ?, ?> parent;
private final @NonNull StmtContext<A, D, E> originalCtx;
private final A argument;
/**
- * Effective substatements, lazily materialized. This field can have three states:
+ * Effective substatements, lazily materialized. This field can have four states:
* <ul>
* <li>it can be {@code null}, in which case no materialization has taken place</li>
* <li>it can be a {@link HashMap}, in which case partial materialization has taken place</li>
* <li>it can be a {@link List}, in which case full materialization has taken place</li>
+ * <li>it can be {@link SWEPT_SUBSTATEMENTS}, in which case materialized state is no longer available</li>
* </ul>
*/
private Object substatements;
super(prototype.definition(), CopyHistory.of(myCopyType, prototype.getCopyHistory()));
this.parent = requireNonNull(parent);
this.prototype = requireNonNull(prototype);
- this.argument = targetModule == null ? prototype.getStatementArgument()
+ this.argument = targetModule == null ? prototype.argument()
: prototype.definition().adaptArgumentValue(prototype, targetModule);
this.childCopyType = requireNonNull(childCopyType);
this.targetModule = targetModule;
this.originalCtx = prototype.getOriginalCtx().orElse(prototype);
+
+ // Mark prototype as blocking statement cleanup
+ prototype.incRef();
}
@Override
} else {
template = prototype.allSubstatementsStream()
.filter(stmt -> stmt.producesEffective(SchemaTreeEffectiveStatement.class)
- && templateQName.equals(stmt.getStatementArgument()))
+ && templateQName.equals(stmt.argument()))
.findAny()
.orElse(null);
}
@SuppressWarnings("unchecked")
final Mutable<QName, Y, Z> ret = (Mutable<QName, Y, Z>) copySubstatement((Mutable<?, ?, ?>) template)
- .orElseThrow(() -> new InferenceException(getStatementSourceReference(),
+ .orElseThrow(() -> new InferenceException(sourceReference(),
"Failed to materialize child %s template %s", qname, template));
ensureCompletedPhase(ret);
addMaterialized(template, ret);
// Instantiate this statement's effective substatements. Note this method has side-effects in namespaces and overall
// BuildGlobalContext, hence it must be called at most once.
private List<StatementContextBase<?, ?, ?>> ensureEffectiveSubstatements() {
+ accessSubstatements();
return substatements instanceof List ? castEffective(substatements)
: initializeSubstatements(castMaterialized(substatements));
}
// from prototype (which is already at ModelProcessingPhase.EFFECTIVE_MODEL).
if (substatements == null) {
return ImmutableList.of();
- } else if (substatements instanceof HashMap) {
+ }
+ accessSubstatements();
+ if (substatements instanceof HashMap) {
return castMaterialized(substatements).values();
} else {
return castEffective(substatements);
@Override
Stream<? extends StmtContext<?, ?, ?>> streamEffective() {
- // FIXME: YANGTOOLS-1184: do not force initialization
+ accessSubstatements();
return ensureEffectiveSubstatements().stream();
}
+ private void accessSubstatements() {
+ verify(substatements != SWEPT_SUBSTATEMENTS, "Attempted to access substatements of %s", this);
+ }
+
+ @Override
+ int sweepSubstatements() {
+ final Object local = substatements;
+ substatements = SWEPT_SUBSTATEMENTS;
+ int count = 0;
+ if (local != null) {
+ final List<StatementContextBase<?, ?, ?>> list = castEffective(local);
+ sweep(list);
+ count = countUnswept(list);
+ }
+ return count;
+ }
+
private List<StatementContextBase<?, ?, ?>> initializeSubstatements(
final Map<StmtContext<?, ?, ?>, StatementContextBase<?, ?, ?>> materializedSchemaTree) {
final Collection<? extends StatementContextBase<?, ?, ?>> declared = prototype.mutableDeclaredSubstatements();
final Collection<? extends Mutable<?, ?, ?>> effective = prototype.mutableEffectiveSubstatements();
- final List<Mutable<?, ?, ?>> buffer = new ArrayList<>(declared.size() + effective.size());
+ final List<Mutable<?, ?, ?>> buffer = new ArrayList<>(declared.size() + effective.size());
for (final Mutable<?, ?, ?> stmtContext : declared) {
if (stmtContext.isSupportedByFeatures()) {
copySubstatement(stmtContext, buffer, materializedSchemaTree);
buffer.size());
ret.addAll((Collection) buffer);
substatements = ret;
+
+ prototype.decRef();
return ret;
}
private void copySubstatement(final Mutable<?, ?, ?> substatement, final Collection<Mutable<?, ?, ?>> buffer,
final Map<StmtContext<?, ?, ?>, StatementContextBase<?, ?, ?>> materializedSchemaTree) {
- final StatementDefinition def = substatement.getPublicDefinition();
+ final StatementDefinition def = substatement.publicDefinition();
// FIXME: YANGTOOLS-652: formerly known as "isReusedByUses"
if (REUSED_DEF_SET.contains(def)) {
- LOG.debug("Reusing substatement {} for {}", substatement, this);
- buffer.add(substatement);
+ LOG.trace("Reusing substatement {} for {}", substatement, this);
+ buffer.add(substatement.replicaAsChildOf(this));
return;
}
(StatementContextBase<?, ?, ?>) copy);
if (existing != null) {
throw new VerifyException(
- "Unexpected duplicate request for " + copy.getStatementArgument() + " previous result was " + existing);
+ "Unexpected duplicate request for " + copy.argument() + " previous result was " + existing);
}
}
*/
@Override
@Deprecated
- public Optional<SchemaPath> getSchemaPath() {
+ Optional<SchemaPath> schemaPath() {
return substatementGetSchemaPath();
}
@Override
- public A getStatementArgument() {
+ public A argument() {
return argument;
}