Browse Source

clientv3: fix WithFromKey

The WithFromKey func should not return error similar to etcdctl usage
of it when an empty key is provided.

Fixed #9833
Sahdev P. Zala 6 years ago
parent
commit
313ab0ba47
2 changed files with 23 additions and 1 deletions
  1. 15 0
      clientv3/integration/kv_test.go
  2. 8 1
      clientv3/op.go

+ 15 - 0
clientv3/integration/kv_test.go

@@ -403,6 +403,21 @@ func TestKVRange(t *testing.T) {
 			0,
 			0,
 			[]clientv3.OpOption{clientv3.WithPrefix(), clientv3.WithSort(clientv3.SortByKey, clientv3.SortAscend)},
 			[]clientv3.OpOption{clientv3.WithPrefix(), clientv3.WithSort(clientv3.SortByKey, clientv3.SortAscend)},
 
 
+			[]*mvccpb.KeyValue{
+				{Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
+				{Key: []byte("b"), Value: nil, CreateRevision: 3, ModRevision: 3, Version: 1},
+				{Key: []byte("c"), Value: nil, CreateRevision: 4, ModRevision: 6, Version: 3},
+				{Key: []byte("foo"), Value: nil, CreateRevision: 7, ModRevision: 7, Version: 1},
+				{Key: []byte("foo/abc"), Value: nil, CreateRevision: 8, ModRevision: 8, Version: 1},
+				{Key: []byte("fop"), Value: nil, CreateRevision: 9, ModRevision: 9, Version: 1},
+			},
+		},
+		// fetch keyspace with empty key using WithFromKey
+		{
+			"", "",
+			0,
+			[]clientv3.OpOption{clientv3.WithFromKey(), clientv3.WithSort(clientv3.SortByKey, clientv3.SortAscend)},
+
 			[]*mvccpb.KeyValue{
 			[]*mvccpb.KeyValue{
 				{Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
 				{Key: []byte("a"), Value: nil, CreateRevision: 2, ModRevision: 2, Version: 1},
 				{Key: []byte("b"), Value: nil, CreateRevision: 3, ModRevision: 3, Version: 1},
 				{Key: []byte("b"), Value: nil, CreateRevision: 3, ModRevision: 3, Version: 1},

+ 8 - 1
clientv3/op.go

@@ -392,7 +392,14 @@ func WithRange(endKey string) OpOption {
 
 
 // WithFromKey specifies the range of 'Get', 'Delete', 'Watch' requests
 // WithFromKey specifies the range of 'Get', 'Delete', 'Watch' requests
 // to be equal or greater than the key in the argument.
 // to be equal or greater than the key in the argument.
-func WithFromKey() OpOption { return WithRange("\x00") }
+func WithFromKey() OpOption {
+	return func(op *Op) {
+		if len(op.key) == 0 {
+			op.key = []byte{0}
+		}
+		op.end = []byte("\x00")
+	}
+}
 
 
 // WithSerializable makes 'Get' request serializable. By default,
 // WithSerializable makes 'Get' request serializable. By default,
 // it's linearizable. Serializable requests are better for lower latency
 // it's linearizable. Serializable requests are better for lower latency