github.com/BlockABC/godash@v0.0.0-20191112120524-f4aa3a32c566/database/ffldb/ldbtreapiter.go (about)

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