private void AddEntityPlaceholder(ReactiveEntityKind kind, string key) { var uri = new Uri(key); #pragma warning disable IDE0079 // Remove unnecessary suppression. #pragma warning disable CA2000 // Dispose objects before losing scope. (Entities are owned by the registry.) var added = kind switch { ReactiveEntityKind.Observable => Registry.Observables.TryAdd(key, ObservableDefinitionEntity.CreateInvalidInstance(uri)), ReactiveEntityKind.Observer => Registry.Observers.TryAdd(key, ObserverDefinitionEntity.CreateInvalidInstance(uri)), ReactiveEntityKind.Stream => Registry.Subjects.TryAdd(key, SubjectEntity.CreateInvalidInstance(uri)), ReactiveEntityKind.StreamFactory => Registry.SubjectFactories.TryAdd(key, StreamFactoryDefinitionEntity.CreateInvalidInstance(uri)), ReactiveEntityKind.SubscriptionFactory => Registry.SubscriptionFactories.TryAdd(key, SubscriptionFactoryDefinitionEntity.CreateInvalidInstance(uri)), ReactiveEntityKind.Subscription => Registry.Subscriptions.TryAdd(key, SubscriptionEntity.CreateInvalidInstance(uri)), ReactiveEntityKind.ReliableSubscription => Registry.ReliableSubscriptions.TryAdd(key, ReliableSubscriptionEntity.CreateInvalidInstance(uri)), ReactiveEntityKind.Other => Registry.Other.TryAdd(key, OtherDefinitionEntity.CreateInvalidInstance(uri)), ReactiveEntityKind.Template => Registry.Templates.TryAdd(key, OtherDefinitionEntity.CreateInvalidInstance(uri)), _ => throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Cannot create an invalid entity '{0}' of type '{1}'.", key, kind)), }; #pragma warning restore CA2000 #pragma warning restore IDE0079 if (added) { Parent.TraceSource.Registry_AddEntityPlaceholder(Parent.Uri, kind.ToString(), key); } }
/// <summary> /// Predicate defining whether the subject state should be included in the checkpoint. /// </summary> /// <param name="entity">The entity.</param> /// <returns> /// True if should be included. /// </returns> protected override bool ShouldSaveSubjectState(SubjectEntity entity) { if (entity.Instance is IStatefulOperator op) { return(op.StateChanged); } return(false); }
/// <summary> /// Creates an invalid instance of the given reactive entity kind with the given URI. /// </summary> /// <param name="uri">The URI.</param> /// <param name="kind">The reactive entity kind.</param> /// <returns> /// The invalid instance, or null if an unexpected kind is provided. /// </returns> public static ReactiveEntity CreateInvalidInstance(Uri uri, ReactiveEntityKind kind) { return(kind switch { ReactiveEntityKind.Observable => ObservableDefinitionEntity.CreateInvalidInstance(uri), ReactiveEntityKind.Observer => ObserverDefinitionEntity.CreateInvalidInstance(uri), ReactiveEntityKind.Stream => SubjectEntity.CreateInvalidInstance(uri), ReactiveEntityKind.StreamFactory => StreamFactoryDefinitionEntity.CreateInvalidInstance(uri), ReactiveEntityKind.Subscription => SubscriptionEntity.CreateInvalidInstance(uri), ReactiveEntityKind.ReliableSubscription => ReliableSubscriptionEntity.CreateInvalidInstance(uri), ReactiveEntityKind.Template or ReactiveEntityKind.Other => OtherDefinitionEntity.CreateInvalidInstance(uri), _ => null, });
/// <summary> /// Saves subject runtime state. /// </summary> /// <param name="entity">The entity to save.</param> /// <param name="stream">The stream to save to.</param> protected virtual void SaveSubjectState(SubjectEntity entity, Stream stream) { Debug.Assert(entity != null, "Entity should not be null."); Debug.Assert(stream != null, "Stream should not be null."); var policy = _engine.Parent._serializationPolicy; using (entity.Measure(EntityMetric.SaveState)) { using var operatorStateWriter = new OperatorStateWriterFactory(stream, policy); operatorStateWriter.WriteHeader(); if (entity.Instance is IStatefulOperator op) { operatorStateWriter.SaveState(op); } } }
/// <summary> /// This constructor is needed to ensure ordering between the checks /// for `IsInitialized`, and the creation of the constant expression. /// If the order were reversed, we could get into a state where the /// constant expression contains a null value, but the entity appears /// to be initialized. /// </summary> /// <param name="entity">The subject.</param> /// <param name="isInitialized"> /// <b>true</b> if the subject is initialized at the time the /// constructor is called, <b>false</b> otherwise. /// </param> private FromSubjectEntity(SubjectEntity entity, bool isInitialized) : base(entity.Uri, Expression.Constant(entity.Instance), entity.State) { IsInitialized = isInitialized; }
public FromSubjectEntity(SubjectEntity subject) : this(subject, subject.IsInitialized) { }
public static ObserverDefinitionEntity FromSubject(SubjectEntity subject) => new FromSubjectEntity(subject);
private bool TryCreateStreamFromMetadata(IReactiveMetadata metadata, string key, out SubjectEntity subject) { // // Lookup the stream definition from metadata. If we find a // remote definition, a singleton instance should be created. // Note, in a proper architecture, the stream would have // already been created (via invocation of a stream factory or // otherwise) before we reached this point in subscription // creation. Thus, we would have found a stream instance in the // local registry, and this code would not be executed. // if (metadata.Streams.TryGetValue(new Uri(key), out IReactiveStreamProcess streamProcess)) { lock (_createStreamGate) { // TODO: rewrite stream expression based on bound type of stream factory if (!_queryEngine._registry.Subjects.TryGetValue(key, out subject)) { var streamExpr = RewriteQuotedReactiveStreamToUntyped(key, streamProcess.Expression); Expression expr = _queryEngine.RewriteQuotedReactiveToSubscribable(streamExpr); _queryEngine._engine.CreateStream(new Uri(key), expr, null); _queryEngine.TraceSource.LazyStream_Created(key, _queryEngine.Uri, expr.ToTraceString()); if (_queryEngine._registry.Subjects.TryGetValue(key, out subject)) { subject.AdvanceState(TransactionState.Active); } else { throw new InvalidOperationException( string.Format(CultureInfo.InvariantCulture, "Attempt to lazily create stream '{0}' on query engine '{1}' has failed.", key, _queryEngine.Uri.ToCanonicalString())); } } } return(true); } subject = null; return(false); }
/// <summary> /// Predicate defining whether the subject state should be included in the checkpoint. /// </summary> /// <param name="entity">The entity.</param> /// <returns> /// True if should be included. /// </returns> protected override bool ShouldSaveSubjectState(SubjectEntity entity) => true;
/// <summary> /// Predicate defining whether the subject state should be included in the checkpoint. /// </summary> /// <param name="entity">The entity.</param> /// <returns>True if should be included.</returns> protected abstract bool ShouldSaveSubjectState(SubjectEntity entity);