forked from longtao.wu/droboticflow
161 lines
5.4 KiB
Go
161 lines
5.4 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/domain"
|
|
"strings"
|
|
)
|
|
|
|
// A list response uses a single request-scoped read snapshot. Never attach this
|
|
// cache to a mutation, a session, or the server: permission changes must be seen
|
|
// by the next request, and claim/decision paths retain their fresh checks.
|
|
type humanListReadsKey struct{}
|
|
type humanListMembershipKey struct{ organization, project, user string }
|
|
type humanListMembershipResult struct {
|
|
roles []annotationProjectRole
|
|
err error
|
|
}
|
|
type humanListReads struct {
|
|
organization, project string
|
|
batches map[string]*domain.HumanBatchRecord
|
|
itemsByTask map[string][]domain.HumanItemRecord
|
|
memberships map[humanListMembershipKey]humanListMembershipResult
|
|
}
|
|
|
|
func (server *server) withHumanListReads(ctx context.Context, org, project string, items []domain.HumanItemRecord) (context.Context, error) {
|
|
cache, ok := ctx.Value(humanListReadsKey{}).(*humanListReads)
|
|
if !ok || cache.organization != org || cache.project != project {
|
|
cache = &humanListReads{organization: org, project: project, batches: map[string]*domain.HumanBatchRecord{}, itemsByTask: map[string][]domain.HumanItemRecord{}, memberships: map[humanListMembershipKey]humanListMembershipResult{}}
|
|
}
|
|
if len(items) > 0 {
|
|
ids := make([]string, 0, len(items))
|
|
seen := map[string]bool{}
|
|
for _, item := range items {
|
|
if _, loaded := cache.batches[item.BatchID]; !loaded && !seen[item.BatchID] {
|
|
ids = append(ids, item.BatchID)
|
|
seen[item.BatchID] = true
|
|
}
|
|
}
|
|
var batches []domain.HumanBatchRecord
|
|
var err error
|
|
if len(ids) > 0 {
|
|
batches, err = server.store.ListHumanBatchAccessRecords(ctx, org, ids)
|
|
}
|
|
if err != nil {
|
|
return ctx, err
|
|
}
|
|
replicaCounts := false
|
|
if reader, ok := server.store.(interface{ UsesQueryReplicaCountRead(context.Context) bool }); ok {
|
|
replicaCounts = reader.UsesQueryReplicaCountRead(ctx)
|
|
}
|
|
if replicaCounts {
|
|
for _, id := range ids {
|
|
cache.batches[id] = nil
|
|
}
|
|
}
|
|
for i := range batches {
|
|
cache.batches[batches[i].ID] = &batches[i]
|
|
}
|
|
if replicaCounts {
|
|
for len(batches) > 0 {
|
|
sources := []string{}
|
|
for _, b := range batches {
|
|
if b.SourceBatchID != "" {
|
|
if _, loaded := cache.batches[b.SourceBatchID]; !loaded {
|
|
sources = append(sources, b.SourceBatchID)
|
|
cache.batches[b.SourceBatchID] = nil
|
|
}
|
|
}
|
|
}
|
|
if len(sources) == 0 {
|
|
break
|
|
}
|
|
batches, err = server.store.ListHumanBatchAccessRecords(ctx, org, sources)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for i := range batches {
|
|
cache.batches[batches[i].ID] = &batches[i]
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// Counts never hydrate task details or compute response-only claim flags.
|
|
// Keep authorization batch/member caches, but do not retain a second copy
|
|
// of every task's history join across the exhaustive count scan.
|
|
if mode, _ := ctx.Value(humanPoolReadModeKey{}).(humanPoolReadMode); mode == humanPoolReadCount {
|
|
// Legacy authority counts also traverse the entire pool. Only joins for
|
|
// the active page are used below; keeping past pages grows with corpus
|
|
// size for every concurrent caller. Permission caches remain request-local.
|
|
clear(cache.itemsByTask)
|
|
}
|
|
if reader, ok := server.store.(interface{ UsesQueryReplicaCountRead(context.Context) bool }); ok && reader.UsesQueryReplicaCountRead(ctx) {
|
|
return context.WithValue(ctx, humanListReadsKey{}, cache), nil
|
|
}
|
|
// A later task join may include linked records absent from the actor's
|
|
// page. Replace each task's group in that read's order; merging the actor
|
|
// page first would change which batch governs legacy supplier-pool reads.
|
|
groups := map[string][]domain.HumanItemRecord{}
|
|
for _, item := range items {
|
|
groups[item.LegacyTaskID] = append(groups[item.LegacyTaskID], item)
|
|
}
|
|
for taskID, group := range groups {
|
|
cache.itemsByTask[taskID] = group
|
|
}
|
|
return context.WithValue(ctx, humanListReadsKey{}, cache), nil
|
|
}
|
|
|
|
func (server *server) humanBatchForListRead(ctx context.Context, id string) (*domain.HumanBatchRecord, error) {
|
|
if cache, ok := ctx.Value(humanListReadsKey{}).(*humanListReads); ok {
|
|
if batch, found := cache.batches[id]; found {
|
|
return batch, nil
|
|
}
|
|
// Source batches can be outside the list's project; retain original lookup.
|
|
batch, err := server.store.GetHumanBatch(ctx, id)
|
|
if err == nil {
|
|
cache.batches[id] = batch
|
|
}
|
|
return batch, err
|
|
}
|
|
return server.store.GetHumanBatch(ctx, id)
|
|
}
|
|
|
|
// Only response rendering needs source stages for anonymization. Counts and
|
|
// already-anonymous records do not need this read. Preserve the existing point
|
|
// lookup fallback when a source is missing, cross-tenant, or the bulk read fails.
|
|
func (server *server) primeAcceptanceSourcesForList(ctx context.Context, items []domain.HumanItemRecord) {
|
|
cache, ok := ctx.Value(humanListReadsKey{}).(*humanListReads)
|
|
if !ok {
|
|
return
|
|
}
|
|
ids := []string{}
|
|
seen := map[string]bool{}
|
|
for n := range items {
|
|
if acceptanceIdentifiersEmpty(&items[n]) {
|
|
continue
|
|
}
|
|
batch := cache.batches[items[n].BatchID]
|
|
if batch == nil {
|
|
continue
|
|
}
|
|
id := strings.TrimSpace(batch.SourceBatchID)
|
|
if id == "" || seen[id] {
|
|
continue
|
|
}
|
|
seen[id] = true
|
|
if _, loaded := cache.batches[id]; !loaded {
|
|
ids = append(ids, id)
|
|
}
|
|
}
|
|
if len(ids) == 0 {
|
|
return
|
|
}
|
|
rows, err := server.store.ListHumanBatchAccessRecords(ctx, cache.organization, ids)
|
|
if err != nil {
|
|
return
|
|
}
|
|
for n := range rows {
|
|
cache.batches[rows[n].ID] = &rows[n]
|
|
}
|
|
}
|