check readonly on batch append
This commit is contained in:
		@@ -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)
 | 
			
		||||
	}
 | 
			
		||||
@@ -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)
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -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")
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user