Skip to content
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 5 commits into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ require (
github.com/cenkalti/backoff/v4 v4.1.3
github.com/clbanning/mxj/v2 v2.5.5
github.com/colinmarc/hdfs v1.1.3
github.com/couchbase/gocb/v2 v2.6.0
github.com/denisenkom/go-mssqldb v0.11.0
github.com/dgraph-io/ristretto v0.1.0
github.com/dustin/go-humanize v1.0.0
Expand Down Expand Up @@ -162,6 +163,7 @@ require (
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/cockroachdb/apd/v2 v2.0.1 // indirect
github.com/containerd/continuity v0.2.2 // indirect
github.com/couchbase/gocbcore/v10 v10.2.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/danieljoos/wincred v1.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,12 @@ github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3Ee
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/couchbase/gocb/v2 v2.6.0 h1:DhkLNatDcddCcS411D6kNwZspSEAWVeI/N3abzt/HLc=
github.com/couchbase/gocb/v2 v2.6.0/go.mod h1:5su8b1gBF3V4j07SiGw+CA0bK9a84YWEb6UH7up0MEs=
github.com/couchbase/gocbcore/v10 v10.2.0 h1:ZoSBLtcmt+lXbxVVT4SAhXDVNR+D48iSOZWNzHucVVk=
github.com/couchbase/gocbcore/v10 v10.2.0/go.mod h1:qkPnOBziCs0guMEEvd0cRFo+AjOW0yEL99cU3I4n3Ao=
github.com/couchbaselabs/gocaves/client v0.0.0-20220223122017-22859b310bd2 h1:UlwJ2GWpZQAQCLHyO3xHKcqAjUUcX2w7FKpbxCIUQks=
github.com/couchbaselabs/gocaves/client v0.0.0-20220223122017-22859b310bd2/go.mod h1:AVekAZwIY2stsJOMWLAS/0uA/+qdp7pjO8EHnl61QkY=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
Expand Down
120 changes: 120 additions & 0 deletions internal/impl/couchbase/client.go
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{})
}
33 changes: 33 additions & 0 deletions internal/impl/couchbase/client/config.go
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"
)
24 changes: 24 additions & 0 deletions internal/impl/couchbase/client/docs.go
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{
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

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"))
}
61 changes: 61 additions & 0 deletions internal/impl/couchbase/couchbase.go
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,
}
}
Loading