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

@ -96,9 +96,10 @@ func TestKVRange(t *testing.T) {
wheader := resp.Header
tests := []struct {
begin, end string
rev int64
sortOption *clientv3.SortOption
begin, end string
rev int64
sortOption *clientv3.SortOption
serializable bool
wantSet []*storagepb.KeyValue
}{
@ -107,6 +108,19 @@ func TestKVRange(t *testing.T) {
"a", "c",
0,
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{
{Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
@ -118,6 +132,7 @@ func TestKVRange(t *testing.T) {
"a", "x",
2,
nil,
false,
[]*storagepb.KeyValue{
{Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
@ -128,6 +143,7 @@ func TestKVRange(t *testing.T) {
"a", "x",
0,
&clientv3.SortOption{Target: clientv3.SortByKey, Order: clientv3.SortAscend},
false,
[]*storagepb.KeyValue{
{Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
@ -143,6 +159,7 @@ func TestKVRange(t *testing.T) {
"a", "x",
0,
&clientv3.SortOption{Target: clientv3.SortByCreatedRev, Order: clientv3.SortDescend},
false,
[]*storagepb.KeyValue{
{Key: []byte("fop"), Value: nil, CreateRevision: 9, ModRevision: 9, Version: 1},
@ -158,6 +175,7 @@ func TestKVRange(t *testing.T) {
"a", "x",
0,
&clientv3.SortOption{Target: clientv3.SortByModifiedRev, Order: clientv3.SortDescend},
false,
[]*storagepb.KeyValue{
{Key: []byte("fop"), Value: nil, CreateRevision: 9, ModRevision: 9, Version: 1},
@ -175,6 +193,9 @@ func TestKVRange(t *testing.T) {
if tt.sortOption != nil {
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...)
if err != nil {
t.Fatalf("#%d: couldn't range (%v)", i, err)

View File

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