-
Notifications
You must be signed in to change notification settings - Fork 8
/
dependent.go
96 lines (77 loc) · 2.04 KB
/
dependent.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
// Copyright 2014 Gyepi Sam. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package redux
// Dependent is the inverse of Prerequisite
type Dependent struct {
Path string
}
func (d Dependent) File(dir string) (*File, error) {
f, err := NewFile(dir, d.Path)
if err != nil {
return nil, err
}
return f, nil
}
func (p Prerequisite) File(dir string) (*File, error) {
f, err := NewFile(dir, p.Path)
if err != nil {
return nil, err
}
return f, nil
}
func (f *File) DependentFiles(prefix string) ([]*File, error) {
data, err := f.db.GetValues(prefix)
if err != nil {
return nil, err
}
files := make([]*File, len(data))
for i, b := range data {
if dep, err := decodeDependent(b); err != nil {
return nil, err
} else if item, err := dep.File(f.RootDir); err != nil {
return nil, err
} else {
files[i] = item
}
}
return files, nil
}
func (f *File) AllDependents() ([]*File, error) {
return f.DependentFiles(f.makeKey(SATISFIES))
}
func (f *File) EventDependents(event Event) ([]*File, error) {
return f.DependentFiles(f.makeKey(SATISFIES, event))
}
func (f *File) DeleteAllDependencies() (err error) {
keys, err := f.db.GetKeys(f.makeKey(SATISFIES))
if err != nil {
return err
}
for _, key := range keys {
if err := f.Delete(key); err != nil {
return err
}
}
return nil
}
func (f *File) DeleteDependency(event Event, hash Hash) error {
return f.Delete(f.makeKey(SATISFIES, event, hash))
}
func (f *File) PutDependency(event Event, hash Hash, dep Dependent) error {
return f.Put(f.makeKey(SATISFIES, event, hash), dep)
}
// NotifyDependents flags dependents as out of date because target has been created, modified, or deleted.
func (f *File) NotifyDependents(event Event) (err error) {
dependents, err := f.EventDependents(event)
if err != nil {
return err
}
for _, dependent := range dependents {
if err := dependent.PutMustRebuild(); err != nil {
return err
}
f.Debug("@Notify %s -> %s\n", event, dependent.Path)
}
return nil
}