From 3f81cca18c6f55f303bddc5c4d992cd53894726e Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Thu, 9 Jan 2025 20:08:22 -0600 Subject: [PATCH] example is working okay --- .gitignore | 1 + addMutex.go | 11 ++-- argv.go | 6 +- example/Makefile | 2 +- example/fruit.proto | 2 +- example/main.go | 138 ++++++++++++++++++++++++++++++++++++++++---- main.go | 3 +- sort.go | 21 +++++-- 8 files changed, 158 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index 1d07498..e334c52 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *.swp +*.pb *.pb.go go.mod go.sum diff --git a/addMutex.go b/addMutex.go index 78abb6b..1b1db0c 100644 --- a/addMutex.go +++ b/addMutex.go @@ -40,15 +40,18 @@ func (pb *Files) addMutex(f *File) error { // marshalThing(w, msg.Name) // log.Info("line:", line) if strings.HasSuffix(line, start) { - if msg.DoMutex { - msg.MutexFound = true - found = true + msg.MutexFound = true + found = true + if argv.Mutex { log.Info("Adding Mutex to line:", line) fmt.Fprintln(w, line) fmt.Fprintln(w, "\tLock sync.RWMutex // auto-added by go.wit.com/apps/autogenpb") fmt.Fprintln(w, "") } else { - log.Info("Skipping. DoMutex = false for", msg.Name) + log.Info("Skipping. Mutex = false for", msg.Name) + fmt.Fprintln(w, line) + fmt.Fprintln(w, "\t// Lock sync.RWMutex // autogenpb skipped this. needs --mutex command line arg") + fmt.Fprintln(w, "") } } } diff --git a/argv.go b/argv.go index 6925956..29c5e57 100644 --- a/argv.go +++ b/argv.go @@ -9,9 +9,9 @@ package main var argv args type args struct { - Package string `arg:"--package" help:"the package name"` - LoBase string `arg:"--lobase" help:"lowercase basename"` - UpBase string `arg:"--upbase" help:"uppercase basename"` + Package string `arg:"--package" help:"the package name"` + // LoBase string `arg:"--lobase" help:"lowercase basename"` + // UpBase string `arg:"--upbase" help:"uppercase basename"` Proto string `arg:"--proto" help:"the .proto filename"` Append string `arg:"--append" help:"will keep this key unique on append"` Sort []string `arg:"-s,--sort,separate" help:"how and what to sort on"` diff --git a/example/Makefile b/example/Makefile index 6aaad50..bc7b48d 100644 --- a/example/Makefile +++ b/example/Makefile @@ -14,7 +14,7 @@ vet: @GO111MODULE=off go vet build: - GO111MODULE=off go build + GO111MODULE=off go build -v withMutex: ../autogenpb --proto fruit.proto --package main --mutex diff --git a/example/fruit.proto b/example/fruit.proto index 821ae6c..a965d42 100644 --- a/example/fruit.proto +++ b/example/fruit.proto @@ -28,7 +28,7 @@ message Pear { // "Fruit" must exist. you can put anything in it message Fruit { // `autogenpb:marshal` - string brand = 1; // `autogenpb:unique` + string brand = 1; // `autogenpb:unique` `autogenpb:sort` repeated Apple apples = 2; repeated Pear pears = 3; string UPC = 4; // `autogenpb:sort` `autogenpb:unique` diff --git a/example/main.go b/example/main.go index 90efe25..c180350 100644 --- a/example/main.go +++ b/example/main.go @@ -4,6 +4,8 @@ package main import ( + "fmt" + "math/rand" "os" "go.wit.com/log" @@ -20,25 +22,105 @@ var uniqueKeys []string var pb *Fruits func main() { - // pb = NewFruits() - pb = new(Fruits) - pb.Uuid = "test" - pb.Version = "v0.0.2" - - fruit := &Fruit{ - Brand: "mom", - City: "New NewYork", + pb = NewFruits() + // attempt to load basket.pb + if err := pb.loadBasket(); err != nil { + log.Info("load basket failed. running this for the first time?") + fruit := &Fruit{ + Brand: "mom", + City: "New NewYork", + } + testAppend(fruit) + appendByUPC(fruit) } + if pb == nil { + log.Info("This should not have happened") + os.Exit(-1) + } + pb.printTable() + pb.addThings() + pb.printTable() + pb.saveBasket() + pb.sortTable(100) +} + +/* x := new(Fruit) x = &Fruit{ - Brand: "dad", + Brand: "fry", City: "Germany", } - appendByUPC(x) - appendByUPC(fruit) + pb.printTable() - testAppend(fruit) testAppend(x) +*/ + +func (pb *Fruits) printTable() { + var count int + all := pb.All() + for all.Scan() { + tmp := all.Next() + count += 1 + log.Printf("found %d %s %s\n", count, tmp.Brand, tmp.City) + if count > 5 { + break + } + } + log.Printf("Total entries: %d\n", pb.Len()) +} + +func (pb *Fruits) sortTable(i int) { + var count int + all := pb.SortByBrand() + for all.Scan() { + tmp := all.Next() + count += 1 + log.Printf("found %d %s %s\n", count, tmp.Brand, tmp.City) + if count > i { + break + } + } + log.Printf("Total entries: %d\n", pb.Len()) +} + +// adds 10 new entries +func (pb *Fruits) addThings() { + var added int + var count int = rand.Intn(10000) + // var base string = "bender" + for { + count += rand.Intn(1000) + var found bool + name := fmt.Sprintf("bender%d", count) + all := pb.All() + for all.Scan() { + tmp := all.Next() + if tmp.Brand == name { + // log.Printf("tmp name EQUAL %d %s vs %s city = %s len=%d\n", count, tmp.Brand, name, tmp.City, pb.Len()) + found = true + break + } + // log.Printf("DID NOT EXIST %d %s vs %s city = %s len=%d\n", count, tmp.Brand, name, tmp.City, pb.Len()) + } + if found { + continue + } + // log.Printf("DID NOT EXIST %d %s len=%d\n", count, name, pb.Len()) + tmp := new(Fruit) + tmp.Brand = name + tmp.City = "paris" + if pb.AppendUniqueBrand(tmp) { + // log.Printf("AppendUniqueBrand() ok len=%s %s %d\n", tmp.Brand, tmp.City, pb.Len()) + } else { + log.Printf("AppendUniqueBrand() ERROR len=%s %s %d\n", tmp.Brand, tmp.City, pb.Len()) + os.Exit(-1) + } + pb.Append(tmp) + added += 1 + if added > 10 { + return + } + } } func testAppend(fruit *Fruit) { @@ -70,3 +152,35 @@ func appendByUPC(fruit *Fruit) { log.Info("AppendUnique() test2 failed ok", fruit.Brand, fruit.City) } } + +func (pb *Fruits) saveBasket() error { + data, err := pb.Marshal() + if err != nil { + return err + } + + w, err := os.OpenFile("basket.pb", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) + if err != nil { + return err + } + w.Write(data) + w.Close() + log.Info("saved basket.pb ok") + return nil +} + +func (pb *Fruits) loadBasket() error { + if pb == nil { + pb = new(Fruits) + } + data, err := os.ReadFile("basket.pb") + if err != nil { + return err + } + + if err := pb.Unmarshal(data); err != nil { + return err + } + log.Info("loaded basket.pb ok") + return nil +} diff --git a/main.go b/main.go index 5a96f25..2e324e2 100644 --- a/main.go +++ b/main.go @@ -27,7 +27,8 @@ var BUILDTIME string var sortmap map[string]string var marshalKeys []string -var uniqueKeys []string + +// var uniqueKeys []string func main() { pp := arg.MustParse(&argv) diff --git a/sort.go b/sort.go index 8704d4e..29d795a 100644 --- a/sort.go +++ b/sort.go @@ -117,13 +117,13 @@ func (pf *File) iterSelect(w io.Writer) { fmt.Fprintln(w, " defer "+LOCK+".RUnlock()") fmt.Fprintln(w, "") fmt.Fprintln(w, " // Create a new slice to hold pointers to each "+BASE+"") - fmt.Fprintln(w, " var aStuff []*"+BASE+"") - fmt.Fprintln(w, " aStuff = make([]*"+BASE+", len(all."+BASES+"))") + fmt.Fprintln(w, " var tmp []*"+BASE+"") + fmt.Fprintln(w, " tmp = make([]*"+BASE+", len(all."+BASES+"))") fmt.Fprintln(w, " for i, p := range all."+BASES+" {") - fmt.Fprintln(w, " aStuff[i] = p // Copy pointers for safe iteration") + fmt.Fprintln(w, " tmp[i] = p // Copy pointers for safe iteration") fmt.Fprintln(w, " }") fmt.Fprintln(w, "") - fmt.Fprintln(w, " return aStuff") + fmt.Fprintln(w, " return tmp") fmt.Fprintln(w, "}") } @@ -142,6 +142,7 @@ func (pf *File) appendUnique(w io.Writer) { LOCK = pf.Bases.Lockname } + // append check for every key fmt.Fprintln(w, "// enforces "+BASE+" is unique") fmt.Fprintln(w, "func (all *"+MSG+") AppendUnique(newP *"+BASE+") bool {") fmt.Fprintln(w, " "+LOCK+".RLock()") @@ -160,6 +161,7 @@ func (pf *File) appendUnique(w io.Writer) { fmt.Fprintln(w, "}") fmt.Fprintln(w, "") + // append for single keys for _, KEY := range pf.Base.Unique { fmt.Fprintln(w, "// enforces "+BASE+" is unique") fmt.Fprintln(w, "func (all *"+MSG+") AppendUnique"+KEY+"(newP *"+BASE+") bool {") @@ -177,6 +179,17 @@ func (pf *File) appendUnique(w io.Writer) { fmt.Fprintln(w, "}") fmt.Fprintln(w, "") } + + // append -- no check at all + fmt.Fprintln(w, "// just a simple Append() with no checking (but still uses the mutex lock)") + fmt.Fprintln(w, "func (all *"+MSG+") Append(newP *"+BASE+") bool {") + fmt.Fprintln(w, " "+LOCK+".RLock()") + fmt.Fprintln(w, " defer "+LOCK+".RUnlock()") + fmt.Fprintln(w, "") + fmt.Fprintln(w, " all."+MSG+" = append(all."+MSG+", newP)") + fmt.Fprintln(w, " return true") + fmt.Fprintln(w, "}") + fmt.Fprintln(w, "") } func iterReplace(w io.Writer, names map[string]string) {