protected virtual void EnsureNonContinuousFeedIsRequested(GetChangesRequest request)
 {
     if (request.Feed.HasValue && request.Feed == ChangesFeed.Continuous)
     {
         throw new ArgumentException(ExceptionStrings.GetChangesForNonContinuousFeedOnly, nameof(request));
     }
 }
        public virtual HttpRequest Create(GetChangesRequest request)
        {
            Ensure.Any.IsNotNull(request, nameof(request));

            return(new HttpRequest(HttpMethod.Get, GenerateRelativeUrl(request))
                   .SetRequestTypeHeader(request.GetType()));
        }
        private static async void MainSync2()
        {
            GetChangesRequest request = new GetChangesRequest();

            request.IncludeDocs = false;
            request.Since       = "0";
            request.Heartbeat   = 1000;
            request.Feed        = ChangesFeed.Continuous;
            request.Style       = ChangesStyle.AllDocs;

            DbConnectionInfo connectionInfo = new DbConnectionInfo("http://localhost:4984", "couchbasegames");
            IDbConnection    db             = new DbConnection(connectionInfo);


            SerializationConfiguration serializationConfiguration = new SerializationConfiguration(new DefaultContractResolver());

            ISerializer serializer = new DefaultSerializer(serializationConfiguration, new DocumentSerializationMetaProvider(), null);

            Changes changes = new Changes(db, serializer);


            IObservable <string> observable = changes.ObserveContinuous(request, new CancellationToken(false));

            observable.Subscribe(new MyObserver()
            {
                InterceptOnNext = v =>
                {
                    // Console.WriteLine(" no conflict detected  Id :  change  {0}",  v);
                    if (!string.IsNullOrEmpty(v))
                    {
                        RootObject c = JsonConvert.DeserializeObject <RootObject>(v);

                        if (c.changes.Count > 1)
                        {
                            if (conflicts.ContainsKey(c.id))
                            {
                                conflicts[c.id] = c.changes;
                            }
                            else
                            {
                                conflicts.Add(c.id, c.changes);
                            }

                            c.changes.ForEach(x => { Console.WriteLine(" conflict detected : id {0} and changes {1}", c.id, x.rev); });
                        }
                        else
                        {
                            Console.WriteLine(" no conflict detected  Id : {0},  change  {1}", c.id, v);
                        }
                    }
                },

                InterceptOnError = e => {
                    Console.WriteLine(e.Message);
                }
            });

            SpinWait.SpinUntil(() => Console.ReadLine() == "C");
        }
Esempio n. 4
0
        public override HttpRequest Create(GetChangesRequest request)
        {
            Ensure.Any.IsNotNull(request, nameof(request));

            EnsureContinuousFeedIsRequested(request);

            return(base.Create(request));
        }
        public virtual async Task <ChangesResponse> GetAsync(GetChangesRequest request)
        {
            var httpRequest = HttpRequestFactory.Create(request);

            using (var httpResponse = await SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead).ForAwait())
            {
                return(await ChangesResponseFactory.CreateAsync(httpResponse).ForAwait());
            }
        }
        /// <summary>
        /// Returns all configured options of <see cref="GetChangesRequest"/> as key values.
        /// The values are formatted to JSON-compatible strings.
        /// </summary>
        /// <returns></returns>
        protected virtual IDictionary <string, string> ConvertRequestToJsonCompatibleKeyValues(GetChangesRequest request)
        {
            var kvs = new Dictionary <string, string>();

            if (request.Feed.HasValue)
            {
                kvs.Add(KeyNames.Feed, request.Feed.Value.AsString());
            }

            if (!string.IsNullOrWhiteSpace(request.Since))
            {
                kvs.Add(KeyNames.Since, request.Since);
            }

            if (request.IncludeDocs.HasValue)
            {
                kvs.Add(KeyNames.IncludeDocs, request.IncludeDocs.Value.ToString().ToLower());
            }

            if (request.Descending.HasValue)
            {
                kvs.Add(KeyNames.Descending, request.Descending.Value.ToString().ToLower());
            }

            if (request.Limit.HasValue)
            {
                kvs.Add(KeyNames.Limit, request.Limit.Value.ToString(MyCouchRuntime.FormatingCulture.NumberFormat));
            }

            if (request.Heartbeat.HasValue)
            {
                kvs.Add(KeyNames.HeartBeat, request.Heartbeat.Value.ToString(MyCouchRuntime.FormatingCulture.NumberFormat));
            }

            if (request.Timeout.HasValue)
            {
                kvs.Add(KeyNames.Timeout, request.Timeout.Value.ToString(MyCouchRuntime.FormatingCulture.NumberFormat));
            }

            if (request.Filter != null)
            {
                kvs.Add(KeyNames.Filter, request.Filter);
            }

            if (request.Style.HasValue)
            {
                kvs.Add(KeyNames.Style, request.Style.Value.AsString());
            }

            return(kvs);
        }
 protected virtual string GenerateQueryStringParams(GetChangesRequest request)
 {
     return(string.Join("&", ConvertRequestToJsonCompatibleKeyValues(request)
                        .Select(kv => string.Format("{0}={1}", kv.Key, UrlParam.Encode(kv.Value)))));
 }
        protected virtual string GenerateQueryString(GetChangesRequest request)
        {
            var p = GenerateQueryStringParams(request);

            return(string.IsNullOrEmpty(p) ? string.Empty : string.Concat("?", p));
        }
 protected virtual string GenerateRelativeUrl(GetChangesRequest request)
 {
     return(string.Format("/_changes{0}",
                          GenerateQueryString(request)));
 }