github.com/cockroachdb/pebble@v0.0.0-20231214172447-ab4952c5f87b/objstorage/objstorageprovider/remote_obj_name_test.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 objstorageprovider
     6  
     7  import (
     8  	"fmt"
     9  	"math/rand"
    10  	"testing"
    11  
    12  	"github.com/cockroachdb/pebble/internal/base"
    13  	"github.com/cockroachdb/pebble/objstorage"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  func TestSharedObjectNames(t *testing.T) {
    18  	t.Run("crosscheck", func(t *testing.T) {
    19  		supportedFileTypes := []base.FileType{
    20  			base.FileTypeTable,
    21  		}
    22  		for it := 0; it < 100; it++ {
    23  			var meta objstorage.ObjectMetadata
    24  			meta.DiskFileNum = base.FileNum(rand.Intn(100000)).DiskFileNum()
    25  			meta.FileType = supportedFileTypes[rand.Int()%len(supportedFileTypes)]
    26  			meta.Remote.CreatorID = objstorage.CreatorID(rand.Int63())
    27  			meta.Remote.CreatorFileNum = base.FileNum(rand.Intn(100000)).DiskFileNum()
    28  			if rand.Intn(4) == 0 {
    29  				meta.Remote.CustomObjectName = fmt.Sprintf("foo-%d.sst", rand.Intn(10000))
    30  			}
    31  
    32  			obj := remoteObjectName(meta)
    33  			// Cross-check against cleaner implementations.
    34  			expObj := meta.Remote.CustomObjectName
    35  			if expObj == "" {
    36  				expObj = fmt.Sprintf("%04x-%s-%s", objHash(meta), meta.Remote.CreatorID, base.MakeFilename(meta.FileType, meta.Remote.CreatorFileNum))
    37  			}
    38  			require.Equal(t, expObj, obj)
    39  
    40  			require.Equal(t, expObj+".ref.", sharedObjectRefPrefix(meta))
    41  
    42  			refCreatorID := objstorage.CreatorID(rand.Int63())
    43  			refObj := sharedObjectRefName(meta, refCreatorID, meta.DiskFileNum)
    44  			expRefObj := fmt.Sprintf("%s.ref.%s.%s", expObj, refCreatorID, meta.DiskFileNum)
    45  			require.Equal(t, refObj, expRefObj)
    46  		}
    47  	})
    48  
    49  	t.Run("example", func(t *testing.T) {
    50  		var meta objstorage.ObjectMetadata
    51  		meta.DiskFileNum = base.FileNum(123).DiskFileNum()
    52  		meta.FileType = base.FileTypeTable
    53  		meta.Remote.CreatorID = objstorage.CreatorID(456)
    54  		meta.Remote.CreatorFileNum = base.FileNum(789).DiskFileNum()
    55  		require.Equal(t, remoteObjectName(meta), "0e17-456-000789.sst")
    56  		require.Equal(t, sharedObjectRefPrefix(meta), "0e17-456-000789.sst.ref.")
    57  
    58  		refCreatorID := objstorage.CreatorID(101112)
    59  		require.Equal(
    60  			t, sharedObjectRefName(meta, refCreatorID, meta.DiskFileNum),
    61  			"0e17-456-000789.sst.ref.101112.000123",
    62  		)
    63  	})
    64  }