github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/gen/fb/serial/fileidentifiers.go (about)

     1  // Copyright 2022 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package serial
    16  
    17  import (
    18  	"encoding/binary"
    19  	"unsafe"
    20  
    21  	fb "github.com/dolthub/flatbuffers/v23/go"
    22  )
    23  
    24  // KEEP THESE IN SYNC WITH .fbs FILES!
    25  
    26  const StoreRootFileID = "STRT"
    27  const TagFileID = "DTAG"
    28  const WorkingSetFileID = "WRST"
    29  const CommitFileID = "DCMT"
    30  const RootValueFileID = "RTVL"
    31  const TableFileID = "DTBL"
    32  const ProllyTreeNodeFileID = "TUPM"
    33  const AddressMapFileID = "ADRM"
    34  const CommitClosureFileID = "CMCL"
    35  const TableSchemaFileID = "DSCH"
    36  const ForeignKeyCollectionFileID = "DFKC"
    37  const MergeArtifactsFileID = "ARTM"
    38  const BlobFileID = "BLOB"
    39  const BranchControlFileID = "BRCL"
    40  const StashListFileID = "SLST"
    41  const StashFileID = "STSH"
    42  const StatisticFileID = "STAT"
    43  const DoltgresRootValueFileID = "DGRV"
    44  
    45  const MessageTypesKind int = 27
    46  
    47  const MessagePrefixSz = 4
    48  
    49  type Message []byte
    50  
    51  func GetFileID(bs []byte) string {
    52  	if len(bs) < 8+MessagePrefixSz {
    53  		return ""
    54  	}
    55  	return byteSliceToString(bs[MessagePrefixSz+4 : MessagePrefixSz+8])
    56  }
    57  
    58  func FinishMessage(b *fb.Builder, off fb.UOffsetT, fileID []byte) Message {
    59  	// We finish the buffer by prefixing it with:
    60  	// 1) 1 byte NomsKind == SerialMessage.
    61  	// 2) big endian 3 byte uint representing the size of the message, not
    62  	// including the kind or size prefix bytes.
    63  	//
    64  	// This allows chunks we serialize here to be read by types binary
    65  	// codec.
    66  	//
    67  	// All accessors in this package expect this prefix to be on the front
    68  	// of the message bytes as well. See |MessagePrefixSz|.
    69  
    70  	b.Prep(1, fb.SizeInt32+4+MessagePrefixSz)
    71  	b.FinishWithFileIdentifier(off, fileID)
    72  
    73  	var size [4]byte
    74  	binary.BigEndian.PutUint32(size[:], uint32(len(b.Bytes)-int(b.Head())))
    75  	if size[0] != 0 {
    76  		panic("message is too large to be encoded")
    77  	}
    78  
    79  	bytes := b.Bytes[b.Head()-MessagePrefixSz:]
    80  	bytes[0] = byte(MessageTypesKind)
    81  	copy(bytes[1:], size[1:])
    82  	return bytes
    83  }
    84  
    85  // byteSliceToString converts a []byte to string without a heap allocation.
    86  // copied from github.com/google/flatbuffers/go/sizes.go
    87  func byteSliceToString(b []byte) string {
    88  	return *(*string)(unsafe.Pointer(&b))
    89  }