Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 0 additions & 124 deletions db/queries/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -1294,37 +1294,6 @@ func UpdateLeafHashesBatch(ctx context.Context, db DBQuerier, mtreeTable string,
return nil
}

func GetBlockRanges(ctx context.Context, db DBQuerier, mtreeTable string) ([]types.BlockRange, error) {
data := map[string]interface{}{
"MtreeTable": mtreeTable,
}

sql, err := RenderSQL(SQLTemplates.GetBlockRanges, data)
if err != nil {
return nil, fmt.Errorf("failed to render GetBlockRanges SQL: %w", err)
}

rows, err := db.Query(ctx, sql)
if err != nil {
return nil, fmt.Errorf("query to get block ranges for '%s' failed: %w", mtreeTable, err)
}
defer rows.Close()

var blockRanges []types.BlockRange
for rows.Next() {
var blockRange types.BlockRange
if err := rows.Scan(&blockRange.NodePosition, &blockRange.RangeStart, &blockRange.RangeEnd); err != nil {
return nil, fmt.Errorf("failed to scan block range: %w", err)
}
blockRanges = append(blockRanges, blockRange)
}

if err := rows.Err(); err != nil {
return nil, fmt.Errorf("error iterating over block ranges: %w", err)
}

return blockRanges, nil
}

func ClearDirtyFlags(ctx context.Context, db DBQuerier, mtreeTable string, nodePositions []int64) error {
data := map[string]interface{}{
Expand Down Expand Up @@ -2013,99 +1982,6 @@ func UpdateBlockRangeStartComposite(ctx context.Context, db DBQuerier, mtreeTabl
return nil
}

func GetSplitPointSimple(ctx context.Context, db DBQuerier, schema, table, key, pkeyType string, rangeStart, rangeEnd interface{}, offset int64) (interface{}, error) {
data := map[string]interface{}{
"SchemaIdent": pgx.Identifier{schema}.Sanitize(),
"TableIdent": pgx.Identifier{table}.Sanitize(),
"Key": key,
"PkeyType": pkeyType,
}
sql, err := RenderSQL(SQLTemplates.GetSplitPointSimple, data)
if err != nil {
return nil, fmt.Errorf("failed to render GetSplitPointSimple SQL: %w", err)
}
var splitPoint interface{}
if err := db.QueryRow(ctx, sql, rangeStart, rangeEnd, pkeyType, offset).Scan(&splitPoint); err != nil {
return nil, fmt.Errorf("query to get split point simple for '%s.%s' failed: %w", schema, table, err)
}
return splitPoint, nil
}

func GetSplitPointComposite(ctx context.Context, db DBQuerier, schema, table string, pkeyCols []string, startVals, endVals []any, offset int64) ([]interface{}, error) {
cols := make([]string, len(pkeyCols))
for i, c := range pkeyCols {
cols[i] = pgx.Identifier{c}.Sanitize()
}
colsStr := strings.Join(cols, ", ")
orderCols := fmt.Sprintf("(%s)", colsStr)

var whereParts []string
args := make([]any, 0, len(startVals)+len(endVals)+1)
argIdx := 1

if len(startVals) > 0 {
startPh := make([]string, len(startVals))
for i := range startVals {
startPh[i] = fmt.Sprintf("$%d", argIdx)
args = append(args, startVals[i])
argIdx++
}
whereParts = append(whereParts, fmt.Sprintf("(%s) >= (%s)", colsStr, strings.Join(startPh, ", ")))
}

if len(endVals) > 0 {
hasNonNil := false
for i := range endVals {
if endVals[i] != nil {
hasNonNil = true
break
}
}
if hasNonNil {
endPh := make([]string, len(endVals))
for i := range endVals {
endPh[i] = fmt.Sprintf("$%d", argIdx)
args = append(args, endVals[i])
argIdx++
}
whereParts = append(whereParts, fmt.Sprintf("(%s) <= (%s)", colsStr, strings.Join(endPh, ", ")))
}
}
whereClause := "TRUE"
if len(whereParts) > 0 {
whereClause = strings.Join(whereParts, " AND ")
}

// The next placeholder index is for OFFSET
offsetPh := fmt.Sprintf("$%d", argIdx)
args = append(args, offset)

data := map[string]interface{}{
"SchemaIdent": pgx.Identifier{schema}.Sanitize(),
"TableIdent": pgx.Identifier{table}.Sanitize(),
"PkeyCols": colsStr,
"WhereClause": whereClause,
"OrderCols": orderCols,
"OffsetPlaceholder": offsetPh,
}
sql, err := RenderSQL(SQLTemplates.GetSplitPointComposite, data)
if err != nil {
return nil, fmt.Errorf("failed to render GetSplitPointComposite SQL: %w", err)
}
sp := make([]interface{}, len(pkeyCols))
destPtrs := make([]interface{}, len(pkeyCols))
for i := range destPtrs {
destPtrs[i] = &sp[i]
}

if err := db.QueryRow(ctx, sql, args...).Scan(destPtrs...); err != nil {
if err == pgx.ErrNoRows {
return nil, nil
}
return nil, fmt.Errorf("query to get split point composite for '%s.%s' failed: %w", schema, table, err)
}
return sp, nil
}

func GetMinValComposite(ctx context.Context, db DBQuerier, schema, table string, pkeyCols []string) ([]interface{}, error) {
cols := make([]string, len(pkeyCols))
Expand Down
59 changes: 3 additions & 56 deletions db/queries/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type Templates struct {
MtreeLeafHashSQL *template.Template
UpdateLeafHashes *template.Template
UpdateLeafHashesBatch *template.Template
GetBlockRanges *template.Template

GetDirtyAndNewBlocks *template.Template
ClearDirtyFlags *template.Template
BuildParentNodes *template.Template
Expand All @@ -64,8 +64,7 @@ type Templates struct {
GetMaxValSimple *template.Template
GetCountComposite *template.Template
GetCountSimple *template.Template
GetSplitPointComposite *template.Template
GetSplitPointSimple *template.Template

DeleteParentNodes *template.Template
GetMaxNodePosition *template.Template
UpdateBlockRangeEnd *template.Template
Expand All @@ -79,7 +78,7 @@ type Templates struct {
GetBlockCountSimple *template.Template
GetBlockSizeFromMetadata *template.Template
GetMaxNodeLevel *template.Template
CompareBlocksSQL *template.Template

DropXORFunction *template.Template
DropMetadataTable *template.Template
DropMtreeTable *template.Template
Expand Down Expand Up @@ -937,18 +936,6 @@ var SQLTemplates = Templates{
node_position = $2
AND mt.node_level = 0
`)),
GetBlockRanges: template.Must(template.New("getBlockRanges").Parse(`
SELECT
node_position,
range_start,
range_end
FROM
{{.MtreeTable}}
WHERE
node_level = 0
ORDER BY
node_position
`)),
GetDirtyAndNewBlocks: template.Must(template.New("getDirtyAndNewBlocks").Parse(`
SELECT
node_position,
Expand Down Expand Up @@ -1135,38 +1122,6 @@ var SQLTemplates = Templates{
FROM {{.SchemaIdent}}.{{.TableIdent}}
WHERE {{.WhereClause}}
`)),
GetSplitPointComposite: template.Must(template.New("getSplitPointComposite").Parse(`
SELECT
{{.PkeyCols}}
FROM
{{.SchemaIdent}}.{{.TableIdent}}
WHERE
{{.WhereClause}}
ORDER BY
{{.OrderCols}}
OFFSET
{{.OffsetPlaceholder}}
LIMIT
1
`)),
GetSplitPointSimple: template.Must(template.New("getSplitPointSimple").Parse(`
SELECT
{{.Key}}
FROM
{{.SchemaIdent}}.{{.TableIdent}}
WHERE
{{.Key}} >= $1
AND (
{{.Key}} < $2
OR $3::{{.PkeyType}} IS NULL
)
ORDER BY
{{.Key}}
OFFSET
$4
LIMIT
1
`)),
DeleteParentNodes: template.Must(template.New("deleteParentNodes").Parse(`
DELETE FROM
{{.MtreeTable}}
Expand Down Expand Up @@ -1354,14 +1309,6 @@ var SQLTemplates = Templates{
FROM
{{.MtreeTable}}
`)),
CompareBlocksSQL: template.Must(template.New("compareBlocksSQL").Parse(`
SELECT
*
FROM
{{.TableName}}
WHERE
{{.WhereClause}}
`)),
DropXORFunction: template.Must(template.New("dropXORFunction").Parse(`
DROP FUNCTION IF EXISTS spock.bytea_xor(bytea, bytea) CASCADE
`)),
Expand Down
4 changes: 2 additions & 2 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ type UserPrivileges struct {
type SpockNodeAndSubInfo struct {
NodeID int64 `db:"node_id"`
NodeName string `db:"node_name"`
Location string `db:"location"`
Country string `db:"country"`
Location *string `db:"location"`
Country *string `db:"country"`
SubID int64 `db:"sub_id"`
SubName string `db:"sub_name"`
SubEnabled bool `db:"sub_enabled"`
Expand Down
Loading