public void TestParseRevID() { var parsed = RevisionInternal.ParseRevId("1-utiopturoewpt"); Assert.AreEqual(1, parsed.Item1); Assert.AreEqual("utiopturoewpt", parsed.Item2); parsed = RevisionInternal.ParseRevId("321-fdjfdsj-e"); Assert.AreEqual(321, parsed.Item1); Assert.AreEqual("fdjfdsj-e", parsed.Item2); parsed = RevisionInternal.ParseRevId("0-fdjfdsj-e"); Assert.IsTrue(parsed.Item1 == 0 && parsed.Item2 == "fdjfdsj-e"); parsed = RevisionInternal.ParseRevId("-4-fdjfdsj-e"); Assert.IsTrue(parsed.Item1 < 0); parsed = RevisionInternal.ParseRevId("5_fdjfdsj-e"); Assert.IsTrue(parsed.Item1 < 0); parsed = RevisionInternal.ParseRevId(" 5-fdjfdsj-e"); Assert.IsTrue(parsed.Item1 < 0); parsed = RevisionInternal.ParseRevId("7 -foo"); Assert.IsTrue(parsed.Item1 < 0); parsed = RevisionInternal.ParseRevId("7-"); Assert.IsTrue(parsed.Item1 < 0); parsed = RevisionInternal.ParseRevId("7"); Assert.IsTrue(parsed.Item1 < 0); parsed = RevisionInternal.ParseRevId("eiuwtiu"); Assert.IsTrue(parsed.Item1 < 0); parsed = RevisionInternal.ParseRevId(string.Empty); Assert.IsTrue(parsed.Item1 < 0); }
/// <summary> /// Finds the common ancestor. /// </summary> /// <remarks> /// Given a revision and an array of revIDs, finds the latest common ancestor revID /// and returns its generation #. If there is none, returns 0. /// </remarks> /// <returns>The common ancestor.</returns> /// <param name="rev">Rev.</param> /// <param name="possibleRevIDs">Possible rev I ds.</param> internal static int FindCommonAncestor(RevisionInternal rev, IList <string> possibleRevIDs) { if (possibleRevIDs == null || possibleRevIDs.Count == 0) { return(0); } var history = Database.ParseCouchDBRevisionHistory(rev.GetProperties()); Debug.Assert(history != null); history = history.Intersect(possibleRevIDs).ToList(); var ancestorID = history.Count == 0 ? null : history[0]; if (ancestorID == null) { return(0); } var parsed = RevisionInternal.ParseRevId(ancestorID); return(parsed.Item1); }
public static ICouchbaseResponseState RevsDiff(ICouchbaseListenerContext context) { // Collect all of the input doc/revision IDs as CBL_Revisions: var revs = new RevisionList(); var body = context.BodyAs <Dictionary <string, object> >(); if (body == null) { return(context.CreateResponse(StatusCode.BadJson).AsDefaultState()); } foreach (var docPair in body) { var revIDs = docPair.Value.AsList <string>(); if (revIDs == null) { return(context.CreateResponse(StatusCode.BadParam).AsDefaultState()); } foreach (var revID in revIDs) { var rev = new RevisionInternal(docPair.Key, revID, false); revs.Add(rev); } } return(PerformLogicWithDatabase(context, true, db => { var response = context.CreateResponse(); // Look them up, removing the existing ones from revs: db.Storage.FindMissingRevisions(revs); // Return the missing revs in a somewhat different format: IDictionary <string, object> diffs = new Dictionary <string, object>(); foreach (var rev in revs) { var docId = rev.GetDocId(); IList <string> missingRevs = null; if (!diffs.ContainsKey(docId)) { missingRevs = new List <string>(); diffs[docId] = new Dictionary <string, IList <string> > { { "missing", missingRevs } }; } else { missingRevs = ((Dictionary <string, IList <string> >)diffs[docId])["missing"]; } missingRevs.Add(rev.GetRevId()); } // Add the possible ancestors for each missing revision: foreach (var docPair in diffs) { IDictionary <string, IList <string> > docInfo = (IDictionary <string, IList <string> >)docPair.Value; int maxGen = 0; string maxRevID = null; foreach (var revId in docInfo["missing"]) { var parsed = RevisionInternal.ParseRevId(revId); if (parsed.Item1 > maxGen) { maxGen = parsed.Item1; maxRevID = revId; } } var rev = new RevisionInternal(docPair.Key, maxRevID, false); var ancestors = db.Storage.GetPossibleAncestors(rev, 0, false); var ancestorList = ancestors == null ? null : ancestors.ToList(); if (ancestorList != null && ancestorList.Count > 0) { docInfo["possible_ancestors"] = ancestorList; } } response.JsonBody = new Body(diffs); return response; }).AsDefaultState()); }