// Processes a change in the subscribed database
        private void DatabaseChanged(object sender, DatabaseChangeEventArgs args)
        {
            foreach (var change in args.Changes)
            {
                var rev        = change.AddedRevision;
                var winningRev = change.WinningRevision;

                if (!ChangesIncludeConflicts)
                {
                    if (winningRev == null)
                    {
                        continue; // this change doesn't affect the winning rev ID, no need to send it
                    }

                    if (rev.Equals(winningRev))
                    {
                        // This rev made a _different_ rev current, so substitute that one.
                        // We need to emit the current sequence # in the feed, so put it in the rev.
                        // This isn't correct internally (this is an old rev so it has an older sequence)
                        // but consumers of the _changes feed don't care about the internal state.
                        if (ChangesIncludeDocs)
                        {
                            _db.LoadRevisionBody(rev, DocumentContentOptions.None);
                        }
                    }
                }

                if (!_db.RunFilter(ChangesFilter, null, rev))
                {
                    continue;
                }

                if (ChangesFeedMode == ChangesFeedMode.LongPoll)
                {
                    _changes.Add(rev);
                }
                else
                {
                    Log.D(TAG, "Sending continuous change chunk");
                    var written = Response.SendContinuousLine(DatabaseMethods.ChangesDictForRev(rev, this), ChangesFeedMode);
                    if (!written)
                    {
                        Terminate();
                    }
                }
            }

            if (ChangesFeedMode == ChangesFeedMode.LongPoll && _changes.Count > 0)
            {
                var body = new Body(DatabaseMethods.ResponseBodyForChanges(_changes, 0, this));
                Response.WriteData(body.AsJson(), true);
                CouchbaseLiteRouter.ResponseFinished(this);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Create a response body for an HTTP response from a given list of DB changes (no conflicts)
        /// </summary>
        /// <returns>The response body</returns>
        /// <param name="changes">The list of changes to be processed</param>
        /// <param name="since">The first change ID to be processed</param>
        /// <param name="responseState">The current response state</param>
        public static IDictionary <string, object> ResponseBodyForChanges(RevisionList changes, long since, DBMonitorCouchbaseResponseState responseState)
        {
            List <IDictionary <string, object> > results = new List <IDictionary <string, object> >();

            foreach (var change in changes)
            {
                results.Add(DatabaseMethods.ChangesDictForRev(change, responseState));
            }

            if (changes.Count > 0)
            {
                since = changes.Last().GetSequence();
            }

            return(new Dictionary <string, object> {
                { "results", results },
                { "last_seq", since }
            });
        }
 private void WriteChanges(RevisionList changes)
 {
     if (changes.Count > 0)
     {
         if (ChangesFeedMode == ChangesFeedMode.LongPoll)
         {
             var body = new Body(DatabaseMethods.ResponseBodyForChanges(changes, 0, this));
             Response.WriteData(body.AsJson(), true);
             Terminate();
         }
         else
         {
             foreach (var rev in changes)
             {
                 var written = Response.SendContinuousLine(DatabaseMethods.ChangesDictForRev(rev, this), ChangesFeedMode);
                 if (!written)
                 {
                     Terminate();
                     break;
                 }
             }
         }
     }
 }