Skip to content

Commit

Permalink
feat: multi mutateIn action impl. (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
mertsen0 authored Sep 4, 2024
1 parent 3a48be1 commit f3290a5
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 4 deletions.
54 changes: 54 additions & 0 deletions couchbase/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,49 @@ func (s *client) GetAgent() *gocbcore.Agent {
return s.agent
}

func (s *client) CreateMultiPath(ctx context.Context,
scopeName string,
collectionName string,
id []byte,
pathValues []PathValue,
flags memd.SubdocDocFlag,
cas *gocbcore.Cas,
expiry uint32,
preserveExpiry bool,
cb gocbcore.MutateInCallback,
) error {
deadline, _ := ctx.Deadline()

ops := make([]gocbcore.SubDocOp, len(pathValues))

for i, pv := range pathValues {
ops[i] = gocbcore.SubDocOp{
Op: memd.SubDocOpDictSet,
Path: string(pv.Path),
Value: pv.Value,
}
}

options := gocbcore.MutateInOptions{
Key: id,
Flags: flags,
Ops: ops,
Expiry: expiry,
PreserveExpiry: preserveExpiry,
Deadline: deadline,
ScopeName: scopeName,
CollectionName: collectionName,
}

if cas != nil {
options.Cas = *cas
}

_, err := s.agent.MutateIn(options, cb)

return err
}

func (s *client) CreatePath(ctx context.Context,
scopeName string,
collectionName string,
Expand Down Expand Up @@ -229,6 +272,17 @@ func (s *client) Execute(ctx context.Context, action *CBActionDocument, callback
func(result *gocbcore.MutateInResult, err error) {
callback(err)
})
case action.Type == MultiMutateIn:
flags := memd.SubdocDocFlagMkDoc
if action.DisableAutoCreate {
flags = memd.SubdocDocFlagNone
}

err = s.CreateMultiPath(ctx, s.config.ScopeName, s.config.CollectionName,
action.ID, action.PathValues, flags, casPtr, action.Expiry, action.PreserveExpiry,
func(result *gocbcore.MutateInResult, err error) {
callback(err)
})
case action.Type == DeletePath:
err = s.DeletePath(ctx, s.config.ScopeName, s.config.CollectionName,
action.ID, action.Path, casPtr, action.Expiry, action.PreserveExpiry,
Expand Down
29 changes: 25 additions & 4 deletions couchbase/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@ package couchbase

type CbAction string

type PathValue struct {
Path []byte
Value []byte
}

const (
Set CbAction = "Set"
Delete CbAction = "Delete"
MutateIn CbAction = "MutateIn"
DeletePath CbAction = "DeletePath"
Set CbAction = "Set"
Delete CbAction = "Delete"
MutateIn CbAction = "MutateIn"
MultiMutateIn CbAction = "MultiMutateIn"
DeletePath CbAction = "DeletePath"
)

type CBActionDocument struct {
Cas *uint64
Type CbAction
PathValues []PathValue
Source []byte
ID []byte
Path []byte
Expand Down Expand Up @@ -54,6 +61,20 @@ func NewSetAction(key []byte, source []byte) CBActionDocument {
}
}

func NewMultiMutateInAction(key []byte, pathValues []PathValue) CBActionDocument {
size := len(key)
for _, pv := range pathValues {
size += len(pv.Path) + len(pv.Value)
}

return CBActionDocument{
ID: key,
PathValues: pathValues,
Type: MultiMutateIn,
Size: size,
}
}

func NewMutateInAction(key []byte, path []byte, source []byte) CBActionDocument {
return CBActionDocument{
ID: key,
Expand Down

0 comments on commit f3290a5

Please sign in to comment.