Skip to content

Commit

Permalink
st -> db.
Browse files Browse the repository at this point in the history
  • Loading branch information
ericsnowcurrently committed May 19, 2016
1 parent b92d019 commit 67425b1
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 34 deletions.
4 changes: 2 additions & 2 deletions component/all/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ func (payloads) registerState() {
return payloadstate.NewUnitPayloads(persist, unit, machine), nil
}

newEnvPayloads := func(persist state.PayloadsEnvPersistence) (state.EnvPayloads, error) {
envPersist := persistence.NewEnvPersistence(persist)
newEnvPayloads := func(db state.Persistence, persist state.PayloadsEnvPersistence) (state.EnvPayloads, error) {
envPersist := persistence.NewEnvPersistence(db, persist)
envPayloads := payloadstate.EnvPayloads{
Persist: envPersist,
}
Expand Down
30 changes: 13 additions & 17 deletions payload/persistence/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ import (
"github.com/juju/juju/payload"
)

// EnvPersistenceBase provides all the information needed to produce
// EnvPersistenceEntities provides all the information needed to produce
// a new EnvPersistence value.
type EnvPersistenceBase interface {
PersistenceBase

type EnvPersistenceEntities interface {
// Machines builds the list of the names that identify
// all machines in State.
Machines() ([]string, error)
Expand All @@ -33,41 +31,39 @@ type unitPersistence interface {
// EnvPersistence provides the persistence functionality for the
// Juju environment as a whole.
type EnvPersistence struct {
base EnvPersistenceBase
st EnvPersistenceEntities

newUnitPersist func(base PersistenceBase, name string) unitPersistence
newUnitPersist func(name string) unitPersistence
}

// NewEnvPersistence wraps the base in a new EnvPersistence.
func NewEnvPersistence(base EnvPersistenceBase) *EnvPersistence {
// NewEnvPersistence wraps the "db" in a new EnvPersistence.
func NewEnvPersistence(db PersistenceBase, st EnvPersistenceEntities) *EnvPersistence {
return &EnvPersistence{
base: base,
newUnitPersist: newUnitPersistence,
st: st,
newUnitPersist: func(name string) unitPersistence {
return NewPersistence(db, name)
},
}
}

func newUnitPersistence(base PersistenceBase, unit string) unitPersistence {
return NewPersistence(base, unit)
}

// ListAll returns the list of all payloads in the environment.
func (ep *EnvPersistence) ListAll() ([]payload.FullPayloadInfo, error) {
logger.Tracef("listing all payloads")

machines, err := ep.base.Machines()
machines, err := ep.st.Machines()
if err != nil {
return nil, errors.Trace(err)
}

var payloads []payload.FullPayloadInfo
for _, machine := range machines {
units, err := ep.base.MachineUnits(machine)
units, err := ep.st.MachineUnits(machine)
if err != nil {
return nil, errors.Trace(err)
}

for _, unit := range units {
persist := ep.newUnitPersist(ep.base, unit)
persist := ep.newUnitPersist(unit)

unitPayloads, err := listUnit(persist, unit, machine)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions payload/persistence/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (s *envPersistenceSuite) TestListAllOkay(c *gc.C) {
p2 := s.newPayload("eggs")
s.base.setPayloads(p1, p2)

persist := NewEnvPersistence(s.base)
persist := NewEnvPersistence(s.base, s.base)
persist.newUnitPersist = s.base.newUnitPersistence

payloads, err := persist.ListAll()
Expand All @@ -83,7 +83,7 @@ func (s *envPersistenceSuite) TestListAllOkay(c *gc.C) {
func (s *envPersistenceSuite) TestListAllEmpty(c *gc.C) {
s.base.setUnits("0")
s.base.setUnits("1", "a-service/0", "a-service/1")
persist := NewEnvPersistence(s.base)
persist := NewEnvPersistence(s.base, s.base)
persist.newUnitPersist = s.base.newUnitPersistence

payloads, err := persist.ListAll()
Expand All @@ -107,7 +107,7 @@ func (s *envPersistenceSuite) TestListAllFailed(c *gc.C) {
failure := errors.Errorf("<failed!>")
s.Stub.SetErrors(failure)

persist := NewEnvPersistence(s.base)
persist := NewEnvPersistence(s.base, s.base)
persist.newUnitPersist = s.base.newUnitPersistence

_, err := persist.ListAll()
Expand Down Expand Up @@ -190,8 +190,8 @@ func (s *stubEnvPersistenceBase) setUnits(machine string, units ...string) {
}
}

func (s *stubEnvPersistenceBase) newUnitPersistence(base PersistenceBase, unit string) unitPersistence {
s.stub.AddCall("newUnitPersistence", base, unit)
func (s *stubEnvPersistenceBase) newUnitPersistence(unit string) unitPersistence {
s.stub.AddCall("newUnitPersistence", unit)
s.stub.NextErr() // pop one off

persist, ok := s.unitPersists[unit]
Expand Down
2 changes: 1 addition & 1 deletion payload/persistence/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (pp Persistence) extractPayload(id string, payloadDocs map[string]payloadDo
}

func (pp Persistence) all(query bson.D, docs interface{}) error {
return errors.Trace(pp.st.All(payloadsC, query, docs))
return errors.Trace(pp.db.All(payloadsC, query, docs))
}

func (pp Persistence) allID(query bson.D, docs interface{}) error {
Expand Down
12 changes: 6 additions & 6 deletions payload/persistence/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ type PersistenceBase interface {
// Persistence exposes the high-level persistence functionality
// related to payloads in Juju.
type Persistence struct {
st PersistenceBase
db PersistenceBase
unit string
}

// NewPersistence builds a new Persistence based on the provided info.
func NewPersistence(st PersistenceBase, unit string) *Persistence {
func NewPersistence(db PersistenceBase, unit string) *Persistence {
return &Persistence{
st: st,
db: db,
unit: unit,
}
}
Expand Down Expand Up @@ -83,7 +83,7 @@ func (pp Persistence) Track(id string, pl payload.Payload) error {
}
return ops, nil
}
if err := pp.st.Run(buildTxn); err != nil {
if err := pp.db.Run(buildTxn); err != nil {
return errors.Trace(err)
}
return nil
Expand Down Expand Up @@ -112,7 +112,7 @@ func (pp Persistence) SetStatus(id, status string) error {
ops = append(ops, pp.newSetRawStatusOps(name, id, status)...)
return ops, nil
}
if err := pp.st.Run(buildTxn); err != nil {
if err := pp.db.Run(buildTxn); err != nil {
return errors.Trace(err)
}
return nil
Expand Down Expand Up @@ -204,7 +204,7 @@ func (pp Persistence) Untrack(id string) error {
ops = append(ops, pp.newRemovePayloadOps(name, id)...)
return ops, nil
}
if err := pp.st.Run(buildTxn); err != nil {
if err := pp.db.Run(buildTxn); err != nil {
return errors.Trace(err)
}
return nil
Expand Down
7 changes: 4 additions & 3 deletions state/payloads.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type PayloadsEnvPersistence interface {
MachineUnits(machineName string) ([]string, error)
}

type newEnvPayloadsFunc func(PayloadsEnvPersistence) (EnvPayloads, error)
type newEnvPayloadsFunc func(Persistence, PayloadsEnvPersistence) (EnvPayloads, error)
type newUnitPayloadsFunc func(persist Persistence, unit, machine string) (UnitPayloads, error)

// TODO(ericsnow) Merge the 2 vars
Expand All @@ -83,11 +83,12 @@ func (st *State) EnvPayloads() (EnvPayloads, error) {
return nil, errors.Errorf("payloads not supported")
}

db := st.newPersistence()
persist := &payloadsEnvPersistence{
Persistence: st.newPersistence(),
Persistence: db,
st: st,
}
envPayloads, err := newEnvPayloads(persist)
envPayloads, err := newEnvPayloads(db, persist)
if err != nil {
return nil, errors.Trace(err)
}
Expand Down

0 comments on commit 67425b1

Please sign in to comment.