-
Notifications
You must be signed in to change notification settings - Fork 0
feat(storage) Adds speculation tree store interface and mysql implementation #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
416d7f5
feat(entities) Adds speculation tree store interface and mysql implem…
manjari25 bad1735
update store based on new speculation tree entity
manjari25 34634d0
Merge branch 'main' into manjari/speculation-tree-store
manjari25 b76c0d4
Merge branch 'main' into manjari/speculation-tree-store
behinddwalls bc82d06
Merge branch 'main' into manjari/speculation-tree-store
behinddwalls File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| package mysql | ||
|
|
||
| import ( | ||
| "context" | ||
| "database/sql" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "github.com/go-sql-driver/mysql" | ||
|
|
||
| "github.com/uber/submitqueue/entity" | ||
| "github.com/uber/submitqueue/extension/storage" | ||
| ) | ||
|
|
||
| type speculationTreeStore struct { | ||
| db *sql.DB | ||
| } | ||
|
|
||
| // NewSpeculationTreeStore creates a new MySQL-backed SpeculationTreeStore. | ||
| func NewSpeculationTreeStore(db *sql.DB) storage.SpeculationTreeStore { | ||
| return &speculationTreeStore{db: db} | ||
| } | ||
|
|
||
| // Get retrieves the speculation tree by batch ID. Returns ErrNotFound if the speculation tree is not found. | ||
| func (s *speculationTreeStore) Get(ctx context.Context, batchID string) (entity.SpeculationTree, error) { | ||
| var st entity.SpeculationTree | ||
| var speculationsJSON []byte | ||
|
|
||
| err := s.db.QueryRowContext(ctx, | ||
| "SELECT batch_id, speculations FROM speculation_tree WHERE batch_id = ?", | ||
| batchID, | ||
| ).Scan(&st.BatchID, &speculationsJSON) | ||
|
|
||
| if errors.Is(err, sql.ErrNoRows) { | ||
| return entity.SpeculationTree{}, storage.WrapNotFound(err) | ||
| } | ||
| if err != nil { | ||
| return entity.SpeculationTree{}, fmt.Errorf("failed to get speculation tree entity batchID=%s from the database: %w", batchID, err) | ||
| } | ||
|
|
||
| if err := json.Unmarshal(speculationsJSON, &st.Speculations); err != nil { | ||
| return entity.SpeculationTree{}, fmt.Errorf("failed to unmarshal speculations for speculation tree entity batchID=%s from the database: %w", batchID, err) | ||
| } | ||
|
|
||
| return st, nil | ||
| } | ||
|
|
||
| // Create creates a new speculation tree. Returns ErrAlreadyExists if the entry already exists. | ||
| func (s *speculationTreeStore) Create(ctx context.Context, speculationTree entity.SpeculationTree) error { | ||
| speculationsJSON, err := json.Marshal(speculationTree.Speculations) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to marshal speculations batchID=%s for Create speculation tree entity: %w", speculationTree.BatchID, err) | ||
| } | ||
|
|
||
| _, err = s.db.ExecContext(ctx, | ||
| "INSERT INTO speculation_tree (batch_id, speculations) VALUES (?, ?)", | ||
| speculationTree.BatchID, speculationsJSON, | ||
| ) | ||
| if err != nil { | ||
| var mysqlErr *mysql.MySQLError | ||
| if errors.As(err, &mysqlErr) && mysqlErr.Number == 1062 { | ||
| return fmt.Errorf("speculation tree entity batchID=%s: %w", speculationTree.BatchID, storage.ErrAlreadyExists) | ||
| } | ||
| return fmt.Errorf("failed to insert speculation tree entity batchID=%s: %w", speculationTree.BatchID, err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // UpdateSpeculations updates the speculations of a speculation tree. Returns ErrNotFound if the speculation tree is not found. | ||
| func (s *speculationTreeStore) UpdateSpeculations(ctx context.Context, batchID string, speculations []entity.SpeculationInfo) error { | ||
| speculationsJSON, err := json.Marshal(speculations) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to marshal speculations batchID=%s for UpdateSpeculations: %w", batchID, err) | ||
| } | ||
|
|
||
| result, err := s.db.ExecContext(ctx, | ||
| "UPDATE speculation_tree SET speculations = ? WHERE batch_id = ?", | ||
| speculationsJSON, batchID, | ||
| ) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to update speculations for batchID=%q: %w", batchID, err) | ||
| } | ||
|
|
||
| rowsAffected, err := result.RowsAffected() | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get rows affected from update for batchID=%q: %w", batchID, err) | ||
| } | ||
|
|
||
| if rowsAffected != 1 { | ||
| return storage.WrapNotFound(fmt.Errorf("speculation tree entity batchID=%s", batchID)) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package storage | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/uber/submitqueue/entity" | ||
| ) | ||
|
|
||
| // SpeculationTreeStore is an interface that defines methods for managing speculation trees in the database. | ||
| type SpeculationTreeStore interface { | ||
| // Get retrieves the speculation tree by batch ID. | ||
| // Returns ErrNotFound if the speculation tree is not found. | ||
| Get(ctx context.Context, batchID string) (entity.SpeculationTree, error) | ||
|
|
||
| // Create creates a new speculation tree. | ||
| // Returns ErrAlreadyExists if the entry already exists. | ||
| Create(ctx context.Context, speculationTree entity.SpeculationTree) error | ||
|
|
||
| // UpdateSpeculations updates the speculations of a speculation tree. | ||
| // Returns ErrNotFound if the speculation tree is not found. | ||
| UpdateSpeculations(ctx context.Context, batchID string, speculations []entity.SpeculationInfo) error | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.