github.com/google/trillian-examples@v0.0.0-20240520080811-0d40d35cef0e/experimental/batchmap/sumdb/build/pipeline/entries.go (about)

     1  // Copyright 2020 Google LLC. All Rights Reserved.
     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 pipeline
    16  
    17  import (
    18  	"crypto"
    19  	"fmt"
    20  	"reflect"
    21  
    22  	"github.com/apache/beam/sdks/v2/go/pkg/beam"
    23  	"github.com/google/trillian/experimental/batchmap"
    24  	"github.com/google/trillian/merkle/coniks"
    25  	"github.com/google/trillian/merkle/smt/node"
    26  )
    27  
    28  func init() {
    29  	beam.RegisterType(reflect.TypeOf((*mapEntryFn)(nil)).Elem())
    30  }
    31  
    32  const hash = crypto.SHA512_256
    33  
    34  // Metadata is the audit.Metadata object with the addition of an ID field.
    35  // It must map to the scheme of the leafMetadata table.
    36  type Metadata struct {
    37  	ID       int64
    38  	Module   string
    39  	Version  string
    40  	RepoHash string
    41  	ModHash  string
    42  }
    43  
    44  // CreateEntries converts the PCollection<Metadata> into a PCollection<Entry> that will be
    45  // committed to by the map.
    46  func CreateEntries(s beam.Scope, treeID int64, records beam.PCollection) beam.PCollection {
    47  	return beam.ParDo(s.Scope("mapentries"), &mapEntryFn{treeID}, records)
    48  }
    49  
    50  type mapEntryFn struct {
    51  	TreeID int64
    52  }
    53  
    54  func (fn *mapEntryFn) ProcessElement(m Metadata, emit func(*batchmap.Entry)) {
    55  	h := hash.New()
    56  	h.Write([]byte(fmt.Sprintf("%s %s/go.mod", m.Module, m.Version)))
    57  	modKey := h.Sum(nil)
    58  	modLeafID := node.NewID(string(modKey), uint(len(modKey)*8))
    59  
    60  	emit(&batchmap.Entry{
    61  		HashKey:   modKey,
    62  		HashValue: coniks.Default.HashLeaf(fn.TreeID, modLeafID, []byte(m.ModHash)),
    63  	})
    64  
    65  	h = hash.New()
    66  	h.Write([]byte(fmt.Sprintf("%s %s", m.Module, m.Version)))
    67  	repoKey := h.Sum(nil)
    68  	repoLeafID := node.NewID(string(repoKey), uint(len(repoKey)*8))
    69  
    70  	emit(&batchmap.Entry{
    71  		HashKey:   repoKey,
    72  		HashValue: coniks.Default.HashLeaf(fn.TreeID, repoLeafID, []byte(m.RepoHash)),
    73  	})
    74  }