-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes 5125: Add support for resumable downloads
- Loading branch information
1 parent
98de418
commit 4f7120b
Showing
14 changed files
with
338 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
20241113084850 | ||
20241203143614 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
BEGIN; | ||
|
||
DROP TABLE IF EXISTS uploads; | ||
|
||
COMMIT; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
BEGIN; | ||
|
||
CREATE TABLE IF NOT EXISTS uploads ( | ||
upload_uuid TEXT NOT NULL PRIMARY KEY, | ||
created_at TIMESTAMP WITH TIME ZONE, | ||
org_id VARCHAR (255) NOT NULL, | ||
chunk_size int NOT NULL, | ||
sha256 TEXT NOT NULL, | ||
chunk_list TEXT[] default '{}' not null | ||
); | ||
|
||
CREATE INDEX IF NOT EXISTS index_orgid_chunksize_sha256 ON uploads(org_id,chunk_size,sha256); | ||
|
||
COMMIT; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package dao | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/content-services/content-sources-backend/pkg/models" | ||
"github.com/content-services/content-sources-backend/pkg/pulp_client" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type uploadDaoImpl struct { | ||
db *gorm.DB | ||
pulpClient pulp_client.PulpClient | ||
} | ||
|
||
func GetUploadDao(db *gorm.DB, pulpClient pulp_client.PulpClient) UploadDao { | ||
return &uploadDaoImpl{ | ||
db: db, | ||
pulpClient: pulpClient, | ||
} | ||
} | ||
|
||
func (t uploadDaoImpl) StoreFileUpload(ctx context.Context, orgID string, uploadUUID string, sha256 string, chunkSize int64) error { | ||
var upload models.Upload | ||
|
||
upload.OrgID = orgID | ||
upload.UploadUUID = uploadUUID | ||
upload.Sha256 = sha256 | ||
upload.ChunkSize = chunkSize | ||
|
||
upload.ChunkList = []string{} | ||
|
||
db := t.db.Model(models.Upload{}).WithContext(ctx).Create(&upload) | ||
if db.Error != nil { | ||
return db.Error | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (t uploadDaoImpl) GetExistingUploadIDAndCompletedChunks(ctx context.Context, orgID string, sha256 string, chunkSize int64) (string, []string, error) { | ||
db := t.db.Model(models.Upload{}).WithContext(ctx) | ||
|
||
var result models.Upload | ||
|
||
db.Where("org_id = ?", orgID).Where("chunk_size = ?", chunkSize).Where("sha256 = ?", sha256).First(&result) | ||
|
||
if db.Error != nil { | ||
return "", []string{}, db.Error | ||
} | ||
|
||
return result.UploadUUID, result.ChunkList, nil | ||
} | ||
|
||
func (t uploadDaoImpl) StoreChunkUpload(ctx context.Context, orgID string, uploadUUID string, sha256 string) error { | ||
db := t.db.Model(models.Upload{}). | ||
WithContext(ctx). | ||
Where("org_id = ?", orgID). | ||
Where("upload_uuid = ?", uploadUUID). | ||
Where("? != all(chunk_list)", sha256). | ||
Update("chunk_list", gorm.Expr(`array_append(chunk_list, ?)`, sha256)) | ||
|
||
if db.Error != nil { | ||
return db.Error | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package dao | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/content-services/content-sources-backend/pkg/pulp_client" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type UploadsSuite struct { | ||
*DaoSuite | ||
mockPulpClient *pulp_client.MockPulpClient | ||
} | ||
|
||
func TestUploadsSuite(t *testing.T) { | ||
m := DaoSuite{} | ||
r := UploadsSuite{DaoSuite: &m} | ||
suite.Run(t, &r) | ||
} | ||
|
||
func (s *UploadsSuite) uploadsDao() uploadDaoImpl { | ||
return uploadDaoImpl{ | ||
db: s.tx, | ||
pulpClient: s.mockPulpClient, | ||
} | ||
} | ||
func (s *UploadsSuite) SetupTest() { | ||
s.DaoSuite.SetupTest() | ||
} | ||
func (s *UploadsSuite) TestStoreFileUpload() { | ||
uploadDao := s.uploadsDao() | ||
ctx := context.Background() | ||
|
||
err := uploadDao.StoreFileUpload(ctx, "bananaOrg", "bananaUUID", "bananaHash256", 16000) | ||
|
||
assert.Equal(s.T(), err, nil) | ||
|
||
uploadUUID, chunkList, err := uploadDao.GetExistingUploadIDAndCompletedChunks(ctx, "bananaOrg", "bananaHash256", 16000) | ||
|
||
assert.Equal(s.T(), nil, err) | ||
assert.Equal(s.T(), "bananaUUID", uploadUUID) | ||
assert.Equal(s.T(), []string{}, chunkList) | ||
|
||
err = uploadDao.StoreChunkUpload(ctx, "bananaOrg", "bananaUUID", "bananaChunkHash256") | ||
|
||
assert.Equal(s.T(), nil, err) | ||
|
||
uploadUUID, chunkList, err = uploadDao.GetExistingUploadIDAndCompletedChunks(ctx, "bananaOrg", "bananaHash256", 16000) | ||
|
||
assert.Equal(s.T(), nil, err) | ||
assert.Equal(s.T(), "bananaUUID", uploadUUID) | ||
assert.Equal(s.T(), []string{"bananaChunkHash256"}, chunkList) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.