Пример #1
0
        public RevisionInternal PutLocalRevision(RevisionInternal revision, string prevRevId, bool obeyMVCC)
        {
            var docId = revision.GetDocId();

            if (!docId.StartsWith("_local/"))
            {
                throw new CouchbaseLiteException("Local revision IDs must start with _local/", StatusCode.BadId);
            }

            if (revision.IsDeleted())
            {
                DeleteLocalRevision(docId, prevRevId, obeyMVCC);
                return(revision);
            }

            var result = default(RevisionInternal);

            RunInTransaction(() =>
            {
                var json = Manager.GetObjectMapper().WriteValueAsString(revision.GetProperties(), true);
                WithC4Raw(docId, "_local", doc =>
                {
                    var generation = RevisionInternal.GenerationFromRevID(prevRevId);
                    if (obeyMVCC)
                    {
                        if (prevRevId != null)
                        {
                            if (prevRevId != (doc != null ? (string)doc->meta : null))
                            {
                                throw new CouchbaseLiteException(StatusCode.Conflict);
                            }

                            if (generation == 0)
                            {
                                throw new CouchbaseLiteException(StatusCode.BadId);
                            }
                        }
                        else if (doc != null)
                        {
                            throw new CouchbaseLiteException(StatusCode.Conflict);
                        }
                    }

                    var newRevId = String.Format("{0}-local", ++generation);
                    ForestDBBridge.Check(err => Native.c4raw_put(Forest, "_local", docId, newRevId, json, err));
                    result = revision.CopyWithDocID(docId, newRevId);
                });

                return(true);
            });

            return(result);
        }