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

Former-commit-id: 82786841f2a1338f1233a2bcbd08ebc301f64882 [formerly ea833c969e278dfa1fad9bb21b8aa74748591832] [formerly 24b4839191af1c04188ad52486d83b87a9a77907 [formerly cfc2cd9a49]]
Former-commit-id: 7f3c3a07765996e4659e2ff1beb2aac04d605723 [formerly 52dbe41753ba4fdd3c695b4436d5cde3008af397]
Former-commit-id: b21f1b966718a3c0f31a8568d5dfe144c2296ec0
This commit is contained in:
Zack Scholl 2016-02-14 09:05:56 -05:00
parent 3452665e87
commit 51f0bee914
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 {
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
}

13
main.go
View File

@ -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{}}

View File

@ -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"):