github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/keys/gen_cpp_keys.go (about) 1 // Copyright 2017 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 // +build ignore 12 13 package main 14 15 import ( 16 "bytes" 17 "fmt" 18 "os" 19 "sort" 20 21 "github.com/cockroachdb/cockroach/pkg/keys" 22 "github.com/cockroachdb/cockroach/pkg/roachpb" 23 ) 24 25 func rocksdbSlice(key []byte) string { 26 var buf bytes.Buffer 27 fmt.Fprintf(&buf, `"`) 28 for _, v := range key { 29 fmt.Fprintf(&buf, "\\x%02x", v) 30 } 31 fmt.Fprintf(&buf, `", %d`, len(key)) 32 return buf.String() 33 } 34 35 func main() { 36 f, err := os.Create("../../c-deps/libroach/keys.h") 37 if err != nil { 38 fmt.Fprintln(os.Stderr, "Error opening file: ", err) 39 os.Exit(1) 40 } 41 42 defer func() { 43 if err := f.Close(); err != nil { 44 fmt.Fprintln(os.Stderr, "Error closing file: ", err) 45 os.Exit(1) 46 } 47 }() 48 49 // First comment for github/Go; second for reviewable. 50 // https://github.com/golang/go/issues/13560#issuecomment-277804473 51 // https://github.com/Reviewable/Reviewable/wiki/FAQ#how-do-i-tell-reviewable-that-a-file-is-generated-and-should-not-be-reviewed 52 // 53 // The funky string concatenation is to foil reviewable so that it doesn't 54 // think this file is generated. 55 fmt.Fprintf(f, `// Code generated by gen_cpp_keys.go; `+`DO `+`NOT `+`EDIT. 56 // `+`GENERATED `+`FILE `+`DO `+`NOT `+`EDIT 57 58 #pragma once 59 60 namespace cockroach { 61 62 `) 63 64 genKey := func(key roachpb.Key, name string) { 65 fmt.Fprintf(f, "const rocksdb::Slice k%s(%s);\n", name, rocksdbSlice(key)) 66 } 67 genKey(keys.LocalMax, "LocalMax") 68 genKey(keys.LocalRangeIDPrefix.AsRawKey(), "LocalRangeIDPrefix") 69 genKey(keys.LocalRangeIDReplicatedInfix, "LocalRangeIDReplicatedInfix") 70 genKey(keys.LocalRangeAppliedStateSuffix, "LocalRangeAppliedStateSuffix") 71 genKey(keys.Meta2KeyMax, "Meta2KeyMax") 72 genKey(keys.TenantPrefix, "TenantPrefix") 73 genKey(keys.MinKey, "MinKey") 74 genKey(keys.MaxKey, "MaxKey") 75 fmt.Fprintf(f, "\n") 76 77 genSortedSpans := func(spans []roachpb.Span, name string) { 78 // Sort the spans by end key which reduces the number of comparisons on 79 // libroach/db.cc:IsValidSplitKey(). 80 sortedSpans := spans 81 sort.Slice(sortedSpans, func(i, j int) bool { 82 return sortedSpans[i].EndKey.Compare(sortedSpans[j].EndKey) > 0 83 }) 84 85 fmt.Fprintf(f, "const std::vector<std::pair<rocksdb::Slice, rocksdb::Slice> > kSorted%s = {\n", name) 86 for _, span := range sortedSpans { 87 fmt.Fprintf(f, " std::make_pair(rocksdb::Slice(%s), rocksdb::Slice(%s)),\n", 88 rocksdbSlice(span.Key), rocksdbSlice(span.EndKey)) 89 } 90 fmt.Fprintf(f, "};\n") 91 } 92 genSortedSpans(keys.NoSplitSpans, "NoSplitSpans") 93 94 fmt.Fprintf(f, ` 95 } // namespace cockroach 96 `) 97 }