2025-01-08 19:58:29 -06:00
|
|
|
//go:build go1.20
|
|
|
|
// +build go1.20
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
2025-01-09 16:18:06 -06:00
|
|
|
import (
|
2025-01-09 20:08:22 -06:00
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
2025-01-09 16:18:06 -06:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"go.wit.com/log"
|
|
|
|
)
|
2025-01-08 19:58:29 -06:00
|
|
|
|
|
|
|
// sent via -ldflags
|
|
|
|
var VERSION string
|
|
|
|
var BUILDTIME string
|
|
|
|
|
|
|
|
var sortmap map[string]string
|
|
|
|
var marshalKeys []string
|
|
|
|
var uniqueKeys []string
|
|
|
|
|
2025-01-09 12:54:04 -06:00
|
|
|
var pb *Fruits
|
2025-01-08 19:58:29 -06:00
|
|
|
|
|
|
|
func main() {
|
2025-01-09 20:08:22 -06:00
|
|
|
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)
|
2025-01-09 16:18:06 -06:00
|
|
|
}
|
2025-01-09 20:08:22 -06:00
|
|
|
if pb == nil {
|
|
|
|
log.Info("This should not have happened")
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
pb.printTable()
|
|
|
|
pb.addThings()
|
|
|
|
pb.printTable()
|
|
|
|
pb.saveBasket()
|
|
|
|
pb.sortTable(100)
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2025-01-09 17:15:53 -06:00
|
|
|
x := new(Fruit)
|
|
|
|
x = &Fruit{
|
2025-01-09 20:08:22 -06:00
|
|
|
Brand: "fry",
|
2025-01-09 17:15:53 -06:00
|
|
|
City: "Germany",
|
|
|
|
}
|
2025-01-09 20:08:22 -06:00
|
|
|
pb.printTable()
|
2025-01-09 17:15:53 -06:00
|
|
|
|
|
|
|
testAppend(x)
|
2025-01-09 20:08:22 -06:00
|
|
|
*/
|
|
|
|
|
|
|
|
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"
|
2025-01-10 21:33:00 -06:00
|
|
|
if pb.AppendUniqueByBrand(tmp) {
|
2025-01-09 20:08:22 -06:00
|
|
|
// log.Printf("AppendUniqueBrand() ok len=%s %s %d\n", tmp.Brand, tmp.City, pb.Len())
|
|
|
|
} else {
|
2025-01-10 21:33:00 -06:00
|
|
|
log.Printf("AppendUniqueByBrand() ERROR len=%s %s %d\n", tmp.Brand, tmp.City, pb.Len())
|
2025-01-09 20:08:22 -06:00
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
pb.Append(tmp)
|
|
|
|
added += 1
|
|
|
|
if added > 10 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2025-01-09 17:15:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func testAppend(fruit *Fruit) {
|
2025-01-09 16:18:06 -06:00
|
|
|
if pb.AppendUnique(fruit) {
|
2025-01-09 17:15:53 -06:00
|
|
|
log.Info("AppendUnique() test1 ok", fruit.Brand, fruit.City)
|
2025-01-09 16:18:06 -06:00
|
|
|
} else {
|
2025-01-09 17:15:53 -06:00
|
|
|
log.Info("AppendUnique() test1 failed", fruit.Brand, fruit.City)
|
2025-01-09 16:18:06 -06:00
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
if pb.AppendUnique(fruit) {
|
2025-01-09 17:15:53 -06:00
|
|
|
log.Info("AppendUnique() test2 worked but should not have", fruit.Brand, fruit.City)
|
|
|
|
os.Exit(-1)
|
|
|
|
} else {
|
|
|
|
log.Info("AppendUnique() test2 failed ok", fruit.Brand, fruit.City)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendByUPC(fruit *Fruit) {
|
|
|
|
if pb.AppendUniqueUPC(fruit) {
|
|
|
|
log.Info("AppendUnique() test1 ok", fruit.Brand, fruit.City)
|
|
|
|
} else {
|
|
|
|
log.Info("AppendUnique() test1 failed", fruit.Brand, fruit.City)
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
if pb.AppendUniqueUPC(fruit) {
|
|
|
|
log.Info("AppendUnique() test2 worked but should not have", fruit.Brand, fruit.City)
|
2025-01-09 16:18:06 -06:00
|
|
|
os.Exit(-1)
|
|
|
|
} else {
|
2025-01-09 17:15:53 -06:00
|
|
|
log.Info("AppendUnique() test2 failed ok", fruit.Brand, fruit.City)
|
2025-01-09 16:18:06 -06:00
|
|
|
}
|
2025-01-08 19:58:29 -06:00
|
|
|
}
|
2025-01-09 20:08:22 -06:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|