Creates a new DatabaseContext on a bucket. The bucket will be closed when this context closes.
(ctx context.Context, dbName string, bucket base.Bucket, autoImport bool, options DatabaseContextOptions)
| 368 | |
| 369 | // Creates a new DatabaseContext on a bucket. The bucket will be closed when this context closes. |
| 370 | func NewDatabaseContext(ctx context.Context, dbName string, bucket base.Bucket, autoImport bool, options DatabaseContextOptions) (dbc *DatabaseContext, returnedError error) { |
| 371 | cleanupFunctions := make([]func(), 0) |
| 372 | |
| 373 | defer func() { |
| 374 | if returnedError != nil { |
| 375 | for _, cleanupFunc := range cleanupFunctions { |
| 376 | cleanupFunc() |
| 377 | } |
| 378 | } |
| 379 | }() |
| 380 | |
| 381 | if err := ValidateDatabaseName(dbName); err != nil { |
| 382 | return nil, err |
| 383 | } |
| 384 | |
| 385 | // add db info to ctx before having a DatabaseContext (cannot call AddDatabaseLogContext), |
| 386 | // in order to pass it to RegisterImportPindexImpl |
| 387 | ctx = base.DatabaseLogCtx(ctx, dbName, options.LoggingConfig) |
| 388 | |
| 389 | if err := base.RequireNoBucketTTL(ctx, bucket); err != nil { |
| 390 | return nil, err |
| 391 | } |
| 392 | |
| 393 | serverUUID, err := base.GetServerUUID(ctx, bucket) |
| 394 | if err != nil { |
| 395 | return nil, err |
| 396 | } |
| 397 | |
| 398 | dbStats, statsError := initDatabaseStats(ctx, dbName, autoImport, options) |
| 399 | if statsError != nil { |
| 400 | return nil, statsError |
| 401 | } |
| 402 | |
| 403 | // options.MetadataStore is always passed via rest._getOrAddDatabase... |
| 404 | // but in db package tests this is unlikely to be set. In this case we'll use the existing bucket connection to store metadata. |
| 405 | metadataStore := options.MetadataStore |
| 406 | if metadataStore == nil { |
| 407 | base.DebugfCtx(ctx, base.KeyAll, "MetadataStore was nil - falling back to use existing bucket connection %q for database %q", bucket.GetName(), dbName) |
| 408 | metadataStore = bucket.DefaultDataStore() |
| 409 | } |
| 410 | |
| 411 | bucketUUID, err := bucket.UUID() |
| 412 | if err != nil { |
| 413 | return nil, err |
| 414 | } |
| 415 | sourceID, err := base.GetSourceID(ctx, bucket) |
| 416 | if err != nil { |
| 417 | return nil, err |
| 418 | } |
| 419 | |
| 420 | // Register the cbgt pindex type for the configGroup |
| 421 | RegisterImportPindexImpl(ctx, options.GroupID) |
| 422 | |
| 423 | dbContext := &DatabaseContext{ |
| 424 | Name: dbName, |
| 425 | UUID: cbgt.NewUUID(), |
| 426 | MetadataStore: metadataStore, |
| 427 | Bucket: bucket, |