From 6b279a99ef5f414d84940bd443ea00343f243157 Mon Sep 17 00:00:00 2001 From: Brett Lawson Date: Fri, 22 May 2015 16:26:37 -0300 Subject: [PATCH] GOCBC-32: Added support for CAS when performing a Touch operation. Additionally removed some additional network overhead associated with using the Touch method. --- bucket.go | 4 ++-- gocbcore/agentops.go | 30 +++++++++++++++++++++++++----- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/bucket.go b/bucket.go index c8eb4a8e..05fb0fbc 100644 --- a/bucket.go +++ b/bucket.go @@ -161,9 +161,9 @@ func (b *Bucket) GetReplica(key string, valuePtr interface{}, replicaIdx int) (u } // Touches a document, specifying a new expiry time for it. -func (b *Bucket) Touch(key string, expiry uint32) (uint64, error) { +func (b *Bucket) Touch(key string, cas uint64, expiry uint32) (uint64, error) { return b.hlpCasExec(func(cb ioCasCallback) (pendingOp, error) { - op, err := b.client.Touch([]byte(key), expiry, gocbcore.TouchCallback(cb)) + op, err := b.client.Touch([]byte(key), cas, expiry, gocbcore.TouchCallback(cb)) return op, err }) } diff --git a/gocbcore/agentops.go b/gocbcore/agentops.go index 7cffa442..e1dfe24e 100644 --- a/gocbcore/agentops.go +++ b/gocbcore/agentops.go @@ -188,11 +188,31 @@ func (c *Agent) GetReplica(key []byte, replicaIdx int, cb GetCallback) (PendingO } } -func (c *Agent) Touch(key []byte, expiry uint32, cb TouchCallback) (PendingOp, error) { - // This seems odd, but this is how it's done in Node.js - return c.GetAndTouch(key, expiry, func(value []byte, flags uint32, cas uint64, err error) { - cb(cas, err) - }) +func (c *Agent) Touch(key []byte, cas uint64, expiry uint32, cb TouchCallback) (PendingOp, error) { + handler := func(resp *memdResponse, err error) { + if err != nil { + cb(0, err) + return + } + cb(resp.Cas, nil) + } + + extraBuf := make([]byte, 4) + binary.BigEndian.PutUint32(extraBuf[0:], expiry) + + req := &memdQRequest{ + memdRequest: memdRequest{ + Magic: ReqMagic, + Opcode: CmdTouch, + Datatype: 0, + Cas: cas, + Extras: extraBuf, + Key: key, + Value: nil, + }, + Callback: handler, + } + return c.dispatchOp(req) } func (c *Agent) Unlock(key []byte, cas uint64, cb UnlockCallback) (PendingOp, error) {