|
| 1 | +package mysql |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "database/sql" |
| 6 | + "encoding/json" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + |
| 10 | + "github.com/go-sql-driver/mysql" |
| 11 | + |
| 12 | + "github.com/uber/submitqueue/entity" |
| 13 | + "github.com/uber/submitqueue/extension/storage" |
| 14 | +) |
| 15 | + |
| 16 | +type speculationTreeStore struct { |
| 17 | + db *sql.DB |
| 18 | +} |
| 19 | + |
| 20 | +// NewSpeculationTreeStore creates a new MySQL-backed SpeculationTreeStore. |
| 21 | +func NewSpeculationTreeStore(db *sql.DB) storage.SpeculationTreeStore { |
| 22 | + return &speculationTreeStore{db: db} |
| 23 | +} |
| 24 | + |
| 25 | +// Get retrieves the speculation tree by batch ID. Returns ErrNotFound if the speculation tree is not found. |
| 26 | +func (s *speculationTreeStore) Get(ctx context.Context, batchID string) (entity.SpeculationTree, error) { |
| 27 | + var st entity.SpeculationTree |
| 28 | + var speculationsJSON []byte |
| 29 | + |
| 30 | + err := s.db.QueryRowContext(ctx, |
| 31 | + "SELECT batch_id, speculations FROM speculation_tree WHERE batch_id = ?", |
| 32 | + batchID, |
| 33 | + ).Scan(&st.BatchID, &speculationsJSON) |
| 34 | + |
| 35 | + if errors.Is(err, sql.ErrNoRows) { |
| 36 | + return entity.SpeculationTree{}, storage.WrapNotFound(err) |
| 37 | + } |
| 38 | + if err != nil { |
| 39 | + return entity.SpeculationTree{}, fmt.Errorf("failed to get speculation tree entity batchID=%s from the database: %w", batchID, err) |
| 40 | + } |
| 41 | + |
| 42 | + if err := json.Unmarshal(speculationsJSON, &st.Speculations); err != nil { |
| 43 | + return entity.SpeculationTree{}, fmt.Errorf("failed to unmarshal speculations for speculation tree entity batchID=%s from the database: %w", batchID, err) |
| 44 | + } |
| 45 | + |
| 46 | + return st, nil |
| 47 | +} |
| 48 | + |
| 49 | +// Create creates a new speculation tree. Returns ErrAlreadyExists if the entry already exists. |
| 50 | +func (s *speculationTreeStore) Create(ctx context.Context, speculationTree entity.SpeculationTree) error { |
| 51 | + speculationsJSON, err := json.Marshal(speculationTree.Speculations) |
| 52 | + if err != nil { |
| 53 | + return fmt.Errorf("failed to marshal speculations batchID=%s for Create speculation tree entity: %w", speculationTree.BatchID, err) |
| 54 | + } |
| 55 | + |
| 56 | + _, err = s.db.ExecContext(ctx, |
| 57 | + "INSERT INTO speculation_tree (batch_id, speculations) VALUES (?, ?)", |
| 58 | + speculationTree.BatchID, speculationsJSON, |
| 59 | + ) |
| 60 | + if err != nil { |
| 61 | + var mysqlErr *mysql.MySQLError |
| 62 | + if errors.As(err, &mysqlErr) && mysqlErr.Number == 1062 { |
| 63 | + return fmt.Errorf("speculation tree entity batchID=%s: %w", speculationTree.BatchID, storage.ErrAlreadyExists) |
| 64 | + } |
| 65 | + return fmt.Errorf("failed to insert speculation tree entity batchID=%s: %w", speculationTree.BatchID, err) |
| 66 | + } |
| 67 | + |
| 68 | + return nil |
| 69 | +} |
| 70 | + |
| 71 | +// UpdateSpeculations updates the speculations of a speculation tree. Returns ErrNotFound if the speculation tree is not found. |
| 72 | +func (s *speculationTreeStore) UpdateSpeculations(ctx context.Context, batchID string, speculations []entity.SpeculationInfo) error { |
| 73 | + speculationsJSON, err := json.Marshal(speculations) |
| 74 | + if err != nil { |
| 75 | + return fmt.Errorf("failed to marshal speculations batchID=%s for UpdateSpeculations: %w", batchID, err) |
| 76 | + } |
| 77 | + |
| 78 | + result, err := s.db.ExecContext(ctx, |
| 79 | + "UPDATE speculation_tree SET speculations = ? WHERE batch_id = ?", |
| 80 | + speculationsJSON, batchID, |
| 81 | + ) |
| 82 | + if err != nil { |
| 83 | + return fmt.Errorf("failed to update speculations for batchID=%q: %w", batchID, err) |
| 84 | + } |
| 85 | + |
| 86 | + rowsAffected, err := result.RowsAffected() |
| 87 | + if err != nil { |
| 88 | + return fmt.Errorf("failed to get rows affected from update for batchID=%q: %w", batchID, err) |
| 89 | + } |
| 90 | + |
| 91 | + if rowsAffected != 1 { |
| 92 | + return storage.WrapNotFound(fmt.Errorf("speculation tree entity batchID=%s", batchID)) |
| 93 | + } |
| 94 | + |
| 95 | + return nil |
| 96 | +} |
0 commit comments