protected async virtual void OnSuccessfulResponse <T>(ChangesResponse <T> response, HttpResponseMessage httpResponse)
 {
     using (var content = await httpResponse.Content.ReadAsStreamAsync().ForAwait())
     {
         Serializer.Populate(response, content);
     }
 }
        private void HandleRelativeChangesOfCollection(ModelAttributesInfo modelAttributesInfo,
                                                       KeyValuePair <Type, string> property, IGrouping <string, ChangeResponse> collectionChanges,
                                                       List <IPrefilterBase> prefilters, List <Subscription> equalCollectionSubscriptions)
        {
            List <ChangeResponse> completeChanges = CollectionChangeHelper.CalculateRelativeChanges(prefilters,
                                                                                                    collectionChanges.ToList(), property);

            Parallel.ForEach(equalCollectionSubscriptions, subscription =>
            {
                Task.Run(() =>
                {
                    List <ChangeResponse> connectionChanges =
                        CollectionChangeHelper.CalculateRelativeAuthenticatedChanges(modelAttributesInfo,
                                                                                     completeChanges,
                                                                                     property, subscription.Connection.Information, serviceProvider);

                    ChangesResponse changesResponse = new ChangesResponse()
                    {
                        ReferenceId = subscription.ReferenceId,
                        Changes     = connectionChanges.Select(change =>
                        {
                            object value =
                                change.Value.GetAuthenticatedQueryModel(subscription.Connection.Information,
                                                                        serviceProvider);
                            return(change.CreateResponse(subscription.ReferenceId, value));
                        }).ToList()
                    };

                    if (changesResponse.Changes.Any())
                    {
                        _ = subscription.Connection.Send(changesResponse);
                    }
                });
            });
        }
示例#3
0
 public virtual async void Materialize <T>(ChangesResponse <T> response, HttpResponseMessage httpResponse)
 {
     using (var content = await httpResponse.Content.ReadAsStreamAsync().ForAwait())
     {
         Serializer.Populate(response, content);
     }
 }
示例#4
0
        protected virtual void VerifyChanges <T>(ChangesResponse <T> previous, ChangesResponse <T> current, string expectedId, string expectedRev, bool shouldBeDeleted)
        {
            current.Should().BeSuccessfulGet();

            var lastChange = current.Results.Select(c => new { NumericSeq = GetSeqNumber(c.Seq), Change = c }).OrderBy(i => i.NumericSeq).Last();

            lastChange.Change.Id.Should().Be(expectedId);
            lastChange.Change.Changes.Single().Rev.Should().Be(expectedRev);
            lastChange.Change.Deleted.Should().Be(shouldBeDeleted);
        }
示例#5
0
        private void AddDeletedDoc(BatchSet changeSet, ChangesResponse <string> .Row row, SyncLogItem logItem)
        {
            if (changeSet.DeletedDocuments == null)
            {
                changeSet.DeletedDocuments = new List <DeletedDocument>();
            }
            DeletedDocument delObj = new DeletedDocument()
            {
                Key = row.Id, Version = row.Changes[0].Rev
            };

            //check uploaded anchor- means cliet just uploaded this record and we should not return back
            if (logItem != null && logItem.KeyVersion != null && logItem.KeyVersion.ContainsKey(delObj.Key) && logItem.KeyVersion[delObj.Key] == delObj.Version)
            {
                return;
            }
            changeSet.DeletedDocuments.Add(delObj);
        }
示例#6
0
        private void AddChangedDoc(BatchSet changeSet, ChangesResponse <string> .Row row, SyncLogItem logItem, Filter query, MyCouch.Serialization.ISerializer serializer)
        {
            if (changeSet.ChangedDocuments == null)
            {
                changeSet.ChangedDocuments = new List <SiaqodbDocument>();
            }
            CouchDBDocument co = serializer.Deserialize <CouchDBDocument>(row.IncludedDoc);

            if (co._id.StartsWith("_design/"))
            {
                return;
            }
            //check uploaded anchor- means cliet just uploaded this record and we should not return back
            if (logItem != null && logItem.KeyVersion != null && logItem.KeyVersion.ContainsKey(co._id) && logItem.KeyVersion[co._id] == co._rev)
            {
                return;
            }
            if (OutOfFilter(query, co))
            {
                return;
            }
            changeSet.ChangedDocuments.Add(Mapper.ToSiaqodbDocument(co));
        }
示例#7
0
 public static ChangesResponseAssertions <T> Should <T>(this ChangesResponse <T> response)
 {
     return(new ChangesResponseAssertions <T>(response));
 }
示例#8
0
 public ChangesResponseAssertions(ChangesResponse <T> response)
 {
     Response = response;
 }
示例#9
0
        public static String GetChangedObjectsTextInfo(this MassResponse_Changes changes)
        {
            ChangesResponse x = changes.Meta.Response;

            return(String.Format("{0,8} records (from {1} till {2})", x.ObtainedRecords, x.FirstRecordTime, x.LastRecordTime));
        }
示例#10
0
 protected virtual void MaterializeFailedResponse <T>(ChangesResponse <T> response, HttpResponseMessage httpResponse)
 {
     FailedResponseMaterializer.Materialize(response, httpResponse);
 }
示例#11
0
 protected virtual void MaterializeSuccessfulResponse <T>(ChangesResponse <T> response, HttpResponseMessage httpResponse)
 {
     SuccessfulResponseMaterializer.Materialize(response, httpResponse);
 }
示例#12
0
 private void AddDeletedDoc(BatchSet changeSet, ChangesResponse<string>.Row row, SyncLogItem logItem)
 {
     if (changeSet.DeletedDocuments == null)
         changeSet.DeletedDocuments = new List<DeletedDocument>();
     DeletedDocument delObj = new DeletedDocument() { Key = row.Id, Version = row.Changes[0].Rev };
     //check uploaded anchor- means cliet just uploaded this record and we should not return back
     if (logItem != null && logItem.KeyVersion != null && logItem.KeyVersion.ContainsKey(delObj.Key) && logItem.KeyVersion[delObj.Key] == delObj.Version)
         return;
     changeSet.DeletedDocuments.Add(delObj);
 }
示例#13
0
 private void AddChangedDoc(BatchSet changeSet, ChangesResponse<string>.Row row, SyncLogItem logItem, Filter query, MyCouch.Serialization.ISerializer serializer)
 {
     if (changeSet.ChangedDocuments == null)
         changeSet.ChangedDocuments = new List<SiaqodbDocument>();
     CouchDBDocument co = serializer.Deserialize<CouchDBDocument>(row.IncludedDoc);
     if (co._id.StartsWith("_design/"))
         return;
     //check uploaded anchor- means cliet just uploaded this record and we should not return back
     if (logItem != null && logItem.KeyVersion != null && logItem.KeyVersion.ContainsKey(co._id) && logItem.KeyVersion[co._id] == co._rev)
         return;
     if (OutOfFilter(query, co))
         return;
     changeSet.ChangedDocuments.Add(Mapper.ToSiaqodbDocument(co));
 }
        public void HandleSubscription(CollectionSubscription subscription, Type dbContextType,
                                       IServiceProvider requestServiceProvider, ConnectionBase connection, KeyValuePair <Type, string> property,
                                       List <ChangeResponse> collectionChanges, List <ChangeResponse> allChanges)
        {
            try
            {
                bool anyCollectionChanges = collectionChanges.Any();

                if ((anyCollectionChanges && subscription.Prefilters.Any(prefilter =>
                                                                         prefilter is IAfterQueryPrefilter || prefilter is TakePrefilter || prefilter is SkipPrefilter)) ||
                    HasIncludePrefilterWithChange(subscription, allChanges))
                {
                    SapphireDbContext db = dbContextAccessor.GetContext(dbContextType, requestServiceProvider);

                    IQueryable <object> collectionValues = db.GetCollectionValues(requestServiceProvider,
                                                                                  connection.Information, property, subscription.Prefilters);

                    IAfterQueryPrefilter afterQueryPrefilter =
                        subscription.Prefilters.OfType <IAfterQueryPrefilter>().FirstOrDefault();

                    if (afterQueryPrefilter != null)
                    {
                        afterQueryPrefilter.Initialize(property.Key);

                        _ = connection.Send(new QueryResponse()
                        {
                            ReferenceId = subscription.ReferenceId,
                            Result      = afterQueryPrefilter.Execute(collectionValues)
                        });
                    }
                    else
                    {
                        _ = connection.Send(new QueryResponse()
                        {
                            ReferenceId = subscription.ReferenceId,
                            Result      = collectionValues
                                          .AsEnumerable()
                                          .Select(v => v.GetAuthenticatedQueryModel(connection.Information, serviceProvider))
                                          .ToList()
                        });
                    }
                }
                else if (anyCollectionChanges)
                {
                    IEnumerable <WherePrefilter> wherePrefilters = subscription.Prefilters.OfType <WherePrefilter>();

                    List <ChangeResponse> oldValuesUnloadResponses = new List <ChangeResponse>();
                    List <ChangeResponse> newValuesLoadResponses   = new List <ChangeResponse>();

                    foreach (WherePrefilter wherePrefilter in wherePrefilters)
                    {
                        wherePrefilter.Initialize(property.Key);

                        // Values that did change know do match the
                        oldValuesUnloadResponses.AddRange(
                            collectionChanges
                            .Where(change => change.State == ChangeResponse.ChangeState.Modified &&
                                   !wherePrefilter.WhereExpressionCompiled(change.Value) &&
                                   wherePrefilter.WhereExpressionCompiled(change.OriginalValue))
                            .Select(change =>
                        {
                            ChangeResponse newChangeResponse = change.CreateResponse(null, change.Value);
                            newChangeResponse.State          = ChangeResponse.ChangeState.Deleted;
                            return(newChangeResponse);
                        })
                            );

                        newValuesLoadResponses.AddRange(
                            collectionChanges
                            .Where(change => change.State == ChangeResponse.ChangeState.Modified &&
                                   wherePrefilter.WhereExpressionCompiled(change.Value) &&
                                   !wherePrefilter.WhereExpressionCompiled(change.OriginalValue))
                            .Select(change =>
                        {
                            ChangeResponse newChangeResponse = change.CreateResponse(null, change.Value);
                            newChangeResponse.State          = ChangeResponse.ChangeState.Added;
                            return(newChangeResponse);
                        })
                            );

                        collectionChanges = collectionChanges
                                            .Where((change) => wherePrefilter.WhereExpressionCompiled(change.Value)).ToList();
                    }

                    IEnumerable <ChangeResponse> changesForWherePrefilter = oldValuesUnloadResponses
                                                                            .Concat(newValuesLoadResponses)
                                                                            .GroupBy(v => v.Value)
                                                                            .Select(g => g.LastOrDefault());

                    collectionChanges = collectionChanges.Concat(changesForWherePrefilter).ToList();

                    ChangesResponse changesResponse = new ChangesResponse()
                    {
                        ReferenceId = subscription.ReferenceId,
                        Changes     = collectionChanges.Select(change =>
                        {
                            object value =
                                change.Value.GetAuthenticatedQueryModel(connection.Information, requestServiceProvider);
                            return(change.CreateResponse(subscription.ReferenceId, value));
                        }).ToList()
                    };

                    if (changesResponse.Changes.Any())
                    {
                        _ = connection.Send(changesResponse);
                    }
                }
            }
            catch (Exception ex)
            {
                SubscribeCommand tempErrorCommand = new SubscribeCommand()
                {
                    CollectionName = subscription.CollectionName,
                    ReferenceId    = subscription.ReferenceId,
                    Prefilters     = subscription.Prefilters
                };

                _ = connection.Send(tempErrorCommand.CreateExceptionResponse <ResponseBase>(ex));
                logger.LogError(
                    $"Error handling subscription '{subscription.ReferenceId}' of {subscription.CollectionName}");
                logger.LogError(ex.Message);
            }
        }