## 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)
120 lines
2.6 KiB
Go
120 lines
2.6 KiB
Go
package ormtable
|
|
|
|
import "github.com/cosmos/cosmos-sdk/orm/model/kv"
|
|
|
|
type batchIndexCommitmentWriter struct {
|
|
Backend
|
|
commitmentWriter *batchStoreWriter
|
|
indexWriter *batchStoreWriter
|
|
}
|
|
|
|
func newBatchIndexCommitmentWriter(store Backend) *batchIndexCommitmentWriter {
|
|
return &batchIndexCommitmentWriter{
|
|
Backend: store,
|
|
commitmentWriter: &batchStoreWriter{
|
|
ReadonlyStore: store.CommitmentStoreReader(),
|
|
curBuf: make([]*batchWriterEntry, 0, capacity),
|
|
},
|
|
indexWriter: &batchStoreWriter{
|
|
ReadonlyStore: store.IndexStoreReader(),
|
|
curBuf: make([]*batchWriterEntry, 0, capacity),
|
|
},
|
|
}
|
|
}
|
|
|
|
func (w *batchIndexCommitmentWriter) CommitmentStore() kv.Store {
|
|
return w.commitmentWriter
|
|
}
|
|
|
|
func (w *batchIndexCommitmentWriter) IndexStore() kv.Store {
|
|
return w.indexWriter
|
|
}
|
|
|
|
// Write flushes any pending writes.
|
|
func (w *batchIndexCommitmentWriter) Write() error {
|
|
err := flushWrites(w.Backend.CommitmentStore(), w.commitmentWriter)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = flushWrites(w.Backend.IndexStore(), w.indexWriter)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// clear writes
|
|
w.Close()
|
|
|
|
return err
|
|
}
|
|
|
|
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 := store.Set(write.key, write.value)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
err := store.Delete(write.key)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Close discards any pending writes and should generally be called using
|
|
// a defer statement.
|
|
func (w *batchIndexCommitmentWriter) Close() {
|
|
w.commitmentWriter.prevBufs = nil
|
|
w.commitmentWriter.curBuf = nil
|
|
w.indexWriter.prevBufs = nil
|
|
w.indexWriter.curBuf = nil
|
|
}
|
|
|
|
type batchWriterEntry struct {
|
|
key, value []byte
|
|
delete bool
|
|
}
|
|
|
|
type batchStoreWriter struct {
|
|
kv.ReadonlyStore
|
|
prevBufs [][]*batchWriterEntry
|
|
curBuf []*batchWriterEntry
|
|
}
|
|
|
|
const capacity = 16
|
|
|
|
func (b *batchStoreWriter) Set(key, value []byte) error {
|
|
b.append(&batchWriterEntry{key: key, value: value})
|
|
return nil
|
|
}
|
|
|
|
func (b *batchStoreWriter) Delete(key []byte) error {
|
|
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{}
|