-
Notifications
You must be signed in to change notification settings - Fork 846
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
Add couchbase processor #1596
Merged
Merged
Add couchbase processor #1596
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 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 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 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,120 @@ | ||
package couchbase | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/couchbase/gocb/v2" | ||
|
||
"github.com/benthosdev/benthos/v4/internal/impl/couchbase/client" | ||
"github.com/benthosdev/benthos/v4/public/service" | ||
) | ||
|
||
// ErrInvalidTranscoder specified transcoder is not supported. | ||
var ErrInvalidTranscoder = errors.New("invalid transcoder") | ||
|
||
type couchbaseClient struct { | ||
collection *gocb.Collection | ||
cluster *gocb.Cluster | ||
} | ||
|
||
func getClient(conf *service.ParsedConfig, mgr *service.Resources) (*couchbaseClient, error) { | ||
|
||
// retrieve params | ||
url, err := conf.FieldString("url") | ||
if err != nil { | ||
return nil, err | ||
} | ||
bucket, err := conf.FieldString("bucket") | ||
if err != nil { | ||
return nil, err | ||
} | ||
timeout, err := conf.FieldDuration("timeout") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// setup couchbase | ||
opts := gocb.ClusterOptions{ | ||
// TODO add opentracing Tracer: | ||
// TODO add metrics Meter: | ||
} | ||
|
||
opts.TimeoutsConfig = gocb.TimeoutsConfig{ | ||
ConnectTimeout: timeout, | ||
KVTimeout: timeout, | ||
KVDurableTimeout: timeout, | ||
ViewTimeout: timeout, | ||
QueryTimeout: timeout, | ||
AnalyticsTimeout: timeout, | ||
SearchTimeout: timeout, | ||
ManagementTimeout: timeout, | ||
} | ||
|
||
if conf.Contains("username") { | ||
username, err := conf.FieldString("username") | ||
if err != nil { | ||
return nil, err | ||
} | ||
password, err := conf.FieldString("password") | ||
if err != nil { | ||
return nil, err | ||
} | ||
opts.Authenticator = gocb.PasswordAuthenticator{ | ||
Username: username, | ||
Password: password, | ||
} | ||
} | ||
|
||
tr, err := conf.FieldString("transcoder") | ||
if err != nil { | ||
return nil, err | ||
} | ||
switch client.Transcoder(tr) { | ||
case client.TranscoderJSON: | ||
opts.Transcoder = gocb.NewJSONTranscoder() | ||
case client.TranscoderRaw: | ||
opts.Transcoder = gocb.NewRawBinaryTranscoder() | ||
case client.TranscoderRawJSON: | ||
opts.Transcoder = gocb.NewRawJSONTranscoder() | ||
case client.TranscoderRawString: | ||
opts.Transcoder = gocb.NewRawStringTranscoder() | ||
case client.TranscoderLegacy: | ||
opts.Transcoder = gocb.NewLegacyTranscoder() | ||
default: | ||
return nil, fmt.Errorf("%w: %s", ErrInvalidTranscoder, tr) | ||
} | ||
|
||
cluster, err := gocb.Connect(url, opts) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// check that we can do query | ||
err = cluster.Bucket(bucket).WaitUntilReady(timeout, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
proc := &couchbaseClient{ | ||
cluster: cluster, | ||
} | ||
|
||
// retrieve collection | ||
if conf.Contains("collection") { | ||
collectionStr, err := conf.FieldString("collection") | ||
if err != nil { | ||
return nil, err | ||
} | ||
proc.collection = cluster.Bucket(bucket).Collection(collectionStr) | ||
} else { | ||
proc.collection = cluster.Bucket(bucket).DefaultCollection() | ||
} | ||
|
||
return proc, nil | ||
} | ||
|
||
func (p *couchbaseClient) Close(ctx context.Context) error { | ||
return p.cluster.Close(&gocb.ClusterCloseOptions{}) | ||
} |
This file contains 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,33 @@ | ||
package client | ||
|
||
// Transcoder represents the transcoder that will be used by Couchbase. | ||
type Transcoder string | ||
|
||
const ( | ||
// TranscoderRaw raw operation. | ||
TranscoderRaw Transcoder = "raw" | ||
// TranscoderRawJSON rawjson transcoder. | ||
TranscoderRawJSON Transcoder = "rawjson" | ||
// TranscoderRawString rawstring transcoder. | ||
TranscoderRawString Transcoder = "rawstring" | ||
// TranscoderJSON JSON transcoder. | ||
TranscoderJSON Transcoder = "json" | ||
// TranscoderLegacy Legacy transcoder. | ||
TranscoderLegacy Transcoder = "legacy" | ||
) | ||
|
||
// Operation represents the operation that will be performed by Couchbase. | ||
type Operation string | ||
|
||
const ( | ||
// OperationGet Get operation. | ||
OperationGet Operation = "get" | ||
// OperationInsert Insert operation. | ||
OperationInsert Operation = "insert" | ||
// OperationRemove Delete operation. | ||
OperationRemove Operation = "remove" | ||
// OperationReplace Replace operation. | ||
OperationReplace Operation = "replace" | ||
// OperationUpsert Upsert operation. | ||
OperationUpsert Operation = "upsert" | ||
) |
This file contains 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,24 @@ | ||
package client | ||
|
||
import ( | ||
"github.com/benthosdev/benthos/v4/public/service" | ||
) | ||
|
||
// NewConfigSpec constructs a new Couchbase ConfigSpec with common config fields | ||
func NewConfigSpec() *service.ConfigSpec { | ||
return service.NewConfigSpec(). | ||
// TODO Stable(). | ||
Field(service.NewStringField("url").Description("Couchbase connection string.").Example("couchbase://localhost:11210")). | ||
Field(service.NewStringField("username").Description("Username to connect to the cluster.").Optional()). | ||
Field(service.NewStringField("password").Description("Password to connect to the cluster.").Optional()). | ||
Field(service.NewStringField("bucket").Description("Couchbase bucket.")). | ||
Field(service.NewStringField("collection").Description("Bucket collection.").Default("_default").Advanced().Optional()). | ||
Field(service.NewStringAnnotatedEnumField("transcoder", map[string]string{ | ||
string(TranscoderRaw): `RawBinaryTranscoder implements passthrough behavior of raw binary data. This transcoder does not apply any serialization. This will apply the following behavior to the value: binary ([]byte) -> binary bytes, binary expectedFlags. default -> error.`, | ||
string(TranscoderRawJSON): `RawJSONTranscoder implements passthrough behavior of JSON data. This transcoder does not apply any serialization. It will forward data across the network without incurring unnecessary parsing costs. This will apply the following behavior to the value: binary ([]byte) -> JSON bytes, JSON expectedFlags. string -> JSON bytes, JSON expectedFlags. default -> error.`, | ||
string(TranscoderRawString): `RawStringTranscoder implements passthrough behavior of raw string data. This transcoder does not apply any serialization. This will apply the following behavior to the value: string -> string bytes, string expectedFlags. default -> error.`, | ||
string(TranscoderJSON): `JSONTranscoder implements the default transcoding behavior and applies JSON transcoding to all values. This will apply the following behavior to the value: binary ([]byte) -> error. default -> JSON value, JSON Flags.`, | ||
string(TranscoderLegacy): `LegacyTranscoder implements the behaviour for a backward-compatible transcoder. This transcoder implements behaviour matching that of gocb v1.This will apply the following behavior to the value: binary ([]byte) -> binary bytes, Binary expectedFlags. string -> string bytes, String expectedFlags. default -> JSON value, JSON expectedFlags.`, | ||
}).Description("Couchbase transcoder to use.").Default(string(TranscoderLegacy)).Advanced()). | ||
Field(service.NewDurationField("timeout").Description("Operation timeout.").Advanced().Default("15s")) | ||
} |
This file contains 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,61 @@ | ||
package couchbase | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/couchbase/gocb/v2" | ||
) | ||
|
||
func valueFromOp(op gocb.BulkOp) (out any, err error) { | ||
switch o := op.(type) { | ||
case *gocb.GetOp: | ||
if o.Err != nil { | ||
return nil, o.Err | ||
} | ||
err := o.Result.Content(&out) | ||
return out, err | ||
case *gocb.InsertOp: | ||
return nil, o.Err | ||
case *gocb.RemoveOp: | ||
return nil, o.Err | ||
case *gocb.ReplaceOp: | ||
return nil, o.Err | ||
case *gocb.UpsertOp: | ||
return nil, o.Err | ||
} | ||
|
||
return nil, fmt.Errorf("type not supported") | ||
} | ||
|
||
func get(key string, _ []byte) gocb.BulkOp { | ||
return &gocb.GetOp{ | ||
ID: key, | ||
} | ||
} | ||
|
||
func insert(key string, data []byte) gocb.BulkOp { | ||
return &gocb.InsertOp{ | ||
ID: key, | ||
Value: data, | ||
} | ||
} | ||
|
||
func remove(key string, _ []byte) gocb.BulkOp { | ||
return &gocb.RemoveOp{ | ||
ID: key, | ||
} | ||
} | ||
|
||
func replace(key string, data []byte) gocb.BulkOp { | ||
return &gocb.ReplaceOp{ | ||
ID: key, | ||
Value: data, | ||
} | ||
} | ||
|
||
func upsert(key string, data []byte) gocb.BulkOp { | ||
return &gocb.UpsertOp{ | ||
ID: key, | ||
Value: data, | ||
} | ||
} |
Oops, something went wrong.
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.
Looks like these transcoder descriptions were copy/pasted from the operation field and need updating.
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.
Yes I totally forget to update it. Sorry.
It should be good now.