Update namespace empty check to use buckets
Directly get and check whether a bucket is empty. This prevents unnecessarily loading all records of the buckets into memory just to check existence. Also added checks for content and snapshots. Signed-off-by: Derek McGowan <derek@mcgstyle.net>
This commit is contained in:
parent
002c0e2901
commit
5a54862ae5
@ -106,13 +106,8 @@ func imagesBucketPath(namespace string) [][]byte {
|
||||
return [][]byte{bucketKeyVersion, []byte(namespace), bucketKeyObjectImages}
|
||||
}
|
||||
|
||||
func withImagesBucket(tx *bolt.Tx, namespace string, fn func(bkt *bolt.Bucket) error) error {
|
||||
bkt, err := createBucketIfNotExists(tx, imagesBucketPath(namespace)...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return fn(bkt)
|
||||
func createImagesBucket(tx *bolt.Tx, namespace string) (*bolt.Bucket, error) {
|
||||
return createBucketIfNotExists(tx, imagesBucketPath(namespace)...)
|
||||
}
|
||||
|
||||
func getImagesBucket(tx *bolt.Tx, namespace string) *bolt.Bucket {
|
||||
@ -143,6 +138,10 @@ func createSnapshotterBucket(tx *bolt.Tx, namespace, snapshotter string) (*bolt.
|
||||
return bkt, nil
|
||||
}
|
||||
|
||||
func getSnapshottersBucket(tx *bolt.Tx, namespace string) *bolt.Bucket {
|
||||
return getBucket(tx, bucketKeyVersion, []byte(namespace), bucketKeyObjectSnapshots)
|
||||
}
|
||||
|
||||
func getSnapshotterBucket(tx *bolt.Tx, namespace, snapshotter string) *bolt.Bucket {
|
||||
return getBucket(tx, bucketKeyVersion, []byte(namespace), bucketKeyObjectSnapshots, []byte(snapshotter))
|
||||
}
|
||||
|
@ -133,31 +133,38 @@ func (s *namespaceStore) Delete(ctx context.Context, namespace string) error {
|
||||
}
|
||||
|
||||
func (s *namespaceStore) namespaceEmpty(ctx context.Context, namespace string) (bool, error) {
|
||||
ctx = namespaces.WithNamespace(ctx, namespace)
|
||||
|
||||
// need to check the various object stores.
|
||||
|
||||
imageStore := NewImageStore(s.tx)
|
||||
images, err := imageStore.List(ctx)
|
||||
if err != nil {
|
||||
// Get all data buckets
|
||||
buckets := []*bolt.Bucket{
|
||||
getImagesBucket(s.tx, namespace),
|
||||
getBlobsBucket(s.tx, namespace),
|
||||
getContainersBucket(s.tx, namespace),
|
||||
}
|
||||
if snbkt := getSnapshottersBucket(s.tx, namespace); snbkt != nil {
|
||||
if err := snbkt.ForEach(func(k, v []byte) error {
|
||||
if v == nil {
|
||||
buckets = append(buckets, snbkt.Bucket(k))
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if len(images) > 0 {
|
||||
}
|
||||
|
||||
// Ensure data buckets are empty
|
||||
for _, bkt := range buckets {
|
||||
if !isBucketEmpty(bkt) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
containerStore := NewContainerStore(s.tx)
|
||||
containers, err := containerStore.List(ctx)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if len(containers) > 0 {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// TODO(stevvooe): Need to add check for content store, as well. Still need
|
||||
// to make content store namespace aware.
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func isBucketEmpty(bkt *bolt.Bucket) bool {
|
||||
if bkt == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
k, _ := bkt.Cursor().First()
|
||||
return k == nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user