package sync
import (
"fmt"
"strings"
"github.com/danmestas/go-libfossil/db"
"github.com/danmestas/go-libfossil/internal/cone"
"github.com/danmestas/go-libfossil/internal/deck"
libfossil "github.com/danmestas/go-libfossil/internal/fsltype"
"github.com/danmestas/go-libfossil/internal/horizon/seed"
)
// horizonHeldPragma states one held entry of a structural clone on every
// structural request (draft-fossil-horizon-00 §6.1).
const horizonHeldPragma = "horizon-held"
// syncConeWithHeld returns c augmented with this store's held entries when c
// carries a structural horizon (§6.1). Stored cones never serialize held
// entries; adding them here makes both conePragmas and Cone.Digest (which keys
// the cursor) reflect the clone's current entries.
func syncConeWithHeld(q db.Querier, c *cone.Cone) (*cone.Cone, error) {
if c == nil {
return nil, nil
}
h, ok := c.Horizon()
if !ok || h.Depth() != cone.DepthStructural {
return c, nil
}
entries, err := heldEntries(q, h.Hash())
if err != nil {
return nil, err
}
if len(entries) == 0 {
return c, nil
}
nh := h.WithHeld(entries)
return c.WithHorizon(&nh)
}
// heldEntries returns this structural store's held entries (§6.1), excluding C
// (which is implied), oldest rid first and capped at cone.MaxHeldEntries:
// every held check-in whose primary parent is not held (including a held root),
// omitting any baseline held below the horizon for a delta's sake unless the
// held seed applies a tag to it.
func heldEntries(q db.Querier, cHash string) ([]string, error) {
if q == nil {
panic("sync.heldEntries: nil querier")
}
seeded, err := seededTargets(q)
if err != nil {
return nil, err
}
baselines, err := deltaBaselines(q)
if err != nil {
return nil, err
}
rows, err := q.Query(`
SELECT b.rid, b.uuid FROM event e
JOIN blob b ON b.rid=e.objid
WHERE e.type='ci'
AND NOT EXISTS(SELECT 1 FROM phantom WHERE rid=e.objid)
AND b.uuid != ?1
AND NOT EXISTS(
SELECT 1 FROM plink p
JOIN event pe ON pe.objid=p.pid AND pe.type='ci'
WHERE p.cid=e.objid AND p.isprim=1
AND NOT EXISTS(SELECT 1 FROM phantom WHERE rid=p.pid)
)
ORDER BY b.rid ASC`, strings.ToLower(cHash))
if err != nil {
return nil, fmt.Errorf("sync: held entries: %w", err)
}
defer rows.Close()
var out []string
for rows.Next() {
var rid libfossil.FslID
var uuid string
if err := rows.Scan(&rid, &uuid); err != nil {
return nil, fmt.Errorf("sync: held entries: %w", err)
}
low := strings.ToLower(uuid)
if baselines[rid] && !seeded[low] {
continue
}
out = append(out, low)
if len(out) >= cone.MaxHeldEntries {
break
}
}
return out, rows.Err()
}
func seededTargets(q db.Querier) (map[string]bool, error) {
raw, err := seed.Held(q)
if err != nil || raw == nil {
return nil, err
}
d, err := deck.Parse(raw)
if err != nil {
return nil, nil
}
out := make(map[string]bool, len(d.T))
for _, tc := range d.T {
if tc.Name == seed.BindingTag {
continue
}
if tc.UUID != "" && tc.UUID != "*" {
out[strings.ToLower(tc.UUID)] = true
}
}
return out, nil
}
func deltaBaselines(q db.Querier) (map[libfossil.FslID]bool, error) {
rows, err := q.Query("SELECT DISTINCT baseid FROM plink WHERE baseid>0")
if err != nil {
return nil, fmt.Errorf("sync: delta baselines: %w", err)
}
defer rows.Close()
out := map[libfossil.FslID]bool{}
for rows.Next() {
var rid libfossil.FslID
if err := rows.Scan(&rid); err != nil {
return nil, fmt.Errorf("sync: delta baselines: %w", err)
}
out[rid] = true
}
return out, rows.Err()
}