From cfc2cd9a49e7494bf3fc42ee896f12de9d83569e Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Sun, 14 Feb 2016 09:05:56 -0500 Subject: [PATCH] Gauranteed no collisions from now on --- db.go | 10 ++++++++++ main.go | 13 +++++++++++++ utils.go | 33 ++++++++++++++++++++++++++++++++- 3 files changed, 55 insertions(+), 1 deletion(-) diff --git a/db.go b/db.go index af5cab1..afe268f 100644 --- a/db.go +++ b/db.go @@ -172,9 +172,19 @@ func (p *WikiData) save(newText string) error { if err != nil { return fmt.Errorf("could not encode WikiData: %s", err) } + p.Title = strings.ToLower(p.Title) err = bucket.Put([]byte(p.Title), enc) + if err != nil { + return fmt.Errorf("could add to bucket: %s", err) + } return err }) + err = db.Update(func(tx *bolt.Tx) error { + b := tx.Bucket([]byte("programdata")) + id, _ := b.NextSequence() + idInt := int(id) + return b.Put(itob(idInt), []byte(p.Title)) + }) return err } diff --git a/main.go b/main.go index be17857..65f758b 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "path" "runtime" + "github.com/boltdb/bolt" "github.com/gin-gonic/gin" ) @@ -63,6 +64,18 @@ Options:`) Open(RuntimeArgs.DatabaseLocation) defer Close() + // create programdata bucket + err := db.Update(func(tx *bolt.Tx) error { + _, err := tx.CreateBucketIfNotExists([]byte("programdata")) + if err != nil { + return fmt.Errorf("create bucket: %s", err) + } + return err + }) + if err != nil { + panic(err) + } + // Default page aboutFile, _ := ioutil.ReadFile(path.Join(RuntimeArgs.SourcePath, "templates/aboutpage.md")) p := WikiData{"about", "", []string{}, []string{}} diff --git a/utils.go b/utils.go index 0f06616..f2ab38b 100644 --- a/utils.go +++ b/utils.go @@ -1,6 +1,7 @@ package main import ( + "encoding/binary" "io/ioutil" "log" "math/rand" @@ -9,6 +10,7 @@ import ( "strings" "time" + "github.com/boltdb/bolt" "github.com/sergi/go-diff/diffmatchpatch" ) @@ -42,10 +44,23 @@ func randomAdjective() string { func randomAlliterateCombo() (combo string) { combo = "" + takenNames := []string{} + err := db.View(func(tx *bolt.Tx) error { + // Assume bucket exists and has keys + b := tx.Bucket([]byte("programdata")) + c := b.Cursor() + for k, v := c.First(); k != nil; k, v = c.Next() { + takenNames = append(takenNames, strings.ToLower(string(v))) + } + return nil + }) + if err != nil { + panic(err) + } for { animal := randomAnimal() adjective := randomAdjective() - if animal[0] == adjective[0] { + if animal[0] == adjective[0] && stringInSlice(strings.ToLower(adjective+animal), takenNames) == false { combo = adjective + animal break } @@ -53,6 +68,22 @@ func randomAlliterateCombo() (combo string) { return } +func stringInSlice(s string, strings []string) bool { + for _, k := range strings { + if s == k { + return true + } + } + return false +} + +// itob returns an 8-byte big endian representation of v. +func itob(v int) []byte { + b := make([]byte, 8) + binary.BigEndian.PutUint64(b, uint64(v)) + return b +} + func contentType(filename string) string { switch { case strings.Contains(filename, ".css"):