corruptRandomLeafPage corrupts one random leaf page.
(t testing.TB, db *bbolt.DB, bucketName []byte)
| 159 | |
| 160 | // corruptRandomLeafPage corrupts one random leaf page. |
| 161 | func corruptRandomLeafPageInBucket(t testing.TB, db *bbolt.DB, bucketName []byte) (victimPageId common.Pgid, validPageIds []common.Pgid) { |
| 162 | bucketRootPageId := mustGetBucketRootPage(t, db, bucketName) |
| 163 | bucketRootPage, _, err := guts_cli.ReadPage(db.Path(), uint64(bucketRootPageId)) |
| 164 | require.NoError(t, err) |
| 165 | require.True(t, bucketRootPage.IsBranchPage()) |
| 166 | |
| 167 | // Retrieve all the leaf pages included in the branch page, and pick up random one from them. |
| 168 | var bucketPageIds []common.Pgid |
| 169 | for _, bpe := range bucketRootPage.BranchPageElements() { |
| 170 | bucketPageIds = append(bucketPageIds, bpe.Pgid()) |
| 171 | } |
| 172 | randomIdx := rand.Intn(len(bucketPageIds)) |
| 173 | victimPageId = bucketPageIds[randomIdx] |
| 174 | validPageIds = append(bucketPageIds[:randomIdx], bucketPageIds[randomIdx+1:]...) |
| 175 | |
| 176 | victimPage, victimBuf, err := guts_cli.ReadPage(db.Path(), uint64(victimPageId)) |
| 177 | require.NoError(t, err) |
| 178 | require.True(t, victimPage.IsLeafPage()) |
| 179 | require.True(t, victimPage.Count() > 1) |
| 180 | |
| 181 | // intentionally make the second key < the first key. |
| 182 | element := victimPage.LeafPageElement(1) |
| 183 | key := element.Key() |
| 184 | key[0] = 0 |
| 185 | |
| 186 | // Write the corrupt page to db file. |
| 187 | err = guts_cli.WritePage(db.Path(), victimBuf) |
| 188 | require.NoError(t, err) |
| 189 | return victimPageId, validPageIds |
| 190 | } |
| 191 | |
| 192 | func corruptRootPage(t testing.TB, db *bbolt.DB, bucketName []byte) { |
| 193 | bucketRootPageId := mustGetBucketRootPage(t, db, bucketName) |
no test coverage detected