1
0
mirror of https://github.com/schollz/cowyo.git synced 2023-08-10 21:13:00 +03:00

Gauranteed no collisions from now on

This commit is contained in:
Zack Scholl 2016-02-14 09:05:56 -05:00
parent 67bb9e5b1a
commit cfc2cd9a49
3 changed files with 55 additions and 1 deletions

10
db.go
View File

@ -172,9 +172,19 @@ func (p *WikiData) save(newText string) error {
if err != nil { if err != nil {
return fmt.Errorf("could not encode WikiData: %s", err) return fmt.Errorf("could not encode WikiData: %s", err)
} }
p.Title = strings.ToLower(p.Title)
err = bucket.Put([]byte(p.Title), enc) err = bucket.Put([]byte(p.Title), enc)
if err != nil {
return fmt.Errorf("could add to bucket: %s", err)
}
return 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 return err
} }

13
main.go
View File

@ -8,6 +8,7 @@ import (
"path" "path"
"runtime" "runtime"
"github.com/boltdb/bolt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -63,6 +64,18 @@ Options:`)
Open(RuntimeArgs.DatabaseLocation) Open(RuntimeArgs.DatabaseLocation)
defer Close() 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 // Default page
aboutFile, _ := ioutil.ReadFile(path.Join(RuntimeArgs.SourcePath, "templates/aboutpage.md")) aboutFile, _ := ioutil.ReadFile(path.Join(RuntimeArgs.SourcePath, "templates/aboutpage.md"))
p := WikiData{"about", "", []string{}, []string{}} p := WikiData{"about", "", []string{}, []string{}}

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"encoding/binary"
"io/ioutil" "io/ioutil"
"log" "log"
"math/rand" "math/rand"
@ -9,6 +10,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/boltdb/bolt"
"github.com/sergi/go-diff/diffmatchpatch" "github.com/sergi/go-diff/diffmatchpatch"
) )
@ -42,10 +44,23 @@ func randomAdjective() string {
func randomAlliterateCombo() (combo string) { func randomAlliterateCombo() (combo string) {
combo = "" 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 { for {
animal := randomAnimal() animal := randomAnimal()
adjective := randomAdjective() adjective := randomAdjective()
if animal[0] == adjective[0] { if animal[0] == adjective[0] && stringInSlice(strings.ToLower(adjective+animal), takenNames) == false {
combo = adjective + animal combo = adjective + animal
break break
} }
@ -53,6 +68,22 @@ func randomAlliterateCombo() (combo string) {
return 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 { func contentType(filename string) string {
switch { switch {
case strings.Contains(filename, ".css"): case strings.Contains(filename, ".css"):