Add WriteTxn interface and convert more functions to ReadTxn

We add a WriteTxn interface for use in updating the usage memdb table,
with the forward-looking prospect of incrementally converting other
functions to accept interfaces.

As well, we use the ReadTxn in new usage code, and as a side effect
convert a couple of existing functions to use that interface as well.
This commit is contained in:
Chris Piraino 2020-09-02 10:24:19 -05:00
parent 3feae7f77b
commit 69dbc926ad
3 changed files with 11 additions and 4 deletions

View File

@ -15,6 +15,13 @@ type ReadTxn interface {
Abort()
}
// WriteTxn is implemented by memdb.Txn to perform write operations.
type WriteTxn interface {
ReadTxn
Insert(table string, obj interface{}) error
Commit() error
}
// Changes wraps a memdb.Changes to include the index at which these changes
// were made.
type Changes struct {

View File

@ -37,7 +37,7 @@ type UsageEntry struct {
// updateUsage takes a set of memdb changes and computes a delta for specific
// usage metrics that we track.
func updateUsage(tx *txn, changes Changes) error {
func updateUsage(tx WriteTxn, changes Changes) error {
usageDeltas := make(map[string]int)
for _, change := range changes.Changes {
var delta int
@ -140,7 +140,7 @@ func (s *Store) ServiceUsage() (uint64, ServiceUsage, error) {
return 0, ServiceUsage{}, fmt.Errorf("failed services lookup: %s", err)
}
results, err := s.compileServiceUsage(tx, usage.Count)
results, err := compileServiceUsage(tx, usage.Count)
if err != nil {
return 0, ServiceUsage{}, fmt.Errorf("failed services lookup: %s", err)
}
@ -148,7 +148,7 @@ func (s *Store) ServiceUsage() (uint64, ServiceUsage, error) {
return usage.Index, results, nil
}
func firstUsageEntry(tx *txn, id string) (*UsageEntry, error) {
func firstUsageEntry(tx ReadTxn, id string) (*UsageEntry, error) {
usage, err := tx.First("usage", "id", id)
if err != nil {
return nil, err

View File

@ -12,7 +12,7 @@ type EnterpriseServiceUsage struct{}
func addEnterpriseUsage(map[string]int, memdb.Change) {}
func (s *Store) compileServiceUsage(tx *txn, totalInstances int) (ServiceUsage, error) {
func compileServiceUsage(tx ReadTxn, totalInstances int) (ServiceUsage, error) {
var totalServices int
results, err := tx.Get(
"index",