From 15aea42b96df015fcf03e3f585559a280f5d526d Mon Sep 17 00:00:00 2001 From: Neeraj Gupta <254676+ua741@users.noreply.github.com> Date: Mon, 24 Feb 2025 14:04:39 +0530 Subject: [PATCH] [server] Minor refactor --- server/pkg/repo/collection.go | 115 -------------------------- server/pkg/repo/collection_files.go | 123 ++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+), 115 deletions(-) create mode 100644 server/pkg/repo/collection_files.go diff --git a/server/pkg/repo/collection.go b/server/pkg/repo/collection.go index c1c75b963e..4843a16b01 100644 --- a/server/pkg/repo/collection.go +++ b/server/pkg/repo/collection.go @@ -340,85 +340,6 @@ func (repo *CollectionRepository) GetAllSharedCollections(ctx context.Context, u return result, nil } -// DoesFileExistInCollections returns true if the file exists in one of the -// provided collections -func (repo *CollectionRepository) DoesFileExistInCollections(fileID int64, cIDs []int64) (bool, error) { - var exists bool - err := repo.DB.QueryRow(`SELECT EXISTS (SELECT 1 FROM collection_files WHERE file_id = $1 AND is_deleted = $2 AND collection_id = ANY ($3))`, - fileID, false, pq.Array(cIDs)).Scan(&exists) - return exists, stacktrace.Propagate(err, "") -} - -func (repo *CollectionRepository) DoAllFilesExistInGivenCollections(fileIDs []int64, cIDs []int64) error { - // Query to get all distinct file_ids that exist in the collections - rows, err := repo.DB.Query(` - SELECT DISTINCT file_id - FROM collection_files - WHERE file_id = ANY ($1) - AND is_deleted = false - AND collection_id = ANY ($2)`, - pq.Array(fileIDs), pq.Array(cIDs)) - - if err != nil { - return stacktrace.Propagate(err, "") - } - defer rows.Close() - - // Create a map of input fileIDs for easy lookup - fileIDMap := make(map[int64]bool) - for _, id := range fileIDs { - fileIDMap[id] = false // false means not found yet - } - // Mark files that were found - for rows.Next() { - var fileID int64 - if err := rows.Scan(&fileID); err != nil { - return stacktrace.Propagate(err, "") - } - fileIDMap[fileID] = true // mark as found - } - - if err = rows.Err(); err != nil { - return stacktrace.Propagate(err, "") - } - - // Collect missing files - var missingFiles []int64 - for id, found := range fileIDMap { - if !found { - missingFiles = append(missingFiles, id) - } - } - if len(missingFiles) > 0 { - return stacktrace.Propagate(fmt.Errorf("missing files %v", missingFiles), "") - } - return nil -} - -// VerifyAllFileIDsExistsInCollection returns error if the fileIDs don't exist in the collection -func (repo *CollectionRepository) VerifyAllFileIDsExistsInCollection(ctx context.Context, cID int64, fileIDs []int64) error { - fileIdMap := make(map[int64]bool) - rows, err := repo.DB.QueryContext(ctx, `SELECT file_id FROM collection_files WHERE collection_id = $1 AND is_deleted = $2 AND file_id = ANY ($3)`, - cID, false, pq.Array(fileIDs)) - if err != nil { - return stacktrace.Propagate(err, "") - } - for rows.Next() { - var fileID int64 - if err := rows.Scan(&fileID); err != nil { - return stacktrace.Propagate(err, "") - } - fileIdMap[fileID] = true - } - // find fileIds that are not present in the collection - for _, fileID := range fileIDs { - if _, ok := fileIdMap[fileID]; !ok { - return stacktrace.Propagate(fmt.Errorf("fileID %d not found in collection %d", fileID, cID), "") - } - } - return nil -} - // GetCollectionShareeRole returns true if the collection is shared with the user func (repo *CollectionRepository) GetCollectionShareeRole(cID int64, userID int64) (*ente.CollectionParticipantRole, error) { var role *ente.CollectionParticipantRole @@ -434,17 +355,6 @@ func (repo *CollectionRepository) GetOwnerID(collectionID int64) (int64, error) return ownerID, stacktrace.Propagate(err, "failed to get collection owner") } -// GetCollectionsFilesCount returns the number of non-deleted files which are present in the given collection -func (repo *CollectionRepository) GetCollectionsFilesCount(collectionID int64) (int64, error) { - row := repo.DB.QueryRow(`SELECT count(*) FROM collection_files WHERE collection_id=$1 AND is_deleted = false`, collectionID) - var count int64 = 0 - err := row.Scan(&count) - if err != nil { - return -1, stacktrace.Propagate(err, "") - } - return count, nil -} - // Share shares a collection with a userID func (repo *CollectionRepository) Share( collectionID int64, @@ -820,21 +730,6 @@ func (repo *CollectionRepository) GetSharees(cID int64) ([]ente.CollectionUser, return users, nil } -// GetCollectionFileIDs return list of fileIDs are currently present in the given collection -// and fileIDs are owned by the collection owner -func (repo *CollectionRepository) GetCollectionFileIDs(collectionID int64, collectionOwnerID int64) ([]int64, error) { - // Collaboration Todo: Filter out files which are not owned by the collection owner - rows, err := repo.DB.Query( - `SELECT file_id - FROM collection_files - WHERE is_deleted=false - AND collection_id =$1 AND (f_owner_id is null or f_owner_id = $2)`, collectionID, collectionOwnerID) - if err != nil { - return make([]int64, 0), stacktrace.Propagate(err, "") - } - return convertRowsToFileId(rows) -} - func convertRowsToFileId(rows *sql.Rows) ([]int64, error) { fileIDs := make([]int64, 0) defer rows.Close() @@ -1015,13 +910,3 @@ func (repo *CollectionRepository) GetSharedCollectionsCount(userID int64) (int64 } return count, nil } - -func (repo *CollectionRepository) GetCollectionCount(fileID int64) (int64, error) { - row := repo.DB.QueryRow(`SELECT count(*) FROM collection_files WHERE file_id = $1 and is_deleted = false`, fileID) - var count int64 = 0 - err := row.Scan(&count) - if err != nil { - return -1, stacktrace.Propagate(err, "") - } - return count, nil -} diff --git a/server/pkg/repo/collection_files.go b/server/pkg/repo/collection_files.go new file mode 100644 index 0000000000..9bab3720cf --- /dev/null +++ b/server/pkg/repo/collection_files.go @@ -0,0 +1,123 @@ +package repo + +import ( + "context" + "fmt" + "github.com/ente-io/stacktrace" + "github.com/lib/pq" +) + +// GetCollectionFileIDs return list of fileIDs are currently present in the given collection +// and fileIDs are owned by the collection owner +func (repo *CollectionRepository) GetCollectionFileIDs(collectionID int64, collectionOwnerID int64) ([]int64, error) { + // Collaboration Todo: Filter out files which are not owned by the collection owner + rows, err := repo.DB.Query( + `SELECT file_id + FROM collection_files + WHERE is_deleted=false + AND collection_id =$1 AND (f_owner_id is null or f_owner_id = $2)`, collectionID, collectionOwnerID) + if err != nil { + return make([]int64, 0), stacktrace.Propagate(err, "") + } + return convertRowsToFileId(rows) +} + +// DoesFileExistInCollections returns true if the file exists in one of the +// provided collections +func (repo *CollectionRepository) DoesFileExistInCollections(fileID int64, cIDs []int64) (bool, error) { + var exists bool + err := repo.DB.QueryRow(`SELECT EXISTS (SELECT 1 FROM collection_files WHERE file_id = $1 AND is_deleted = $2 AND collection_id = ANY ($3))`, + fileID, false, pq.Array(cIDs)).Scan(&exists) + return exists, stacktrace.Propagate(err, "") +} + +func (repo *CollectionRepository) DoAllFilesExistInGivenCollections(fileIDs []int64, cIDs []int64) error { + // Query to get all distinct file_ids that exist in the collections + rows, err := repo.DB.Query(` + SELECT DISTINCT file_id + FROM collection_files + WHERE file_id = ANY ($1) + AND is_deleted = false + AND collection_id = ANY ($2)`, + pq.Array(fileIDs), pq.Array(cIDs)) + + if err != nil { + return stacktrace.Propagate(err, "") + } + defer rows.Close() + + // Create a map of input fileIDs for easy lookup + fileIDMap := make(map[int64]bool) + for _, id := range fileIDs { + fileIDMap[id] = false // false means not found yet + } + // Mark files that were found + for rows.Next() { + var fileID int64 + if err := rows.Scan(&fileID); err != nil { + return stacktrace.Propagate(err, "") + } + fileIDMap[fileID] = true // mark as found + } + + if err = rows.Err(); err != nil { + return stacktrace.Propagate(err, "") + } + + // Collect missing files + var missingFiles []int64 + for id, found := range fileIDMap { + if !found { + missingFiles = append(missingFiles, id) + } + } + if len(missingFiles) > 0 { + return stacktrace.Propagate(fmt.Errorf("missing files %v", missingFiles), "") + } + return nil +} + +// VerifyAllFileIDsExistsInCollection returns error if the fileIDs don't exist in the collection +func (repo *CollectionRepository) VerifyAllFileIDsExistsInCollection(ctx context.Context, cID int64, fileIDs []int64) error { + fileIdMap := make(map[int64]bool) + rows, err := repo.DB.QueryContext(ctx, `SELECT file_id FROM collection_files WHERE collection_id = $1 AND is_deleted = $2 AND file_id = ANY ($3)`, + cID, false, pq.Array(fileIDs)) + if err != nil { + return stacktrace.Propagate(err, "") + } + for rows.Next() { + var fileID int64 + if err := rows.Scan(&fileID); err != nil { + return stacktrace.Propagate(err, "") + } + fileIdMap[fileID] = true + } + // find fileIds that are not present in the collection + for _, fileID := range fileIDs { + if _, ok := fileIdMap[fileID]; !ok { + return stacktrace.Propagate(fmt.Errorf("fileID %d not found in collection %d", fileID, cID), "") + } + } + return nil +} + +// GetCollectionsFilesCount returns the number of non-deleted files which are present in the given collection +func (repo *CollectionRepository) GetCollectionsFilesCount(collectionID int64) (int64, error) { + row := repo.DB.QueryRow(`SELECT count(*) FROM collection_files WHERE collection_id=$1 AND is_deleted = false`, collectionID) + var count int64 = 0 + err := row.Scan(&count) + if err != nil { + return -1, stacktrace.Propagate(err, "") + } + return count, nil +} + +func (repo *CollectionRepository) GetCollectionCount(fileID int64) (int64, error) { + row := repo.DB.QueryRow(`SELECT count(*) FROM collection_files WHERE file_id = $1 and is_deleted = false`, fileID) + var count int64 = 0 + err := row.Scan(&count) + if err != nil { + return -1, stacktrace.Propagate(err, "") + } + return count, nil +}