UpdateMetadataDocument retries on CAS mismatch
(ctx context.Context, location, docID string, updateCallback func(bucketConfig []byte, rawBucketConfigCas uint64) (newConfig []byte, err error))
| 381 | |
| 382 | // UpdateMetadataDocument retries on CAS mismatch |
| 383 | func (cc *CouchbaseCluster) UpdateMetadataDocument(ctx context.Context, location, docID string, updateCallback func(bucketConfig []byte, rawBucketConfigCas uint64) (newConfig []byte, err error)) (newCAS uint64, err error) { |
| 384 | if cc == nil { |
| 385 | return 0, errors.New("nil CouchbaseCluster") |
| 386 | } |
| 387 | |
| 388 | b, teardown, err := cc.getBucket(ctx, location) |
| 389 | if err != nil { |
| 390 | return 0, err |
| 391 | } |
| 392 | defer teardown() |
| 393 | |
| 394 | collection := b.DefaultCollection() |
| 395 | |
| 396 | for { |
| 397 | bucketValue, cas, err := cc.configPersistence.loadRawConfig(ctx, collection, docID) |
| 398 | if err != nil { |
| 399 | return 0, err |
| 400 | } |
| 401 | newConfig, err := updateCallback(bucketValue, uint64(cas)) |
| 402 | if err != nil { |
| 403 | return 0, err |
| 404 | } |
| 405 | |
| 406 | // handle delete when updateCallback returns nil |
| 407 | if newConfig == nil { |
| 408 | removeCasOut, err := cc.configPersistence.removeRawConfig(collection, docID, cas) |
| 409 | if err != nil { |
| 410 | // retry on cas failure |
| 411 | if errors.Is(err, gocb.ErrCasMismatch) { |
| 412 | continue |
| 413 | } |
| 414 | return 0, err |
| 415 | } |
| 416 | return uint64(removeCasOut), nil |
| 417 | } |
| 418 | |
| 419 | replaceCfgCasOut, err := cc.configPersistence.replaceRawConfig(collection, docID, newConfig, cas) |
| 420 | if err != nil { |
| 421 | if errors.Is(err, gocb.ErrCasMismatch) { |
| 422 | // retry on cas failure |
| 423 | continue |
| 424 | } |
| 425 | return 0, err |
| 426 | } |
| 427 | |
| 428 | return uint64(replaceCfgCasOut), nil |
| 429 | } |
| 430 | |
| 431 | } |
| 432 | |
| 433 | // KeyExists checks whether a key exists in the default collection for the specified bucket |
| 434 | func (cc *CouchbaseCluster) KeyExists(ctx context.Context, location, docID string) (exists bool, err error) { |
nothing calls this directly
no test coverage detected