Skip to content

Commit

Permalink
Add offset,limit to objects, blobs stuff
Browse files Browse the repository at this point in the history
Signed-off-by: Igor Shishkin <me@teran.dev>
  • Loading branch information
teran committed Jul 7, 2024
1 parent e917206 commit 0d523d8
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 5 deletions.
2 changes: 1 addition & 1 deletion repositories/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Repository interface {
MarkVersionPublished(ctx context.Context, container, version string) error

CreateObject(ctx context.Context, container, version, key, casKey string) error
ListObjects(ctx context.Context, container, version, key string) ([]string, error)
ListObjects(ctx context.Context, container, version string, offset, limit uint64) ([]string, error)
DeleteObject(ctx context.Context, container, version, key string) error
RemapObject(ctx context.Context, container, version, key, newCASKey string) error

Expand Down
97 changes: 93 additions & 4 deletions repositories/metadata/postgresql/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (r *repository) CreateObject(ctx context.Context, container, version, key,
return nil
}

func (r *repository) ListObjects(ctx context.Context, container, version, key string) ([]string, error) {
func (r *repository) ListObjects(ctx context.Context, container, version string, offset, limit uint64) ([]string, error) {
row := psql.
Select("v.id").
From("versions v").
Expand All @@ -85,12 +85,14 @@ func (r *repository) ListObjects(ctx context.Context, container, version, key st
}

rows, err := psql.
Select("name").
Select("key").
From("objects").
Where(sq.Eq{
"version_id": versionID,
}).
OrderBy("id").
Offset(offset).
Limit(limit).
RunWith(r.db).
QueryContext(ctx)
if err != nil {
Expand All @@ -112,9 +114,96 @@ func (r *repository) ListObjects(ctx context.Context, container, version, key st
}

func (r *repository) DeleteObject(ctx context.Context, container, version, key string) error {
panic("not implemented")
tx, err := r.db.BeginTx(ctx, nil)
if err != nil {
return errors.Wrap(err, "error beginning transaction")
}
defer tx.Rollback()

row := psql.
Select("v.id").
From("versions v").
Join("containers c ON v.container_id = c.id").
Where(sq.Eq{
"c.name": container,
"v.name": version,
}).
RunWith(tx).
QueryRowContext(ctx)

var versionID uint
if err := row.Scan(&versionID); err != nil {
return errors.Wrap(err, "error looking up version")
}

_, err = psql.
Delete("objects").
Where(sq.Eq{
"version_id": versionID,
"key": key,
}).
RunWith(tx).
ExecContext(ctx)
if err != nil {
return errors.Wrap(err, "error executing SQL query")
}

if err := tx.Commit(); err != nil {
return errors.Wrap(err, "error committing transaction")
}
return nil
}

func (r *repository) RemapObject(ctx context.Context, container, version, key, newCASKey string) error {
panic("not implemented")
tx, err := r.db.BeginTx(ctx, nil)
if err != nil {
return errors.Wrap(err, "error beginning transaction")
}
defer tx.Rollback()

row := psql.
Select("v.id").
From("versions v").
Join("containers c ON v.container_id = c.id").
Where(sq.Eq{
"c.name": container,
"v.name": version,
}).
RunWith(tx).
QueryRowContext(ctx)

var versionID uint
if err := row.Scan(&versionID); err != nil {
return errors.Wrap(err, "error looking up version")
}

row = psql.
Select("id").
From("blobs").
Where(sq.Eq{"checksum": newCASKey}).
RunWith(tx).
QueryRowContext(ctx)

var blobID uint
if err := row.Scan(&blobID); err != nil {
return errors.Wrap(err, "error looking up blob")
}

_, err = psql.
Update("objects").
Set("blob_id", blobID).
Where(sq.Eq{
"version_id": versionID,
"key": key,
}).
RunWith(tx).
ExecContext(ctx)
if err != nil {
return errors.Wrap(err, "error executing SQL query")
}

if err := tx.Commit(); err != nil {
return errors.Wrap(err, "error committing transaction")
}
return nil
}
17 changes: 17 additions & 0 deletions repositories/metadata/postgresql/objects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,21 @@ func (s *postgreSQLRepositoryTestSuite) TestObjects() {

err = s.repo.CreateObject(s.ctx, containerName, versionID, "data/some-key.txt", "deadbeef")
s.Require().NoError(err)

objects, err := s.repo.ListObjects(s.ctx, containerName, versionID, 0, 100)
s.Require().NoError(err)
s.Require().Equal([]string{"data/some-key.txt"}, objects)

err = s.repo.CreateBLOB(s.ctx, "deadbeef2", 10, "text/plain")
s.Require().NoError(err)

err = s.repo.RemapObject(s.ctx, containerName, versionID, "data/some-key.txt", "deadbeef2")
s.Require().NoError(err)

err = s.repo.DeleteObject(s.ctx, containerName, versionID, "data/some-key.txt")
s.Require().NoError(err)

objects, err = s.repo.ListObjects(s.ctx, containerName, versionID, 0, 100)
s.Require().NoError(err)
s.Require().Equal([]string{}, objects)
}

0 comments on commit 0d523d8

Please sign in to comment.