github.com/cockroachdb/pebble@v1.1.2/iterator_example_test.go (about)

     1  // Copyright 2021 The LevelDB-Go and Pebble Authors. All rights reserved. Use
     2  // of this source code is governed by a BSD-style license that can be found in
     3  // the LICENSE file.
     4  
     5  package pebble_test
     6  
     7  import (
     8  	"fmt"
     9  	"log"
    10  
    11  	"github.com/cockroachdb/pebble"
    12  	"github.com/cockroachdb/pebble/vfs"
    13  )
    14  
    15  func ExampleIterator() {
    16  	db, err := pebble.Open("", &pebble.Options{FS: vfs.NewMem()})
    17  	if err != nil {
    18  		log.Fatal(err)
    19  	}
    20  
    21  	keys := []string{"hello", "world", "hello world"}
    22  	for _, key := range keys {
    23  		if err := db.Set([]byte(key), nil, pebble.Sync); err != nil {
    24  			log.Fatal(err)
    25  		}
    26  	}
    27  
    28  	iter, _ := db.NewIter(nil)
    29  	for iter.First(); iter.Valid(); iter.Next() {
    30  		fmt.Printf("%s\n", iter.Key())
    31  	}
    32  	if err := iter.Close(); err != nil {
    33  		log.Fatal(err)
    34  	}
    35  	if err := db.Close(); err != nil {
    36  		log.Fatal(err)
    37  	}
    38  	// Output:
    39  	// hello
    40  	// hello world
    41  	// world
    42  }
    43  
    44  func ExampleIterator_prefixIteration() {
    45  	db, err := pebble.Open("", &pebble.Options{FS: vfs.NewMem()})
    46  	if err != nil {
    47  		log.Fatal(err)
    48  	}
    49  
    50  	keyUpperBound := func(b []byte) []byte {
    51  		end := make([]byte, len(b))
    52  		copy(end, b)
    53  		for i := len(end) - 1; i >= 0; i-- {
    54  			end[i] = end[i] + 1
    55  			if end[i] != 0 {
    56  				return end[:i+1]
    57  			}
    58  		}
    59  		return nil // no upper-bound
    60  	}
    61  
    62  	prefixIterOptions := func(prefix []byte) *pebble.IterOptions {
    63  		return &pebble.IterOptions{
    64  			LowerBound: prefix,
    65  			UpperBound: keyUpperBound(prefix),
    66  		}
    67  	}
    68  
    69  	keys := []string{"hello", "world", "hello world"}
    70  	for _, key := range keys {
    71  		if err := db.Set([]byte(key), nil, pebble.Sync); err != nil {
    72  			log.Fatal(err)
    73  		}
    74  	}
    75  
    76  	iter, _ := db.NewIter(prefixIterOptions([]byte("hello")))
    77  	for iter.First(); iter.Valid(); iter.Next() {
    78  		fmt.Printf("%s\n", iter.Key())
    79  	}
    80  	if err := iter.Close(); err != nil {
    81  		log.Fatal(err)
    82  	}
    83  	if err := db.Close(); err != nil {
    84  		log.Fatal(err)
    85  	}
    86  	// Output:
    87  	// hello
    88  	// hello world
    89  }
    90  
    91  func ExampleIterator_SeekGE() {
    92  	db, err := pebble.Open("", &pebble.Options{FS: vfs.NewMem()})
    93  	if err != nil {
    94  		log.Fatal(err)
    95  	}
    96  
    97  	keys := []string{"hello", "world", "hello world"}
    98  	for _, key := range keys {
    99  		if err := db.Set([]byte(key), nil, pebble.Sync); err != nil {
   100  			log.Fatal(err)
   101  		}
   102  	}
   103  
   104  	iter, _ := db.NewIter(nil)
   105  	if iter.SeekGE([]byte("a")); iter.Valid() {
   106  		fmt.Printf("%s\n", iter.Key())
   107  	}
   108  	if iter.SeekGE([]byte("hello w")); iter.Valid() {
   109  		fmt.Printf("%s\n", iter.Key())
   110  	}
   111  	if iter.SeekGE([]byte("w")); iter.Valid() {
   112  		fmt.Printf("%s\n", iter.Key())
   113  	}
   114  	if err := iter.Close(); err != nil {
   115  		log.Fatal(err)
   116  	}
   117  	if err := db.Close(); err != nil {
   118  		log.Fatal(err)
   119  	}
   120  	// Output:
   121  	// hello
   122  	// hello world
   123  	// world
   124  }