-
Notifications
You must be signed in to change notification settings - Fork 502
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
exp/ingest/io: Refactor readers #2644
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 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 was deleted.
Oops, something went wrong.
This file contains 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,114 @@ | ||
package io | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/stellar/go/exp/ingest/ledgerbackend" | ||
) | ||
|
||
// ChangeReader provides convenient, streaming access to a sequence of Changes. | ||
type ChangeReader interface { | ||
// Read should return the next `Change` in the leader. If there are no more | ||
// changes left it should return an `io.EOF` error. | ||
Read() (Change, error) | ||
// Close should be called when reading is finished. This is especially | ||
// helpful when there are still some changes available so reader can stop | ||
// streaming them. | ||
Close() error | ||
} | ||
|
||
type ledgerChangeReaderState int | ||
|
||
const ( | ||
feeChangesState ledgerChangeReaderState = iota | ||
metaChangesState | ||
upgradeChangesState | ||
) | ||
|
||
// LedgerChangeReader is a ChangeReader which returns Changes from Stellar Core | ||
// for a single ledger | ||
type LedgerChangeReader struct { | ||
*LedgerTransactionReader | ||
state ledgerChangeReaderState | ||
pending []Change | ||
pendingIndex int | ||
upgradeIndex int | ||
} | ||
|
||
// Ensure LedgerChangeReader implements ChangeReader | ||
var _ ChangeReader = (*LedgerChangeReader)(nil) | ||
|
||
// NewLedgerChangeReader constructs a new LedgerChangeReader instance bound to the given ledger. | ||
// Note that the returned LedgerChangeReader is not thread safe and should not be shared | ||
// by multiple goroutines. | ||
func NewLedgerChangeReader(backend ledgerbackend.LedgerBackend, sequence uint32) (*LedgerChangeReader, error) { | ||
transactionReader, err := NewLedgerTransactionReader(backend, sequence) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &LedgerChangeReader{ | ||
LedgerTransactionReader: transactionReader, | ||
state: feeChangesState, | ||
}, nil | ||
} | ||
|
||
// Read returns the next change in the stream. | ||
// If there are no changes remaining io.EOF is returned as an error. | ||
func (r *LedgerChangeReader) Read() (Change, error) { | ||
if r.pendingIndex < len(r.pending) { | ||
next := r.pending[r.pendingIndex] | ||
r.pendingIndex++ | ||
if r.pendingIndex == len(r.pending) { | ||
r.pendingIndex = 0 | ||
r.pending = r.pending[:0] | ||
} | ||
return next, nil | ||
} | ||
|
||
switch r.state { | ||
case feeChangesState, metaChangesState: | ||
tx, err := r.LedgerTransactionReader.Read() | ||
if err != nil { | ||
if err == io.EOF { | ||
// If done streaming fee changes rewind to stream meta changes | ||
if r.state == feeChangesState { | ||
r.LedgerTransactionReader.Rewind() | ||
} | ||
r.state++ | ||
return r.Read() | ||
} | ||
return Change{}, err | ||
} | ||
|
||
switch r.state { | ||
case feeChangesState: | ||
r.pending = append(r.pending, tx.GetFeeChanges()...) | ||
case metaChangesState: | ||
metaChanges, err := tx.GetChanges() | ||
if err != nil { | ||
return Change{}, err | ||
} | ||
r.pending = append(r.pending, metaChanges...) | ||
} | ||
return r.Read() | ||
case upgradeChangesState: | ||
// Get upgrade changes | ||
if r.upgradeIndex < len(r.LedgerTransactionReader.LedgerReader.ledgerCloseMeta.UpgradesMeta) { | ||
changes := GetChangesFromLedgerEntryChanges( | ||
r.LedgerTransactionReader.LedgerReader.ledgerCloseMeta.UpgradesMeta[r.upgradeIndex], | ||
) | ||
r.pending = append(r.pending, changes...) | ||
r.upgradeIndex++ | ||
return r.Read() | ||
} | ||
} | ||
|
||
return Change{}, io.EOF | ||
} | ||
|
||
// Close should be called when reading is finished. | ||
func (r *LedgerChangeReader) Close() error { | ||
r.pending = nil | ||
return r.LedgerTransactionReader.Close() | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bartekn would it make sense to put this whole section in its own function to better describe what this is doing? it seems like this function not only "reads" but also if there are no pending, loads tx data and fills pending.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be helpful to document
state
and how we use it to move across the different kind of changesThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a comment inside this method that explains what it's doing.
I can move parts to separate function but since this method actually fits the screen I think it's more readable that way. Let me know what you think.