feat(orm): range/prefix deletion support (#11103)

## Description

This adds `DeleteBy...` methods to the ORM codegen for the primary key + every unique index.



---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
This commit is contained in:
Aaron Craelius
2022-02-03 10:04:02 +00:00
committed by GitHub
parent 5126ec3c79
commit 63a248ef03
12 changed files with 398 additions and 54 deletions
+36 -13
View File
@@ -11,14 +11,13 @@ type batchIndexCommitmentWriter struct {
func newBatchIndexCommitmentWriter(store Backend) *batchIndexCommitmentWriter {
return &batchIndexCommitmentWriter{
Backend: store,
// optimal array capacities are estimated here:
commitmentWriter: &batchStoreWriter{
ReadonlyStore: store.CommitmentStoreReader(),
writes: make([]batchWriterEntry, 0, 2),
curBuf: make([]*batchWriterEntry, 0, capacity),
},
indexWriter: &batchStoreWriter{
ReadonlyStore: store.IndexStoreReader(),
writes: make([]batchWriterEntry, 0, 16),
curBuf: make([]*batchWriterEntry, 0, capacity),
},
}
}
@@ -33,12 +32,12 @@ func (w *batchIndexCommitmentWriter) IndexStore() kv.Store {
// Write flushes any pending writes.
func (w *batchIndexCommitmentWriter) Write() error {
err := flushWrites(w.Backend.CommitmentStore(), w.commitmentWriter.writes)
err := flushWrites(w.Backend.CommitmentStore(), w.commitmentWriter)
if err != nil {
return err
}
err = flushWrites(w.Backend.IndexStore(), w.indexWriter.writes)
err = flushWrites(w.Backend.IndexStore(), w.indexWriter)
if err != nil {
return err
}
@@ -49,15 +48,25 @@ func (w *batchIndexCommitmentWriter) Write() error {
return err
}
func flushWrites(writer kv.Store, writes []batchWriterEntry) error {
func flushWrites(store kv.Store, writer *batchStoreWriter) error {
for _, buf := range writer.prevBufs {
err := flushBuf(store, buf)
if err != nil {
return err
}
}
return flushBuf(store, writer.curBuf)
}
func flushBuf(store kv.Store, writes []*batchWriterEntry) error {
for _, write := range writes {
if !write.delete {
err := writer.Set(write.key, write.value)
err := store.Set(write.key, write.value)
if err != nil {
return err
}
} else {
err := writer.Delete(write.key)
err := store.Delete(write.key)
if err != nil {
return err
}
@@ -69,8 +78,10 @@ func flushWrites(writer kv.Store, writes []batchWriterEntry) error {
// Close discards any pending writes and should generally be called using
// a defer statement.
func (w *batchIndexCommitmentWriter) Close() {
w.commitmentWriter.writes = nil
w.indexWriter.writes = nil
w.commitmentWriter.prevBufs = nil
w.commitmentWriter.curBuf = nil
w.indexWriter.prevBufs = nil
w.indexWriter.curBuf = nil
}
type batchWriterEntry struct {
@@ -80,17 +91,29 @@ type batchWriterEntry struct {
type batchStoreWriter struct {
kv.ReadonlyStore
writes []batchWriterEntry
prevBufs [][]*batchWriterEntry
curBuf []*batchWriterEntry
}
const capacity = 16
func (b *batchStoreWriter) Set(key, value []byte) error {
b.writes = append(b.writes, batchWriterEntry{key: key, value: value})
b.append(&batchWriterEntry{key: key, value: value})
return nil
}
func (b *batchStoreWriter) Delete(key []byte) error {
b.writes = append(b.writes, batchWriterEntry{key: key, delete: true})
b.append(&batchWriterEntry{key: key, delete: true})
return nil
}
func (b *batchStoreWriter) append(entry *batchWriterEntry) {
if len(b.curBuf) == capacity {
b.prevBufs = append(b.prevBufs, b.curBuf)
b.curBuf = make([]*batchWriterEntry, 0, capacity)
}
b.curBuf = append(b.curBuf, entry)
}
var _ Backend = &batchIndexCommitmentWriter{}