Пример #1
0
        /// <exception cref="System.Exception"></exception>
        public virtual void TestCorrectWinningRevisionTiebreaker()
        {
            // Create a conflict on purpose
            Document      doc   = database.CreateDocument();
            SavedRevision rev1  = doc.CreateRevision().Save();
            SavedRevision rev2a = CreateRevisionWithRandomProps(rev1, false);
            SavedRevision rev2b = CreateRevisionWithRandomProps(rev1, true);
            // the tiebreaker will happen based on which rev hash has lexicographically higher sort order
            SavedRevision expectedWinner = null;

            if (Sharpen.Runtime.CompareOrdinal(rev2a.GetId(), rev2b.GetId()) > 0)
            {
                expectedWinner = rev2a;
            }
            else
            {
                if (Sharpen.Runtime.CompareOrdinal(rev2a.GetId(), rev2b.GetId()) < 0)
                {
                    expectedWinner = rev2b;
                }
            }
            RevisionInternal revFound = database.GetDocumentWithIDAndRev(doc.GetId(), null, EnumSet
                                                                         .NoneOf <Database.TDContentOptions>());

            NUnit.Framework.Assert.AreEqual(expectedWinner.GetId(), revFound.GetRevId());
        }
Пример #2
0
        public SavedRevision GetRevision(string id)
        {
            if (currentRevision != null && id.Equals(currentRevision.GetId()))
            {
                return(currentRevision);
            }
            EnumSet <Database.TDContentOptions> contentOptions = EnumSet.NoneOf <Database.TDContentOptions
                                                                                 >();
            RevisionInternal revisionInternal = database.GetDocumentWithIDAndRev(GetId(), id,
                                                                                 contentOptions);
            SavedRevision revision = null;

            revision = GetRevisionFromRev(revisionInternal);
            return(revision);
        }
 protected internal UnsavedRevision(Document document, SavedRevision parentRevision
     ) : base(document)
 {
     if (parentRevision == null)
     {
         parentRevID = null;
     }
     else
     {
         parentRevID = parentRevision.GetId();
     }
     IDictionary<string, object> parentRevisionProperties;
     if (parentRevision == null)
     {
         parentRevisionProperties = null;
     }
     else
     {
         parentRevisionProperties = parentRevision.GetProperties();
     }
     if (parentRevisionProperties == null)
     {
         properties = new Dictionary<string, object>();
         properties.Put("_id", document.GetId());
         if (parentRevID != null)
         {
             properties.Put("_rev", parentRevID);
         }
     }
     else
     {
         properties = new Dictionary<string, object>(parentRevisionProperties);
     }
 }
Пример #4
0
        /// <summary>https://github.com/couchbase/couchbase-lite-java-core/issues/164</summary>
        /// <exception cref="System.Exception"></exception>
        public virtual void TestRevisionIdEquivalentRevisions()
        {
            // two revisions with the same content and the same json
            // should have the exact same revision id, because their content
            // will have an identical hash
            IDictionary <string, object> properties = new Dictionary <string, object>();

            properties.Put("testName", "testCreateRevisions");
            properties.Put("tag", 1337);
            IDictionary <string, object> properties2 = new Dictionary <string, object>();

            properties2.Put("testName", "testCreateRevisions");
            properties2.Put("tag", 1338);
            Document        doc    = database.CreateDocument();
            UnsavedRevision newRev = doc.CreateRevision();

            newRev.SetUserProperties(properties);
            SavedRevision   rev1     = newRev.Save();
            UnsavedRevision newRev2a = rev1.CreateRevision();

            newRev2a.SetUserProperties(properties2);
            SavedRevision   rev2a    = newRev2a.Save();
            UnsavedRevision newRev2b = rev1.CreateRevision();

            newRev2b.SetUserProperties(properties2);
            SavedRevision rev2b = newRev2b.Save(true);

            NUnit.Framework.Assert.AreEqual(rev2a.GetId(), rev2b.GetId());
        }
        protected internal UnsavedRevision(Document document, SavedRevision parentRevision
                                           ) : base(document)
        {
            if (parentRevision == null)
            {
                parentRevID = null;
            }
            else
            {
                parentRevID = parentRevision.GetId();
            }
            IDictionary <string, object> parentRevisionProperties;

            if (parentRevision == null)
            {
                parentRevisionProperties = null;
            }
            else
            {
                parentRevisionProperties = parentRevision.GetProperties();
            }
            if (parentRevisionProperties == null)
            {
                properties = new Dictionary <string, object>();
                properties.Put("_id", document.GetId());
                if (parentRevID != null)
                {
                    properties.Put("_rev", parentRevID);
                }
            }
            else
            {
                properties = new Dictionary <string, object>(parentRevisionProperties);
            }
        }
Пример #6
0
        public string GetCurrentRevisionId()
        {
            SavedRevision rev = GetCurrentRevision();

            if (rev == null)
            {
                return(null);
            }
            return(rev.GetId());
        }
Пример #7
0
            public void Changed(Database.ChangeEvent @event)
            {
                NUnit.Framework.Assert.AreEqual(1, @event.GetChanges().Count);
                DocumentChange change = @event.GetChanges()[0];

                NUnit.Framework.Assert.AreEqual(DocumentId, change.GetDocumentId());
                NUnit.Framework.Assert.AreEqual(rev.GetRevId(), change.GetRevisionId());
                NUnit.Framework.Assert.IsTrue(change.IsCurrentRevision());
                NUnit.Framework.Assert.IsFalse(change.IsConflict());
                SavedRevision current = this._enclosing.database.GetDocument(change.GetDocumentId
                                                                                 ()).GetCurrentRevision();

                NUnit.Framework.Assert.AreEqual(rev.GetRevId(), current.GetId());
            }
Пример #8
0
        /// <exception cref="System.Exception"></exception>
        public virtual void TestCorrectWinningRevisionLongerBranch()
        {
            // Create a conflict on purpose
            Document      doc   = database.CreateDocument();
            SavedRevision rev1  = doc.CreateRevision().Save();
            SavedRevision rev2a = CreateRevisionWithRandomProps(rev1, false);
            SavedRevision rev2b = CreateRevisionWithRandomProps(rev1, true);
            SavedRevision rev3b = CreateRevisionWithRandomProps(rev2b, true);
            // rev3b should be picked as the winner since it has a longer branch
            SavedRevision    expectedWinner = rev3b;
            RevisionInternal revFound       = database.GetDocumentWithIDAndRev(doc.GetId(), null, EnumSet
                                                                               .NoneOf <Database.TDContentOptions>());

            NUnit.Framework.Assert.AreEqual(expectedWinner.GetId(), revFound.GetRevId());
        }
Пример #9
0
        /// <exception cref="System.Exception"></exception>
        public virtual void TestWinningRevIDOfDoc()
        {
            IDictionary <string, object> properties = new Dictionary <string, object>();

            properties.Put("testName", "testCreateRevisions");
            properties.Put("tag", 1337);
            IDictionary <string, object> properties2a = new Dictionary <string, object>();

            properties2a.Put("testName", "testCreateRevisions");
            properties2a.Put("tag", 1338);
            IDictionary <string, object> properties2b = new Dictionary <string, object>();

            properties2b.Put("testName", "testCreateRevisions");
            properties2b.Put("tag", 1339);
            IList <bool> outIsDeleted  = new AList <bool>();
            IList <bool> outIsConflict = new AList <bool>();
            // Create a conflict on purpose
            Document        doc     = database.CreateDocument();
            UnsavedRevision newRev1 = doc.CreateRevision();

            newRev1.SetUserProperties(properties);
            SavedRevision rev1         = newRev1.Save();
            long          docNumericId = database.GetDocNumericID(doc.GetId());

            NUnit.Framework.Assert.IsTrue(docNumericId != 0);
            NUnit.Framework.Assert.AreEqual(rev1.GetId(), database.WinningRevIDOfDoc(docNumericId
                                                                                     , outIsDeleted, outIsConflict));
            NUnit.Framework.Assert.IsTrue(outIsConflict.Count == 0);
            outIsDeleted  = new AList <bool>();
            outIsConflict = new AList <bool>();
            UnsavedRevision newRev2a = rev1.CreateRevision();

            newRev2a.SetUserProperties(properties2a);
            SavedRevision rev2a = newRev2a.Save();

            NUnit.Framework.Assert.AreEqual(rev2a.GetId(), database.WinningRevIDOfDoc(docNumericId
                                                                                      , outIsDeleted, outIsConflict));
            NUnit.Framework.Assert.IsTrue(outIsConflict.Count == 0);
            outIsDeleted  = new AList <bool>();
            outIsConflict = new AList <bool>();
            UnsavedRevision newRev2b = rev1.CreateRevision();

            newRev2b.SetUserProperties(properties2b);
            SavedRevision rev2b = newRev2b.Save(true);

            database.WinningRevIDOfDoc(docNumericId, outIsDeleted, outIsConflict);
            NUnit.Framework.Assert.IsTrue(outIsConflict.Count > 0);
        }
Пример #10
0
        /// <summary>https://github.com/couchbase/couchbase-lite-java-core/issues/106</summary>
        /// <exception cref="System.Exception"></exception>
        public virtual void TestResolveConflict()
        {
            IDictionary <string, object> properties = new Dictionary <string, object>();

            properties.Put("testName", "testCreateRevisions");
            properties.Put("tag", 1337);
            // Create a conflict on purpose
            Document        doc     = database.CreateDocument();
            UnsavedRevision newRev1 = doc.CreateRevision();

            newRev1.SetUserProperties(properties);
            SavedRevision rev1       = newRev1.Save();
            SavedRevision rev2a      = CreateRevisionWithRandomProps(rev1, false);
            SavedRevision rev2b      = CreateRevisionWithRandomProps(rev1, true);
            SavedRevision winningRev = null;
            SavedRevision losingRev  = null;

            if (doc.GetCurrentRevisionId().Equals(rev2a.GetId()))
            {
                winningRev = rev2a;
                losingRev  = rev2b;
            }
            else
            {
                winningRev = rev2b;
                losingRev  = rev2a;
            }
            NUnit.Framework.Assert.AreEqual(2, doc.GetConflictingRevisions().Count);
            NUnit.Framework.Assert.AreEqual(2, doc.GetLeafRevisions().Count);
            // let's manually choose the losing rev as the winner.  First, delete winner, which will
            // cause losing rev to be the current revision.
            SavedRevision         deleteRevision       = winningRev.DeleteDocument();
            IList <SavedRevision> conflictingRevisions = doc.GetConflictingRevisions();

            NUnit.Framework.Assert.AreEqual(1, conflictingRevisions.Count);
            NUnit.Framework.Assert.AreEqual(2, doc.GetLeafRevisions().Count);
            NUnit.Framework.Assert.AreEqual(3, deleteRevision.GetGeneration());
            NUnit.Framework.Assert.AreEqual(losingRev.GetId(), doc.GetCurrentRevision().GetId
                                                ());
            // Finally create a new revision rev3 based on losing rev
            SavedRevision rev3 = CreateRevisionWithRandomProps(losingRev, true);

            NUnit.Framework.Assert.AreEqual(rev3.GetId(), doc.GetCurrentRevisionId());
            IList <SavedRevision> conflictingRevisions1 = doc.GetConflictingRevisions();

            NUnit.Framework.Assert.AreEqual(1, conflictingRevisions1.Count);
            NUnit.Framework.Assert.AreEqual(2, doc.GetLeafRevisions().Count);
        }
Пример #11
0
        /// <summary>https://github.com/couchbase/couchbase-lite-java-core/issues/106</summary>
        /// <exception cref="System.Exception"></exception>
        public virtual void TestResolveConflict()
        {
            IDictionary <string, object> result;
            // Create a conflict on purpose
            Document      doc        = database.CreateDocument();
            SavedRevision rev1       = doc.CreateRevision().Save();
            SavedRevision rev2a      = CreateRevisionWithRandomProps(rev1, false);
            SavedRevision rev2b      = CreateRevisionWithRandomProps(rev1, true);
            SavedRevision winningRev = null;
            SavedRevision losingRev  = null;

            if (doc.GetCurrentRevisionId().Equals(rev2a.GetId()))
            {
                winningRev = rev2a;
                losingRev  = rev2b;
            }
            else
            {
                winningRev = rev2b;
                losingRev  = rev2a;
            }
            NUnit.Framework.Assert.AreEqual(2, doc.GetConflictingRevisions().Count);
            NUnit.Framework.Assert.AreEqual(2, doc.GetLeafRevisions().Count);
            result = (IDictionary <string, object>)Send("GET", string.Format("/%s/%s?conflicts=true"
                                                                             , DefaultTestDb, doc.GetId()), Status.Ok, null);
            IList <string> conflicts = (IList)result.Get("_conflicts");

            NUnit.Framework.Assert.AreEqual(1, conflicts.Count);
            string conflictingRevId = conflicts[0];

            NUnit.Framework.Assert.AreEqual(losingRev.GetId(), conflictingRevId);
            long docNumericID = database.GetDocNumericID(doc.GetId());

            NUnit.Framework.Assert.IsTrue(docNumericID != 0);
            NUnit.Framework.Assert.IsNotNull(database.GetDocument(doc.GetId()));
            Log.D(Tag, "docNumericID for " + doc.GetId() + " is: " + docNumericID);
            result = (IDictionary <string, object>)Send("DELETE", string.Format("/%s/%s?rev=%s"
                                                                                , DefaultTestDb, doc.GetId(), conflictingRevId), Status.Ok, null);
            result = (IDictionary <string, object>)Send("GET", string.Format("/%s/%s?conflicts=true"
                                                                             , DefaultTestDb, doc.GetId()), Status.Ok, null);
            conflicts = (IList)result.Get("_conflicts");
            NUnit.Framework.Assert.AreEqual(0, conflicts.Count);
        }
Пример #12
0
        /// <summary>https://github.com/couchbase/couchbase-lite-java-core/issues/164</summary>
        /// <exception cref="System.Exception"></exception>
        public virtual void TestRevisionIdDifferentRevisions()
        {
            // two revisions with different json should have different rev-id's
            // because their content will have a different hash (even though
            // they have the same generation number)
            IDictionary <string, object> properties = new Dictionary <string, object>();

            properties.Put("testName", "testCreateRevisions");
            properties.Put("tag", 1337);
            Document        doc    = database.CreateDocument();
            UnsavedRevision newRev = doc.CreateRevision();

            newRev.SetUserProperties(properties);
            SavedRevision rev1  = newRev.Save();
            SavedRevision rev2a = CreateRevisionWithRandomProps(rev1, false);
            SavedRevision rev2b = CreateRevisionWithRandomProps(rev1, true);

            NUnit.Framework.Assert.AreNotSame(rev2a.GetId(), rev2b.GetId());
        }
Пример #13
0
        /// <summary>https://github.com/couchbase/couchbase-lite-java-core/issues/135</summary>
        /// <exception cref="System.Exception"></exception>
        public virtual void TestCorrectWinningRevisionHighRevisionNumber()
        {
            // Create a conflict on purpose
            Document         doc      = database.CreateDocument();
            SavedRevision    rev1     = doc.CreateRevision().Save();
            SavedRevision    rev2a    = CreateRevisionWithRandomProps(rev1, false);
            SavedRevision    rev2b    = CreateRevisionWithRandomProps(rev1, true);
            SavedRevision    rev3b    = CreateRevisionWithRandomProps(rev2b, true);
            SavedRevision    rev4b    = CreateRevisionWithRandomProps(rev3b, true);
            SavedRevision    rev5b    = CreateRevisionWithRandomProps(rev4b, true);
            SavedRevision    rev6b    = CreateRevisionWithRandomProps(rev5b, true);
            SavedRevision    rev7b    = CreateRevisionWithRandomProps(rev6b, true);
            SavedRevision    rev8b    = CreateRevisionWithRandomProps(rev7b, true);
            SavedRevision    rev9b    = CreateRevisionWithRandomProps(rev8b, true);
            SavedRevision    rev10b   = CreateRevisionWithRandomProps(rev9b, true);
            RevisionInternal revFound = database.GetDocumentWithIDAndRev(doc.GetId(), null, EnumSet
                                                                         .NoneOf <Database.TDContentOptions>());

            NUnit.Framework.Assert.AreEqual(rev10b.GetId(), revFound.GetRevId());
        }
Пример #14
0
        public override bool Equals(object o)
        {
            bool result = false;

            if (o is SavedRevision)
            {
                SavedRevision other = (SavedRevision)o;
                if (document.GetId().Equals(other.GetDocument().GetId()) && GetId().Equals(other.
                                                                                           GetId()))
                {
                    result = true;
                }
            }
            return(result);
        }