package dobotaiops import ( "context" "encoding/json" "strings" "time" ) const MaxMirrorRecords = 100000 const mirrorFingerprintSQL = `encode(sha256(convert_to((payload-'_quality_observed_at')::text,'UTF8')),'hex')` type MirrorKey struct { ID string Fingerprint string } type MirrorManifest struct { Keys []MirrorKey AsOf time.Time Safety SourceSafety } type MirrorRecord struct { Key MirrorKey Payload json.RawMessage } func validMirrorTenant(tenant string) bool { return strings.TrimSpace(tenant) == tenant && len(tenant) > 0 && len(tenant) <= 128 && !strings.ContainsRune(tenant, 0) } // Only a manifest (IDs and stable hashes), not all long answers, crosses the // network on unchanged polls. Each read retains the source ACL and read-only gates. func (c *Client) ReadMirrorManifest(ctx context.Context, tenant string) (MirrorManifest, error) { out := MirrorManifest{Keys: []MirrorKey{}} if !validMirrorTenant(tenant) { return out, ErrInvalidQuery } err := c.withReadOnlyTx(ctx, func(ctx context.Context, q querier) error { if q.QueryRow(ctx, "SELECT transaction_timestamp()").Scan(&out.AsOf) != nil { return ErrUnavailable } rows, e := q.Query(ctx, recordsBaseSQL+` SELECT id,`+mirrorFingerprintSQL+` FROM records ORDER BY id LIMIT 100001`, tenant) if e != nil { return ErrUnavailable } defer rows.Close() for rows.Next() { var k MirrorKey if rows.Scan(&k.ID, &k.Fingerprint) != nil { return ErrUnavailable } if len(out.Keys) >= MaxMirrorRecords { return ErrInvalidQuery } out.Keys = append(out.Keys, k) } return rows.Err() }) out.Safety = c.Safety() return out, err } func (c *Client) ReadMirrorRecords(ctx context.Context, tenant string, ids []string) ([]MirrorRecord, error) { if !validMirrorTenant(tenant) || len(ids) == 0 || len(ids) > 100 { return nil, ErrInvalidQuery } seen := map[string]bool{} for _, id := range ids { if id == "" || len(id) > 256 || strings.ContainsRune(id, 0) || seen[id] { return nil, ErrInvalidQuery } seen[id] = true } out := make([]MirrorRecord, 0, len(ids)) size := 0 err := c.withReadOnlyTx(ctx, func(ctx context.Context, q querier) error { rows, e := q.Query(ctx, recordsBaseSQL+` SELECT id,`+mirrorFingerprintSQL+`,payload FROM records WHERE id=ANY($2::text[]) ORDER BY id`, tenant, ids) if e != nil { return ErrUnavailable } defer rows.Close() for rows.Next() { var r MirrorRecord if rows.Scan(&r.Key.ID, &r.Key.Fingerprint, &r.Payload) != nil { return ErrUnavailable } size += len(r.Payload) if len(r.Payload) > 8<<20 || size > 32<<20 { return ErrInvalidQuery } out = append(out, r) } if rows.Err() != nil { return ErrUnavailable } if len(out) != len(ids) { return ErrUnavailable } return nil }) return out, err }