Skip to content

Commit

Permalink
Ensure a single connect per logical query (#224)
Browse files Browse the repository at this point in the history
* Ensure a single connect per logical query

When the claimstore executes multiple queries against the crud store to
execute a logical query, e.g. ReadInstallationStatus, ensure that it
only results in a single call to Connect.

I've updated the claim store tests to use our MockStore so that we can
assert on connects/closes. The MockStore was using nested maps that was
hard to understand so I refactored it so it's easier to understand.

Signed-off-by: Carolyn Van Slyck <me@carolynvanslyck.com>

* Fix setting MockStore count node name

Signed-off-by: Carolyn Van Slyck <me@carolynvanslyck.com>

* Alway sort in list operations

Signed-off-by: Carolyn Van Slyck <me@carolynvanslyck.com>

* Add missing newline

Signed-off-by: Carolyn Van Slyck <me@carolynvanslyck.com>
  • Loading branch information
carolynvs-msft authored Jul 6, 2020
1 parent 3fca591 commit a6382d9
Show file tree
Hide file tree
Showing 5 changed files with 257 additions and 130 deletions.
69 changes: 68 additions & 1 deletion claim/claimstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func (s Store) ListResults(claimID string) ([]string, error) {
return nil, err
}

sort.Strings(results)
return results, nil
}

Expand All @@ -125,11 +126,17 @@ func (s Store) ListOutputs(resultID string) ([]string, error) {
for i, fullName := range outputNames {
outputNames[i] = strings.TrimLeft(fullName, resultID+"-")
}

sort.Strings(outputNames)
return outputNames, nil
}

func (s Store) ReadInstallation(installation string) (Installation, error) {
handleClose, err := s.backingStore.HandleConnect()
defer handleClose()
if err != nil {
return Installation{}, err
}

claims, err := s.ReadAllClaims(installation)
if err != nil {
return Installation{}, err
Expand All @@ -153,6 +160,12 @@ func (s Store) ReadInstallation(installation string) (Installation, error) {
}

func (s Store) ReadInstallationStatus(installation string) (Installation, error) {
handleClose, err := s.backingStore.HandleConnect()
defer handleClose()
if err != nil {
return Installation{}, err
}

claimIds, err := s.ListClaims(installation)
if err != nil {
return Installation{}, err
Expand Down Expand Up @@ -191,6 +204,12 @@ func (s Store) ReadInstallationStatus(installation string) (Installation, error)
}

func (s Store) ReadAllInstallationStatus() ([]Installation, error) {
handleClose, err := s.backingStore.HandleConnect()
defer handleClose()
if err != nil {
return nil, err
}

names, err := s.ListInstallations()
if err != nil {
return nil, err
Expand Down Expand Up @@ -254,6 +273,12 @@ func (s Store) ReadAllClaims(installation string) ([]Claim, error) {
}

func (s Store) ReadLastClaim(installation string) (Claim, error) {
handleClose, err := s.backingStore.HandleConnect()
defer handleClose()
if err != nil {
return Claim{}, err
}

claimIds, err := s.backingStore.List(ItemTypeClaims, installation)
if err != nil {
return Claim{}, s.handleNotExistsError(err, ErrInstallationNotFound)
Expand Down Expand Up @@ -306,12 +331,24 @@ func (s Store) ReadAllResults(claimID string) ([]Result, error) {
// ReadLastOutputs returns the most recent (last) value of each output associated
// with the installation.
func (s Store) ReadLastOutputs(installation string) (Outputs, error) {
handleClose, err := s.backingStore.HandleConnect()
defer handleClose()
if err != nil {
return Outputs{}, err
}

return s.readLastOutputs(installation, "")
}

// ReadLastOutput returns the most recent value (last) of the specified Output associated
// with the installation.
func (s Store) ReadLastOutput(installation string, name string) (Output, error) {
handleClose, err := s.backingStore.HandleConnect()
defer handleClose()
if err != nil {
return Output{}, err
}

outputs, err := s.readLastOutputs(installation, name)
if err != nil {
return Output{}, err
Expand Down Expand Up @@ -380,6 +417,12 @@ func (s Store) readLastOutputs(installation string, filterOutput string) (Output
}

func (s Store) ReadLastResult(claimID string) (Result, error) {
handleClose, err := s.backingStore.HandleConnect()
defer handleClose()
if err != nil {
return Result{}, err
}

resultIDs, err := s.backingStore.List(ItemTypeResults, claimID)
if err != nil {
return Result{}, s.handleNotExistsError(err, ErrClaimNotFound)
Expand Down Expand Up @@ -417,6 +460,12 @@ func (s Store) ReadOutput(c Claim, r Result, outputName string) (Output, error)
}

func (s Store) SaveClaim(c Claim) error {
handleClose, err := s.backingStore.HandleConnect()
defer handleClose()
if err != nil {
return err
}

bytes, err := json.MarshalIndent(c, "", " ")
if err != nil {
return err
Expand Down Expand Up @@ -466,6 +515,12 @@ func (s Store) SaveOutput(o Output) error {
}

func (s Store) DeleteInstallation(installation string) error {
handleClose, err := s.backingStore.HandleConnect()
defer handleClose()
if err != nil {
return err
}

claimIds, err := s.ListClaims(installation)
if err != nil {
return err
Expand All @@ -483,6 +538,12 @@ func (s Store) DeleteInstallation(installation string) error {
}

func (s Store) DeleteClaim(claimID string) error {
handleClose, err := s.backingStore.HandleConnect()
defer handleClose()
if err != nil {
return err
}

resultIds, err := s.ListResults(claimID)
if err != nil {
return err
Expand All @@ -500,6 +561,12 @@ func (s Store) DeleteClaim(claimID string) error {
}

func (s Store) DeleteResult(resultID string) error {
handleClose, err := s.backingStore.HandleConnect()
defer handleClose()
if err != nil {
return err
}

outputNames, err := s.ListOutputs(resultID)
if err != nil {
return err
Expand Down
Loading

0 comments on commit a6382d9

Please sign in to comment.