github.com/cockroachdb/pebble@v0.0.0-20231214172447-ab4952c5f87b/objstorage/objstorageprovider/remote_obj_name.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  
    10  	"github.com/cockroachdb/pebble/internal/base"
    11  	"github.com/cockroachdb/pebble/objstorage"
    12  )
    13  
    14  // remoteObjectName returns the name of an object on remote storage.
    15  //
    16  // For sstables, the format is: <hash>-<creator-id>-<file-num>.sst
    17  // For example: 1a3f-2-000001.sst
    18  func remoteObjectName(meta objstorage.ObjectMetadata) string {
    19  	if meta.Remote.CustomObjectName != "" {
    20  		return meta.Remote.CustomObjectName
    21  	}
    22  	switch meta.FileType {
    23  	case base.FileTypeTable:
    24  		return fmt.Sprintf(
    25  			"%04x-%d-%06d.sst",
    26  			objHash(meta), meta.Remote.CreatorID, meta.Remote.CreatorFileNum.FileNum(),
    27  		)
    28  	}
    29  	panic("unknown FileType")
    30  }
    31  
    32  // sharedObjectRefName returns the name of the object's ref marker associated
    33  // with a given referencing provider. This name is the object's name concatenated with
    34  // ".ref.<ref-creator-id>.<local-file-num>".
    35  //
    36  // For example: 1a3f-2-000001.sst.ref.5.000008
    37  func sharedObjectRefName(
    38  	meta objstorage.ObjectMetadata, refCreatorID objstorage.CreatorID, refFileNum base.DiskFileNum,
    39  ) string {
    40  	if meta.Remote.CleanupMethod != objstorage.SharedRefTracking {
    41  		panic("ref object used when ref tracking disabled")
    42  	}
    43  	if meta.Remote.CustomObjectName != "" {
    44  		return fmt.Sprintf(
    45  			"%s.ref.%d.%06d", meta.Remote.CustomObjectName, refCreatorID, refFileNum.FileNum(),
    46  		)
    47  	}
    48  	switch meta.FileType {
    49  	case base.FileTypeTable:
    50  		return fmt.Sprintf(
    51  			"%04x-%d-%06d.sst.ref.%d.%06d",
    52  			objHash(meta), meta.Remote.CreatorID, meta.Remote.CreatorFileNum.FileNum(), refCreatorID, refFileNum.FileNum(),
    53  		)
    54  	}
    55  	panic("unknown FileType")
    56  }
    57  
    58  func sharedObjectRefPrefix(meta objstorage.ObjectMetadata) string {
    59  	if meta.Remote.CustomObjectName != "" {
    60  		return meta.Remote.CustomObjectName + ".ref."
    61  	}
    62  	switch meta.FileType {
    63  	case base.FileTypeTable:
    64  		return fmt.Sprintf(
    65  			"%04x-%d-%06d.sst.ref.",
    66  			objHash(meta), meta.Remote.CreatorID, meta.Remote.CreatorFileNum.FileNum(),
    67  		)
    68  	}
    69  	panic("unknown FileType")
    70  }
    71  
    72  // sharedObjectRefName returns the name of the object's ref marker associated
    73  // with this provider. This name is the object's name concatenated with
    74  // ".ref.<creator-id>.<local-file-num>".
    75  //
    76  // For example: 1a3f-2-000001.sst.ref.5.000008
    77  func (p *provider) sharedObjectRefName(meta objstorage.ObjectMetadata) string {
    78  	if meta.Remote.CleanupMethod != objstorage.SharedRefTracking {
    79  		panic("ref object used when ref tracking disabled")
    80  	}
    81  	return sharedObjectRefName(meta, p.remote.shared.creatorID, meta.DiskFileNum)
    82  }
    83  
    84  // objHash returns a 16-bit hash value derived from the creator ID and creator
    85  // file num. We prepend this value to object names to ensure balanced
    86  // partitioning with AWS (and likely other blob storage providers).
    87  func objHash(meta objstorage.ObjectMetadata) uint16 {
    88  	const prime1 = 7459
    89  	const prime2 = 17539
    90  	return uint16(uint64(meta.Remote.CreatorID)*prime1 + uint64(meta.Remote.CreatorFileNum.FileNum())*prime2)
    91  }