You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: top.go
    path of file: ./repos/parca/pkg/query
    
    the code of the file until where you have to start completion: // Copyright 2022-2024 The Parca Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package query

import (
	"context"
	"sort"

	metastorev1alpha1 "github.com/parca-dev/parca/gen/proto/go/parca/metastore/v1alpha1"
	pb "github.com/parca-dev/parca/gen/proto/go/parca/query/v1alpha1"
	parcaprofile "github.com/parca-dev/parca/pkg/profile"
)

func GenerateTopTable(ctx context.Context, p parcaprofile.OldProfile) (*pb.Top, int64, error) {
	// Iterate over all samples and their locations.
	// Calculate the cumulative value of all locations of all samples.
	// In the end return a *pb.TopNode for each location including all the metadata we have.
	locationsTopNodes := map[string]*pb.TopNode{}
	cumulative := int64(0)
	for _, sample := range p.Samples {
		cumulative += sample.Value
		for i, location := range sample.Locations {
			if node, found := locati
    
func GenerateTopTable(ctx context.Context, p parcaprofile.OldProfile) (*pb.Top, int64, error) {
	// Iterate over all samples and their locations.
	// Calculate the cumulative value of all locations of all samples.
	// In the end return a *pb.TopNode for each location including all the metadata we have.
	locationsTopNodes := map[string]*pb.TopNode{}
	cumulative := int64(0)
	for _, sample := range p.Samples {
		cumulative += sample.Value
		for i, location := range sample.Locations {
			if node, found := locationsTopNodes[location.ID]; found {
				node.Cumulative += sample.Value
				node.Diff += sample.DiffValue

				if i == 0 {
					node.Flat += sample.Value
				}
			} else {
				node := &pb.TopNode{
					Cumulative: sample.Value,
					Diff:       sample.DiffValue,
					Meta: &pb.TopNodeMeta{
						Mapping: location.Mapping,
						Location: &metastorev1alpha1.Location{
							Id:        location.ID,
							Address:   location.Address,
							MappingId: location.Mapping.GetId(),
							IsFolded:  location.IsFolded,
						},
					},
				}
				if len(location.Lines) > 0 {
					// TODO: Return or merge multiple lines for samples
					node.Meta.Function = location.Lines[0].Function
					node.Meta.Line = &metastorev1alpha1.Line{
						FunctionId: location.Lines[0].Function.Id,
						Line:       location.Lines[0].Line,
					}
				}
				if i == 0 {
					node.Flat = sample.Value
					node.Diff = sample.DiffValue
				}
				locationsTopNodes[location.ID] = node
			}
		}
	}

	list := make([]*pb.TopNode, 0, len(locationsTopNodes))
	for _, f := range locationsTopNodes {
		list = append(list, f)
	}

	top := &pb.Top{
		List:     list,
		Reported: int32(len(list)),
		Total:    int32(cumulative),
		Unit:     p.Meta.SampleType.Unit,
	}

	return aggregateTopByFunction(top), cumulative, nil
}