Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

CAS Implementation & Update import paths for gomemcached #7

Merged
merged 2 commits into from

1 participant

@maniktaneja
  • Added CAS (compare and set ) API
  • Changed import paths for dustin/gomemcached to couchbase/gomemcached.
@maniktaneja maniktaneja merged commit 9f5e76e into couchbase:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 14, 2014
  1. @maniktaneja

    CAS API go-couchbase

    maniktaneja authored
  2. @maniktaneja
This page is out of date. Refresh to see the latest.
View
46 client.go
@@ -35,8 +35,8 @@ import (
"sync"
"time"
- "github.com/dustin/gomemcached"
- "github.com/dustin/gomemcached/client"
+ "github.com/couchbase/gomemcached"
+ "github.com/couchbase/gomemcached/client"
)
// Maximum number of times to retry a chunk of a bulk get on error.
@@ -433,6 +433,48 @@ func (b *Bucket) Write(k string, flags, exp int, v interface{},
return err
}
+func (b *Bucket) WriteCas(k string, flags, exp int, cas uint64, v interface{},
+ opt WriteOptions) (err error) {
+
+ if ClientOpCallback != nil {
+ defer func(t time.Time) {
+ ClientOpCallback(fmt.Sprintf("Write(%v)", opt), k, t, err)
+ }(time.Now())
+ }
+
+ var data []byte
+ if opt&Raw == 0 {
+ data, err = json.Marshal(v)
+ if err != nil {
+ return err
+ }
+ } else if v != nil {
+ data = v.([]byte)
+ }
+
+ var res *gomemcached.MCResponse
+ err = b.Do(k, func(mc *memcached.Client, vb uint16) error {
+ res, err = mc.SetCas(vb, k, flags, exp, cas, data)
+ return err
+ })
+
+ if err == nil && (opt&(Persist|Indexable) != 0) {
+ err = b.WaitForPersistence(k, res.Cas, data == nil)
+ }
+
+ return err
+}
+
+// Set a value in this bucket with Cas
+func (b *Bucket) Cas(k string, exp int, cas uint64, v interface{}) error {
+ return b.WriteCas(k, 0, exp, cas, v, 0)
+}
+
+// Set a value in this bucket with Cas without json encoding it
+func (b *Bucket) CasRaw(k string, exp int, cas uint64, v interface{}) error {
+ return b.WriteCas(k, 0, exp, cas, v, Raw)
+}
+
// Set a value in this bucket.
// The value will be serialized into a JSON document.
func (b *Bucket) Set(k string, exp int, v interface{}) error {
View
2  conn_pool.go
@@ -4,7 +4,7 @@ import (
"errors"
"time"
- "github.com/dustin/gomemcached/client"
+ "github.com/couchbase/gomemcached/client"
)
var errClosedPool = errors.New("the pool is closed")
View
4 conn_pool_test.go
@@ -6,8 +6,8 @@ import (
"testing"
"time"
- "github.com/dustin/gomemcached"
- "github.com/dustin/gomemcached/client"
+ "github.com/couchbase/gomemcached"
+ "github.com/couchbase/gomemcached/client"
)
type testT struct {
View
33 examples/hello/hello.go
@@ -34,6 +34,38 @@ func doOps(b *couchbase.Bucket) {
total*3, time.Now().Sub(start).String())
}
+func doMoreOps(b *couchbase.Bucket) {
+ fmt.Printf("Doing some Cas ops on %s\n", b.Name)
+ start := time.Now()
+ total := 2048
+ for i := 0; i < total; i++ {
+ k := fmt.Sprintf("k2%d", i)
+ maybeFatal(b.Set(k, 0, []string{"a", "b", "c"}))
+ rv := make([]string, 0, 10)
+ var cas uint64
+ maybeFatal(b.Gets(k, &rv, &cas))
+ if fmt.Sprintf("%#v", rv) != `[]string{"a", "b", "c"}` {
+ log.Fatalf("Expected %#v, got %#v",
+ []string{"a", "b", "c"}, rv)
+ }
+ maybeFatal(b.Cas(k, 0, cas, []string{"a", "b", "d"}))
+ maybeFatal(b.Get(k, &rv))
+ if fmt.Sprintf("%#v", rv) != `[]string{"a", "b", "d"}` {
+ log.Fatalf("Expected %#v, got %#v",
+ []string{"a", "b", "c"}, rv)
+ }
+ // this should fail since we don't know the latest cas value
+ err := b.Cas(k, 0, cas, []string{"a", "b", "x"})
+ if err == nil {
+ log.Fatalf("Expected \"Data exists for key\"")
+ }
+
+ maybeFatal(b.Delete(k))
+ }
+ fmt.Printf("Did %d ops in %s\n",
+ total*6, time.Now().Sub(start).String())
+}
+
func exploreBucket(bucket *couchbase.Bucket) {
vbm := bucket.VBServerMap()
fmt.Printf(" %v uses %s\n", bucket.Name, vbm.HashAlgorithm)
@@ -48,6 +80,7 @@ func exploreBucket(bucket *couchbase.Bucket) {
}
doOps(bucket)
+ doMoreOps(bucket)
}
View
2  examples/hello_tap/hello_tap.go
@@ -5,8 +5,8 @@ import (
"fmt"
"log"
+ "github.com/couchbase/gomemcached/client"
"github.com/couchbaselabs/go-couchbase"
- "github.com/dustin/gomemcached/client"
)
var poolName = flag.String("pool", "default", "Pool name")
View
2  tap.go
@@ -4,7 +4,7 @@ import (
"log"
"time"
- "github.com/dustin/gomemcached/client"
+ "github.com/couchbase/gomemcached/client"
)
const initialRetryInterval = 1 * time.Second
View
4 upr.go
@@ -29,8 +29,8 @@ package couchbase
import (
"encoding/binary"
"fmt"
- mcd "github.com/dustin/gomemcached"
- mc "github.com/dustin/gomemcached/client"
+ mcd "github.com/couchbase/gomemcached"
+ mc "github.com/couchbase/gomemcached/client"
"log"
"time"
)
View
2  uprtrans.go
@@ -3,7 +3,7 @@ package couchbase
import (
"encoding/binary"
"fmt"
- mcd "github.com/dustin/gomemcached"
+ mcd "github.com/couchbase/gomemcached"
)
const opaqueOpen = 0xBEAF0001
View
2  uprtrans_test.go
@@ -3,7 +3,7 @@ package couchbase
import (
"encoding/binary"
"fmt"
- mcd "github.com/dustin/gomemcached"
+ mcd "github.com/couchbase/gomemcached"
"testing"
)
Something went wrong with that request. Please try again.