De-generalizes graveyard since that ended up as a YAGNI (only useful for KV).

This commit is contained in:
James Phillips 2015-10-07 08:58:17 -07:00
parent e7de7a2b8c
commit e25fb6c5ed
3 changed files with 21 additions and 23 deletions

View File

@ -6,31 +6,29 @@ import (
"github.com/hashicorp/go-memdb"
)
// tombstone is the internal type used to track tombstones.
// Tombstone is the internal type used to track tombstones.
type Tombstone struct {
Key string
Index uint64
}
// Graveyard manages a set of tombstones for a table. This is just used for
// KVS right now but we've broken it out for other table types later.
// Graveyard manages a set of tombstones.
type Graveyard struct {
Table string
}
// NewGraveyard returns a new graveyard.
func NewGraveyard(table string) *Graveyard {
return &Graveyard{Table: "tombstones_" + table}
func NewGraveyard() *Graveyard {
return &Graveyard{}
}
// InsertTxn adds a new tombstone.
func (g *Graveyard) InsertTxn(tx *memdb.Txn, context string, idx uint64) error {
stone := &Tombstone{Key: context, Index: idx}
if err := tx.Insert(g.Table, stone); err != nil {
func (g *Graveyard) InsertTxn(tx *memdb.Txn, key string, idx uint64) error {
stone := &Tombstone{Key: key, Index: idx}
if err := tx.Insert("tombstones", stone); err != nil {
return fmt.Errorf("failed inserting tombstone: %s", err)
}
if err := tx.Insert("index", &IndexEntry{g.Table, idx}); err != nil {
if err := tx.Insert("index", &IndexEntry{"tombstones", idx}); err != nil {
return fmt.Errorf("failed updating index: %s", err)
}
return nil
@ -38,8 +36,8 @@ func (g *Graveyard) InsertTxn(tx *memdb.Txn, context string, idx uint64) error {
// GetMaxIndexTxn returns the highest index tombstone whose key matches the
// given context, using a prefix match.
func (g *Graveyard) GetMaxIndexTxn(tx *memdb.Txn, context string) (uint64, error) {
stones, err := tx.Get(g.Table, "id_prefix", context)
func (g *Graveyard) GetMaxIndexTxn(tx *memdb.Txn, prefix string) (uint64, error) {
stones, err := tx.Get("tombstones", "id_prefix", prefix)
if err != nil {
return 0, fmt.Errorf("failed querying tombstones: %s", err)
}
@ -56,7 +54,7 @@ func (g *Graveyard) GetMaxIndexTxn(tx *memdb.Txn, context string) (uint64, error
// DumpTxn returns all the tombstones.
func (g *Graveyard) DumpTxn(tx *memdb.Txn) ([]*Tombstone, error) {
stones, err := tx.Get(g.Table, "id")
stones, err := tx.Get("tombstones", "id")
if err != nil {
return nil, fmt.Errorf("failed querying tombstones: %s", err)
}
@ -71,11 +69,11 @@ func (g *Graveyard) DumpTxn(tx *memdb.Txn) ([]*Tombstone, error) {
// RestoreTxn is used when restoring from a snapshot. For general inserts, use
// InsertTxn.
func (g *Graveyard) RestoreTxn(tx *memdb.Txn, stone *Tombstone) error {
if err := tx.Insert(g.Table, stone); err != nil {
if err := tx.Insert("tombstones", stone); err != nil {
return fmt.Errorf("failed inserting tombstone: %s", err)
}
if err := indexUpdateMaxTxn(tx, stone.Index, g.Table); err != nil {
if err := indexUpdateMaxTxn(tx, stone.Index, "tombstones"); err != nil {
return fmt.Errorf("failed updating index: %s", err)
}
return nil
@ -87,14 +85,14 @@ func (g *Graveyard) ReapTxn(tx *memdb.Txn, idx uint64) error {
// This does a full table scan since we currently can't index on a
// numeric value. Since this is all in-memory and done infrequently
// this pretty reasonable.
stones, err := tx.Get(g.Table, "id")
stones, err := tx.Get("tombstones", "id")
if err != nil {
return fmt.Errorf("failed querying tombstones: %s", err)
}
for stone := stones.Next(); stone != nil; stone = stones.Next() {
if stone.(*Tombstone).Index <= idx {
if err := tx.Delete(g.Table, stone); err != nil {
if err := tx.Delete("tombstones", stone); err != nil {
return fmt.Errorf("failed deleting tombstone: %s", err)
}
}

View File

@ -25,7 +25,7 @@ func stateStoreSchema() *memdb.DBSchema {
servicesTableSchema,
checksTableSchema,
kvsTableSchema,
func() *memdb.TableSchema { return tombstonesTableSchema("kvs") },
tombstonesTableSchema,
sessionsTableSchema,
sessionChecksTableSchema,
aclsTableSchema,
@ -230,11 +230,11 @@ func kvsTableSchema() *memdb.TableSchema {
}
// tombstonesTableSchema returns a new table schema used for
// storing tombstones during the given table's delete operations
// to prevent the index from sliding backwards.
func tombstonesTableSchema(table string) *memdb.TableSchema {
// storing tombstones during KV delete operations to prevent
// the index from sliding backwards.
func tombstonesTableSchema() *memdb.TableSchema {
return &memdb.TableSchema{
Name: "tombstones_" + table,
Name: "tombstones",
Indexes: map[string]*memdb.IndexSchema{
"id": &memdb.IndexSchema{
Name: "id",

View File

@ -102,7 +102,7 @@ func NewStateStore(logOutput io.Writer) (*StateStore, error) {
db: db,
tableWatches: tableWatches,
kvsWatch: NewPrefixWatch(),
kvsGraveyard: NewGraveyard("kvs"),
kvsGraveyard: NewGraveyard(),
lockDelay: NewDelay(),
}
return s, nil