Skip to content

Commit

Permalink
Add an option to create MemDB iterators with no Mutex for the CacheKV…
Browse files Browse the repository at this point in the history
…Store (#187)

* Add an option to create MemDB iterators with no Mutex for the CacheKVStore

* Update changelog
  • Loading branch information
ValarDragon authored Oct 10, 2021
1 parent 6f9a08c commit c6724ca
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

.idea
.idea
vendor/*
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Pending

* Add an option for creating a mem db with no mutex

## 0.6.4

**2021-02-09**
Expand Down
16 changes: 16 additions & 0 deletions memdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,19 @@ func (db *MemDB) ReverseIterator(start, end []byte) (Iterator, error) {
}
return newMemDBIterator(db, start, end, true), nil
}

// IteratorNoMtx makes an iterator with no mutex.
func (db *MemDB) IteratorNoMtx(start, end []byte) (Iterator, error) {
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) {
return nil, errKeyEmpty
}
return newMemDBIteratorMtxChoice(db, start, end, false, false), nil
}

// ReverseIteratorNoMtx makes an iterator with no mutex.
func (db *MemDB) ReverseIteratorNoMtx(start, end []byte) (Iterator, error) {
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) {
return nil, errKeyEmpty
}
return newMemDBIteratorMtxChoice(db, start, end, true, false), nil
}
14 changes: 12 additions & 2 deletions memdb_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,34 @@ type memDBIterator struct {
item *item
start []byte
end []byte
useMtx bool
}

var _ Iterator = (*memDBIterator)(nil)

// newMemDBIterator creates a new memDBIterator.
func newMemDBIterator(db *MemDB, start []byte, end []byte, reverse bool) *memDBIterator {
return newMemDBIteratorMtxChoice(db, start, end, reverse, true)
}

func newMemDBIteratorMtxChoice(db *MemDB, start []byte, end []byte, reverse bool, useMtx bool) *memDBIterator {
ctx, cancel := context.WithCancel(context.Background())
ch := make(chan *item, chBufferSize)
iter := &memDBIterator{
ch: ch,
cancel: cancel,
start: start,
end: end,
useMtx: useMtx,
}

db.mtx.RLock()
if useMtx {
db.mtx.RLock()
}
go func() {
defer db.mtx.RUnlock()
if useMtx {
defer db.mtx.RUnlock()
}
// Because we use [start, end) for reverse ranges, while btree uses (start, end], we need
// the following variables to handle some reverse iteration conditions ourselves.
var (
Expand Down

0 comments on commit c6724ca

Please sign in to comment.