Delete removes a key from the bucket. If the key does not exist then nothing is done and a nil error is returned. Returns an error if the bucket was created from a read-only transaction.
(key []byte)
| 497 | // If the key does not exist then nothing is done and a nil error is returned. |
| 498 | // Returns an error if the bucket was created from a read-only transaction. |
| 499 | func (b *Bucket) Delete(key []byte) (err error) { |
| 500 | if lg := b.tx.db.Logger(); lg != discardLogger { |
| 501 | lg.Debugf("Deleting key %q", key) |
| 502 | defer func() { |
| 503 | if err != nil { |
| 504 | lg.Errorf("Deleting key %q failed: %v", key, err) |
| 505 | } else { |
| 506 | lg.Debugf("Deleting key %q successfully", key) |
| 507 | } |
| 508 | }() |
| 509 | } |
| 510 | |
| 511 | if b.tx.db == nil { |
| 512 | return errors.ErrTxClosed |
| 513 | } else if !b.Writable() { |
| 514 | return errors.ErrTxNotWritable |
| 515 | } |
| 516 | |
| 517 | // Move cursor to correct position. |
| 518 | c := b.Cursor() |
| 519 | k, _, flags := c.seek(key) |
| 520 | |
| 521 | // Return nil if the key doesn't exist. |
| 522 | if !bytes.Equal(key, k) { |
| 523 | return nil |
| 524 | } |
| 525 | |
| 526 | // Return an error if there is already existing bucket value. |
| 527 | if (flags & common.BucketLeafFlag) != 0 { |
| 528 | return errors.ErrIncompatibleValue |
| 529 | } |
| 530 | |
| 531 | // Delete the node if we have a matching key. |
| 532 | c.node().del(key) |
| 533 | |
| 534 | return nil |
| 535 | } |
| 536 | |
| 537 | // Sequence returns the current integer for the bucket without incrementing it. |
| 538 | func (b *Bucket) Sequence() uint64 { |