1 package ffldb
2 3 import (
4 "github.com/btcsuite/goleveldb/leveldb/iterator"
5 "github.com/btcsuite/goleveldb/leveldb/util"
6 7 "github.com/p9c/p9/pkg/util/treap"
8 )
9 10 // ldbTreapIter wraps a treap iterator to provide the additional functionality needed to satisfy the leveldb
11 // iterator.Iterator interface.
12 type ldbTreapIter struct {
13 *treap.Iterator
14 tx *transaction
15 released bool
16 }
17 18 // Enforce ldbTreapIter implements the leveldb iterator.Iterator interface.
19 var _ iterator.Iterator = (*ldbTreapIter)(nil)
20 21 // Error is only provided to satisfy the iterator interface as there are no errors for this memory-only structure.
22 //
23 // This is part of the leveldb iterator.Iterator interface implementation.
24 func (iter *ldbTreapIter) Error() (e error) {
25 return nil
26 }
27 28 // SetReleaser is only provided to satisfy the iterator interface as there is no need to override it.
29 //
30 // This is part of the leveldb iterator.Iterator interface implementation.
31 func (iter *ldbTreapIter) SetReleaser(releaser util.Releaser) {
32 }
33 34 // Release releases the iterator by removing the underlying treap iterator from the list of active iterators against the
35 // pending keys treap.
36 //
37 // This is part of the leveldb iterator.Iterator interface implementation.
38 func (iter *ldbTreapIter) Release() {
39 if !iter.released {
40 iter.tx.removeActiveIter(iter.Iterator)
41 iter.released = true
42 }
43 }
44 45 // newLdbTreapIter creates a new treap iterator for the given slice against the pending keys for the passed transaction
46 // and returns it wrapped in an ldbTreapIter so it can be used as a leveldb iterator. It also adds the new iterator to
47 // the list of active iterators for the transaction.
48 func newLdbTreapIter(tx *transaction, slice *util.Range) *ldbTreapIter {
49 iter := tx.pendingKeys.Iterator(slice.Start, slice.Limit)
50 tx.addActiveIter(iter)
51 return &ldbTreapIter{Iterator: iter, tx: tx}
52 }
53