Commit e977bcbb authored by ale's avatar ale

Fix error in FindRequest.NumVersions handling

parent 61866cab
......@@ -3,6 +3,7 @@ package server
import (
"context"
"database/sql"
"fmt"
"log"
"path/filepath"
"strings"
......@@ -64,6 +65,11 @@ func (a *dbAtom) getAtom() tabacco.Atom {
}
func normalizeAtoms(dbAtoms []dbAtom, numVersions int) [][]tabacco.Version {
// numVersions == 0 is remapped to 1.
if numVersions < 1 {
numVersions = 1
}
// Accumulate versions keyed by backup ID first, dataset name
// next. Preserve the ordering of backups in dbAtoms, which we
// are going to use later to apply a per-dataset limit.
......@@ -104,11 +110,10 @@ func normalizeAtoms(dbAtoms []dbAtom, numVersions int) [][]tabacco.Version {
backup := backupMap[backupID]
var tmpv []tabacco.Version
for _, ds := range tmp {
if dsCount[ds.Name] >= numVersions {
continue
if dsCount[ds.Name] < numVersions {
tmpv = append(tmpv, tabacco.Version{Backup: *backup, Dataset: *ds})
dsCount[ds.Name]++
}
tmpv = append(tmpv, tabacco.Version{Backup: *backup, Dataset: *ds})
dsCount[ds.Name]++
}
if len(tmpv) > 0 {
out = append(out, tmpv)
......@@ -193,6 +198,8 @@ func (s *Service) FindAtoms(ctx context.Context, req tabacco.FindRequest) ([][]t
}
defer tx.Rollback() // nolint
// Build the SQL query. Assemble the WHERE clauses and their
// respective arguments first.
var where []string
var args []interface{}
if req.Host != "" {
......@@ -204,11 +211,16 @@ func (s *Service) FindAtoms(ctx context.Context, req tabacco.FindRequest) ([][]t
args = append(args, strings.Replace(req.Pattern, "*", "%", -1))
}
q := ` SELECT
backup_id, backup_timestamp, backup_host,
dataset_name, dataset_handler,
atom_name, atom_path, atom_source_path, atom_relative_path
FROM log WHERE ` + strings.Join(where, " AND ")
// Build the final query and execute it.
q := fmt.Sprintf(
`SELECT
backup_id, backup_timestamp, backup_host,
dataset_name, dataset_handler,
atom_name, atom_path, atom_source_path, atom_relative_path
FROM log WHERE %s
ORDER BY backup_timestamp DESC`,
strings.Join(where, " AND "),
)
rows, err := tx.Query(q, args...)
if err != nil {
return nil, err
......
......@@ -69,7 +69,8 @@ func TestService_FindAtoms(t *testing.T) {
vv, err := svc.FindAtoms(
context.Background(),
tabacco.FindRequest{
Pattern: "*/sub1",
Pattern: "*/sub1",
NumVersions: 10,
},
)
if err != nil {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment