-
Notifications
You must be signed in to change notification settings - Fork 21
DOC-9567 - Add scoped index management examples #166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
maria-robobug
merged 1 commit into
couchbase:release/2.5
from
maria-robobug:DOC-9567-2-5
Apr 12, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| gocb "github.com/couchbase/gocb/v2" | ||
| ) | ||
|
|
||
| func main() { | ||
| // tag::creating-index-mgr[] | ||
| cluster, err := gocb.Connect("localhost", gocb.ClusterOptions{ | ||
| Authenticator: gocb.PasswordAuthenticator{ | ||
| Username: "Administrator", | ||
| Password: "password", | ||
| }, | ||
| }) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| if err = cluster.WaitUntilReady(5*time.Second, nil); err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| queryIndexMgr := cluster.QueryIndexes() | ||
| // end::creating-index-mgr[] | ||
|
|
||
| primaryIndex(queryIndexMgr) | ||
| secondaryIndex(queryIndexMgr) | ||
| deferAndWatchIndex(queryIndexMgr) | ||
| dropPrimaryAndSecondaryIndex(queryIndexMgr) | ||
| } | ||
|
|
||
| func primaryIndex(queryIndexMgr *gocb.QueryIndexManager) { | ||
| fmt.Println("Example - [primary]") | ||
| // tag::primary[] | ||
| if err := queryIndexMgr.CreatePrimaryIndex("travel-sample", | ||
| &gocb.CreatePrimaryQueryIndexOptions{ | ||
| ScopeName: "tenant_agent_01", | ||
| CollectionName: "users", | ||
| // Set this if you wish to use a custom name | ||
| // CustomName: "custom_name", | ||
| IgnoreIfExists: true, | ||
| }, | ||
| ); err != nil { | ||
| if errors.Is(err, gocb.ErrIndexExists) { | ||
| fmt.Println("Index already exists") | ||
| } else { | ||
| panic(err) | ||
| } | ||
| } | ||
| // end::primary[] | ||
| } | ||
|
|
||
| func secondaryIndex(queryIndexMgr *gocb.QueryIndexManager) { | ||
| fmt.Println("\nExample - [secondary]") | ||
| // tag::secondary[] | ||
| if err := queryIndexMgr.CreateIndex("travel-sample", "tenant_agent_01_users_email", []string{"preferred_email"}, | ||
| &gocb.CreateQueryIndexOptions{ | ||
| ScopeName: "tenant_agent_01", | ||
| CollectionName: "users", | ||
| }, | ||
| ); err != nil { | ||
| if errors.Is(err, gocb.ErrIndexExists) { | ||
| fmt.Println("Index already exists") | ||
| } else { | ||
| panic(err) | ||
| } | ||
| } | ||
| // end::secondary[] | ||
| } | ||
|
|
||
| func deferAndWatchIndex(queryIndexMgr *gocb.QueryIndexManager) { | ||
| fmt.Println("\nExample - [defer-indexes]") | ||
| // tag::defer-indexes[] | ||
| // Create a deferred index | ||
| if err := queryIndexMgr.CreateIndex("travel-sample", "tenant_agent_01_users_phone", []string{"preferred_phone"}, | ||
| &gocb.CreateQueryIndexOptions{ | ||
| ScopeName: "tenant_agent_01", | ||
| CollectionName: "users", | ||
| Deferred: true, | ||
| }, | ||
| ); err != nil { | ||
| if errors.Is(err, gocb.ErrIndexExists) { | ||
| fmt.Println("Index already exists") | ||
| } else { | ||
| panic(err) | ||
| } | ||
| } | ||
|
|
||
| // Build any deferred indexes within `travel-sample`.tenant_agent_01.users | ||
| indexesToBuild, err := queryIndexMgr.BuildDeferredIndexes("travel-sample", | ||
| &gocb.BuildDeferredQueryIndexOptions{ | ||
| ScopeName: "tenant_agent_01", | ||
| CollectionName: "users", | ||
| }, | ||
| ) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| // Wait for indexes to come online | ||
| if err = queryIndexMgr.WatchIndexes("travel-sample", indexesToBuild, time.Duration(30*time.Second), | ||
| &gocb.WatchQueryIndexOptions{ | ||
| ScopeName: "tenant_agent_01", | ||
| CollectionName: "users", | ||
| }, | ||
| ); err != nil { | ||
| panic(err) | ||
| } | ||
| // end::defer-indexes[] | ||
| } | ||
|
|
||
| func dropPrimaryAndSecondaryIndex(queryIndexMgr *gocb.QueryIndexManager) { | ||
| fmt.Println("\nExample - [drop-primary-or-secondary-index]") | ||
| // tag::drop-primary-or-secondary-index[] | ||
| // Drop a primary index | ||
| if err := queryIndexMgr.DropPrimaryIndex("travel-sample", | ||
| &gocb.DropPrimaryQueryIndexOptions{ | ||
| ScopeName: "tenant_agent_01", | ||
| CollectionName: "users", | ||
| }, | ||
| ); err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| // Drop a secondary index | ||
| if err := queryIndexMgr.DropIndex("travel-sample", "tenant_agent_01_users_email", | ||
| &gocb.DropQueryIndexOptions{ | ||
| ScopeName: "tenant_agent_01", | ||
| CollectionName: "users", | ||
| }, | ||
| ); err != nil { | ||
| panic(err) | ||
| } | ||
| // end::drop-primary-or-secondary-index[] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,27 +9,32 @@ load 'test_helper' | |
| } | ||
|
|
||
| @test "[howtos] - query.go" { | ||
| runExample $HOWTOS_DIR query.go | ||
| assert_success | ||
| assert_output --partial "map[greeting:hello]" | ||
| runExample $HOWTOS_DIR query.go | ||
| assert_success | ||
| assert_output --partial "map[greeting:hello]" | ||
| } | ||
|
|
||
| @test "[howtos] - collection-manager.go" { | ||
| runExample $HOWTOS_DIR collection-manager.go | ||
| assert_success | ||
| assert_output --partial "drop-scope" | ||
| runExample $HOWTOS_DIR collection-manager.go | ||
| assert_success | ||
| assert_output --partial "drop-scope" | ||
| } | ||
|
|
||
| @test "[howtos] - subdoc.go" { | ||
| echo "create example document" | ||
| cbimport json --verbose \ | ||
| -c localhost -u Administrator -p password \ | ||
| -b travel-sample \ | ||
| -f lines \ | ||
| -d file:///modules/test/fixtures/customer123.json \ | ||
| -g customer123 | ||
| -c localhost -u Administrator -p password \ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. woot! your shell plugin has started doing indentation correctly! 👍 😉 |
||
| -b travel-sample \ | ||
| -f lines \ | ||
| -d file:///modules/test/fixtures/customer123.json \ | ||
| -g customer123 | ||
| echo | ||
|
|
||
| runExample $HOWTOS_DIR subdoc.go | ||
| assert_success | ||
| } | ||
|
|
||
| @test "[howtos] - query-index-manager.go" { | ||
| runExample $HOWTOS_DIR query-index-manager.go | ||
| assert_success | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, why is this plural btw? We don't say "Indexes management" for example.
Oh, is it because of ambiguity of "View" (verb/noun)? Hmm. OK, still, seems a bit inconsistent, only just noticed this time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Views is short for Map/Reduce Views.