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

Method OnDemandImportForWrite

db/crud.go:963–1002  ·  view source on GitHub ↗

UPDATING DOCUMENTS: OnDemandImportForWrite imports a document before a subsequent document is written to Sync Gateway on top of the import document. Returns base.ErrCasFailureShouldRetry in the case that this import nees to be retried. This function is expected to be called within a callback to Writ

(ctx context.Context, docid string, doc *Document, deleted bool)

Source from the content-addressed store, hash-verified

961
962// OnDemandImportForWrite imports a document before a subsequent document is written to Sync Gateway on top of the import document. Returns base.ErrCasFailureShouldRetry in the case that this import nees to be retried. This function is expected to be called within a callback to WriteUpdateWithXattrs.
963func (db *DatabaseCollectionWithUser) OnDemandImportForWrite(ctx context.Context, docid string, doc *Document, deleted bool) error {
964 revSeqNo, cas, err := db.getRevSeqNo(ctx, docid)
965 if err != nil {
966 return err
967 }
968 if cas != doc.Cas {
969 return base.ErrCasFailureShouldRetry
970 }
971
972 if syncDataErr := doc.validateSyncDataForImport(ctx, db.dbCtx, docid); syncDataErr != nil {
973 return syncDataErr
974 }
975 // Check whether the doc requiring import is an SDK delete
976 isDelete := false
977 if doc.Body(ctx) == nil {
978 isDelete = true
979 } else {
980 isDelete = deleted
981 }
982 // Use an admin-scoped database for import
983 importDb := DatabaseCollectionWithUser{DatabaseCollection: db.DatabaseCollection, user: nil}
984
985 importOpts := importDocOptions{
986 expiry: nil,
987 mode: ImportOnDemand,
988 isDelete: isDelete,
989 revSeqNo: revSeqNo,
990 }
991 importedDoc, importErr := importDb.ImportDoc(ctx, docid, doc, importOpts) // nolint:staticcheck
992
993 if importErr == base.ErrImportCancelledFilter {
994 // Document exists, but existing doc wasn't imported based on import filter. Treat write as insert
995 doc.SyncData = SyncData{History: make(RevTree)}
996 } else if importErr != nil {
997 return importErr
998 } else {
999 doc = importedDoc // nolint:staticcheck
1000 }
1001 return nil
1002}
1003
1004// updateHLV updates the HLV in the sync data appropriately based on what type of document update event we are encountering. mouMatch represents if the _mou.cas == doc.cas
1005func (db *DatabaseCollectionWithUser) updateHLV(ctx context.Context, d *Document, docUpdateEvent DocUpdateType, mouMatch bool) (*Document, error) {

Callers 4

PutMethod · 0.95
CreateDocNoHLVMethod · 0.95

Calls 4

ImportDocMethod · 0.95
getRevSeqNoMethod · 0.80
BodyMethod · 0.45

Tested by

no test coverage detected