check readonly on batch append

This commit is contained in:
Sina Mahmoodi 2022-01-11 10:09:57 +01:00
parent 208c7a9d45
commit 2ddb5ec4ba
2 changed files with 15 additions and 6 deletions

View File

@ -115,6 +115,11 @@ func (batch *freezerTableBatch) reset() {
// precautionary parameter to ensure data correctness, but the table will reject already // precautionary parameter to ensure data correctness, but the table will reject already
// existing data. // existing data.
func (batch *freezerTableBatch) Append(item uint64, data interface{}) error { 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 { if item != batch.curItem {
return fmt.Errorf("%w: have %d want %d", errOutOrderInsertion, item, batch.curItem) return fmt.Errorf("%w: have %d want %d", errOutOrderInsertion, item, batch.curItem)
} }
@ -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 // precautionary parameter to ensure data correctness, but the table will reject already
// existing data. // existing data.
func (batch *freezerTableBatch) AppendRaw(item uint64, blob []byte) error { 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 { if item != batch.curItem {
return fmt.Errorf("%w: have %d want %d", errOutOrderInsertion, item, batch.curItem) return fmt.Errorf("%w: have %d want %d", errOutOrderInsertion, item, batch.curItem)
} }

View File

@ -903,13 +903,12 @@ func TestFreezerReadonlyBasics(t *testing.T) {
if !bytes.Equal(v, exp) { if !bytes.Equal(v, exp) {
t.Errorf("retrieved value is incorrect") 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() batch := f.newBatch()
writeErr := batch.AppendRaw(32, make([]byte, 1)) if err := batch.AppendRaw(32, make([]byte, 1)); err == nil {
if writeErr == nil { t.Errorf("Writing to readonly table should fail")
writeErr = batch.commit()
} }
if writeErr == nil { if err := batch.Append(0, make([]byte, 1)); err == nil {
t.Fatalf("Writing to readonly table should fail") t.Errorf("Writing to readonly table should fail")
} }
} }