Exemple #1
0
        public void CastGrainRefUpCastFromGrandchild()
        {
            // GeneratorTestDerivedGrain1Reference derives from GeneratorTestGrainReference
            // GeneratorTestDerivedGrain2Reference derives from GeneratorTestGrainReference
            // GeneratorTestDerivedDerivedGrainReference derives from GeneratorTestDerivedGrain2Reference

            GrainReference cast;
            GrainReference grain = (GrainReference)GrainClient.GrainFactory.GetGrain <IGeneratorTestDerivedDerivedGrain>(GetRandomGrainId());

            // Parent
            cast = (GrainReference)grain.AsReference <IGeneratorTestDerivedGrain2>();
            Assert.IsAssignableFrom <IGeneratorTestDerivedDerivedGrain>(cast);
            Assert.IsAssignableFrom <IGeneratorTestDerivedGrain2>(cast);
            Assert.IsAssignableFrom <IGeneratorTestGrain>(cast);

            // Cross-cast outside the inheritance hierarchy should not work
            Assert.False(cast is IGeneratorTestDerivedGrain1);

            // Grandparent
            cast = (GrainReference)grain.AsReference <IGeneratorTestGrain>();
            Assert.IsAssignableFrom <IGeneratorTestDerivedDerivedGrain>(cast);
            Assert.IsAssignableFrom <IGeneratorTestGrain>(cast);

            // Cross-cast outside the inheritance hierarchy should not work
            Assert.False(cast is IGeneratorTestDerivedGrain1);
        }
        public void CastGrainRefCastFromMyType()
        {
            GrainReference grain = (GrainReference)GrainClient.GrainFactory.GetGrain <ISimpleGrain>(random.Next(), SimpleGrain.SimpleGrainNamePrefix);
            GrainReference cast  = (GrainReference)grain.AsReference <ISimpleGrain>();

            Assert.IsInstanceOfType(cast, grain.GetType());
            Assert.IsInstanceOfType(cast, typeof(ISimpleGrain));
        }
Exemple #3
0
        public void CastGrainRefCastFromMyType()
        {
            GrainReference grain = (GrainReference)GrainClient.GrainFactory.GetGrain <ISimpleGrain>(random.Next(), SimpleGrain.SimpleGrainNamePrefix);
            GrainReference cast  = (GrainReference)grain.AsReference <ISimpleGrain>();

            Assert.IsAssignableFrom(grain.GetType(), cast);
            Assert.IsAssignableFrom <ISimpleGrain>(cast);
        }
Exemple #4
0
        public void CastGrainRefUpCastFromChild()
        {
            // GeneratorTestDerivedGrain1Reference extends GeneratorTestGrainReference
            GrainReference grain = (GrainReference)GrainClient.GrainFactory.GetGrain <IGeneratorTestDerivedGrain1>(GetRandomGrainId());
            GrainReference cast  = (GrainReference)grain.AsReference <IGeneratorTestGrain>();

            Assert.IsAssignableFrom <IGeneratorTestDerivedGrain1>(cast);
            Assert.IsAssignableFrom <IGeneratorTestGrain>(cast);
        }
        public void CastGrainRefUpCastFromChild()
        {
            // GeneratorTestDerivedGrain1Reference extends GeneratorTestGrainReference
            GrainReference grain = (GrainReference)GrainClient.GrainFactory.GetGrain <IGeneratorTestDerivedGrain1>(GetRandomGrainId());
            GrainReference cast  = (GrainReference)grain.AsReference <IGeneratorTestGrain>();

            Assert.IsInstanceOfType(cast, typeof(IGeneratorTestDerivedGrain1));
            Assert.IsInstanceOfType(cast, typeof(IGeneratorTestGrain));
        }
        public async Task <StreamSubscription> AddSubscription(string streamProviderName, StreamId streamId, GrainReference grainRef)
        {
            var consumer         = grainRef.AsReference <IStreamConsumerExtension>();
            var internalStreamId = new InternalStreamId(streamProviderName, streamId);
            var subscriptionId   = streamPubSub.CreateSubscriptionId(internalStreamId, consumer);
            await streamPubSub.RegisterConsumer(subscriptionId, internalStreamId, consumer);

            var newSub = new StreamSubscription(subscriptionId.Guid, streamProviderName, streamId, grainRef.GrainId);

            return(newSub);
        }
Exemple #7
0
        public async Task <StreamSubscription> AddSubscription(string streamProviderName, IStreamIdentity streamIdentity, GrainReference grainRef)
        {
            var consumer       = grainRef.AsReference <IStreamConsumerExtension>();
            var streamId       = StreamId.GetStreamId(streamIdentity.Guid, streamProviderName, streamIdentity.Namespace);
            var subscriptionId = streamPubSub.CreateSubscriptionId(
                streamId, consumer);
            await streamPubSub.RegisterConsumer(subscriptionId, streamId, streamProviderName, consumer, null);

            var newSub = new StreamSubscription(subscriptionId.Guid, streamProviderName, streamId, grainRef.GrainId);

            return(newSub);
        }
        public void CastGrainRefUpCastFromDerivedDerivedChild()
        {
            // GeneratorTestDerivedDerivedGrainReference extends GeneratorTestDerivedGrain2Reference
            // GeneratorTestDerivedGrain2Reference extends GeneratorTestGrainReference
            GrainReference grain = (GrainReference)this.GrainFactory.GetGrain <IGeneratorTestDerivedDerivedGrain>(GetRandomGrainId());
            GrainReference cast  = (GrainReference)grain.AsReference <IGeneratorTestDerivedGrain2>();

            Assert.IsAssignableFrom <IGeneratorTestDerivedDerivedGrain>(cast);
            Assert.IsAssignableFrom <IGeneratorTestDerivedGrain2>(cast);
            Assert.IsAssignableFrom <IGeneratorTestGrain>(cast);
            Assert.False(cast is IGeneratorTestDerivedGrain1);
        }
        //g.addV(T.label, 'person', T.id, '9', 'name', 'Brian', 'age', 42).as('a').properties('name').hasValue('Brian').property('acl','public').property('createdOn','12/01/2016').select('a').properties('age').hasValue(42).property('acl','private').select('a')
        internal async Task ClearEdgeStateAsync(GrainReference grainReference, EdgeState edgeState)
        {
            var graphElementGrain = grainReference.AsReference <IGraphElementGrain>();

            var feedOptions = new FeedOptions
            {
                PartitionKey = new PartitionKey(graphElementGrain.GetGraphPartition())
            };

            var dropCommand = $"g.E('{graphElementGrain.ToKeyString()}')";
            var writeQuery  = client.CreateGremlinQuery <CosmosDbEdge>(graph, dropCommand, feedOptions);
            var response    = await writeQuery.ExecuteNextAsync <CosmosDbEdge>();

            edgeState.Persisted = false;

            log.Info($"CosmosDB: Drop Vertex State: Request Charge: {response.RequestCharge}");
        }
        internal async Task ReadEdgeStateAsync(GrainReference grainReference, EdgeState edgeState)
        {
            var graphElementGrain = grainReference.AsReference <IGraphElementGrain>();

            var readExpression = $"g.E('{grainReference.ToKeyString()}')";

            var feedOptions = new FeedOptions
            {
                MaxItemCount = 1,
                PartitionKey = new PartitionKey(graphElementGrain.GetGraphPartition())
            };

            var readQuery = client.CreateGremlinQuery <CosmosDbEdge>(graph, readExpression, feedOptions, GraphSONMode.Normal);
            var response  = await readQuery.ExecuteNextAsync <CosmosDbEdge>();

            log.Info($"CosmosDB: Read Edge State: Request Charge: {response.RequestCharge}");

            var edge = response.FirstOrDefault();

            if (edge == null)
            {
                return;
            }

            edgeState.Persisted = true;

            var inV  = grainReferenceConverter.GetGrainFromKeyString(edge.InVertexId.ToString());
            var outV = grainReferenceConverter.GetGrainFromKeyString(edge.OutVertexId.ToString());

            inV.BindGrainReference(grainFactory);
            outV.BindGrainReference(grainFactory);

            edgeState.SetInVertex(inV.AsReference <IVertex>());
            edgeState.SetOutVertex(outV.AsReference <IVertex>());

            foreach (var property in edge.GetProperties())
            {
                if (property.Key[0] == '@' || property.Key == "partition")
                {
                    continue;
                }

                edgeState[property.Key] = property.Value.ToString();
            }
        }
        internal async Task WriteEdgeStateAsync(GrainReference grainReference, EdgeState edgeState)
        {
            var graphElementGrain = grainReference.AsReference <IGraphElementGrain>();

            var upsertExpression = (edgeState.Persisted ?
                                    CreateUpdateExpression(grainReference, edgeState, graphElementGrain) :
                                    CreateInsertExpression(grainReference, edgeState, graphElementGrain))
                                   .ToString();

            var feedOptions = new FeedOptions
            {
                MaxItemCount = 1,
                PartitionKey = new PartitionKey(graphElementGrain.GetGraphPartition())
            };

            var writeQuery = client.CreateGremlinQuery <CosmosDbEdge>(graph, upsertExpression, feedOptions, GraphSONMode.Normal);

            log.Info($"CosmosDB: Writing EdgeState for grain Id '{graphElementGrain.ToKeyString()}'");
            var response = await writeQuery.ExecuteNextAsync <CosmosDbEdge>();

            log.Info($"CosmosDB: Writing EdgeState complete: Request Charge: {response.RequestCharge}");

            edgeState.Persisted = true;
        }
        internal async Task ReadVertexStateAsync(GrainReference grainReference, VertexState vertexState)
        {
            var readExpression    = $"g.V('{grainReference.ToKeyString()}')";
            var graphElementGrain = grainReference.AsReference <IGraphElementGrain>();

            var feedOptions = new FeedOptions
            {
                MaxItemCount = 1,
                PartitionKey = new PartitionKey(graphElementGrain.GetGraphPartition())
            };

            var readQuery = client.CreateGremlinQuery <CosmosDbVertex>(graph, readExpression, feedOptions, GraphSONMode.Normal);

            var response = await readQuery.ExecuteNextAsync <CosmosDbVertex>();

            log.Info($"CosmosDB: Read Vertex State: Request Charge: {response.RequestCharge}");

            var vertex = response.FirstOrDefault();

            if (vertex == null)
            {
                return;
            }

            vertexState.Persisted = true;

            foreach (var edge in vertex.GetInEdges())
            {
                var edgeReference = grainReferenceConverter.GetGrainFromKeyString(edge.Id.ToString());
                edgeReference.BindGrainReference(grainFactory);

                var vertexReference = grainReferenceConverter.GetGrainFromKeyString(edge.InVertexId.ToString());
                vertexReference.BindGrainReference(grainFactory);

                vertexState.AddInEdge(edgeReference.AsReference <IEdge>(), vertexReference.AsReference <IVertex>());
            }

            foreach (var edge in vertex.GetOutEdges())
            {
                var edgeReference = grainReferenceConverter.GetGrainFromKeyString(edge.Id.ToString());
                edgeReference.BindGrainReference(grainFactory);

                var vertexReference = grainReferenceConverter.GetGrainFromKeyString(edge.InVertexId.ToString());
                vertexReference.BindGrainReference(grainFactory);

                vertexState.AddOutEdge(edgeReference.AsReference <IEdge>(), vertexReference.AsReference <IVertex>());
            }

            foreach (var property in vertex.GetVertexProperties())
            {
                if (property.Key[0] == '@' || property.Key == "partition")
                {
                    continue;
                }

                var vertexProperty = vertexState.SetProperty(property.Key, property.Value.ToString());

                try
                {
                    foreach (var subProperty in property.GetProperties())
                    {
                        if (subProperty.Key[0] == '@')
                        {
                            continue;
                        }

                        vertexProperty.SetMeta(subProperty.Key, subProperty.Value.ToString());
                    }
                }
                // BUG: The Microsoft.Azure.Graphs library throws an exception when enumerating over empty properties. How do we check or work around this?
                catch (NullReferenceException) { }
            }
        }