-
Notifications
You must be signed in to change notification settings - Fork 360
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/db read entry - read entry/s using simple union sql, instead …
…of the complex views/sqEntriesLineage (#783)
- Loading branch information
Showing
4 changed files
with
76 additions
and
12 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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package catalog | ||
|
||
import ( | ||
"strconv" | ||
|
||
sq "github.com/Masterminds/squirrel" | ||
"github.com/treeverse/lakefs/db" | ||
) | ||
|
||
// sqEntryLineageSelect select path/s from a branch, including lineage | ||
// 1. Union all the branches in the lineage. | ||
// 2. if multiple branches have this path - select the one closest to the requested branch | ||
// 3. filterDeleted param = true will remove results that were deleted | ||
func sqEntryLineageSelect(tx db.Tx, branchID int64, commitID CommitID, filterDeleted bool, paths []string) (sq.SelectBuilder, error) { | ||
lineage, err := getLineage(tx, branchID, commitID) | ||
if err != nil { | ||
return sq.SelectBuilder{}, err | ||
} | ||
unionSelect := sqEntryBranchSelect(branchID, commitID, paths).Column("? as lineage_order", "0"). | ||
Prefix("(").Suffix(")") | ||
for i, branch := range lineage { | ||
unionSelect = unionSelect.SuffixExpr(sq.ConcatExpr( | ||
" UNION ALL (", | ||
sqEntryBranchSelect(branch.BranchID, branch.CommitID, paths).Column("? as lineage_order", strconv.Itoa(i+1)), | ||
")")) | ||
} | ||
distinctSelect := sq.Select("*"). | ||
FromSelect(unionSelect, "c"). | ||
Distinct().Options("ON (path)"). | ||
OrderBy("path", "lineage_order") | ||
finalSelect := sq.Select("path", "physical_address", "creation_date", "size", "checksum", "metadata", "is_expired"). | ||
FromSelect(distinctSelect, "t") | ||
if filterDeleted { | ||
finalSelect = finalSelect.Where("max_commit = ?", MaxCommitID) | ||
} | ||
return finalSelect, nil | ||
} | ||
|
||
// sqEntryBranchSelect select path/s from a single branch. | ||
// 1. Get the requested commit | ||
// 2. If a path has multiple versions in various commits - Return the row with highest min commit | ||
// 3. If the version was deleted after the requested commit - the row max-commit will be set to uncommitted | ||
func sqEntryBranchSelect(branchID int64, commitID CommitID, paths []string) sq.SelectBuilder { | ||
rawSelect := sq.Select("path", "physical_address", "creation_date", "size", "checksum", "metadata", "is_expired"). | ||
Distinct().Options("ON (branch_id,path)"). | ||
From("catalog_entries"). | ||
Where("branch_id = ?", branchID). | ||
OrderBy("branch_id", "path", "min_commit desc") | ||
l := len(paths) | ||
// the case of single path is handled differently than multiple pathes because my tests showed there is a significant | ||
// difference between (about 25%) between "where path = X" and "where path in (X)". even though the optimization is | ||
// the same. may be a driver issue in the client application | ||
if l == 1 { | ||
rawSelect = rawSelect.Where("path = ?", paths[0]) | ||
} else { | ||
rawSelect = rawSelect.Where(sq.Eq{"path": paths}) | ||
} | ||
switch commitID { | ||
case CommittedID: | ||
rawSelect = rawSelect.Where("min_commit < ?", MaxCommitID). | ||
Column("max_commit") | ||
case UncommittedID: | ||
rawSelect = rawSelect.Column("max_commit") | ||
default: | ||
rawSelect = rawSelect.Where("min_commit between 1 and ?", commitID). | ||
Column("CASE WHEN max_commit >= ? THEN ? ELSE max_commit END AS max_commit", commitID, MaxCommitID) | ||
} | ||
return rawSelect | ||
} |
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