Skip to content

Commit

Permalink
Use generics in readContainerStore
Browse files Browse the repository at this point in the history
... and use it in many more places.

Signed-off-by: Miloslav Trmač <[email protected]>
  • Loading branch information
mtrmac committed Apr 17, 2023
1 parent 1cd37e8 commit 74899ec
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 106 deletions.
8 changes: 4 additions & 4 deletions check.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,10 +570,10 @@ func (s *store) Check(options *CheckOptions) (CheckReport, error) {
}

// Iterate over each container in turn.
if done, err := s.readContainerStore(func() (bool, error) {
if _, err := readContainerStore(s, func() (void, error) {
containers, err := s.containerStore.Containers()
if err != nil {
return true, err
return void{}, err
}
for i := range containers {
container := containers[i]
Expand Down Expand Up @@ -622,8 +622,8 @@ func (s *store) Check(options *CheckOptions) (CheckReport, error) {
referencedLayers[container.LayerID] = true
}
}
return false, nil
}); done {
return void{}, nil
}); err != nil {
return CheckReport{}, err
}

Expand Down
171 changes: 69 additions & 102 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -1234,9 +1234,10 @@ func writeToImageStore[T any](s *store, fn func() (T, error)) (T, error) {
// readContainerStore is a convenience helper for working with store.containerStore:
// It locks the store for reading, checks for updates, and calls fn(), which can then access store.containerStore.
// It returns the return value of fn, or its own error initializing the store.
func (s *store) readContainerStore(fn func() (bool, error)) (bool, error) {
func readContainerStore[T any](s *store, fn func() (T, error)) (T, error) {
if err := s.containerStore.startReading(); err != nil {
return true, err
var zeroRes T // A zero value of T
return zeroRes, err
}
defer s.containerStore.stopReading()
return fn()
Expand Down Expand Up @@ -1827,19 +1828,12 @@ func (s *store) Metadata(id string) (string, error) {
return res, err
}

var res string
if done, err := s.readContainerStore(func() (bool, error) {
return readContainerStore(s, func() (string, error) {
if s.containerStore.Exists(id) {
var err error
res, err = s.containerStore.Metadata(id)
return true, err
return s.containerStore.Metadata(id)
}
return false, nil
}); done {
return res, err
}

return "", ErrNotAnID
return "", ErrNotAnID
})
}

func (s *store) ListImageBigData(id string) ([]string, error) {
Expand Down Expand Up @@ -2135,12 +2129,9 @@ func (s *store) ContainerSize(id string) (int64, error) {
}

func (s *store) ListContainerBigData(id string) ([]string, error) {
if err := s.containerStore.startReading(); err != nil {
return nil, err
}
defer s.containerStore.stopReading()

return s.containerStore.BigDataNames(id)
return readContainerStore(s, func() ([]string, error) {
return s.containerStore.BigDataNames(id)
})
}

func (s *store) ContainerBigDataSize(id, key string) (int64, error) {
Expand All @@ -2156,11 +2147,9 @@ func (s *store) ContainerBigDataDigest(id, key string) (digest.Digest, error) {
}

func (s *store) ContainerBigData(id, key string) ([]byte, error) {
if err := s.containerStore.startReading(); err != nil {
return nil, err
}
defer s.containerStore.stopReading()
return s.containerStore.BigData(id, key)
return readContainerStore(s, func() ([]byte, error) {
return s.containerStore.BigData(id, key)
})
}

func (s *store) SetContainerBigData(id, key string, data []byte) error {
Expand Down Expand Up @@ -2189,11 +2178,10 @@ func (s *store) Exists(id string) bool {
return res // false if err != nil
}

if err := s.containerStore.startReading(); err != nil {
return false
}
defer s.containerStore.stopReading()
return s.containerStore.Exists(id)
res, _ := readContainerStore(s, func() (bool, error) {
return s.containerStore.Exists(id), nil
})
return res // false if err != nil
}

func dedupeStrings(names []string) []string {
Expand Down Expand Up @@ -2326,14 +2314,12 @@ func (s *store) Names(id string) ([]string, error) {
return res, err
}

if err := s.containerStore.startReading(); err != nil {
return nil, err
}
defer s.containerStore.stopReading()
if c, err := s.containerStore.Get(id); c != nil && err == nil {
return c.Names, nil
}
return nil, ErrLayerUnknown
return readContainerStore(s, func() ([]string, error) {
if c, err := s.containerStore.Get(id); c != nil && err == nil {
return c.Names, nil
}
return nil, ErrLayerUnknown
})
}

func (s *store) Lookup(name string) (string, error) {
Expand All @@ -2355,15 +2341,12 @@ func (s *store) Lookup(name string) (string, error) {
return res, err
}

if err := s.containerStore.startReading(); err != nil {
return "", err
}
defer s.containerStore.stopReading()
if c, err := s.containerStore.Get(name); c != nil && err == nil {
return c.ID, nil
}

return "", ErrLayerUnknown
return readContainerStore(s, func() (string, error) {
if c, err := s.containerStore.Get(name); c != nil && err == nil {
return c.ID, nil
}
return "", ErrLayerUnknown
})
}

func (s *store) DeleteLayer(id string) error {
Expand Down Expand Up @@ -2967,12 +2950,9 @@ func (s *store) Images() ([]Image, error) {
}

func (s *store) Containers() ([]Container, error) {
if err := s.containerStore.startReading(); err != nil {
return nil, err
}
defer s.containerStore.stopReading()

return s.containerStore.Containers()
return readContainerStore(s, func() ([]Container, error) {
return s.containerStore.Containers()
})
}

func (s *store) Layer(id string) (*Layer, error) {
Expand Down Expand Up @@ -3138,36 +3118,29 @@ func (s *store) ImagesByDigest(d digest.Digest) ([]*Image, error) {
}

func (s *store) Container(id string) (*Container, error) {
if err := s.containerStore.startReading(); err != nil {
return nil, err
}
defer s.containerStore.stopReading()

return s.containerStore.Get(id)
return readContainerStore(s, func() (*Container, error) {
return s.containerStore.Get(id)
})
}

func (s *store) ContainerLayerID(id string) (string, error) {
if err := s.containerStore.startReading(); err != nil {
return "", err
}
defer s.containerStore.stopReading()
container, err := s.containerStore.Get(id)
if err != nil {
return "", err
}
return container.LayerID, nil
return readContainerStore(s, func() (string, error) {
container, err := s.containerStore.Get(id)
if err != nil {
return "", err
}
return container.LayerID, nil
})
}

func (s *store) ContainerByLayer(id string) (*Container, error) {
layer, err := s.Layer(id)
if err != nil {
return nil, err
}
if err := s.containerStore.startReading(); err != nil {
return nil, err
}
defer s.containerStore.stopReading()
containerList, err := s.containerStore.Containers()
containerList, err := readContainerStore(s, func() ([]Container, error) {
return s.containerStore.Containers()
})
if err != nil {
return nil, err
}
Expand All @@ -3181,41 +3154,35 @@ func (s *store) ContainerByLayer(id string) (*Container, error) {
}

func (s *store) ContainerDirectory(id string) (string, error) {
if err := s.containerStore.startReading(); err != nil {
return "", err
}
defer s.containerStore.stopReading()

id, err := s.containerStore.Lookup(id)
if err != nil {
return "", err
}
return readContainerStore(s, func() (string, error) {
id, err := s.containerStore.Lookup(id)
if err != nil {
return "", err
}

middleDir := s.graphDriverName + "-containers"
gcpath := filepath.Join(s.GraphRoot(), middleDir, id, "userdata")
if err := os.MkdirAll(gcpath, 0700); err != nil {
return "", err
}
return gcpath, nil
middleDir := s.graphDriverName + "-containers"
gcpath := filepath.Join(s.GraphRoot(), middleDir, id, "userdata")
if err := os.MkdirAll(gcpath, 0700); err != nil {
return "", err
}
return gcpath, nil
})
}

func (s *store) ContainerRunDirectory(id string) (string, error) {
if err := s.containerStore.startReading(); err != nil {
return "", err
}
defer s.containerStore.stopReading()

id, err := s.containerStore.Lookup(id)
if err != nil {
return "", err
}
return readContainerStore(s, func() (string, error) {
id, err := s.containerStore.Lookup(id)
if err != nil {
return "", err
}

middleDir := s.graphDriverName + "-containers"
rcpath := filepath.Join(s.RunRoot(), middleDir, id, "userdata")
if err := os.MkdirAll(rcpath, 0700); err != nil {
return "", err
}
return rcpath, nil
middleDir := s.graphDriverName + "-containers"
rcpath := filepath.Join(s.RunRoot(), middleDir, id, "userdata")
if err := os.MkdirAll(rcpath, 0700); err != nil {
return "", err
}
return rcpath, nil
})
}

func (s *store) SetContainerDirectoryFile(id, file string, data []byte) error {
Expand Down

0 comments on commit 74899ec

Please sign in to comment.