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

     1  // Copyright 2020 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  //go:build make_test_find_db
     6  // +build make_test_find_db
     7  
     8  // Run using: go run -tags make_test_find_db ./tool/make_test_find_db.go
     9  package main
    10  
    11  import (
    12  	"log"
    13  
    14  	"github.com/cockroachdb/pebble"
    15  	"github.com/cockroachdb/pebble/internal/base"
    16  	"github.com/cockroachdb/pebble/objstorage/objstorageprovider"
    17  	"github.com/cockroachdb/pebble/sstable"
    18  	"github.com/cockroachdb/pebble/vfs"
    19  )
    20  
    21  type db struct {
    22  	db       *pebble.DB
    23  	comparer *base.Comparer
    24  	merger   *base.Merger
    25  }
    26  
    27  func open(fs vfs.FS, dir string) *db {
    28  	c := *base.DefaultComparer
    29  	c.Name = "alt-comparer"
    30  
    31  	m := *base.DefaultMerger
    32  	m.Name = "test-merger"
    33  
    34  	lel := pebble.MakeLoggingEventListener(pebble.DefaultLogger)
    35  	d, err := pebble.Open(dir, &pebble.Options{
    36  		Cleaner:       pebble.ArchiveCleaner{},
    37  		Comparer:      &c,
    38  		EventListener: &lel,
    39  		FS:            fs,
    40  		Merger:        &m,
    41  	})
    42  	if err != nil {
    43  		log.Fatal(err)
    44  	}
    45  	return &db{
    46  		db:       d,
    47  		comparer: &c,
    48  		merger:   &m,
    49  	}
    50  }
    51  
    52  func (d *db) close() {
    53  	if err := d.db.Close(); err != nil {
    54  		log.Fatal(err)
    55  	}
    56  }
    57  
    58  func (d *db) set(key, value string) {
    59  	if err := d.db.Set([]byte(key), []byte(value), nil); err != nil {
    60  		log.Fatal(err)
    61  	}
    62  }
    63  
    64  func (d *db) merge(key, value string) {
    65  	if err := d.db.Merge([]byte(key), []byte(value), nil); err != nil {
    66  		log.Fatal(err)
    67  	}
    68  }
    69  
    70  func (d *db) delete(key string) {
    71  	if err := d.db.Delete([]byte(key), nil); err != nil {
    72  		log.Fatal(err)
    73  	}
    74  }
    75  
    76  func (d *db) singleDelete(key string) {
    77  	if err := d.db.SingleDelete([]byte(key), nil); err != nil {
    78  		log.Fatal(err)
    79  	}
    80  }
    81  
    82  func (d *db) deleteRange(start, end string) {
    83  	if err := d.db.DeleteRange([]byte(start), []byte(end), nil); err != nil {
    84  		log.Fatal(err)
    85  	}
    86  }
    87  
    88  func (d *db) ingest(keyVals ...string) {
    89  	const path = "tool/testdata/ingest.tmp"
    90  
    91  	if len(keyVals)%2 != 0 {
    92  		log.Fatalf("even number of key/values required")
    93  	}
    94  
    95  	fs := vfs.Default
    96  	f, err := fs.Create(path)
    97  	if err != nil {
    98  		log.Fatal(err)
    99  	}
   100  	w := sstable.NewWriter(objstorageprovider.NewFileWritable(f), sstable.WriterOptions{
   101  		Comparer:   d.comparer,
   102  		MergerName: d.merger.Name,
   103  	})
   104  
   105  	for i := 0; i < len(keyVals); i += 2 {
   106  		key := keyVals[i]
   107  		value := keyVals[i+1]
   108  		if err := w.Set([]byte(key), []byte(value)); err != nil {
   109  			log.Fatal(err)
   110  		}
   111  	}
   112  
   113  	if err := w.Close(); err != nil {
   114  		log.Fatal(err)
   115  	}
   116  
   117  	if err := d.db.Ingest([]string{path}); err != nil {
   118  		log.Fatal(err)
   119  	}
   120  }
   121  
   122  func (d *db) flush() {
   123  	if err := d.db.Flush(); err != nil {
   124  		log.Fatal(err)
   125  	}
   126  }
   127  
   128  func (d *db) compact(start, end string) {
   129  	if err := d.db.Compact([]byte(start), []byte(end), false); err != nil {
   130  		log.Fatal(err)
   131  	}
   132  }
   133  
   134  func (d *db) snapshot() *pebble.Snapshot {
   135  	return d.db.NewSnapshot()
   136  }
   137  
   138  func main() {
   139  	const dir = "tool/testdata/find-db"
   140  
   141  	fs := vfs.Default
   142  	if err := fs.RemoveAll(dir); err != nil {
   143  		log.Fatal(err)
   144  	}
   145  
   146  	d := open(fs, dir)
   147  	defer d.close()
   148  
   149  	d.set("aaa", "1")
   150  	d.set("bbb", "2")
   151  	d.merge("ccc", "3")
   152  	d.merge("ccc", "4")
   153  	d.merge("ccc", "5")
   154  	d.flush()
   155  	d.compact("a", "z")
   156  
   157  	defer d.snapshot().Close()
   158  
   159  	d.ingest("bbb", "22", "ccc", "6")
   160  	d.ingest("ddd", "33")
   161  	d.compact("a", "z")
   162  
   163  	defer d.snapshot().Close()
   164  
   165  	d.delete("aaa")
   166  	d.singleDelete("ccc")
   167  	d.deleteRange("bbb", "eee")
   168  	d.flush()
   169  
   170  	d.compact("a", "z")
   171  }