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

Method handleBulkDocs

rest/bulk_api.go:523–644  ·  view source on GitHub ↗

HTTP handler for a POST to _bulk_docs

()

Source from the content-addressed store, hash-verified

521
522// HTTP handler for a POST to _bulk_docs
523func (h *handler) handleBulkDocs() error {
524
525 if h.db.DatabaseContext.Options.UnsupportedOptions != nil && h.db.DatabaseContext.Options.UnsupportedOptions.RejectWritesWithSkippedSequences {
526 // if we are in slow broadcast mode reject write with 503 and increment rejected writes stat
527 if h.db.BroadcastSlowMode.Load() {
528 h.db.DbStats.DatabaseStats.NumDocWritesRejected.Add(1)
529 return base.HTTPErrorf(http.StatusServiceUnavailable, "Database cache is behind and cannot accept writes at this time. Please try again later.")
530 }
531 }
532
533 startTime := time.Now()
534 defer func() {
535 h.db.DbStats.CBLReplicationPush().WriteProcessingTime.Add(time.Since(startTime).Nanoseconds())
536 }()
537
538 body, err := h.readJSON()
539 if err != nil {
540 return err
541 }
542
543 newEdits, ok := body["new_edits"].(bool)
544 if !ok {
545 newEdits = true
546 }
547
548 userDocs, ok := body["docs"].([]interface{})
549 if !ok {
550 err = base.HTTPErrorf(http.StatusBadRequest, "missing 'docs' property")
551 return err
552 }
553 lenDocs := len(userDocs)
554
555 // split out local docs, save them on their own
556 localDocs := make([]interface{}, 0, lenDocs)
557 docs := make([]interface{}, 0, lenDocs)
558 for _, item := range userDocs {
559 doc, ok := item.(map[string]interface{})
560 if !ok {
561 err = base.HTTPErrorf(http.StatusBadRequest, "Document body must be JSON")
562 return err
563 }
564
565 // If ID is present, check whether local doc. (note: if _id is absent or non-string, docid will be
566 // empty string and handled during normal doc processing)
567 docid, _ := doc[db.BodyId].(string)
568
569 if strings.HasPrefix(docid, db.LocalDocPrefix) {
570 localDocs = append(localDocs, doc)
571 } else {
572 docs = append(docs, doc)
573 }
574 }
575
576 result := make([]db.Body, 0, len(docs))
577 for _, item := range docs {
578 doc := item.(map[string]interface{})
579 docid, _ := doc[db.BodyId].(string)
580 var docErr error

Callers

nothing calls this directly

Calls 15

readJSONMethod · 0.95
ctxMethod · 0.95
writeJSONStatusMethod · 0.95
HTTPErrorfFunction · 0.92
ParseRevisionsFunction · 0.92
ErrorAsHTTPStatusFunction · 0.92
CouchHTTPErrorNameFunction · 0.92
InfofCtxFunction · 0.92
UDFunction · 0.92
FixJSONNumbersFunction · 0.92
LoadMethod · 0.80

Tested by

no test coverage detected