コード例 #1
0
        public async Task TryDeleteStateAsync_ValidateOptions(
            ConsistencyMode consistencyMode,
            ConcurrencyMode concurrencyMode,
            StateConsistency expectedConsistency,
            StateConcurrency expectedConcurrency)
        {
            await using var client = TestClient.CreateForDaprClient();

            var stateOptions = new StateOptions
            {
                Concurrency = concurrencyMode,
                Consistency = consistencyMode
            };

            var request = await client.CaptureGrpcRequestAsync(async daprClient =>
            {
                return await daprClient.TryDeleteStateAsync("testStore", "test", "Test_Etag", stateOptions);
            });

            request.Dismiss();

            // Get Request and validate
            var envelope = await request.GetRequestEnvelopeAsync<Autogenerated.DeleteStateRequest>();
            envelope.StoreName.Should().Be("testStore");
            envelope.Key.Should().Be("test");
            envelope.Etag.Value.Should().Be("Test_Etag");
            envelope.Options.Concurrency.Should().Be(expectedConcurrency);
            envelope.Options.Consistency.Should().Be(expectedConsistency);
        }
コード例 #2
0
        public async Task TryDeleteStateAsync_ValidateOptions(
            ConsistencyMode consistencyMode,
            ConcurrencyMode concurrencyMode,
            StateConsistency expectedConsistency,
            StateConcurrency expectedConcurrency)
        {
            // Configure Client
            var httpClient = new TestHttpClient();
            var daprClient = new DaprClientBuilder()
                             .UseGrpcChannelOptions(new GrpcChannelOptions {
                HttpClient = httpClient
            })
                             .Build();

            var stateOptions = new StateOptions
            {
                Concurrency = concurrencyMode,
                Consistency = consistencyMode
            };

            var task = daprClient.TryDeleteStateAsync("testStore", "test", "Test_Etag", stateOptions);

            // Get Request and validate
            httpClient.Requests.TryDequeue(out var entry).Should().BeTrue();
            var request = await GrpcUtils.GetRequestFromRequestMessageAsync <Autogenerated.DeleteStateRequest>(entry.Request);

            request.StoreName.Should().Be("testStore");
            request.Key.Should().Be("test");
            request.Etag.Should().Be("Test_Etag");
            request.Options.Concurrency.Should().Be(expectedConcurrency);
            request.Options.Consistency.Should().Be(expectedConsistency);
        }
        public async Task <ActionResult <Guid> > SubmitOrder(Order order, [FromServices] DaprClient daprClient)
        {
            if (!Validate(order))
            {
                return(BadRequest());
            }
            // order validated
            order.Id = Guid.NewGuid();

            var state = await daprClient.GetStateEntryAsync <OrderState>(StoreName, order.Id.ToString());

            state.Value ??= new OrderState()
            {
                CreatedOn = DateTime.UtcNow, UpdatedOn = DateTime.UtcNow, Order = order
            };

            Console.WriteLine($"ETag {state.ETag}");
            var options = new StateOptions()
            {
                Concurrency = ConcurrencyMode.FirstWrite, Consistency = ConsistencyMode.Strong
            };
            await state.SaveAsync(stateOptions : options);

            await daprClient.PublishEventAsync <Order>(PubSub, common.Topics.OrderSubmittedTopicName, order);

            Console.WriteLine($"Submitted order {order.Id}");
            return(order.Id);
        }
コード例 #4
0
 public OrderEditCommand(DeliveryPlaceOptions deliveryPlace, DateTime?deliveryPredicateAt,
                         DateTime?deliveryAt, StateOptions state, string comment)
 {
     DeliveryPlace       = deliveryPlace;
     DeliveryPredicateAt = deliveryPredicateAt;
     DeliveryAt          = deliveryAt;
     State   = state;
     Comment = comment;
 }
コード例 #5
0
ファイル: StateEntry.cs プロジェクト: wmadzha/dotnet-sdk
 /// <summary>
 /// Tries to delete the the state using the
 /// <see cref="ETag"/> from the Dapr state. State store implementation will allow the delete only if the attached ETag matches with the latest ETag in the state store.
 /// </summary>
 /// <param name="metadata">An key/value pair that may be consumed by the state store.  This is dependent on the type of state store used.</param>
 /// <param name="stateOptions">Options for Save state operation.</param>
 /// <param name="cancellationToken">A <see cref="CancellationToken" /> that can be used to cancel the operation.</param>
 /// <returns>A <see cref="ValueTask" /> that will complete when the operation has completed.  If the wrapped value is true the operation suceeded.</returns>
 public ValueTask <bool> TryDeleteAsync(StateOptions stateOptions = default, Dictionary <string, string> metadata = default, CancellationToken cancellationToken = default)
 {
     return(this.client.TryDeleteStateAsync(
                this.StoreName,
                this.Key,
                this.ETag,
                stateOptions,
                cancellationToken));
 }
コード例 #6
0
 public OrderDetail(Product product, float quantity, DateTime createdAt,
                    StateOptions state, DateTime deliveryPredicateAt, DateTime?deliveryAt)
 {
     Product             = product;
     Quantity            = quantity;
     CreatedAt           = createdAt;
     State               = state;
     DeliveryPredicateAt = deliveryPredicateAt;
     DeliveryAt          = deliveryAt;
 }
コード例 #7
0
        public async Task TrySaveStateAsync_ValidateOptions(
            ConsistencyMode consistencyMode,
            ConcurrencyMode concurrencyMode,
            StateConsistency expectedConsistency,
            StateConcurrency expectedConcurrency)
        {
            // Configure Client
            var httpClient = new TestHttpClient();
            var daprClient = new DaprClientBuilder()
                             .UseGrpcChannelOptions(new GrpcChannelOptions {
                HttpClient = httpClient
            })
                             .Build();

            var widget = new Widget()
            {
                Size = "small", Color = "yellow",
            };
            var stateOptions = new StateOptions
            {
                Concurrency = concurrencyMode,
                Consistency = consistencyMode
            };

            var metadata = new Dictionary <string, string>
            {
                { "key1", "value1" },
                { "key2", "value2" }
            };
            var task = daprClient.TrySaveStateAsync("testStore", "test", widget, "Test_Etag", stateOptions, metadata);

            // Get Request and validate
            httpClient.Requests.TryDequeue(out var entry).Should().BeTrue();
            var request = await GrpcUtils.GetRequestFromRequestMessageAsync <Autogenerated.SaveStateRequest>(entry.Request);

            request.StoreName.Should().Be("testStore");
            request.States.Count.Should().Be(1);
            var state = request.States[0];

            state.Key.Should().Be("test");
            state.Etag.Should().Be("Test_Etag");
            state.Metadata.Count.Should().Be(2);
            state.Metadata.Keys.Contains("key1").Should().BeTrue();
            state.Metadata.Keys.Contains("key2").Should().BeTrue();
            state.Metadata["key1"].Should().Be("value1");
            state.Metadata["key2"].Should().Be("value2");
            state.Options.Concurrency.Should().Be(expectedConcurrency);
            state.Options.Consistency.Should().Be(expectedConsistency);

            var stateJson        = state.Value.ToStringUtf8();
            var stateFromRequest = JsonSerializer.Deserialize <Widget>(stateJson);

            stateFromRequest.Size.Should().Be(widget.Size);
            stateFromRequest.Color.Should().Be(widget.Color);
        }
コード例 #8
0
ファイル: StateEntry.cs プロジェクト: dapr/dotnet-sdk
 /// <summary>
 /// Tries to save the state using the etag to the Dapr state. State store implementation will allow the update only if the attached ETag matches with the latest ETag in the state store.
 /// </summary>
 /// <param name="metadata">An key/value pair that may be consumed by the state store.  This is dependent on the type of state store used.</param>
 /// <param name="stateOptions">Options for Save state operation.</param>
 /// <param name="cancellationToken">A <see cref="CancellationToken" /> that can be used to cancel the operation.</param>
 /// <returns>A <see cref="Task" /> that will complete when the operation has completed.  If the wrapped value is true the operation suceeded.</returns>
 public Task <bool> TrySaveAsync(StateOptions stateOptions = default, IReadOnlyDictionary <string, string> metadata = default, CancellationToken cancellationToken = default)
 {
     return(this.client.TrySaveStateAsync(
                this.StoreName,
                this.Key,
                this.Value,
                this.ETag,
                stateOptions,
                metadata,
                cancellationToken));
 }
コード例 #9
0
 public OrderListModel(string id, string number, string customer, int productsCount,
                       double amount, DateTime createdAt, StateOptions status)
 {
     Id            = id;
     Number        = number;
     Customer      = customer;
     ProductsCount = productsCount.ToString("N0");
     Amount        = amount.ToString("N0");
     Since         = createdAt.ToRelativeDate();
     Status        = status.ToString().Replace("_", " ");
 }
コード例 #10
0
 /// <summary>
 /// Saves the current value of <see cref="Value" /> to the state store.
 /// </summary>
 /// <param name="stateOptions">Options for Save state operation.</param>
 /// <param name="metadata">An key/value pair that may be consumed by the state store.  This is dependent on the type of state store used.</param>
 /// <param name="cancellationToken">A <see cref="CancellationToken" /> that can be used to cancel the operation.</param>
 /// <returns>A <see cref="Task" /> that will complete when the operation has completed.</returns>
 public Task SaveAsync(StateOptions stateOptions = default, Dictionary <string, string> metadata = default, CancellationToken cancellationToken = default)
 {
     // ETag is intentionally not specified
     return(this.client.SaveStateAsync(
                storeName: this.StoreName,
                key: this.Key,
                value: this.Value,
                metadata: metadata,
                stateOptions: stateOptions,
                cancellationToken: cancellationToken));
 }
コード例 #11
0
ファイル: StateApiTest.cs プロジェクト: tstojecki/dotnet-sdk
        public async Task SaveStateAsync_ValidateOptions(
            ConsistencyMode consistencyMode,
            ConcurrencyMode concurrencyMode,
            StateConsistency expectedConsistency,
            StateConcurrency expectedConcurrency)
        {
            await using var client = TestClient.CreateForDaprClient();

            var widget = new Widget()
            {
                Size = "small", Color = "yellow",
            };
            var stateOptions = new StateOptions
            {
                Concurrency = concurrencyMode,
                Consistency = consistencyMode
            };

            var metadata = new Dictionary <string, string>
            {
                { "key1", "value1" },
                { "key2", "value2" }
            };

            var request = await client.CaptureGrpcRequestAsync(async daprClient =>
            {
                await daprClient.SaveStateAsync("testStore", "test", widget, stateOptions, metadata);
            });

            request.Dismiss();

            // Get Request and validate
            var envelope = await request.GetRequestEnvelopeAsync <Autogenerated.SaveStateRequest>();

            envelope.StoreName.Should().Be("testStore");
            envelope.States.Count.Should().Be(1);
            var state = envelope.States[0];

            state.Key.Should().Be("test");
            state.Metadata.Count.Should().Be(2);
            state.Metadata.Keys.Contains("key1").Should().BeTrue();
            state.Metadata.Keys.Contains("key2").Should().BeTrue();
            state.Metadata["key1"].Should().Be("value1");
            state.Metadata["key2"].Should().Be("value2");
            state.Options.Concurrency.Should().Be(expectedConcurrency);
            state.Options.Consistency.Should().Be(expectedConsistency);

            var stateJson        = state.Value.ToStringUtf8();
            var stateFromRequest = JsonSerializer.Deserialize <Widget>(stateJson, client.InnerClient.JsonSerializerOptions);

            stateFromRequest.Size.Should().Be(widget.Size);
            stateFromRequest.Color.Should().Be(widget.Color);
        }
コード例 #12
0
 public OrderDto(string id, string number, StateOptions state, string comment, IEnumerable <OrderDetailDto> details, UserDto customer, DateTime createdAt, DeliveryPlaceOptions deliveryPlace,
                 DateTime?deliveryPredicateAt, DateTime?deliveryAt)
 {
     Id                  = id;
     Number              = number;
     State               = state;
     Comment             = comment;
     Details             = details;
     Customer            = customer;
     CreatedAt           = createdAt;
     DeliveryPlace       = deliveryPlace;
     DeliveryPredicateAt = deliveryPredicateAt;
     DeliveryAt          = deliveryAt;
 }
コード例 #13
0
        public async Task <ActionResult <string> > Create(
            ArticleItem item,
            [FromServices] DaprClient daprClient)
        {
            // unique key for vote like (article and liker)
            string key = item.articleid;

            Console.WriteLine("Enter Create for article {0}", key);

            ArticleItem newItem = item;
            var         options = new StateOptions()
            {
                Concurrency = ConcurrencyMode.FirstWrite
            };
            await daprClient.SaveStateAsync(StoreName, key, newItem, options);

            return(string.Format("Article created key: {0}", key));
        }
コード例 #14
0
ファイル: StateApiTest.cs プロジェクト: czmirek/dotnet-sdk
        public async Task TryDeleteStateAsync_ValidateOptions(
            ConsistencyMode consistencyMode,
            ConcurrencyMode concurrencyMode,
            RetryMode retryMode,
            StateConsistency expectedConsistency,
            StateConcurrency expectedConcurrency,
            RetryPattern expectedRetryMode)
        {
            // Configure Client
            var httpClient = new TestHttpClient();
            var daprClient = new DaprClientBuilder()
                             .UseGrpcChannelOptions(new GrpcChannelOptions {
                HttpClient = httpClient
            })
                             .Build();

            var stateOptions = new StateOptions
            {
                Concurrency  = concurrencyMode,
                Consistency  = consistencyMode,
                RetryOptions = new RetryOptions
                {
                    RetryInterval  = TimeSpan.FromSeconds(5),
                    RetryMode      = retryMode,
                    RetryThreshold = 10
                }
            };

            var task = daprClient.TryDeleteStateAsync("testStore", "test", "Test_Etag", stateOptions);

            // Get Request and validate
            httpClient.Requests.TryDequeue(out var entry).Should().BeTrue();
            var request = await GrpcUtils.GetRequestFromRequestMessageAsync <Autogenerated.DeleteStateRequest>(entry.Request);

            request.StoreName.Should().Be("testStore");
            request.Key.Should().Be("test");
            request.Etag.Should().Be("Test_Etag");
            request.Options.Concurrency.Should().Be(expectedConcurrency);
            request.Options.Consistency.Should().Be(expectedConsistency);
            request.Options.RetryPolicy.Pattern.Should().Be(expectedRetryMode);
            request.Options.RetryPolicy.Threshold.Should().Be(10);
            request.Options.RetryPolicy.Interval.Seconds.Should().Be(5);
        }
コード例 #15
0
        public async Task <ActionResult> LikeProcess(VoteItem vote, [FromServices] DaprClient daprClient)
        {
            Console.WriteLine("Enter LikeProcess for article {0}", vote.articleid);

            // getting etag to avoid concurrent writes (https://github.com/dapr/dotnet-sdk/pull/498/files)
            var(state, etag) = await daprClient.GetStateAndETagAsync <ArticleItem>(StoreName, vote.articleid);

            state ??= new ArticleItem()
            {
                articleid = vote.articleid, voteCount = 0
            };

            state.voteCount++;
            Console.WriteLine("Article {0} voteCount increased to: {1} etag {2}", vote.articleid, state.voteCount, etag);

            try {
                var options = new StateOptions()
                {
                    Concurrency = ConcurrencyMode.FirstWrite
                };
                bool isSaveStateSuccess = await daprClient.TrySaveStateAsync <ArticleItem>(StoreName, vote.articleid, state, etag);

                if (isSaveStateSuccess)
                {
                    Console.WriteLine("Article {0} voteCount saved.", vote.articleid);
                }
                else
                {
                    Console.WriteLine("Article {0} voteCount NOT saved, error eTag {1}.", vote.articleid, isSaveStateSuccess);
                    throw new Exception("Wrong eTag - version has changed !");
                    // TODO: retry to get etag and update it again
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Article {0} voteCount ERROR {1}.", vote.articleid, ex.Message);
                return(BadRequest());
            }

            Console.WriteLine("Finished LikeProcess for article {0}", vote.articleid);
            return(Ok());
        }
コード例 #16
0
ファイル: StateOptions.cs プロジェクト: jujianfei/AC
        /// <summary>
        /// 获取通道服务运行状态的文字说明。
        /// </summary>
        /// <param name="channelServiceState">通道服务状态</param>
        /// <returns>通道服务运行状态的文字说明</returns>
        public static string GetDescription(this StateOptions channelServiceState)
        {
            switch (channelServiceState)
            {
            case StateOptions.Free:
                return("空闲");

            case StateOptions.Running:
                return("运行");

            case StateOptions.Stopped:
                return("停止");

            case StateOptions.Exception:
                return("故障");

            default:
                throw new NotImplementedException("尚未实现该枚举。");
            }
        }
コード例 #17
0
 public OrderCreateModel(string id, string number, UserListModel customer,
                         DateTime createdDate, IEnumerable <OrderDetailModel> details,
                         DeliveryPlaceOptions deliveryPlace, DateTime?deliveryPredicateAt,
                         DateTime?deliveryAt, StateOptions state, string comment) : this()
 {
     Id                  = id;
     Number              = number;
     Customer            = customer;
     CreatedDate         = createdDate.ToRelativeDate();
     Details             = details;
     DeliveryPlaceId     = ((int)deliveryPlace).ToString();
     DeliveryPredicateAt = deliveryPredicateAt;
     DeliveryAt          = deliveryAt;
     StateId             = ((int)state).ToString();
     Comment             = comment;
     foreach (DeliveryPlaceOptions options in Enum.GetValues(typeof(DeliveryPlaceOptions)))
     {
         Places.Add
         (
             new EnumModel
             (
                 ((int)options).ToString(), options.ToString().Replace("_", " ")
             )
         );
     }
     foreach (StateOptions options in Enum.GetValues(typeof(StateOptions)))
     {
         States.Add
         (
             new EnumModel
             (
                 ((int)options).ToString(), options.ToString().Replace("_", " ")
             )
         );
     }
 }
コード例 #18
0
        public async Task ExecuteStateTransactionAsync_CanSaveState()
        {
            // Configure Client
            var httpClient = new TestHttpClient();
            var daprClient = new DaprClientBuilder()
                             .UseGrpcChannelOptions(new GrpcChannelOptions {
                HttpClient = httpClient
            })
                             .Build();

            var stateValue1 = new Widget()
            {
                Size = "small", Color = "yellow",
            };
            var metadata1 = new Dictionary <string, string>()
            {
                { "a", "b" }
            };
            var options1 = new StateOptions();

            options1.Concurrency = ConcurrencyMode.LastWrite;

            var state1      = new StateTransactionRequest("stateKey1", JsonSerializer.SerializeToUtf8Bytes(stateValue1), StateOperationType.Upsert, "testEtag", metadata1, options1);
            var stateValue2 = 100;
            var state2      = new StateTransactionRequest("stateKey2", JsonSerializer.SerializeToUtf8Bytes(stateValue2), StateOperationType.Delete);

            var stateValue3 = "teststring";
            var state3      = new StateTransactionRequest("stateKey3", JsonSerializer.SerializeToUtf8Bytes(stateValue3), StateOperationType.Upsert);

            var states = new List <StateTransactionRequest>();

            states.Add(state1);
            states.Add(state2);
            states.Add(state3);

            var task = daprClient.ExecuteStateTransactionAsync("testStore", states);

            // Get Request and validate
            httpClient.Requests.TryDequeue(out var entry).Should().BeTrue();
            var transactionRequest = await GrpcUtils.GetRequestFromRequestMessageAsync <Autogenerated.ExecuteStateTransactionRequest>(entry.Request);

            transactionRequest.StoreName.Should().Be("testStore");
            transactionRequest.Operations.Count.Should().Be(3);

            var req1 = transactionRequest.Operations[0];

            req1.Request.Key.Should().Be("stateKey1");
            req1.OperationType.Should().Be(StateOperationType.Upsert.ToString().ToLower());
            var valueJson1 = req1.Request.Value.ToStringUtf8();
            var value1     = JsonSerializer.Deserialize <Widget>(valueJson1);

            value1.Size.Should().Be(stateValue1.Size);
            value1.Color.Should().Be(stateValue1.Color);
            req1.Request.Etag.Should().Be("testEtag");
            req1.Request.Metadata.Count.Should().Be(1);
            req1.Request.Metadata["a"].Should().Be("b");
            req1.Request.Options.Concurrency.Should().Be(2);

            var req2 = transactionRequest.Operations[1];

            req2.Request.Key.Should().Be("stateKey2");
            req2.OperationType.Should().Be(StateOperationType.Delete.ToString().ToLower());
            var valueJson2 = req2.Request.Value.ToStringUtf8();
            var value2     = JsonSerializer.Deserialize <int>(valueJson2);

            value2.Should().Be(100);

            var req3 = transactionRequest.Operations[2];

            req3.Request.Key.Should().Be("stateKey3");
            req3.OperationType.Should().Be(StateOperationType.Upsert.ToString().ToLower());
            var valueJson3 = req3.Request.Value.ToStringUtf8();
            var value3     = JsonSerializer.Deserialize <string>(valueJson3);

            value3.Should().Be("teststring");
        }
コード例 #19
0
        public async Task ExecuteStateTransactionAsync_CanSaveState()
        {
            await using var client = TestClient.CreateForDaprClient();

            var stateValue1 = new Widget() { Size = "small", Color = "yellow", };
            var metadata1 = new Dictionary<string, string>()
            {
                {"a", "b" }
            };
            var options1 = new StateOptions
            {
                Concurrency = ConcurrencyMode.LastWrite
            };

            var state1 = new StateTransactionRequest("stateKey1", JsonSerializer.SerializeToUtf8Bytes(stateValue1), StateOperationType.Upsert, "testEtag", metadata1, options1);
            var stateValue2 = 100;
            var state2 = new StateTransactionRequest("stateKey2", JsonSerializer.SerializeToUtf8Bytes(stateValue2), StateOperationType.Delete);

            var stateValue3 = "teststring";
            var state3 = new StateTransactionRequest("stateKey3", JsonSerializer.SerializeToUtf8Bytes(stateValue3), StateOperationType.Upsert);

            var states = new List<StateTransactionRequest>
            {
                state1,
                state2,
                state3
            };

            var request = await client.CaptureGrpcRequestAsync(async daprClient =>
            {
                await daprClient.ExecuteStateTransactionAsync("testStore", states);
            });

            request.Dismiss();

            // Get Request and validate
            var envelope = await request.GetRequestEnvelopeAsync<Autogenerated.ExecuteStateTransactionRequest>();

            envelope.StoreName.Should().Be("testStore");
            envelope.Operations.Count.Should().Be(3);

            var req1 = envelope.Operations[0];
            req1.Request.Key.Should().Be("stateKey1");
            req1.OperationType.Should().Be(StateOperationType.Upsert.ToString().ToLower());
            var valueJson1 = req1.Request.Value.ToStringUtf8();
            var value1 = JsonSerializer.Deserialize<Widget>(valueJson1, client.InnerClient.JsonSerializerOptions);
            value1.Size.Should().Be(stateValue1.Size);
            value1.Color.Should().Be(stateValue1.Color);
            req1.Request.Etag.Value.Should().Be("testEtag");
            req1.Request.Metadata.Count.Should().Be(1);
            req1.Request.Metadata["a"].Should().Be("b");
            req1.Request.Options.Concurrency.Should().Be(2);

            var req2 = envelope.Operations[1];
            req2.Request.Key.Should().Be("stateKey2");
            req2.OperationType.Should().Be(StateOperationType.Delete.ToString().ToLower());
            var valueJson2 = req2.Request.Value.ToStringUtf8();
            var value2 = JsonSerializer.Deserialize<int>(valueJson2, client.InnerClient.JsonSerializerOptions);
            value2.Should().Be(100);

            var req3 = envelope.Operations[2];
            req3.Request.Key.Should().Be("stateKey3");
            req3.OperationType.Should().Be(StateOperationType.Upsert.ToString().ToLower());
            var valueJson3 = req3.Request.Value.ToStringUtf8();
            var value3 = JsonSerializer.Deserialize<string>(valueJson3, client.InnerClient.JsonSerializerOptions);
            value3.Should().Be("teststring");
        }
コード例 #20
0
        /// <summary>
        /// Initializes a new instance of the <see cref="StateTransactionRequest"/> class.
        /// </summary>
        /// <param name="key">The state key.</param>
        /// <param name="value">The serialized state value.</param>
        /// <param name="operationType">The operation type.</param>
        /// <param name="etag">The etag (optional).</param>
        /// <param name="metadata">Additional key value pairs for the state (optional).</param>
        /// <param name="options">State options (optional).</param>
        public StateTransactionRequest(string key, byte[] value, StateOperationType operationType, string etag = default, IReadOnlyDictionary <string, string> metadata = default, StateOptions options = default)
        {
            ArgumentVerifier.ThrowIfNull(key, nameof(key));

            this.Key           = key;
            this.Value         = value;
            this.OperationType = operationType;
            this.ETag          = etag;
            this.Metadata      = metadata;
            this.Options       = options;
        }
コード例 #21
0
 /// <summary>
 /// Deletes the entry associated with <see cref="Key" /> in the state store.
 /// </summary>
 /// <param name="stateOptions">A <see cref="StateOptions"/> object.</param>
 /// <param name="metadata">An key/value pair that may be consumed by the state store.  This depends on the state store used.</param>
 /// <param name="cancellationToken">A <see cref="CancellationToken" /> that can be used to cancel the operation.</param>
 /// <returns>A <see cref="Task" /> that will complete when the operation has completed.</returns>
 public Task DeleteAsync(StateOptions stateOptions = default, Dictionary <string, string> metadata = default, CancellationToken cancellationToken = default)
 {
     // ETag is intentionally not specified
     return(this.client.DeleteStateAsync(this.StoreName, this.Key, stateOptions, metadata, cancellationToken));
 }
コード例 #22
0
        public ArgsObject(List <string> args)
        {
            if (args.Count >= 4)
            {
                switch (args[0].ToUpper().Trim())
                {
                case "HIDE": State = StateOptions.Hide; args.RemoveAt(0); break;

                case "SHOW": State = StateOptions.Show; args.RemoveAt(0); break;

                default: State = StateOptions.Minimize; break;
                }



                if (new string[] { "ADD", "UPDATE", "DELETE", "READ", "IMPORT" }.Contains(args[0].ToUpper().Trim()) == true)
                {
                    Command = args[0].ToUpper().Trim();
                }
                else
                {
                    Command = "ERROR";
                }


                if (args[1].ToUpper().Trim().StartsWith("HTTP") == true)
                {
                    URL = args[1].Trim();
                }


                if (args[2].Trim().Length >= 1)
                {
                    Table = args[2].Trim();
                }


                if (Command == "IMPORT")
                {
                    PrimaryKey = new ColumnName("ID", "", "ID");
                    Values.Add(new ColumnName(args[3].Trim(), ""));
                }
                else if (args[3].Contains("|") == true)
                {
                    string[] pair = args[3].Split('|');
                    if (pair[0].ToUpper() == "ID")
                    {
                        PrimaryKey = new ColumnName(pair[0].Trim(), pair[1], "ID");
                    }
                    else
                    {
                        PrimaryKey = new ColumnName(pair[0].Trim(), pair[1]);
                    }
                }
                else if (args[3].Trim().Length >= 1)
                {
                    if (args[3].ToUpper() == "ID")
                    {
                        PrimaryKey = new ColumnName(args[3].Trim(), "", "ID");
                    }
                    else
                    {
                        PrimaryKey = new ColumnName(args[3].Trim(), "");
                    }
                }


                if (args.Count > 4)
                {
                    for (int i = 4; i < args.Count; i++)
                    {
                        if (args[i].Contains("|") == true)
                        {
                            string[] pair = args[i].Split('|');
                            if (pair[0].ToUpper() == "ID")
                            {
                                Values.Add(new ColumnName(pair[0].Trim(), pair[1].Trim(), "ID"));
                            }
                            else
                            {
                                Values.Add(new ColumnName(pair[0].Trim(), pair[1].Trim()));
                            }
                        }
                        else if (args[i].Trim().Length >= 1)
                        {
                            if (args[i].ToUpper() == "ID")
                            {
                                Values.Add(new ColumnName(args[i].Trim(), "", "ID"));
                            }
                            else
                            {
                                Values.Add(new ColumnName(args[i].Trim(), ""));
                            }
                        }
                    }
                }


                if (Command == "READ" || Command == "IMPORT")
                {
                    if (Values.Last().Value != "")
                    {
                        FilePath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location).Trim('\\') + "\\Default.CSV";
                    }
                    else
                    {
                        if (System.IO.Path.IsPathRooted(Values.Last().Name) == true)
                        {
                            FilePath = Values.Last().Name;
                        }
                        else
                        {
                            FilePath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location).Trim('\\') + "\\" + Values.Last().Name;
                        }
                        if (Values.Count >= 1)
                        {
                            Values.RemoveAt(Values.Count - 1);
                        }
                    }
                }
                else
                {
                    FilePath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location).Trim('\\') + "\\Default.CSV";
                }
            }
        }
コード例 #23
0
ファイル: StateApiTest.cs プロジェクト: czmirek/dotnet-sdk
        public async Task SaveStateAsync_ValidateOptions(
            ConsistencyMode consistencyMode,
            ConcurrencyMode concurrencyMode,
            RetryMode retryMode,
            StateConsistency expectedConsistency,
            StateConcurrency expectedConcurrency,
            RetryPattern expectedRetryMode)
        {
            // Configure Client
            var httpClient = new TestHttpClient();
            var daprClient = new DaprClientBuilder()
                             .UseGrpcChannelOptions(new GrpcChannelOptions {
                HttpClient = httpClient
            })
                             .Build();

            var widget = new Widget()
            {
                Size = "small", Color = "yellow",
            };
            var stateOptions = new StateOptions
            {
                Concurrency  = concurrencyMode,
                Consistency  = consistencyMode,
                RetryOptions = new RetryOptions
                {
                    RetryInterval  = TimeSpan.FromSeconds(5),
                    RetryMode      = retryMode,
                    RetryThreshold = 10
                }
            };

            var metadata = new Dictionary <string, string>();

            metadata.Add("key1", "value1");
            metadata.Add("key2", "value2");
            var task = daprClient.SaveStateAsync("testStore", "test", widget, stateOptions, metadata);

            // Get Request and validate
            httpClient.Requests.TryDequeue(out var entry).Should().BeTrue();
            var request = await GrpcUtils.GetRequestFromRequestMessageAsync <Autogenerated.SaveStateRequest>(entry.Request);

            request.StoreName.Should().Be("testStore");
            request.States.Count.Should().Be(1);
            var state = request.States[0];

            state.Key.Should().Be("test");
            state.Metadata.Count.Should().Be(2);
            state.Metadata.Keys.Contains("key1").Should().BeTrue();
            state.Metadata.Keys.Contains("key2").Should().BeTrue();
            state.Metadata["key1"].Should().Be("value1");
            state.Metadata["key2"].Should().Be("value2");
            state.Options.Concurrency.Should().Be(expectedConcurrency);
            state.Options.Consistency.Should().Be(expectedConsistency);
            state.Options.RetryPolicy.Pattern.Should().Be(expectedRetryMode);
            state.Options.RetryPolicy.Threshold.Should().Be(10);
            state.Options.RetryPolicy.Interval.Seconds.Should().Be(5);

            var stateJson        = state.Value.ToStringUtf8();
            var stateFromRequest = JsonSerializer.Deserialize <Widget>(stateJson);

            stateFromRequest.Size.Should().Be(widget.Size);
            stateFromRequest.Color.Should().Be(widget.Color);
        }
コード例 #24
0
ファイル: FirstLinkGetter.cs プロジェクト: kpeckham/wikipedia
    public static void Main()
    {
        string home = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        string path = home + "/firstLinks2.txt";

        using (StreamWriter streamWriter = new StreamWriter(path)) {
            using (StreamReader streamReader = new StreamReader("/mnt/volume-nyc3-04/enwiki-20180401-pages-articles.xml")) {
                StateOptions state      = StateOptions.NEXTPAGE;
                Regex        spaceRegex = new Regex(" +", RegexOptions.Compiled);

                string line = "";
                string id   = "";
                string link = null;

                bool isRedirect        = false;
                bool skipLinkRemainder = false;

                int curlyLevel    = 0;
                int squareLevel   = 0;
                int parenLevel    = 0;
                int progressCheck = 0;

                // checking for ENDPAGE in while because it's easier than figuring out how to backtrack a line
                // once we figured out that we'd hit the end of a page.
                // this way we can just switch to the end page state without reading in a new line.
                while (state == StateOptions.ENDPAGE || (line = streamReader.ReadLine()) != null)
                {
                    switch (state)
                    {
                    case StateOptions.NEXTPAGE:

                        if (line == "  <page>")
                        {
                            id   = "";
                            link = null;

                            isRedirect        = false;
                            skipLinkRemainder = false;

                            curlyLevel  = 0;
                            squareLevel = 0;
                            parenLevel  = 0;

                            state = StateOptions.METADATA;
                        }
                        break;

                    case StateOptions.METADATA:
                        if (line.StartsWith("    <ns>", StringComparison.CurrentCulture))
                        {
                            if (line != "    <ns>0</ns>")
                            {
                                state = StateOptions.NEXTPAGE;
                            }
                        }

                        //id line format: "    <id>10</id>"
                        else if (line.StartsWith("    <id>", StringComparison.CurrentCulture))
                        {
                            id = line.Substring(8, line.Length - 8 - 5);
                            if (Convert.ToInt32(id) > progressCheck)
                            {
                                Console.WriteLine("Progress: {0}", id);
                                progressCheck += 1000000;
                            }
                        }
                        //redirect line format: "    <redirect title="Computer accessibility" />"
                        else if (line.StartsWith("    <redirect title=\"", StringComparison.CurrentCulture))
                        {
                            isRedirect = true;
                            link       = line.Substring(21, line.Length - 21 - 4);
                            state      = StateOptions.ENDPAGE;
                        }
                        else if (line.StartsWith("      <format>", StringComparison.CurrentCulture))
                        {
                            //some types are text/css and text/javascript - we don't want to deal with those
                            if (line != "      <format>text/x-wiki</format>")
                            {
                                state = StateOptions.NEXTPAGE;
                            }
                            else
                            {
                                state = StateOptions.TEXT;
                            }
                        }

                        break;

                    case StateOptions.TEXT:

                        if (line == "  </page>")
                        {
                            state = StateOptions.ENDPAGE;
                        }
                        else
                        {
                            foreach (char item in line)
                            {
                                bool linkEndFlag = false;

                                switch (item)
                                {
                                case '[':
                                    squareLevel += 1;
                                    break;

                                case ']':
                                    linkEndFlag |= (squareLevel == 2 && curlyLevel == 0 && parenLevel == 0);
                                    squareLevel -= 1;
                                    break;

                                case '{':
                                    curlyLevel += 1;
                                    break;

                                case '}':
                                    curlyLevel -= 1;
                                    break;

                                case '(':
                                    if (squareLevel != 2)
                                    {
                                        parenLevel += 1;
                                    }
                                    break;

                                case ')':
                                    if (squareLevel != 2)
                                    {
                                        parenLevel -= 1;
                                    }
                                    break;
                                }



                                if (parenLevel == 0 && curlyLevel == 0 && squareLevel == 2 && item != '[' && !skipLinkRemainder)
                                {
                                    if (item == '|' || item == '#')
                                    {
                                        skipLinkRemainder = true;
                                    }

                                    else
                                    {
                                        if (link == null)
                                        {
                                            link = "";
                                        }
                                        link += item;
                                    }
                                }

                                if (linkEndFlag)
                                {
                                    skipLinkRemainder = false;
                                    if (link != null && (link.StartsWith("File:") || link.StartsWith("Image:")))
                                    {
                                        link = null;
                                    }
                                    else
                                    {
                                        state = StateOptions.ENDPAGE;
                                        break;
                                    }
                                }
                            }
                        }
                        break;

                    case StateOptions.ENDPAGE:
                        if (link == null)
                        {
                            link = "(no link found)";
                        }
                        if (!isRedirect && (squareLevel != 1 || curlyLevel != 0))
                        {
                            link = "(bad wikitext)";
                        }
                        //Canonicalization - https://en.wikipedia.org/wiki/Help:Link#Conversion_to_canonical_form

                        link = link.Replace(' ', '_');
                        link = link.Replace("\\", "\\\\");
                        link = link.Trim(new char[] { '_' });

                        if (link.Length > 0)
                        {
                            link = char.ToUpper(link[0]) + link.Substring(1);
                        }

                        link = spaceRegex.Replace(link, " ");
                        link = WebUtility.HtmlDecode(link);

                        streamWriter.WriteLine(id + "\t" + (isRedirect ? "1" : "0") + "\t" + link);
                        state = StateOptions.NEXTPAGE;
                        //check for broken wikitext and figure out array out of bounds exception

                        break;
                    }
                }
            }
        }
    }