7
7
"github.com/icinga/icinga-go-library/database"
8
8
"github.com/icinga/icinga-go-library/types"
9
9
"github.com/icinga/icinga-notifications/internal/utils"
10
+ "github.com/jmoiron/sqlx"
10
11
"github.com/pkg/errors"
11
12
"golang.org/x/sync/errgroup"
12
13
"sync"
@@ -30,14 +31,34 @@ func DeleteFromCache(id types.Binary) {
30
31
//
31
32
// Returns an error on any database failure and panics when trying to cache an object that's already in the cache store.
32
33
func RestoreMutedObjects (ctx context.Context , db * database.DB ) error {
34
+ query := db .BuildSelectStmt (new (Object ), new (Object )) + " WHERE mute_reason IS NOT NULL " +
35
+ "AND NOT EXISTS((SELECT 1 FROM incident WHERE object_id = object.id AND recovered_at IS NULL))"
36
+ return restoreObjectsFromQuery (ctx , db , query )
37
+ }
38
+
39
+ // RestoreObjects restores all objects and their (extra)tags matching the given IDs from the database.
40
+ // Returns error on any database failures and panics when trying to cache an object that's already in the cache store.
41
+ func RestoreObjects (ctx context.Context , db * database.DB , ids []types.Binary ) error {
42
+ var obj * Object
43
+ query , args , err := sqlx .In (db .BuildSelectStmt (obj , obj )+ " WHERE id IN (?)" , ids )
44
+ if err != nil {
45
+ return errors .Wrapf (err , "cannot build placeholders for %q" , query )
46
+ }
47
+
48
+ return restoreObjectsFromQuery (ctx , db , query , args ... )
49
+ }
50
+
51
+ // restoreObjectsFromQuery takes a query that returns rows of the object table, executes it and loads the returned
52
+ // objects into the local cache.
53
+ //
54
+ // Returns an error on any database failure and panics when trying to cache an object that's already in the cache store.
55
+ func restoreObjectsFromQuery (ctx context.Context , db * database.DB , query string , args ... any ) error {
33
56
objects := make (chan * Object )
34
57
g , ctx := errgroup .WithContext (ctx )
35
58
g .Go (func () error {
36
59
defer close (objects )
37
60
38
- clause := `WHERE mute_reason IS NOT NULL AND NOT EXISTS((SELECT 1 FROM incident WHERE object_id = object.id AND recovered_at IS NULL))`
39
- query := fmt .Sprintf ("%s %s" , db .BuildSelectStmt (new (Object ), new (Object )), clause )
40
- err := utils .ExecAndApply [Object ](ctx , db , query , nil , func (o * Object ) {
61
+ err := utils .ExecAndApply [Object ](ctx , db , query , args , func (o * Object ) {
41
62
o .db = db
42
63
o .Tags = map [string ]string {}
43
64
o .ExtraTags = map [string ]string {}
@@ -64,8 +85,8 @@ func RestoreMutedObjects(ctx context.Context, db *database.DB) error {
64
85
}
65
86
66
87
g .Go (func () error {
67
- var ids []types.Binary
68
- objectsMap := map [string ]* Object {}
88
+ ids := make ( []types.Binary , 0 , len ( bulk ))
89
+ objectsMap := make ( map [string ]* Object , len ( bulk ))
69
90
for _ , obj := range bulk {
70
91
objectsMap [obj .ID .String ()] = obj
71
92
ids = append (ids , obj .ID )
@@ -87,7 +108,16 @@ func RestoreMutedObjects(ctx context.Context, db *database.DB) error {
87
108
return errors .Wrap (err , "cannot restore objects extra tags" )
88
109
}
89
110
90
- addObjectsToCache (objectsMap )
111
+ cacheMu .Lock ()
112
+ defer cacheMu .Unlock ()
113
+
114
+ for _ , o := range objectsMap {
115
+ if obj , ok := cache [o .ID .String ()]; ok {
116
+ panic (fmt .Sprintf ("Object %q is already in the cache" , obj .DisplayName ()))
117
+ }
118
+
119
+ cache [o .ID .String ()] = o
120
+ }
91
121
92
122
return nil
93
123
})
@@ -97,18 +127,3 @@ func RestoreMutedObjects(ctx context.Context, db *database.DB) error {
97
127
98
128
return g .Wait ()
99
129
}
100
-
101
- // addObjectsToCache adds the objects from the given map to the global object cache store.
102
- // Panics when trying to cache an object that's already in the cache store.
103
- func addObjectsToCache (objects map [string ]* Object ) {
104
- cacheMu .Lock ()
105
- defer cacheMu .Unlock ()
106
-
107
- for _ , o := range objects {
108
- if obj , ok := cache [o .ID .String ()]; ok {
109
- panic (fmt .Sprintf ("Object %q is already in the cache" , obj .DisplayName ()))
110
- }
111
-
112
- cache [o .ID .String ()] = o
113
- }
114
- }
0 commit comments