clientv3: support serializable

This commit is contained in:
Xiang Li 2016-02-12 12:24:46 -08:00
parent 00f89941a9
commit 5908e5b601
2 changed files with 35 additions and 6 deletions

View File

@ -99,6 +99,7 @@ func TestKVRange(t *testing.T) {
begin, end string begin, end string
rev int64 rev int64
sortOption *clientv3.SortOption sortOption *clientv3.SortOption
serializable bool
wantSet []*storagepb.KeyValue wantSet []*storagepb.KeyValue
}{ }{
@ -107,6 +108,19 @@ func TestKVRange(t *testing.T) {
"a", "c", "a", "c",
0, 0,
nil, nil,
false,
[]*storagepb.KeyValue{
{Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
{Key: []byte("b"), Value: nil, CreateRevision: 3, ModRevision: 3, Version: 1},
},
},
// range first two with serializable
{
"a", "c",
0,
nil,
true,
[]*storagepb.KeyValue{ []*storagepb.KeyValue{
{Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1}, {Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
@ -118,6 +132,7 @@ func TestKVRange(t *testing.T) {
"a", "x", "a", "x",
2, 2,
nil, nil,
false,
[]*storagepb.KeyValue{ []*storagepb.KeyValue{
{Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1}, {Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
@ -128,6 +143,7 @@ func TestKVRange(t *testing.T) {
"a", "x", "a", "x",
0, 0,
&clientv3.SortOption{Target: clientv3.SortByKey, Order: clientv3.SortAscend}, &clientv3.SortOption{Target: clientv3.SortByKey, Order: clientv3.SortAscend},
false,
[]*storagepb.KeyValue{ []*storagepb.KeyValue{
{Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1}, {Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
@ -143,6 +159,7 @@ func TestKVRange(t *testing.T) {
"a", "x", "a", "x",
0, 0,
&clientv3.SortOption{Target: clientv3.SortByCreatedRev, Order: clientv3.SortDescend}, &clientv3.SortOption{Target: clientv3.SortByCreatedRev, Order: clientv3.SortDescend},
false,
[]*storagepb.KeyValue{ []*storagepb.KeyValue{
{Key: []byte("fop"), Value: nil, CreateRevision: 9, ModRevision: 9, Version: 1}, {Key: []byte("fop"), Value: nil, CreateRevision: 9, ModRevision: 9, Version: 1},
@ -158,6 +175,7 @@ func TestKVRange(t *testing.T) {
"a", "x", "a", "x",
0, 0,
&clientv3.SortOption{Target: clientv3.SortByModifiedRev, Order: clientv3.SortDescend}, &clientv3.SortOption{Target: clientv3.SortByModifiedRev, Order: clientv3.SortDescend},
false,
[]*storagepb.KeyValue{ []*storagepb.KeyValue{
{Key: []byte("fop"), Value: nil, CreateRevision: 9, ModRevision: 9, Version: 1}, {Key: []byte("fop"), Value: nil, CreateRevision: 9, ModRevision: 9, Version: 1},
@ -175,6 +193,9 @@ func TestKVRange(t *testing.T) {
if tt.sortOption != nil { if tt.sortOption != nil {
opts = append(opts, clientv3.WithSort(tt.sortOption.Target, tt.sortOption.Order)) opts = append(opts, clientv3.WithSort(tt.sortOption.Target, tt.sortOption.Order))
} }
if tt.serializable == true {
opts = append(opts, clientv3.WithSerializable())
}
resp, err := kv.Get(ctx, tt.begin, opts...) resp, err := kv.Get(ctx, tt.begin, opts...)
if err != nil { if err != nil {
t.Fatalf("#%d: couldn't range (%v)", i, err) t.Fatalf("#%d: couldn't range (%v)", i, err)

View File

@ -38,6 +38,7 @@ type Op struct {
limit int64 limit int64
rev int64 rev int64
sort *SortOption sort *SortOption
serializable bool
// for put // for put
val []byte val []byte
@ -86,6 +87,8 @@ func OpDelete(key string, opts ...OpOption) Op {
panic("unexpected revision in delete") panic("unexpected revision in delete")
case ret.sort != nil: case ret.sort != nil:
panic("unexpected sort in delete") panic("unexpected sort in delete")
case ret.serializable != false:
panic("unexpected serializable in delete")
} }
return ret return ret
} }
@ -102,6 +105,8 @@ func OpPut(key, val string, opts ...OpOption) Op {
panic("unexpected revision in put") panic("unexpected revision in put")
case ret.sort != nil: case ret.sort != nil:
panic("unexpected sort in put") panic("unexpected sort in put")
case ret.serializable != false:
panic("unexpected serializable in delete")
} }
return ret return ret
} }
@ -127,3 +132,6 @@ func WithSort(tgt SortTarget, order SortOrder) OpOption {
func WithRange(endKey string) OpOption { func WithRange(endKey string) OpOption {
return func(op *Op) { op.end = []byte(endKey) } return func(op *Op) { op.end = []byte(endKey) }
} }
func WithSerializable() OpOption {
return func(op *Op) { op.serializable = true }
}