MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / handlePostDoc

Method handlePostDoc

rest/doc_api.go:745–781  ·  view source on GitHub ↗

HTTP handler for a POST to a database (creating a document)

()

Source from the content-addressed store, hash-verified

743
744// HTTP handler for a POST to a database (creating a document)
745func (h *handler) handlePostDoc() error {
746 if h.isReadOnlyGuest() {
747 return base.HTTPErrorf(http.StatusForbidden, auth.GuestUserReadOnly)
748 }
749
750 if h.db.DatabaseContext.Options.UnsupportedOptions != nil && h.db.DatabaseContext.Options.UnsupportedOptions.RejectWritesWithSkippedSequences {
751 // if we are in slow broadcast mode reject write with 503 and increment rejected writes stat
752 if h.db.BroadcastSlowMode.Load() {
753 h.db.DbStats.DatabaseStats.NumDocWritesRejected.Add(1)
754 return base.HTTPErrorf(http.StatusServiceUnavailable, "Database cache is behind and cannot accept writes at this time. Please try again later.")
755 }
756 }
757
758 roundTrip := h.getBoolQuery("roundtrip")
759 body, err := h.readDocument()
760 if err != nil {
761 return err
762 }
763
764 docid, newRev, doc, err := h.collection.Post(h.ctx(), body)
765 if err != nil {
766 return err
767 }
768
769 if doc != nil && roundTrip {
770 err := h.collection.WaitForSequenceNotSkipped(h.ctx(), doc.Sequence)
771 if err != nil {
772 return err
773 }
774 }
775
776 h.setHeader("Location", docid)
777 h.setEtag(newRev)
778
779 h.writeRawJSONStatus(http.StatusOK, []byte(`{"id":`+base.ConvertToJSONString(docid)+`,"ok":true,"rev":"`+newRev+`","cv":"`+doc.HLV.GetCurrentVersionString()+`"}`))
780 return nil
781}
782
783// docVersionFromOCCValue converts an OCC value and type into a DocVersion struct.
784func docVersionFromOCCValue(occValue string, occValueType occVersionType) (docVersion db.DocVersion, err error) {

Callers

nothing calls this directly

Calls 14

isReadOnlyGuestMethod · 0.95
getBoolQueryMethod · 0.95
readDocumentMethod · 0.95
ctxMethod · 0.95
setHeaderMethod · 0.95
setEtagMethod · 0.95
writeRawJSONStatusMethod · 0.95
HTTPErrorfFunction · 0.92
ConvertToJSONStringFunction · 0.92
LoadMethod · 0.80
PostMethod · 0.80

Tested by

no test coverage detected