/
persistence.go
117 lines (96 loc) · 3.51 KB
/
persistence.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state
import (
"github.com/juju/errors"
jujutxn "github.com/juju/txn"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/txn"
"github.com/juju/juju/state/storage"
)
// Persistence exposes persistence-layer functionality of State.
type Persistence interface {
// One populates doc with the document corresponding to the given
// ID. Missing documents result in errors.NotFound.
One(collName, id string, doc interface{}) error
// All populates docs with the list of the documents corresponding
// to the provided query.
All(collName string, query, docs interface{}) error
// Run runs the transaction generated by the provided factory
// function. It may be retried several times.
Run(transactions jujutxn.TransactionSource) error
// NewStorage returns a new blob storage for the environment.
NewStorage() storage.Storage
// ApplicationExistsOps returns the operations that verify that the
// identified application exists.
ApplicationExistsOps(applicationID string) []txn.Op
// IncCharmModifiedVersionOps returns the operations necessary to increment
// the CharmModifiedVersion field for the given application.
IncCharmModifiedVersionOps(applicationID string) []txn.Op
// NewCleanupOp creates a mgo transaction operation that queues up
// some cleanup action in state.
NewCleanupOp(kind, prefix string) txn.Op
}
type statePersistence struct {
st *State
}
// newPersistence builds a new StatePersistence that wraps State.
func (st *State) newPersistence() Persistence {
return &statePersistence{st: st}
}
// One gets the identified document from the collection.
func (sp statePersistence) One(collName, id string, doc interface{}) error {
coll, closeColl := sp.st.getCollection(collName)
defer closeColl()
err := coll.FindId(id).One(doc)
if err == mgo.ErrNotFound {
return errors.NotFoundf(id)
}
if err != nil {
return errors.Trace(err)
}
return nil
}
// All gets all documents from the collection matching the query.
func (sp statePersistence) All(collName string, query, docs interface{}) error {
coll, closeColl := sp.st.getCollection(collName)
defer closeColl()
if err := coll.Find(query).All(docs); err != nil {
return errors.Trace(err)
}
return nil
}
// Run runs the transaction produced by the provided factory function.
func (sp statePersistence) Run(transactions jujutxn.TransactionSource) error {
if err := sp.st.run(transactions); err != nil {
return errors.Trace(err)
}
return nil
}
// NewStorage returns a new blob storage for the environment.
func (sp *statePersistence) NewStorage() storage.Storage {
envUUID := sp.st.ModelUUID()
// TODO(ericsnow) Copy the session?
session := sp.st.session
store := storage.NewStorage(envUUID, session)
return store
}
// ApplicationExistsOps returns the operations that verify that the
// identified service exists.
func (sp *statePersistence) ApplicationExistsOps(applicationID string) []txn.Op {
return []txn.Op{{
C: applicationsC,
Id: applicationID,
Assert: isAliveDoc,
}}
}
// IncCharmModifiedVersionOps returns the operations necessary to increment the
// CharmModifiedVersion field for the given service.
func (sp *statePersistence) IncCharmModifiedVersionOps(applicationID string) []txn.Op {
return incCharmModifiedVersionOps(applicationID)
}
// NewCleanupOp creates a mgo transaction operation that queues up
// some cleanup action in state.
func (sp *statePersistence) NewCleanupOp(kind, prefix string) txn.Op {
return newCleanupOp(cleanupKind(kind), prefix)
}