MCPcopy Create free account
hub / github.com/Shopify/ghostferry / Each

Method Each

cursor.go:87–159  ·  view source on GitHub ↗
(f func(*RowBatch) error)

Source from the content-addressed store, hash-verified

85}
86
87func (c *Cursor) Each(f func(*RowBatch) error) error {
88 c.logger = LogWithFields(Fields{
89 "table": c.Table.String(),
90 "tag": "cursor",
91 })
92 c.paginationKeyColumn = c.Table.GetPaginationColumn()
93
94 if len(c.ColumnsToSelect) == 0 {
95 c.ColumnsToSelect = []string{"*"}
96 }
97
98 for c.lastSuccessfulPaginationKey.Compare(c.MaxPaginationKey) < 0 {
99 var tx SqlPreparerAndRollbacker
100 var batch *RowBatch
101 var paginationKeypos PaginationKey
102
103 err := WithRetries(c.ReadRetries, 1*time.Second, c.logger, "fetch rows", func() (err error) {
104 if c.Throttler != nil {
105 WaitForThrottle(c.Throttler)
106 }
107
108 // Only need to use a transaction if RowLock == true. Otherwise
109 // we'd be wasting two extra round trips per batch, doing
110 // essentially a no-op.
111 if c.RowLock {
112 tx, err = c.DB.Begin()
113 if err != nil {
114 return err
115 }
116 } else {
117 tx = &SqlDBWithFakeRollback{c.DB}
118 }
119
120 batch, paginationKeypos, err = c.Fetch(tx)
121 if err == nil {
122 return nil
123 }
124
125 tx.Rollback()
126 return err
127 })
128
129 if err != nil {
130 return err
131 }
132
133 if batch.Size() == 0 {
134 tx.Rollback()
135 c.logger.Debug("did not reach max primary key, but the table is complete as there are no more rows")
136 break
137 }
138
139 if paginationKeypos.Compare(c.lastSuccessfulPaginationKey) <= 0 {
140 tx.Rollback()
141 err = fmt.Errorf("new paginationKeypos %s <= lastSuccessfulPaginationKey %s", paginationKeypos.String(), c.lastSuccessfulPaginationKey.String())
142 c.logger.WithError(err).Errorf("last successful paginationKey position did not advance")
143 return err
144 }

Callers 2

RunMethod · 0.95

Calls 14

StringMethod · 0.95
CompareMethod · 0.95
FetchMethod · 0.95
RollbackMethod · 0.95
SizeMethod · 0.95
LogWithFieldsFunction · 0.85
WithRetriesFunction · 0.85
WaitForThrottleFunction · 0.85
GetPaginationColumnMethod · 0.80
BeginMethod · 0.80
DebugMethod · 0.65
ErrorfMethod · 0.65

Tested by

no test coverage detected