// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.29.0
// source: trace_seg.sql

package lrdb

import (
	"context"

	"github.com/google/uuid"
)

const getTraceLabelNameMaps = `-- name: GetTraceLabelNameMaps :many
SELECT
    segment_id,
    label_name_map
FROM trace_seg
WHERE organization_id = $1
  AND dateint = $2
  AND segment_id = ANY($3::BIGINT[])
  AND label_name_map IS NOT NULL
`

type GetTraceLabelNameMapsParams struct {
	OrganizationID uuid.UUID `json:"organization_id"`
	Dateint        int32     `json:"dateint"`
	SegmentIds     []int64   `json:"segment_ids"`
}

type GetTraceLabelNameMapsRow struct {
	SegmentID    int64  `json:"segment_id"`
	LabelNameMap []byte `json:"label_name_map"`
}

func (q *Queries) GetTraceLabelNameMaps(ctx context.Context, arg GetTraceLabelNameMapsParams) ([]GetTraceLabelNameMapsRow, error) {
	rows, err := q.db.Query(ctx, getTraceLabelNameMaps, arg.OrganizationID, arg.Dateint, arg.SegmentIds)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []GetTraceLabelNameMapsRow
	for rows.Next() {
		var i GetTraceLabelNameMapsRow
		if err := rows.Scan(&i.SegmentID, &i.LabelNameMap); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getTraceSeg = `-- name: GetTraceSeg :one
SELECT organization_id, dateint, segment_id, instance_num, fingerprints, record_count, file_size, ingest_dateint, ts_range, created_by, created_at, compacted, published, label_name_map
FROM trace_seg
WHERE organization_id = $1
  AND dateint = $2
  AND segment_id = $3
  AND instance_num = $4
`

type GetTraceSegParams struct {
	OrganizationID uuid.UUID `json:"organization_id"`
	Dateint        int32     `json:"dateint"`
	SegmentID      int64     `json:"segment_id"`
	InstanceNum    int16     `json:"instance_num"`
}

func (q *Queries) GetTraceSeg(ctx context.Context, arg GetTraceSegParams) (TraceSeg, error) {
	row := q.db.QueryRow(ctx, getTraceSeg,
		arg.OrganizationID,
		arg.Dateint,
		arg.SegmentID,
		arg.InstanceNum,
	)
	var i TraceSeg
	err := row.Scan(
		&i.OrganizationID,
		&i.Dateint,
		&i.SegmentID,
		&i.InstanceNum,
		&i.Fingerprints,
		&i.RecordCount,
		&i.FileSize,
		&i.IngestDateint,
		&i.TsRange,
		&i.CreatedBy,
		&i.CreatedAt,
		&i.Compacted,
		&i.Published,
		&i.LabelNameMap,
	)
	return i, err
}

const listTraceSegmentsForQuery = `-- name: ListTraceSegmentsForQuery :many
SELECT
    t.fp::bigint                    AS fingerprint,
    s.instance_num,
    s.segment_id,
    lower(s.ts_range)::bigint        AS start_ts,
    (upper(s.ts_range) - 1)::bigint  AS end_ts
FROM trace_seg AS s
         CROSS JOIN LATERAL
    unnest(s.fingerprints) AS t(fp)
WHERE
    s.organization_id = $1
  AND s.dateint       = $2
  AND s.published     = true
  AND s.fingerprints && $3::BIGINT[]
  AND t.fp            = ANY($3::BIGINT[])
  AND ts_range && int8range($4, $5, '[)')
`

type ListTraceSegmentsForQueryParams struct {
	OrganizationID uuid.UUID `json:"organization_id"`
	Dateint        int32     `json:"dateint"`
	Fingerprints   []int64   `json:"fingerprints"`
	S              int64     `json:"s"`
	E              int64     `json:"e"`
}

type ListTraceSegmentsForQueryRow struct {
	Fingerprint int64 `json:"fingerprint"`
	InstanceNum int16 `json:"instance_num"`
	SegmentID   int64 `json:"segment_id"`
	StartTs     int64 `json:"start_ts"`
	EndTs       int64 `json:"end_ts"`
}

func (q *Queries) ListTraceSegmentsForQuery(ctx context.Context, arg ListTraceSegmentsForQueryParams) ([]ListTraceSegmentsForQueryRow, error) {
	rows, err := q.db.Query(ctx, listTraceSegmentsForQuery,
		arg.OrganizationID,
		arg.Dateint,
		arg.Fingerprints,
		arg.S,
		arg.E,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []ListTraceSegmentsForQueryRow
	for rows.Next() {
		var i ListTraceSegmentsForQueryRow
		if err := rows.Scan(
			&i.Fingerprint,
			&i.InstanceNum,
			&i.SegmentID,
			&i.StartTs,
			&i.EndTs,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const markTraceSegsCompactedByKeys = `-- name: MarkTraceSegsCompactedByKeys :exec
UPDATE trace_seg
SET compacted = true
WHERE organization_id = $1
  AND dateint         = $2
  AND instance_num    = $3
  AND segment_id      = ANY($4::bigint[])
  AND compacted       = false
`

type MarkTraceSegsCompactedByKeysParams struct {
	OrganizationID uuid.UUID `json:"organization_id"`
	Dateint        int32     `json:"dateint"`
	InstanceNum    int16     `json:"instance_num"`
	SegmentIds     []int64   `json:"segment_ids"`
}

func (q *Queries) MarkTraceSegsCompactedByKeys(ctx context.Context, arg MarkTraceSegsCompactedByKeysParams) error {
	_, err := q.db.Exec(ctx, markTraceSegsCompactedByKeys,
		arg.OrganizationID,
		arg.Dateint,
		arg.InstanceNum,
		arg.SegmentIds,
	)
	return err
}

const insertTraceSegmentDirect = `-- name: insertTraceSegmentDirect :exec
INSERT INTO trace_seg (
  organization_id,
  dateint,
  segment_id,
  instance_num,
  ts_range,
  record_count,
  file_size,
  created_by,
  fingerprints,
  published,
  compacted,
  label_name_map
)
VALUES (
  $1,
  $2,
  $3,
  $4,
  int8range($5, $6, '[)'),
  $7,
  $8,
  $9,
  $10::bigint[],
  $11,
  $12,
  $13
)
`

type InsertTraceSegmentParams struct {
	OrganizationID uuid.UUID `json:"organization_id"`
	Dateint        int32     `json:"dateint"`
	SegmentID      int64     `json:"segment_id"`
	InstanceNum    int16     `json:"instance_num"`
	StartTs        int64     `json:"start_ts"`
	EndTs          int64     `json:"end_ts"`
	RecordCount    int64     `json:"record_count"`
	FileSize       int64     `json:"file_size"`
	CreatedBy      CreatedBy `json:"created_by"`
	Fingerprints   []int64   `json:"fingerprints"`
	Published      bool      `json:"published"`
	Compacted      bool      `json:"compacted"`
	LabelNameMap   []byte    `json:"label_name_map"`
}

func (q *Queries) insertTraceSegmentDirect(ctx context.Context, arg InsertTraceSegmentParams) error {
	_, err := q.db.Exec(ctx, insertTraceSegmentDirect,
		arg.OrganizationID,
		arg.Dateint,
		arg.SegmentID,
		arg.InstanceNum,
		arg.StartTs,
		arg.EndTs,
		arg.RecordCount,
		arg.FileSize,
		arg.CreatedBy,
		arg.Fingerprints,
		arg.Published,
		arg.Compacted,
		arg.LabelNameMap,
	)
	return err
}

const markTraceSegsCompactedUnpublishedByKeys = `-- name: markTraceSegsCompactedUnpublishedByKeys :exec
UPDATE trace_seg
SET compacted = true, published = false
WHERE organization_id = $1
  AND dateint         = $2
  AND instance_num    = $3
  AND segment_id      = ANY($4::bigint[])
  AND compacted       = false
`

type markTraceSegsCompactedUnpublishedByKeysParams struct {
	OrganizationID uuid.UUID `json:"organization_id"`
	Dateint        int32     `json:"dateint"`
	InstanceNum    int16     `json:"instance_num"`
	SegmentIds     []int64   `json:"segment_ids"`
}

func (q *Queries) markTraceSegsCompactedUnpublishedByKeys(ctx context.Context, arg markTraceSegsCompactedUnpublishedByKeysParams) error {
	_, err := q.db.Exec(ctx, markTraceSegsCompactedUnpublishedByKeys,
		arg.OrganizationID,
		arg.Dateint,
		arg.InstanceNum,
		arg.SegmentIds,
	)
	return err
}
