Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

CAS Implementation & Update import paths for gomemcached #7

Open
wants to merge 2 commits into from

1 participant

Manik Taneja
Manik Taneja
Owner
  • Added CAS (compare and set ) API
  • Changed import paths for dustin/gomemcached to couchbase/gomemcached.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Apr 14, 2014
Manik Taneja CAS API go-couchbase ac173b2
Apr 15, 2014
Manik Taneja Update import paths for memcached client d40d47f
This page is out of date. Refresh to see the latest.
46  client.go
@@ -35,8 +35,8 @@ import (
35 35
 	"sync"
36 36
 	"time"
37 37
 
38  
-	"github.com/dustin/gomemcached"
39  
-	"github.com/dustin/gomemcached/client"
  38
+	"github.com/couchbase/gomemcached"
  39
+	"github.com/couchbase/gomemcached/client"
40 40
 )
41 41
 
42 42
 // 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{},
433 433
 	return err
434 434
 }
435 435
 
  436
+func (b *Bucket) WriteCas(k string, flags, exp int, cas uint64, v interface{},
  437
+	opt WriteOptions) (err error) {
  438
+
  439
+	if ClientOpCallback != nil {
  440
+		defer func(t time.Time) {
  441
+			ClientOpCallback(fmt.Sprintf("Write(%v)", opt), k, t, err)
  442
+		}(time.Now())
  443
+	}
  444
+
  445
+	var data []byte
  446
+	if opt&Raw == 0 {
  447
+		data, err = json.Marshal(v)
  448
+		if err != nil {
  449
+			return err
  450
+		}
  451
+	} else if v != nil {
  452
+		data = v.([]byte)
  453
+	}
  454
+
  455
+	var res *gomemcached.MCResponse
  456
+	err = b.Do(k, func(mc *memcached.Client, vb uint16) error {
  457
+		res, err = mc.SetCas(vb, k, flags, exp, cas, data)
  458
+		return err
  459
+	})
  460
+
  461
+	if err == nil && (opt&(Persist|Indexable) != 0) {
  462
+		err = b.WaitForPersistence(k, res.Cas, data == nil)
  463
+	}
  464
+
  465
+	return err
  466
+}
  467
+
  468
+// Set a value in this bucket with Cas
  469
+func (b *Bucket) Cas(k string, exp int, cas uint64, v interface{}) error {
  470
+	return b.WriteCas(k, 0, exp, cas, v, 0)
  471
+}
  472
+
  473
+// Set a value in this bucket with Cas without json encoding it
  474
+func (b *Bucket) CasRaw(k string, exp int, cas uint64, v interface{}) error {
  475
+	return b.WriteCas(k, 0, exp, cas, v, Raw)
  476
+}
  477
+
436 478
 // Set a value in this bucket.
437 479
 // The value will be serialized into a JSON document.
438 480
 func (b *Bucket) Set(k string, exp int, v interface{}) error {
2  conn_pool.go
@@ -4,7 +4,7 @@ import (
4 4
 	"errors"
5 5
 	"time"
6 6
 
7  
-	"github.com/dustin/gomemcached/client"
  7
+	"github.com/couchbase/gomemcached/client"
8 8
 )
9 9
 
10 10
 var errClosedPool = errors.New("the pool is closed")
4  conn_pool_test.go
@@ -6,8 +6,8 @@ import (
6 6
 	"testing"
7 7
 	"time"
8 8
 
9  
-	"github.com/dustin/gomemcached"
10  
-	"github.com/dustin/gomemcached/client"
  9
+	"github.com/couchbase/gomemcached"
  10
+	"github.com/couchbase/gomemcached/client"
11 11
 )
12 12
 
13 13
 type testT struct {
33  examples/hello/hello.go
@@ -34,6 +34,38 @@ func doOps(b *couchbase.Bucket) {
34 34
 		total*3, time.Now().Sub(start).String())
35 35
 }
36 36
 
  37
+func doMoreOps(b *couchbase.Bucket) {
  38
+	fmt.Printf("Doing some Cas ops on %s\n", b.Name)
  39
+	start := time.Now()
  40
+	total := 2048
  41
+	for i := 0; i < total; i++ {
  42
+		k := fmt.Sprintf("k2%d", i)
  43
+		maybeFatal(b.Set(k, 0, []string{"a", "b", "c"}))
  44
+		rv := make([]string, 0, 10)
  45
+		var cas uint64
  46
+		maybeFatal(b.Gets(k, &rv, &cas))
  47
+		if fmt.Sprintf("%#v", rv) != `[]string{"a", "b", "c"}` {
  48
+			log.Fatalf("Expected %#v, got %#v",
  49
+				[]string{"a", "b", "c"}, rv)
  50
+		}
  51
+		maybeFatal(b.Cas(k, 0, cas, []string{"a", "b", "d"}))
  52
+		maybeFatal(b.Get(k, &rv))
  53
+		if fmt.Sprintf("%#v", rv) != `[]string{"a", "b", "d"}` {
  54
+			log.Fatalf("Expected %#v, got %#v",
  55
+				[]string{"a", "b", "c"}, rv)
  56
+		}
  57
+		// this should fail since we don't know the latest cas value
  58
+		err := b.Cas(k, 0, cas, []string{"a", "b", "x"})
  59
+		if err == nil {
  60
+			log.Fatalf("Expected \"Data exists for key\"")
  61
+		}
  62
+
  63
+		maybeFatal(b.Delete(k))
  64
+	}
  65
+	fmt.Printf("Did %d ops in %s\n",
  66
+		total*6, time.Now().Sub(start).String())
  67
+}
  68
+
37 69
 func exploreBucket(bucket *couchbase.Bucket) {
38 70
 	vbm := bucket.VBServerMap()
39 71
 	fmt.Printf("     %v uses %s\n", bucket.Name, vbm.HashAlgorithm)
@@ -48,6 +80,7 @@ func exploreBucket(bucket *couchbase.Bucket) {
48 80
 	}
49 81
 
50 82
 	doOps(bucket)
  83
+	doMoreOps(bucket)
51 84
 
52 85
 }
53 86
 
2  examples/hello_tap/hello_tap.go
@@ -5,8 +5,8 @@ import (
5 5
 	"fmt"
6 6
 	"log"
7 7
 
  8
+	"github.com/couchbase/gomemcached/client"
8 9
 	"github.com/couchbaselabs/go-couchbase"
9  
-	"github.com/dustin/gomemcached/client"
10 10
 )
11 11
 
12 12
 var poolName = flag.String("pool", "default", "Pool name")
2  tap.go
@@ -4,7 +4,7 @@ import (
4 4
 	"log"
5 5
 	"time"
6 6
 
7  
-	"github.com/dustin/gomemcached/client"
  7
+	"github.com/couchbase/gomemcached/client"
8 8
 )
9 9
 
10 10
 const initialRetryInterval = 1 * time.Second
4  upr.go
@@ -29,8 +29,8 @@ package couchbase
29 29
 import (
30 30
 	"encoding/binary"
31 31
 	"fmt"
32  
-	mcd "github.com/dustin/gomemcached"
33  
-	mc "github.com/dustin/gomemcached/client"
  32
+	mcd "github.com/couchbase/gomemcached"
  33
+	mc "github.com/couchbase/gomemcached/client"
34 34
 	"log"
35 35
 	"time"
36 36
 )
2  uprtrans.go
@@ -3,7 +3,7 @@ package couchbase
3 3
 import (
4 4
 	"encoding/binary"
5 5
 	"fmt"
6  
-	mcd "github.com/dustin/gomemcached"
  6
+	mcd "github.com/couchbase/gomemcached"
7 7
 )
8 8
 
9 9
 const opaqueOpen = 0xBEAF0001
2  uprtrans_test.go
@@ -3,7 +3,7 @@ package couchbase
3 3
 import (
4 4
 	"encoding/binary"
5 5
 	"fmt"
6  
-	mcd "github.com/dustin/gomemcached"
  6
+	mcd "github.com/couchbase/gomemcached"
7 7
 	"testing"
8 8
 )
9 9
 
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.