github.com/cockroachdb/pebble@v1.1.1-0.20240513155919-3622ade60459/tool/make_test_remotecat.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  //go:build make_test_remotecat
     6  // +build make_test_remotecat
     7  
     8  // Run using: go run -tags make_test_remotecat ./tool/make_test_remotecat.go
     9  package main
    10  
    11  import (
    12  	"log"
    13  	"os"
    14  	"path/filepath"
    15  
    16  	"github.com/cockroachdb/pebble/internal/base"
    17  	"github.com/cockroachdb/pebble/objstorage"
    18  	"github.com/cockroachdb/pebble/objstorage/objstorageprovider/remoteobjcat"
    19  	"github.com/cockroachdb/pebble/vfs"
    20  )
    21  
    22  func main() {
    23  	dir, err := os.MkdirTemp("", "make-test-remotecat")
    24  	if err != nil {
    25  		log.Fatal(err)
    26  	}
    27  	catalog, _, err := remoteobjcat.Open(vfs.Default, dir)
    28  	if err != nil {
    29  		log.Fatal(err)
    30  	}
    31  	if err := catalog.SetCreatorID(3); err != nil {
    32  		log.Fatal(err)
    33  	}
    34  
    35  	var b remoteobjcat.Batch
    36  	b.AddObject(remoteobjcat.RemoteObjectMetadata{
    37  		FileNum:        base.FileNum(1).DiskFileNum(),
    38  		FileType:       base.FileTypeTable,
    39  		CreatorID:      3,
    40  		CreatorFileNum: base.FileNum(1).DiskFileNum(),
    41  		CleanupMethod:  objstorage.SharedRefTracking,
    42  		Locator:        "foo",
    43  	})
    44  	if err := catalog.ApplyBatch(b); err != nil {
    45  		log.Fatal(err)
    46  	}
    47  	b.Reset()
    48  	b.AddObject(remoteobjcat.RemoteObjectMetadata{
    49  		FileNum:        base.FileNum(2).DiskFileNum(),
    50  		FileType:       base.FileTypeTable,
    51  		CreatorID:      5,
    52  		CreatorFileNum: base.FileNum(10).DiskFileNum(),
    53  		CleanupMethod:  objstorage.SharedRefTracking,
    54  		Locator:        "foo",
    55  	})
    56  	b.DeleteObject(base.FileNum(1).DiskFileNum())
    57  	b.AddObject(remoteobjcat.RemoteObjectMetadata{
    58  		FileNum:          base.FileNum(3).DiskFileNum(),
    59  		FileType:         base.FileTypeTable,
    60  		CleanupMethod:    objstorage.SharedRefTracking,
    61  		Locator:          "bar",
    62  		CustomObjectName: "external.sst",
    63  	})
    64  	if err := catalog.ApplyBatch(b); err != nil {
    65  		log.Fatal(err)
    66  	}
    67  	if err := catalog.Close(); err != nil {
    68  		log.Fatal(err)
    69  	}
    70  	contents, err := os.ReadFile(filepath.Join(dir, "REMOTE-OBJ-CATALOG-000001"))
    71  	if err != nil {
    72  		log.Fatal(err)
    73  	}
    74  	if err := os.WriteFile("tool/testdata/REMOTE-OBJ-CATALOG", contents, 0666); err != nil {
    75  		log.Fatal(err)
    76  	}
    77  }