github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/database/leveldb.chai2010/iterator.go (about)

     1  // Copyright 2013 <chaishushan{AT}gmail.com>. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package leveldb
     6  
     7  import (
     8  	"runtime"
     9  )
    10  
    11  // Iterator iterates over a DB's key/value pairs in key order.
    12  type Iterator struct {
    13  	*iteratorHandler
    14  }
    15  type iteratorHandler struct {
    16  	it *leveldb_iterator_t
    17  	ro *leveldb_readoptions_t
    18  }
    19  
    20  // NewIterator returns an Iterator over the the database that uses the
    21  // ReadOptions given.
    22  //
    23  // Often, this is used for large, offline bulk reads while serving live
    24  // traffic. In that case, it may be wise to disable caching so that the data
    25  // processed by the returned Iterator does not displace the already cached
    26  // data. This can be done by calling SetFillCache(false) on the ReadOptions
    27  // before passing it here.
    28  //
    29  // Similiarly, ReadOptions.SetSnapshot is also useful.
    30  func (p *dbHandler) NewIterator(opt *ReadOptions) *Iterator {
    31  	ro := leveldb_readoptions_create_copy(opt)
    32  	it := leveldb_create_iterator(p.db, ro)
    33  	h := &iteratorHandler{
    34  		it: it,
    35  		ro: ro,
    36  	}
    37  	runtime.SetFinalizer(h, (*iteratorHandler).Release)
    38  	return &Iterator{h}
    39  }
    40  
    41  // Release releases the Iterator.
    42  func (p *iteratorHandler) Release() {
    43  	runtime.SetFinalizer(p, nil)
    44  
    45  	leveldb_readoptions_destroy(p.ro)
    46  	leveldb_iter_destroy(p.it)
    47  	*p = iteratorHandler{}
    48  }
    49  
    50  // An iterator is either positioned at a key/value pair, or
    51  // not valid.  This method returns true iff the iterator is valid.
    52  func (p *iteratorHandler) Valid() bool {
    53  	return leveldb_iter_valid(p.it)
    54  }
    55  
    56  // Position at the first key in the source.  The iterator is Valid()
    57  // after this call iff the source is not empty.
    58  func (p *iteratorHandler) SeekToFirst() {
    59  	leveldb_iter_seek_to_first(p.it)
    60  }
    61  
    62  // Position at the last key in the source.  The iterator is
    63  // Valid() after this call iff the source is not empty.
    64  func (p *iteratorHandler) SeekToLast() {
    65  	leveldb_iter_seek_to_last(p.it)
    66  }
    67  
    68  // Position at the first key in the source that at or past target
    69  // The iterator is Valid() after this call iff the source contains
    70  // an entry that comes at or past target.
    71  func (p *iteratorHandler) Seek(target []byte) {
    72  	leveldb_iter_seek(p.it, target)
    73  }
    74  
    75  // Moves to the next entry in the source.  After this call, Valid() is
    76  // true iff the iterator was not positioned at the last entry in the source.
    77  // REQUIRES: Valid()
    78  func (p *iteratorHandler) Next() {
    79  	leveldb_iter_next(p.it)
    80  }
    81  
    82  // Moves to the previous entry in the source.  After this call, Valid() is
    83  // true iff the iterator was not positioned at the first entry in source.
    84  // REQUIRES: Valid()
    85  func (p *iteratorHandler) Prev() {
    86  	leveldb_iter_prev(p.it)
    87  }
    88  
    89  // Return the key for the current entry.
    90  //
    91  // The underlying storage for the returned slice is valid only until
    92  // the next modification of the iterator.
    93  func (p *iteratorHandler) Key() []byte {
    94  	k := leveldb_iter_key(p.it)
    95  	return leveldb_slice_data(k)
    96  }
    97  
    98  // Return the value for the current entry.
    99  //
   100  // The underlying storage for the returned slice is valid only until
   101  // the next modification of the iterator.
   102  func (p *iteratorHandler) Value() []byte {
   103  	k := leveldb_iter_value(p.it)
   104  	return leveldb_slice_data(k)
   105  }
   106  
   107  // GetError returns an *Error from LevelDB if it had one during iteration.
   108  //
   109  // This method is safe to call when Valid returns false.
   110  func (p *iteratorHandler) GetError() error {
   111  	return leveldb_iter_get_error(p.it)
   112  }