Browse Source

storage: add rev into kv interface

Xiang Li 10 years ago
parent
commit
6d1f0ce89f
3 changed files with 22 additions and 0 deletions
  1. 3 0
      storage/kv.go
  2. 7 0
      storage/kvstore.go
  3. 12 0
      storage/kvstore_test.go

+ 3 - 0
storage/kv.go

@@ -11,6 +11,9 @@ import (
 type CancelFunc func()
 type CancelFunc func()
 
 
 type KV interface {
 type KV interface {
+	// Rev returns the current revision of the KV.
+	Rev() int64
+
 	// Range gets the keys in the range at rangeRev.
 	// Range gets the keys in the range at rangeRev.
 	// If rangeRev <=0, range gets the keys at currentRev.
 	// If rangeRev <=0, range gets the keys at currentRev.
 	// If `end` is nil, the request returns the key.
 	// If `end` is nil, the request returns the key.

+ 7 - 0
storage/kvstore.go

@@ -69,6 +69,13 @@ func newStore(path string) *store {
 	return s
 	return s
 }
 }
 
 
+func (s *store) Rev() int64 {
+	s.mu.RLock()
+	defer s.mu.RUnlock()
+
+	return s.currentRev.main
+}
+
 func (s *store) Put(key, value []byte) int64 {
 func (s *store) Put(key, value []byte) int64 {
 	id := s.TxnBegin()
 	id := s.TxnBegin()
 	s.put(key, value)
 	s.put(key, value)

+ 12 - 0
storage/kvstore_test.go

@@ -16,6 +16,18 @@ import (
 	"github.com/coreos/etcd/storage/storagepb"
 	"github.com/coreos/etcd/storage/storagepb"
 )
 )
 
 
+func TestStoreRev(t *testing.T) {
+	s := newStore(tmpPath)
+	defer os.Remove(tmpPath)
+
+	for i := 0; i < 3; i++ {
+		s.Put([]byte("foo"), []byte("bar"))
+		if r := s.Rev(); r != int64(i+1) {
+			t.Errorf("#%d: rev = %d, want %d", i, r, i+1)
+		}
+	}
+}
+
 func TestStorePut(t *testing.T) {
 func TestStorePut(t *testing.T) {
 	tests := []struct {
 	tests := []struct {
 		rev revision
 		rev revision