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