Encode writes an index into the commit-graph file
(idx Index)
| 25 | |
| 26 | // Encode writes an index into the commit-graph file |
| 27 | func (e *Encoder) Encode(idx Index) error { |
| 28 | // Get all the hashes in the input index |
| 29 | hashes := idx.Hashes() |
| 30 | |
| 31 | // Sort the inout and prepare helper structures we'll need for encoding |
| 32 | hashToIndex, fanout, extraEdgesCount, generationV2OverflowCount := e.prepare(idx, hashes) |
| 33 | |
| 34 | chunkSignatures := [][]byte{OIDFanoutChunk.Signature(), OIDLookupChunk.Signature(), CommitDataChunk.Signature()} |
| 35 | chunkSizes := []uint64{szUint32 * lenFanout, uint64(len(hashes)) * hash.Size, uint64(len(hashes)) * (hash.Size + szCommitData)} |
| 36 | if extraEdgesCount > 0 { |
| 37 | chunkSignatures = append(chunkSignatures, ExtraEdgeListChunk.Signature()) |
| 38 | chunkSizes = append(chunkSizes, uint64(extraEdgesCount)*szUint32) |
| 39 | } |
| 40 | if idx.HasGenerationV2() { |
| 41 | chunkSignatures = append(chunkSignatures, GenerationDataChunk.Signature()) |
| 42 | chunkSizes = append(chunkSizes, uint64(len(hashes))*szUint32) |
| 43 | if generationV2OverflowCount > 0 { |
| 44 | chunkSignatures = append(chunkSignatures, GenerationDataOverflowChunk.Signature()) |
| 45 | chunkSizes = append(chunkSizes, uint64(generationV2OverflowCount)*szUint64) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | if err := e.encodeFileHeader(len(chunkSignatures)); err != nil { |
| 50 | return err |
| 51 | } |
| 52 | if err := e.encodeChunkHeaders(chunkSignatures, chunkSizes); err != nil { |
| 53 | return err |
| 54 | } |
| 55 | if err := e.encodeFanout(fanout); err != nil { |
| 56 | return err |
| 57 | } |
| 58 | if err := e.encodeOidLookup(hashes); err != nil { |
| 59 | return err |
| 60 | } |
| 61 | |
| 62 | extraEdges, generationV2Data, err := e.encodeCommitData(hashes, hashToIndex, idx) |
| 63 | if err != nil { |
| 64 | return err |
| 65 | } |
| 66 | if err = e.encodeExtraEdges(extraEdges); err != nil { |
| 67 | return err |
| 68 | } |
| 69 | if idx.HasGenerationV2() { |
| 70 | overflows, err := e.encodeGenerationV2Data(generationV2Data) |
| 71 | if err != nil { |
| 72 | return err |
| 73 | } |
| 74 | if err = e.encodeGenerationV2Overflow(overflows); err != nil { |
| 75 | return err |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | return e.encodeChecksum() |
| 80 | } |
| 81 | |
| 82 | func (e *Encoder) prepare(idx Index, hashes []plumbing.Hash) (hashToIndex map[plumbing.Hash]uint32, fanout []uint32, extraEdgesCount uint32, generationV2OverflowCount uint32) { |
| 83 | // Sort the hashes and build our index |