Skip to content

Commit

Permalink
talk updates
Browse files Browse the repository at this point in the history
  • Loading branch information
adg committed Feb 6, 2011
1 parent 5a2aa6f commit acd4763
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 124 deletions.
12 changes: 8 additions & 4 deletions talk/code/0/store.go
Expand Up @@ -5,7 +5,6 @@ import "sync"
type URLStore struct {
urls map[string]string
mu sync.RWMutex
count int
}

func NewURLStore() *URLStore {
Expand All @@ -28,13 +27,18 @@ func (s *URLStore) Set(key, url string) bool {
return true
}

func (s *URLStore) Count() int {
s.mu.RLock()
defer s.mu.RUnlock()
return len(s.urls)
}

func (s *URLStore) Put(url string) string {
for {
key := genKey(s.count)
s.count++
key := genKey(s.Count())
if ok := s.Set(key, url); ok {
return key
}
}
return ""
panic("shouldn't get here")
}
12 changes: 8 additions & 4 deletions talk/code/1/store.go
Expand Up @@ -10,7 +10,6 @@ import (
type URLStore struct {
urls map[string]string
mu sync.RWMutex
count int
file *os.File
}

Expand All @@ -22,7 +21,7 @@ func NewURLStore(filename string) *URLStore {
s := &URLStore{urls: make(map[string]string)}
f, err := os.Open(filename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Exit("URLStore:", err)
log.Fatal("URLStore:", err)
}
s.file = f
if err := s.load(); err != nil {
Expand All @@ -47,10 +46,15 @@ func (s *URLStore) Set(key, url string) bool {
return true
}

func (s *URLStore) Count() int {
s.mu.RLock()
defer s.mu.RUnlock()
return len(s.urls)
}

func (s *URLStore) Put(url string) string {
for {
key := genKey(s.count)
s.count++
key := genKey(s.Count())
if ok := s.Set(key, url); ok {
if err := s.save(key, url); err != nil {
log.Println("URLStore:", err)
Expand Down
15 changes: 12 additions & 3 deletions talk/code/2/main.go
@@ -1,16 +1,25 @@
package main

import (
"flag"
"fmt"
"http"
)

var store = NewURLStore("store.gob")
var (
listenAddr = flag.String("http", ":8080", "http listen address")
dataFile = flag.String("file", "store.gob", "data store file name")
hostname = flag.String("host", "localhost:8080", "http host name")
)

var store *URLStore

func main() {
flag.Parse()
store = NewURLStore(*dataFile)
http.HandleFunc("/", Redirect)
http.HandleFunc("/add", Add)
http.ListenAndServe(":8080", nil)
http.ListenAndServe(*listenAddr, nil)
}

func Redirect(w http.ResponseWriter, r *http.Request) {
Expand All @@ -30,7 +39,7 @@ func Add(w http.ResponseWriter, r *http.Request) {
return
}
key := store.Put(url)
fmt.Fprintf(w, "http://localhost:8080/%s", key)
fmt.Fprintf(w, "http://%s/%s", *hostname, key)
}

const AddForm = `
Expand Down
12 changes: 8 additions & 4 deletions talk/code/2/store.go
Expand Up @@ -12,7 +12,6 @@ const saveQueueLength = 1000
type URLStore struct {
urls map[string]string
mu sync.RWMutex
count int
save chan record
}

Expand Down Expand Up @@ -48,10 +47,15 @@ func (s *URLStore) Set(key, url string) bool {
return true
}

func (s *URLStore) Count() int {
s.mu.RLock()
defer s.mu.RUnlock()
return len(s.urls)
}

func (s *URLStore) Put(url string) string {
for {
key := genKey(s.count)
s.count++
key := genKey(s.Count())
if ok := s.Set(key, url); ok {
s.save <- record{key, url}
return key
Expand Down Expand Up @@ -82,7 +86,7 @@ func (s *URLStore) load(filename string) os.Error {
func (s *URLStore) saveLoop(filename string) {
f, err := os.Open(filename, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Exit("URLStore:", err)
log.Fatal("URLStore:", err)
}
e := gob.NewEncoder(f)
for {
Expand Down
12 changes: 8 additions & 4 deletions talk/code/3/store.go
Expand Up @@ -18,7 +18,6 @@ type Store interface {
type URLStore struct {
urls map[string]string
mu sync.RWMutex
count int
save chan record
}

Expand Down Expand Up @@ -58,10 +57,15 @@ func (s *URLStore) Set(key, url *string) os.Error {
return nil
}

func (s *URLStore) Count() int {
s.mu.RLock()
defer s.mu.RUnlock()
return len(s.urls)
}

func (s *URLStore) Put(url, key *string) os.Error {
for {
*key = genKey(s.count)
s.count++
*key = genKey(s.Count())
if err := s.Set(key, url); err == nil {
break
}
Expand Down Expand Up @@ -94,7 +98,7 @@ func (s *URLStore) load(filename string) os.Error {
func (s *URLStore) saveLoop(filename string) {
f, err := os.Open(filename, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Exit("URLStore:", err)
log.Fatal("URLStore:", err)
}
e := gob.NewEncoder(f)
for {
Expand Down

0 comments on commit acd4763

Please sign in to comment.