Beispiel #1
0
 /// <summary>
 /// Creates a new metadata cache using the specified external and local metadata services.
 /// </summary>
 /// <param name="external">External metadata service to fall back to for lookups.</param>
 /// <param name="local">Local metadata service used for caching of immutable resources that can be shared safely across users of the metadata service.</param>
 public ReactiveMetadataCache(IReactiveMetadata external, IReactiveMetadataInMemory local)
 {
     _observables           = new LeveledCacheQueryableDictionary <IReactiveObservableDefinition>(external.Observables, local.Observables);
     _observers             = new LeveledCacheQueryableDictionary <IReactiveObserverDefinition>(external.Observers, local.Observers);
     _streamFactories       = new LeveledCacheQueryableDictionary <IReactiveStreamFactoryDefinition>(external.StreamFactories, local.StreamFactories);
     _subscriptionFactories = new LeveledCacheQueryableDictionary <IReactiveSubscriptionFactoryDefinition>(external.SubscriptionFactories, local.SubscriptionFactories);
     _subscriptions         = new LeveledCacheQueryableDictionary <IReactiveSubscriptionProcess>(external.Subscriptions, local.Subscriptions);
     _streams = new LeveledCacheQueryableDictionary <IReactiveStreamProcess>(external.Streams, local.Streams);
 }
Beispiel #2
0
 /// <summary>
 /// Creates a new query engine that's aware of host services.
 /// </summary>
 /// <param name="uri">URI identifying the query engine.</param>
 /// <param name="serviceResolver">Resolver used by the query engine to
 /// resolve artifacts to reactive services for delegation of requests or
 /// other operations.</param>
 /// <param name="scheduler">Scheduler used by the query engine to
 /// schedule work, also allowing for pausing during checkpointing
 /// operations.</param>
 /// <param name="metadataRegistry">Metadata consulted by the query
 /// engine to lookup artifact definitions.</param>
 /// <param name="traceSource">Tracer to log to.</param>
 public QueryEvaluatorQueryEngine(
     Uri uri,
     IReactiveServiceResolver serviceResolver,
     IScheduler scheduler,
     IReactiveMetadata metadataRegistry,
     IKeyValueStore keyValueStore,
     TraceSource traceSource,
     IReadOnlyDictionary <string, object> context)
     : base(uri, serviceResolver, scheduler, metadataRegistry, keyValueStore, SerializationPolicy.Default, s_map, traceSource)
 {
     _context     = context;
     _traceSource = traceSource;
 }
Beispiel #3
0
            public bool TryLookupSubscriptionFactory(IReactiveMetadata metadata, string key, out SubscriptionFactoryDefinitionEntity entity)
            {
                if (metadata.SubscriptionFactories.TryGetValue(new Uri(key), out IReactiveSubscriptionFactoryDefinition subscriptionFactoryDefinition))
                {
                    var expression = _metadataRewrites.GetValue(
                        subscriptionFactoryDefinition.Expression,
                        e => _queryEngine.RewriteQuotedReactiveToSubscribable(e));
                    entity = new SubscriptionFactoryDefinitionEntity(subscriptionFactoryDefinition.Uri, expression, subscriptionFactoryDefinition.State);
                    return(true);
                }

                entity = null;
                return(false);
            }
Beispiel #4
0
            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);
            }
Beispiel #5
0
            public bool TryLookupObservable(IReactiveMetadata metadata, string key, out ObservableDefinitionEntity entity)
            {
                if (metadata.Observables.TryGetValue(new Uri(key), out IReactiveObservableDefinition observableDefinition))
                {
                    var expression = _metadataRewrites.GetValue(
                        observableDefinition.Expression,
                        e => _queryEngine.RewriteQuotedReactiveToSubscribable(e));
                    entity = new ObservableDefinitionEntity(observableDefinition.Uri, expression, observableDefinition.State);
                    return(true);
                }

                // Will result in a stream being created if a definition exists
                // in the remote metadata store. This path should only succeed
                // when hit by the definition inlining binder.
                if (TryCreateStreamFromMetadata(metadata, key, out var subject))
                {
                    entity = ObservableDefinitionEntity.FromSubject(subject);
                    return(true);
                }

                entity = null;
                return(false);
            }
Beispiel #6
0
            public BuiltinEntitiesRegistry(CheckpointingQueryEngine queryEngine, IReactiveMetadata metadata)
            {
                Debug.Assert(queryEngine != null);
                Debug.Assert(metadata != null);

                _queryEngine = queryEngine;
                _subjects    = new ReactiveEntityCollection <string, SubjectEntity>(StringComparer.Ordinal);

                var localObservables            = new ReactiveEntityCollection <string, ObservableDefinitionEntity>(StringComparer.Ordinal);
                var localObservablesAndSubjects = new ChainedLookupReactiveEntityCollection <string, ObservableDefinitionEntity, SubjectEntity>(localObservables, _subjects, s => ObservableDefinitionEntity.FromSubject(s));

                _observables = new ExternalLookupReactiveEntityCollection <ObservableDefinitionEntity>(localObservablesAndSubjects, TryLookupObservable, metadata);

                var localObservers            = new ReactiveEntityCollection <string, ObserverDefinitionEntity>(StringComparer.Ordinal);
                var localObserversAndSubjects = new ChainedLookupReactiveEntityCollection <string, ObserverDefinitionEntity, SubjectEntity>(localObservers, _subjects, s => ObserverDefinitionEntity.FromSubject(s));

                _observers = new ExternalLookupReactiveEntityCollection <ObserverDefinitionEntity>(localObserversAndSubjects, TryLookupObserver, metadata);

                var localSubjectFactories = new ReactiveEntityCollection <string, StreamFactoryDefinitionEntity>(StringComparer.Ordinal);

                _subjectFactories = new ExternalLookupReactiveEntityCollection <StreamFactoryDefinitionEntity>(localSubjectFactories, TryLookupStreamFactory, metadata);

                var localSubscriptionFactories = new ReactiveEntityCollection <string, SubscriptionFactoryDefinitionEntity>(StringComparer.Ordinal);

                _subscriptionFactories = new ExternalLookupReactiveEntityCollection <SubscriptionFactoryDefinitionEntity>(localSubscriptionFactories, TryLookupSubscriptionFactory, metadata);

                _other     = new ReactiveEntityCollection <string, DefinitionEntity>(StringComparer.Ordinal);
                _templates = new InvertedLookupReactiveEntityCollection <string, DefinitionEntity>(StringComparer.Ordinal, InvertedDefinitionEntityComparer.Default);

                _subscriptions         = new ReactiveEntityCollection <string, SubscriptionEntity>(StringComparer.Ordinal);
                _reliableSubscriptions = new ReactiveEntityCollection <string, ReliableSubscriptionEntity>(StringComparer.Ordinal);

                _metadataRewrites = new ConditionalWeakTable <Expression, Expression>();

                InitializeBuiltinDefinitions(queryEngine);
            }
        protected override CheckpointingQueryEngine CreateQueryEngine(System.Uri uri, IReactiveServiceResolver resolver, IScheduler scheduler, IReactiveMetadata metadata, IKeyValueStore keyValueStore, TraceSource traceSource, IReadOnlyDictionary <string, object> contextElements)
        {
            var engine = base.CreateQueryEngine(uri, resolver, scheduler, metadata, keyValueStore, traceSource, contextElements);

#pragma warning disable IDE0034 // Simplify 'default' expression (illustrative of method signature)
#pragma warning disable CA1305  // Specify IFormatProvider (captured in expression tree)
            var constantHoister = ConstantHoister.Create(
                true,
                (Expression <Func <string, string> >)(c => string.Format(c, default(object))),
                (Expression <Func <string, string> >)(c => string.Format(c, default(object), default(object))),
                (Expression <Func <string, string> >)(c => string.Format(c, default(object), default(object), default(object))),
                (Expression <Func <string, string> >)(c => string.Format(c, default(object[]))),
                (Expression <Func <string, string> >)(c => string.Format(default(IFormatProvider), c, default(object[]))),
                (Expression <Func <string, JProperty> >)(c => new JProperty(c, default(object))),
                (Expression <Func <string, JProperty> >)(c => new JProperty(c, default(object[]))));
#pragma warning restore CA1305  // Specify IFormatProvider
#pragma warning restore IDE0034 // Simplify 'default' expression

            engine.Options.ExpressionPolicy.DelegateCache   = new SimpleCompiledDelegateCache();
            engine.Options.ExpressionPolicy.InMemoryCache   = new QueryEvaluatorExpressionHeap(constantHoister);
            engine.Options.ExpressionPolicy.ConstantHoister = constantHoister;

            return(engine);
        }
Beispiel #8
0
 public ExternalLookupReactiveEntityCollection(IReactiveEntityCollection <string, TDefinitionEntity> local, TryLookup <IReactiveMetadata, string, TDefinitionEntity> lookupFunc, IReactiveMetadata external)
     : base(local, lookupFunc, external)
 {
 }
Beispiel #9
0
        /// <summary>
        /// Looks up the free variable name in the Reactive metadata and attempts to unify entity types.
        /// </summary>
        /// <param name="free">The free variable.</param>
        /// <param name="metadata">The metadata to unify aginst.</param>
        /// <returns>
        /// Unifications between the free variable type and the known resource type.
        /// </returns>
        internal static IEnumerable <KeyValuePair <TypeSlim, Type> > FindAndUnify(ParameterExpressionSlim free, IReactiveMetadata metadata)
        {
            var reactiveType = ReactiveEntityTypeExtensions.FromTypeSlim(free.Type);

            if ((reactiveType & ReactiveEntityType.Func) == ReactiveEntityType.Func)
            {
                reactiveType ^= ReactiveEntityType.Func;
            }

            var metadataType = default(Type);

            switch (reactiveType)
            {
            case ReactiveEntityType.Observer:
                if (metadata.Observers.TryGetValue(new Uri(free.Name), out var observer))
                {
                    metadataType = observer.Expression.Type;
                }
                break;

            case ReactiveEntityType.Observable:
                if (metadata.Observables.TryGetValue(new Uri(free.Name), out var observable))
                {
                    metadataType = observable.Expression.Type;
                }
                break;

            case ReactiveEntityType.SubscriptionFactory:
                if (metadata.SubscriptionFactories.TryGetValue(new Uri(free.Name), out var subscriptionFactory))
                {
                    metadataType = subscriptionFactory.Expression.Type;
                }
                break;

            case ReactiveEntityType.Subscription:
                if (metadata.Subscriptions.TryGetValue(new Uri(free.Name), out var subscription))
                {
                    metadataType = subscription.Expression.Type;
                }
                break;

            case ReactiveEntityType.StreamFactory:
                if (metadata.StreamFactories.TryGetValue(new Uri(free.Name), out var streamFactory))
                {
                    metadataType = streamFactory.Expression.Type;
                }
                break;

            case ReactiveEntityType.Stream:
                if (metadata.Streams.TryGetValue(new Uri(free.Name), out var stream))
                {
                    metadataType = stream.Expression.Type;
                }
                break;
            }

            if (metadataType != null)
            {
                var unifier = new DataModelTypeUnifier();
                if (unifier.Unify(metadataType, free.Type))
                {
                    return(unifier.Entries);
                }
            }

            return(Array.Empty <KeyValuePair <TypeSlim, Type> >());
        }
Beispiel #10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="UnifyingSerializationHelpers"/>
 /// class to serialize and deserialize expressions and data model-compliant objects.
 /// </summary>
 /// <param name="metadata">
 /// The source of unbound parameter definitions for unification.
 /// </param>
 public UnifyingSerializationHelpers(IReactiveMetadata metadata)
 {
     _metadata = metadata;
 }
 public CheckpointingQueryEngine(Uri uri, IReactiveServiceResolver serviceResolver, IScheduler scheduler, IReactiveMetadata metadataRegistry, IKeyValueStore keyValueStore, IQuotedTypeConversionTargets quotedTypeConversionTargets, TraceSource traceSource, ICompiledDelegateCache delegateCache)
     : base(uri, serviceResolver, scheduler, metadataRegistry, keyValueStore, SerializationPolicy.Default, quotedTypeConversionTargets, traceSource, delegateCache)
 {
     _objectSpace = new PersistedObjectSpace(new SerializationFactory());
 }