Debian Codegraph @

plumbing/format/commitgraph/v2/chunk.go

raw
1 package v2
2
3 import "bytes"
4
5 const (
6 szChunkSig = 4 // Length of a chunk signature
7 chunkSigOffset = 4 // Offset of each chunk signature in chunkSignatures
8 )
9
10 // chunkSignatures contains the coalesced byte signatures for each chunk type.
11 // The order of the signatures must match the order of the ChunkType constants.
12 // (When adding new chunk types you must avoid introducing ambiguity, and you may need to add padding separators to this list or reorder these signatures.)
13 // (i.e. it would not be possible to add a new chunk type with the signature "IDFO" without some reordering or the addition of separators.)
14 var chunkSignatures = []byte("OIDFOIDLCDATGDA2GDO2EDGEBIDXBDATBASE\000\000\000\000")
15
16 // ChunkType represents the type of a chunk in the commit graph file.
17 type ChunkType int
18
19 const (
20 OIDFanoutChunk ChunkType = iota // "OIDF"
21 OIDLookupChunk // "OIDL"
22 CommitDataChunk // "CDAT"
23 GenerationDataChunk // "GDA2"
24 GenerationDataOverflowChunk // "GDO2"
25 ExtraEdgeListChunk // "EDGE"
26 BloomFilterIndexChunk // "BIDX"
27 BloomFilterDataChunk // "BDAT"
28 BaseGraphsListChunk // "BASE"
29 ZeroChunk // "\000\000\000\000"
30 )
31 const lenChunks = int(ZeroChunk) // ZeroChunk is not a valid chunk type, but it is used to determine the length of the chunk type list.
32
33 // Signature returns the byte signature for the chunk type.
34 func (ct ChunkType) Signature() []byte {
35 if ct >= BaseGraphsListChunk || ct < 0 { // not a valid chunk type just return ZeroChunk
36 return chunkSignatures[ZeroChunk*chunkSigOffset : ZeroChunk*chunkSigOffset+szChunkSig]
37 }
38
39 return chunkSignatures[ct*chunkSigOffset : ct*chunkSigOffset+szChunkSig]
40 }
41
42 // ChunkTypeFromBytes returns the chunk type for the given byte signature.
43 func ChunkTypeFromBytes(b []byte) (ChunkType, bool) {
44 idx := bytes.Index(chunkSignatures, b)
45 if idx == -1 || idx%chunkSigOffset != 0 { // not found, or not aligned at chunkSigOffset
46 return -1, false
47 }
48 return ChunkType(idx / chunkSigOffset), true
49 }