Skip to content

Commit

Permalink
check readonly on batch append
Browse files Browse the repository at this point in the history
  • Loading branch information
s1na committed Jan 11, 2022
1 parent 208c7a9 commit 2ddb5ec
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
10 changes: 10 additions & 0 deletions core/rawdb/freezer_batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ func (batch *freezerTableBatch) reset() {
// precautionary parameter to ensure data correctness, but the table will reject already
// existing data.
func (batch *freezerTableBatch) Append(item uint64, data interface{}) error {
// Sanity check. In principle this method shouldn't be called at all
// in readonly mode.
if batch.t.readonly {
return fmt.Errorf("permission error: table in readonly mode")
}
if item != batch.curItem {
return fmt.Errorf("%w: have %d want %d", errOutOrderInsertion, item, batch.curItem)
}
Expand All @@ -135,6 +140,11 @@ func (batch *freezerTableBatch) Append(item uint64, data interface{}) error {
// precautionary parameter to ensure data correctness, but the table will reject already
// existing data.
func (batch *freezerTableBatch) AppendRaw(item uint64, blob []byte) error {
// Sanity check. In principle this method shouldn't be called at all
// in readonly mode.
if batch.t.readonly {
return fmt.Errorf("permission error: table in readonly mode")
}
if item != batch.curItem {
return fmt.Errorf("%w: have %d want %d", errOutOrderInsertion, item, batch.curItem)
}
Expand Down
11 changes: 5 additions & 6 deletions core/rawdb/freezer_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -903,13 +903,12 @@ func TestFreezerReadonlyBasics(t *testing.T) {
if !bytes.Equal(v, exp) {
t.Errorf("retrieved value is incorrect")
}
// Now write some data. This should fail either during AppendRaw or Commit
// Now write some data. Append/AppendRaw should fail.
batch := f.newBatch()
writeErr := batch.AppendRaw(32, make([]byte, 1))
if writeErr == nil {
writeErr = batch.commit()
if err := batch.AppendRaw(32, make([]byte, 1)); err == nil {
t.Errorf("Writing to readonly table should fail")
}
if writeErr == nil {
t.Fatalf("Writing to readonly table should fail")
if err := batch.Append(0, make([]byte, 1)); err == nil {
t.Errorf("Writing to readonly table should fail")
}
}

0 comments on commit 2ddb5ec

Please sign in to comment.