github.com/cockroachdb/pebble@v0.0.0-20231214172447-ab4952c5f87b/testdata/make-db.go (about)

     1  // Copyright 2023 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 main
     6  
     7  import (
     8  	"fmt"
     9  	"log"
    10  	"os"
    11  	"strconv"
    12  
    13  	"github.com/cockroachdb/pebble"
    14  )
    15  
    16  const version = pebble.FormatFlushableIngest
    17  
    18  func usage() {
    19  	fmt.Fprintf(os.Stderr, "usage: %s [1,2,3,4]\n", os.Args[0])
    20  	os.Exit(1)
    21  }
    22  
    23  func main() {
    24  	if len(os.Args) != 2 {
    25  		usage()
    26  	}
    27  	// The program consists of up to 4 stages. If stage is in the range [1, 4],
    28  	// the program will exit after the stage'th stage.
    29  	// 1. create an empty DB.
    30  	// 2. add some key/value pairs.
    31  	// 3. close and re-open the DB, which forces a compaction.
    32  	// 4. add some more key/value pairs.
    33  	stage, err := strconv.Atoi(os.Args[1])
    34  	if err != nil || stage < 1 || stage > 4 {
    35  		usage()
    36  	}
    37  	dbName := fmt.Sprintf("db-stage-%d", stage)
    38  	opts := &pebble.Options{
    39  		FormatMajorVersion: version,
    40  	}
    41  
    42  	fmt.Printf("Stage 1\n")
    43  	db, err := pebble.Open(dbName, opts)
    44  	if err != nil {
    45  		log.Fatal(err)
    46  	}
    47  	defer func() {
    48  		if db != nil {
    49  			db.Close()
    50  		}
    51  	}()
    52  
    53  	if stage < 2 {
    54  		return
    55  	}
    56  	fmt.Printf("Stage 2\n")
    57  
    58  	if err := db.Set([]byte("foo"), []byte("one"), pebble.Sync); err != nil {
    59  		log.Fatal(err)
    60  	}
    61  	if err := db.Set([]byte("bar"), []byte("two"), pebble.Sync); err != nil {
    62  		log.Fatal(err)
    63  	}
    64  	if err := db.Set([]byte("baz"), []byte("three"), pebble.Sync); err != nil {
    65  		log.Fatal(err)
    66  	}
    67  	if err := db.Set([]byte("foo"), []byte("four"), pebble.Sync); err != nil {
    68  		log.Fatal(err)
    69  	}
    70  	if err := db.Delete([]byte("bar"), pebble.Sync); err != nil {
    71  		log.Fatal(err)
    72  	}
    73  
    74  	if stage < 3 {
    75  		return
    76  	}
    77  	fmt.Printf("Stage 3\n")
    78  
    79  	db.Close()
    80  	db = nil
    81  	db, err = pebble.Open(dbName, opts)
    82  	if err != nil {
    83  		log.Fatal(err)
    84  	}
    85  
    86  	if stage < 4 {
    87  		return
    88  	}
    89  	fmt.Printf("Stage 4\n")
    90  
    91  	if err := db.Set([]byte("foo"), []byte("five"), pebble.Sync); err != nil {
    92  		log.Fatal(err)
    93  	}
    94  	if err := db.Set([]byte("quux"), []byte("six"), pebble.Sync); err != nil {
    95  		log.Fatal(err)
    96  	}
    97  	if err := db.Delete([]byte("baz"), pebble.Sync); err != nil {
    98  		log.Fatal(err)
    99  	}
   100  }