-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstore.go
168 lines (147 loc) · 2.99 KB
/
store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"gob"
"io"
"log"
"os"
"rpc"
"sync"
"time"
)
const saveTimeout = 60e9
type Store interface {
Put(url, key *string) os.Error
Get(key, url *string) os.Error
}
type URLStore struct {
mu sync.Mutex
urls *URLMap
count int
filename string
dirty chan bool
}
func NewURLStore(filename string) *URLStore {
s := &URLStore{
urls: NewURLMap(),
filename: filename,
dirty: make(chan bool, 1),
}
if err := s.load(); err != nil {
log.Println("URLStore:", err)
}
go s.saveLoop()
return s
}
func (s *URLStore) Get(key, url *string) os.Error {
//log.Println("URLStore: Get", *key)
if u := s.urls.Get(*key); u != "" {
*url = u
return nil
}
return os.NewError("key not found")
}
func (s *URLStore) Put(url, key *string) os.Error {
//log.Println("URLStore: Put", *url)
s.mu.Lock()
for {
//*key = genKey(s.count)
//s.count++
if u := s.urls.Get(*key); u == "" {
break
}else{
log.Println("URLStore: Put", u)
}
}
s.urls.Set(*key, *url)
s.mu.Unlock()
select {
case s.dirty <- true:
default:
}
return nil
}
func (s *URLStore) load() os.Error {
f, err := os.Open(s.filename)
if err != nil {
return err
}
defer f.Close()
return s.urls.ReadFrom(f)
}
func (s *URLStore) save() os.Error {
f, err := os.Create(s.filename)
if err != nil {
return err
}
defer f.Close()
return s.urls.WriteTo(f)
}
func (s *URLStore) saveLoop() {
for {
<-s.dirty
log.Println("URLStore: saving")
if err := s.save(); err != nil { log.Println("URLStore:", err) }
time.Sleep(saveTimeout)
}
}
type ProxyStore struct {
urls *URLMap
client *rpc.Client
}
func NewProxyStore(addr string) *ProxyStore {
client, err := rpc.DialHTTP("tcp", addr)
if err != nil {
log.Println("ProxyStore:", err)
}
return &ProxyStore{urls: NewURLMap(), client: client}
}
func (s *ProxyStore) Get(key, url *string) os.Error {
if u := s.urls.Get(*key); u != "" {
*url = u
log.Println("ProxyStore: Get cache hit", *key)
return nil
}
log.Println("ProxyStore: Get cache miss", *key)
err := s.client.Call("Store.Get", key, url)
if err == nil {
s.urls.Set(*key, *url)
}
return err
}
func (s *ProxyStore) Put(url, key *string) os.Error {
log.Println("ProxyStore: Put", *url)
err := s.client.Call("Store.Put", url, key)
if err == nil {
s.urls.Set(*key, *url)
}
return err
}
type URLMap struct {
mu sync.RWMutex
urls map[string]string
}
func NewURLMap() *URLMap {
return &URLMap{urls: make(map[string]string)}
}
func (m *URLMap) Set(key, url string) {
m.mu.Lock()
defer m.mu.Unlock()
m.urls[key] = url
}
func (m *URLMap) Get(key string) string {
m.mu.RLock()
defer m.mu.RUnlock()
return m.urls[key]
}
func (m *URLMap) WriteTo(w io.Writer) os.Error {
m.mu.RLock()
defer m.mu.RUnlock() //當function結束時, 呼叫defer
e := gob.NewEncoder(w)
return e.Encode(m.urls)
}
func (m *URLMap) ReadFrom(r io.Reader) os.Error {
m.mu.Lock()
defer m.mu.Unlock()
d := gob.NewDecoder(r)
return d.Decode(&m.urls)
}