consul: Implement KVSDelete

This commit is contained in:
Armon Dadgar 2014-03-31 12:24:06 -07:00
parent 6744ee62be
commit f87c47424c
2 changed files with 51 additions and 2 deletions

View File

@ -754,8 +754,26 @@ func (s *StateStore) KVSList() (uint64, structs.DirEntries, error) {
} }
// KVSDelete is used to delete a KVS entry // KVSDelete is used to delete a KVS entry
func (s *StateStore) KVSDelete() error { func (s *StateStore) KVSDelete(index uint64, key string) error {
return nil // Start a new txn
tx, err := s.kvsTable.StartTxn(false, nil)
if err != nil {
return err
}
defer tx.Abort()
num, err := s.kvsTable.DeleteTxn(tx, "id", key)
if err != nil {
return err
}
if num > 0 {
if err := s.kvsTable.SetLastIndexTxn(tx, index); err != nil {
return err
}
defer s.watch[s.kvsTable].Notify()
}
return tx.Commit()
} }
// KVSDeleteTree is used to delete all keys with a given prefix // KVSDeleteTree is used to delete all keys with a given prefix

View File

@ -1013,3 +1013,34 @@ func TestKVSSet_Get(t *testing.T) {
t.Fatalf("bad: %v", d) t.Fatalf("bad: %v", d)
} }
} }
func TestKVSDelete(t *testing.T) {
store, err := testStateStore()
if err != nil {
t.Fatalf("err: %v", err)
}
defer store.Close()
// Create the entry
d := &structs.DirEntry{Key: "/foo", Flags: 42, Value: []byte("test")}
if err := store.KVSSet(1000, d); err != nil {
t.Fatalf("err: %v", err)
}
// Delete the entry
if err := store.KVSDelete(1020, "/foo"); err != nil {
t.Fatalf("err: %v", err)
}
// Should not exist
idx, d, err := store.KVSGet("/foo")
if err != nil {
t.Fatalf("err: %v", err)
}
if idx != 1020 {
t.Fatalf("bad: %v", idx)
}
if d != nil {
t.Fatalf("bad: %v", d)
}
}