|
| 1 | +// Copyright 2025 The LevelDB-Go and Pebble Authors. All rights reserved. Use |
| 2 | +// of this source code is governed by a BSD-style license that can be found in |
| 3 | +// the LICENSE file. |
| 4 | + |
| 5 | +package main |
| 6 | + |
| 7 | +import ( |
| 8 | + "bytes" |
| 9 | + "go/ast" |
| 10 | + "go/format" |
| 11 | + "go/token" |
| 12 | + "log" |
| 13 | + "os" |
| 14 | + "path/filepath" |
| 15 | + "reflect" |
| 16 | + "slices" |
| 17 | + "strings" |
| 18 | + "text/template" |
| 19 | + |
| 20 | + "golang.org/x/tools/go/packages" |
| 21 | +) |
| 22 | + |
| 23 | +// Field represents a struct field tagged with `prop:"..."`. |
| 24 | +type Field struct { |
| 25 | + Name string // Go identifier |
| 26 | + Tag string // tag value |
| 27 | + Kind string // bool | uint32 | uint64 | string |
| 28 | + EncodeEmpty bool // whether to encode empty a zero value |
| 29 | +} |
| 30 | + |
| 31 | +// Template for sstable/properties_gen.go. |
| 32 | +const tmpl = `// Code generated by genprops; DO NOT EDIT. |
| 33 | +package sstable |
| 34 | +
|
| 35 | +import ( |
| 36 | + "bytes" |
| 37 | + "encoding/binary" |
| 38 | + "fmt" |
| 39 | + "iter" |
| 40 | + "maps" |
| 41 | + "slices" |
| 42 | + "strings" |
| 43 | +
|
| 44 | + "github.com/cockroachdb/pebble/internal/intern" |
| 45 | +) |
| 46 | +
|
| 47 | +// load populates *Properties from an iterator and records which fields were |
| 48 | +// present using the bit‑vector. |
| 49 | +func (p *Properties) load(i iter.Seq2[[]byte, []byte]) error { |
| 50 | + p.Loaded = 0 |
| 51 | + for k, v := range i { |
| 52 | + switch string(k) { |
| 53 | +{{- range .Fields }} |
| 54 | + case "{{ .Tag }}": |
| 55 | + p.Loaded |= 1 << _bit_{{ .Name }} |
| 56 | +{{- if eq .Kind "bool" }} |
| 57 | + p.{{ .Name }} = len(v) == 1 && v[0] == '1' |
| 58 | +{{- else if eq .Kind "uint32" }} |
| 59 | + p.{{ .Name }} = binary.LittleEndian.Uint32(v) |
| 60 | +{{- else if eq .Kind "uint64" }} |
| 61 | + n, _ := binary.Uvarint(v) |
| 62 | + p.{{ .Name }} = n |
| 63 | +{{- else if eq .Kind "string" }} |
| 64 | + p.{{ .Name }} = string(v) |
| 65 | +{{- end }} |
| 66 | +{{- end }} |
| 67 | + default: |
| 68 | + if _, denied := ignoredInternalProperties[string(k)]; !denied { |
| 69 | + if p.UserProperties == nil { |
| 70 | + p.UserProperties = make(map[string]string) |
| 71 | + } |
| 72 | + p.UserProperties[intern.Bytes(k)] = string(v) |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + return nil |
| 77 | +} |
| 78 | +
|
| 79 | +// encodeAll returns a map of property keys and encoded values. |
| 80 | +func (p *Properties) encodeAll() map[string][]byte { |
| 81 | + m := make(map[string][]byte, _numPropBits+len(p.UserProperties)) |
| 82 | + var allocBuf []byte |
| 83 | + alloc := func(n int) []byte { |
| 84 | + if len(allocBuf) < n { |
| 85 | + allocBuf = make([]byte, n + 512) |
| 86 | + } |
| 87 | + res := allocBuf[:n] |
| 88 | + allocBuf = allocBuf[n:] |
| 89 | + return res |
| 90 | + } |
| 91 | + |
| 92 | +{{- range .Fields }} |
| 93 | +{{- if .EncodeEmpty }} |
| 94 | + if true { |
| 95 | +{{- else }} |
| 96 | + if p.{{ .Name }} != {{ zeroVal .Kind }} { |
| 97 | +{{- end }} |
| 98 | +{{- if eq .Kind "bool" }} |
| 99 | + val := alloc(1) |
| 100 | + val[0] = '0' |
| 101 | + if p.{{ .Name }} { |
| 102 | + val[0] = '1' |
| 103 | + } |
| 104 | +{{- else if eq .Kind "uint32" }} |
| 105 | + val := alloc(4) |
| 106 | + binary.LittleEndian.PutUint32(val, p.{{ .Name }}) |
| 107 | +{{- else if eq .Kind "uint64" }} |
| 108 | + val := alloc(10) |
| 109 | + n := binary.PutUvarint(val, p.{{ .Name }}) |
| 110 | + val = val[:n] |
| 111 | +{{- else if eq .Kind "string" }} |
| 112 | + val := alloc(len(p.{{ .Name }})) |
| 113 | + copy(val, p.{{ .Name }}) |
| 114 | +{{- end }} |
| 115 | + m["{{ .Tag }}"] = val |
| 116 | + } |
| 117 | +{{- end }} |
| 118 | + return m |
| 119 | +} |
| 120 | +
|
| 121 | +// isLoaded returns true if the bit corresponding to field bit is set. |
| 122 | +func (p *Properties) isLoaded(bit int) bool { return p.Loaded&(1<<bit) != 0 } |
| 123 | +
|
| 124 | +// String writes a human‑readable representation of Properties, matching the |
| 125 | +// previous reflection‑based output. |
| 126 | +func (p *Properties) String() string { |
| 127 | + var buf bytes.Buffer |
| 128 | +{{- range .Fields }} |
| 129 | + if p.{{ .Name }} != {{ zeroVal .Kind }} || p.isLoaded(_bit_{{ .Name }}) { |
| 130 | + fmt.Fprintf(&buf, "%s: %v\n", "{{ .Tag }}", p.{{ .Name }}) |
| 131 | + } |
| 132 | +{{- end }} |
| 133 | + if len(p.UserProperties) > 0 { |
| 134 | + // Print the user properties in alphabetical order. |
| 135 | + for _, k := range slices.Sorted(maps.Keys(p.UserProperties)) { |
| 136 | + v := p.UserProperties[k] |
| 137 | + if strings.IndexFunc(v, func(r rune) bool { return r < ' ' || r > '~' }) != -1 { |
| 138 | + fmt.Fprintf(&buf, "%s: hex:%x\n", k, v) |
| 139 | + } else { |
| 140 | + fmt.Fprintf(&buf, "%s: %s\n", k, v) |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + return buf.String() |
| 145 | +} |
| 146 | +
|
| 147 | +// Bit positions for property field. |
| 148 | +const ( |
| 149 | +{{- range $i, $f := .Fields }} |
| 150 | + _bit_{{$f.Name}} = {{$i}} |
| 151 | +{{- end }} |
| 152 | + _numPropBits = {{ len .Fields }} |
| 153 | +) |
| 154 | +` // end template |
| 155 | + |
| 156 | +// zeroVal returns a literal zero value string for the given kind. |
| 157 | +func zeroVal(kind string) string { |
| 158 | + switch kind { |
| 159 | + case "bool": |
| 160 | + return "false" |
| 161 | + case "uint32", "uint64": |
| 162 | + return "0" |
| 163 | + case "string": |
| 164 | + return `""` |
| 165 | + default: |
| 166 | + return "nil" |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +func main() { |
| 171 | + cfg := &packages.Config{ |
| 172 | + Mode: packages.NeedTypes | packages.NeedSyntax | packages.NeedDeps | |
| 173 | + packages.NeedTypesInfo | packages.NeedFiles | packages.NeedName, |
| 174 | + } |
| 175 | + |
| 176 | + pkgs, err := packages.Load(cfg, "github.com/cockroachdb/pebble/sstable") |
| 177 | + if err != nil { |
| 178 | + log.Fatalf("loading packages: %v", err) |
| 179 | + } |
| 180 | + |
| 181 | + var fields []Field |
| 182 | + for _, pkg := range pkgs { |
| 183 | + for _, file := range pkg.Syntax { |
| 184 | + for _, decl := range file.Decls { |
| 185 | + gd, ok := decl.(*ast.GenDecl) |
| 186 | + if !ok || gd.Tok != token.TYPE { |
| 187 | + continue |
| 188 | + } |
| 189 | + for _, spec := range gd.Specs { |
| 190 | + ts := spec.(*ast.TypeSpec) |
| 191 | + st, ok := ts.Type.(*ast.StructType) |
| 192 | + if !ok { |
| 193 | + continue |
| 194 | + } |
| 195 | + qname := pkg.Types.Path() + "." + ts.Name.Name |
| 196 | + if qname != "github.com/cockroachdb/pebble/sstable.Properties" && |
| 197 | + qname != "github.com/cockroachdb/pebble/sstable.CommonProperties" { |
| 198 | + continue |
| 199 | + } |
| 200 | + for _, f := range st.Fields.List { |
| 201 | + if f.Tag == nil || len(f.Names) == 0 { |
| 202 | + continue |
| 203 | + } |
| 204 | + tags := reflect.StructTag(strings.Trim(f.Tag.Value, "`")) |
| 205 | + tag := tags.Get("prop") |
| 206 | + if tag == "" { |
| 207 | + continue |
| 208 | + } |
| 209 | + options := strings.Split(tags.Get("options"), ",") |
| 210 | + name := f.Names[0].Name |
| 211 | + typ := pkg.TypesInfo.Types[f.Type].Type.String() |
| 212 | + var kind string |
| 213 | + switch typ { |
| 214 | + case "bool": |
| 215 | + kind = "bool" |
| 216 | + case "uint32": |
| 217 | + kind = "uint32" |
| 218 | + case "uint64": |
| 219 | + kind = "uint64" |
| 220 | + case "string": |
| 221 | + kind = "string" |
| 222 | + default: |
| 223 | + log.Fatalf("unsupported property type %s", typ) |
| 224 | + } |
| 225 | + // We always encode some properties, even if they are zero. |
| 226 | + encodeEmpty := slices.Contains(options, "encodeempty") |
| 227 | + fields = append(fields, Field{Name: name, Tag: tag, Kind: kind, EncodeEmpty: encodeEmpty}) |
| 228 | + } |
| 229 | + } |
| 230 | + } |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + if len(fields) > 64 { |
| 235 | + log.Fatalf("too many prop fields (%d), exceeds 64‑bit bitfield", len(fields)) |
| 236 | + } |
| 237 | + var sstableDir string |
| 238 | + for _, pkg := range pkgs { |
| 239 | + if pkg.PkgPath == "github.com/cockroachdb/pebble/sstable" { |
| 240 | + sstableDir = filepath.Dir(pkg.GoFiles[0]) |
| 241 | + break |
| 242 | + } |
| 243 | + } |
| 244 | + if sstableDir == "" { |
| 245 | + log.Fatalf("sstable package not found") |
| 246 | + } |
| 247 | + outputFile := filepath.Join(sstableDir, "properties_gen.go") |
| 248 | + |
| 249 | + t := template.Must(template.New("gen").Funcs(template.FuncMap{ |
| 250 | + "zeroVal": zeroVal, |
| 251 | + }).Parse(tmpl)) |
| 252 | + |
| 253 | + var buf bytes.Buffer |
| 254 | + if err := t.Execute(&buf, map[string]any{"Fields": fields}); err != nil { |
| 255 | + log.Fatalf("executing template: %v", err) |
| 256 | + } |
| 257 | + |
| 258 | + src, err := format.Source(buf.Bytes()) |
| 259 | + if err != nil { |
| 260 | + _ = os.WriteFile(outputFile, buf.Bytes(), 0o644) |
| 261 | + log.Fatalf("formatting source: %v\n", err) |
| 262 | + } |
| 263 | + |
| 264 | + if err := os.WriteFile(outputFile, src, 0o644); err != nil { |
| 265 | + log.Fatalf("writing generated file: %v", err) |
| 266 | + } |
| 267 | +} |
0 commit comments