Sequential Delete

This commit is contained in:
Abhishek Shroff
2024-08-07 00:20:07 +05:30
parent d8a2a19536
commit d7b9712f13
7 changed files with 48 additions and 34 deletions

View File

@@ -231,7 +231,7 @@ func (f filesystem) DeleteRecursive(r Resource, hardDelete bool) error {
if err != nil {
return err
}
errors := f.cs.Delete(deleted)
errors := f.cs.DeleteAll(deleted)
for err := range errors {
logrus.Warn(err)
}

View File

@@ -82,27 +82,22 @@ WITH RECURSIVE nodes(id, parent) AS (
)
DELETE FROM resources
WHERE id in (SELECT id FROM nodes)
RETURNING id, dir
RETURNING id
`
type HardDeleteRecursiveRow struct {
ID uuid.UUID
Dir bool
}
func (q *Queries) HardDeleteRecursive(ctx context.Context, id uuid.UUID) ([]HardDeleteRecursiveRow, error) {
func (q *Queries) HardDeleteRecursive(ctx context.Context, id uuid.UUID) ([]uuid.UUID, error) {
rows, err := q.db.Query(ctx, hardDeleteRecursive, id)
if err != nil {
return nil, err
}
defer rows.Close()
var items []HardDeleteRecursiveRow
var items []uuid.UUID
for rows.Next() {
var i HardDeleteRecursiveRow
if err := rows.Scan(&i.ID, &i.Dir); err != nil {
var id uuid.UUID
if err := rows.Scan(&id); err != nil {
return nil, err
}
items = append(items, i)
items = append(items, id)
}
if err := rows.Err(); err != nil {
return nil, err

View File

@@ -6,11 +6,11 @@ import (
"errors"
"hash"
"io"
"io/fs"
"os"
"path/filepath"
"github.com/google/uuid"
"github.com/shroff/phylum/server/internal/db"
)
type localStorage struct {
@@ -59,14 +59,16 @@ func (l localStorage) OpenWrite(id uuid.UUID, callback func(int, string) error)
}}, nil
}
func (l localStorage) Delete(rows []db.HardDeleteRecursiveRow) []error {
func (l localStorage) Delete(id uuid.UUID) error {
return os.Remove(l.path(id))
}
func (l localStorage) DeleteAll(ids uuid.UUIDs) []error {
errs := make([]error, 0)
for _, row := range rows {
if !row.Dir {
err := os.Remove(l.path(row.ID))
if err != nil {
errs = append(errs, err)
}
for _, id := range ids {
err := l.Delete(id)
if err != nil && !errors.Is(err, fs.ErrNotExist) {
errs = append(errs, err)
}
}
return errs

View File

@@ -14,7 +14,6 @@ import (
"github.com/google/uuid"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/shroff/phylum/server/internal/db"
)
type minioStorage struct {
@@ -90,15 +89,17 @@ func (s minioStorage) OpenWrite(id uuid.UUID, callback func(int, string) error)
}
func (s minioStorage) Delete(rows []db.HardDeleteRecursiveRow) []error {
func (s minioStorage) Delete(id uuid.UUID) error {
return s.client.RemoveObject(context.Background(), s.bucketName, s.prefix+id.String(), minio.RemoveObjectOptions{})
}
func (s minioStorage) DeleteAll(ids uuid.UUIDs) []error {
objs := make(chan minio.ObjectInfo)
go func() {
defer close(objs)
for _, row := range rows {
if !row.Dir {
objs <- minio.ObjectInfo{
Key: s.prefix + row.ID.String(),
}
for _, id := range ids {
objs <- minio.ObjectInfo{
Key: s.prefix + id.String(),
}
}
}()

View File

@@ -14,7 +14,8 @@ type Storage interface {
ListBackends() map[string]Backend
OpenRead(id uuid.UUID, start, length int64) (io.ReadCloser, error)
OpenWrite(id uuid.UUID, callback func(int, string) error) (io.WriteCloser, error)
Delete(rows []db.HardDeleteRecursiveRow) []error
Delete(id uuid.UUID) error
DeleteAll(ids uuid.UUIDs) []error
}
type storage struct {
@@ -49,10 +50,25 @@ func (s storage) OpenWrite(id uuid.UUID, callback func(int, string) error) (io.W
return backend.OpenWrite(id, callback)
}
}
func (s storage) Delete(rows []db.HardDeleteRecursiveRow) []error {
// TODO: implement
return nil
func (s storage) Delete(id uuid.UUID) error {
if backend, err := s.findStorageBackend(id); err != nil {
return err
} else {
return backend.Delete(id)
}
}
func (s storage) DeleteAll(ids uuid.UUIDs) []error {
// TODO: Batch delete
errs := make([]error, 1)
for _, id := range ids {
err := s.Delete(id)
if err != nil {
errs = append(errs, err)
}
}
return errs
}
func (s *storage) findStorageBackend(id uuid.UUID) (Backend, error) {

View File

@@ -4,13 +4,13 @@ import (
"io"
"github.com/google/uuid"
"github.com/shroff/phylum/server/internal/db"
)
type Backend interface {
Name() string
OpenRead(id uuid.UUID, start, length int64) (io.ReadCloser, error)
OpenWrite(id uuid.UUID, callback func(int, string) error) (io.WriteCloser, error)
Delete(rows []db.HardDeleteRecursiveRow) []error
Delete(id uuid.UUID) error
DeleteAll(ids uuid.UUIDs) []error
String() string
}

View File

@@ -69,4 +69,4 @@ WITH RECURSIVE nodes(id, parent) AS (
)
DELETE FROM resources
WHERE id in (SELECT id FROM nodes)
RETURNING id, dir;
RETURNING id;