diff --git a/storage/kv.go b/storage/kv.go index fb6f973a1..c10917a17 100644 --- a/storage/kv.go +++ b/storage/kv.go @@ -11,6 +11,9 @@ import ( type CancelFunc func() type KV interface { + // Rev returns the current revision of the KV. + Rev() int64 + // Range gets the keys in the range at rangeRev. // If rangeRev <=0, range gets the keys at currentRev. // If `end` is nil, the request returns the key. diff --git a/storage/kvstore.go b/storage/kvstore.go index a04fa7c8d..4f01122d6 100644 --- a/storage/kvstore.go +++ b/storage/kvstore.go @@ -69,6 +69,13 @@ func newStore(path string) *store { 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 { id := s.TxnBegin() s.put(key, value) diff --git a/storage/kvstore_test.go b/storage/kvstore_test.go index a8c2ed71c..48cce34fc 100644 --- a/storage/kvstore_test.go +++ b/storage/kvstore_test.go @@ -16,6 +16,18 @@ import ( "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) { tests := []struct { rev revision