|
| 1 | +// Copyright (c) 2026 Couchbase, 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 cmd |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/binary" |
| 19 | + "fmt" |
| 20 | + |
| 21 | + "github.com/spf13/cobra" |
| 22 | +) |
| 23 | + |
| 24 | +// edgeListCmd represents the edge command |
| 25 | +var edgeListCmd = &cobra.Command{ |
| 26 | + Use: "edgeList", |
| 27 | + Short: "prints the edge list for nested documents", |
| 28 | + Long: `The edgeList command will print the edge list for nested documents in the segment.`, |
| 29 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 30 | + edgeListAddr, err := segment.EdgeListAddr() |
| 31 | + if err != nil { |
| 32 | + return fmt.Errorf("error getting edge list: %v", err) |
| 33 | + } |
| 34 | + if edgeListAddr == 0 { |
| 35 | + fmt.Println("no edge list present") |
| 36 | + return nil |
| 37 | + } |
| 38 | + data := segment.Data() |
| 39 | + // read edge list |
| 40 | + // pos stores the current read position |
| 41 | + pos := edgeListAddr |
| 42 | + // read number of nested documents which is also the number of edges |
| 43 | + numEdges, read := binary.Uvarint(data[pos : pos+binary.MaxVarintLen64]) |
| 44 | + if read <= 0 { |
| 45 | + return fmt.Errorf("error reading number of edges in nested edge list") |
| 46 | + } |
| 47 | + pos += uint64(read) |
| 48 | + // if no edges or no nested documents, return |
| 49 | + if numEdges == 0 { |
| 50 | + fmt.Println("no nested documents present") |
| 51 | + return nil |
| 52 | + } |
| 53 | + // edgeList as a map[node]parent |
| 54 | + edgeList := make(map[uint64]uint64, numEdges) |
| 55 | + for i := uint64(0); i < numEdges; i++ { |
| 56 | + child, read := binary.Uvarint(data[pos : pos+binary.MaxVarintLen64]) |
| 57 | + if read <= 0 { |
| 58 | + return fmt.Errorf("error reading child doc id in nested edge list") |
| 59 | + } |
| 60 | + pos += uint64(read) |
| 61 | + parent, read := binary.Uvarint(data[pos : pos+binary.MaxVarintLen64]) |
| 62 | + if read <= 0 { |
| 63 | + return fmt.Errorf("error reading parent doc id in nested edge list") |
| 64 | + } |
| 65 | + pos += uint64(read) |
| 66 | + edgeList[child] = parent |
| 67 | + } |
| 68 | + // print number of edges / nested documents |
| 69 | + fmt.Printf("number of edges / nested documents: %d\n", len(edgeList)) |
| 70 | + fmt.Printf("child document number -> parent document number\n") |
| 71 | + for child, parent := range edgeList { |
| 72 | + fmt.Printf("%d -> %d\n", child, parent) |
| 73 | + } |
| 74 | + return nil |
| 75 | + }, |
| 76 | +} |
| 77 | + |
| 78 | +func init() { |
| 79 | + RootCmd.AddCommand(edgeListCmd) |
| 80 | +} |
0 commit comments