1package dag
2
3import (
4 "fmt"
5
6 "github.com/MichaelMure/git-bug/identity"
7 "github.com/MichaelMure/git-bug/repository"
8)
9
10// ClockLoader is the repository.ClockLoader for Entity
11func ClockLoader(defs ...Definition) repository.ClockLoader {
12 clocks := make([]string, len(defs)*2)
13 for _, def := range defs {
14 clocks = append(clocks, fmt.Sprintf(creationClockPattern, def.namespace))
15 clocks = append(clocks, fmt.Sprintf(editClockPattern, def.namespace))
16 }
17
18 return repository.ClockLoader{
19 Clocks: clocks,
20 Witnesser: func(repo repository.ClockedRepo) error {
21 // We don't care about the actual identity so an IdentityStub will do
22 resolver := identity.NewStubResolver()
23
24 for _, def := range defs {
25 // override the resolver
26 def := def
27 def.identityResolver = resolver
28
29 // we actually just need to read all entities,
30 // as that will create and update the clocks
31 // TODO: concurrent loading to be faster?
32 for b := range ReadAll(def, repo) {
33 if b.Err != nil {
34 return b.Err
35 }
36 }
37 }
38 return nil
39 },
40 }
41}