2011-09-17 17:26:32 +00:00
|
|
|
/*
|
|
|
|
Copyright 2011 Google Inc.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"gob"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"camli/client"
|
|
|
|
"camli/osutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
type fileInfoPutRes struct {
|
|
|
|
Fi os.FileInfo
|
|
|
|
Pr client.PutResult
|
|
|
|
}
|
|
|
|
|
|
|
|
// FlatCache is an ugly hack, until leveldb-go is ready
|
|
|
|
// (http://code.google.com/p/leveldb-go/)
|
|
|
|
type FlatCache struct {
|
2011-09-17 22:14:37 +00:00
|
|
|
mu sync.Mutex
|
2011-09-17 17:26:32 +00:00
|
|
|
filename string
|
|
|
|
m map[string]fileInfoPutRes
|
|
|
|
dirty map[string]fileInfoPutRes
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFlatCache() *FlatCache {
|
|
|
|
filename := filepath.Join(osutil.CacheDir(), "camput.cache")
|
|
|
|
fc := &FlatCache{
|
|
|
|
filename: filename,
|
|
|
|
m: make(map[string]fileInfoPutRes),
|
2011-09-17 22:14:37 +00:00
|
|
|
dirty: make(map[string]fileInfoPutRes),
|
2011-09-17 17:26:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if f, err := os.Open(filename); err == nil {
|
|
|
|
defer f.Close()
|
|
|
|
d := gob.NewDecoder(f)
|
|
|
|
for {
|
|
|
|
var key string
|
|
|
|
var val fileInfoPutRes
|
|
|
|
if d.Decode(&key) != nil || d.Decode(&val) != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
val.Pr.Skipped = true
|
|
|
|
fc.m[key] = val
|
2011-09-17 22:14:37 +00:00
|
|
|
log.Printf("Read %q: %v", key, val)
|
2011-09-17 17:26:32 +00:00
|
|
|
}
|
2011-09-17 22:14:37 +00:00
|
|
|
log.Printf("Flatcache read %d entries from %s", len(fc.m), filename)
|
2011-09-17 17:26:32 +00:00
|
|
|
}
|
|
|
|
return fc
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ UploadCache = (*FlatCache)(nil)
|
|
|
|
|
|
|
|
var ErrCacheMiss = os.NewError("not in cache")
|
|
|
|
|
|
|
|
// filename may be relative.
|
|
|
|
// returns ErrCacheMiss on miss
|
|
|
|
func cacheKey(pwd, filename string) string {
|
2011-09-17 22:14:37 +00:00
|
|
|
return filepath.Clean(pwd) + "\x00" + filepath.Clean(filename)
|
2011-09-17 17:26:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *FlatCache) CachedPutResult(pwd, filename string, fi *os.FileInfo) (*client.PutResult, os.Error) {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
2011-09-17 22:14:37 +00:00
|
|
|
key := cacheKey(pwd, filename)
|
|
|
|
val, ok := c.m[key]
|
2011-09-17 17:26:32 +00:00
|
|
|
if !ok {
|
2011-09-17 22:14:37 +00:00
|
|
|
log.Printf("cache MISS on %q: not in cache", key)
|
2011-09-17 17:26:32 +00:00
|
|
|
return nil, ErrCacheMiss
|
|
|
|
}
|
2011-09-17 22:14:37 +00:00
|
|
|
if !reflect.DeepEqual(&val.Fi, fi) {
|
|
|
|
log.Printf("cache MISS on %q: stats not equal:\n%#v\n%#v", key, val.Fi, fi)
|
2011-09-17 17:26:32 +00:00
|
|
|
return nil, ErrCacheMiss
|
|
|
|
}
|
|
|
|
pr := val.Pr
|
|
|
|
return &pr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *FlatCache) AddCachedPutResult(pwd, filename string, fi *os.FileInfo, pr *client.PutResult) {
|
|
|
|
c.mu.Lock()
|
2011-09-17 22:14:37 +00:00
|
|
|
defer c.mu.Unlock()
|
2011-09-17 17:26:32 +00:00
|
|
|
key := cacheKey(pwd, filename)
|
2011-09-17 22:14:37 +00:00
|
|
|
val := fileInfoPutRes{*fi, *pr}
|
2011-09-17 17:26:32 +00:00
|
|
|
|
2011-09-17 22:14:37 +00:00
|
|
|
vprintf("Adding to stat cache %q: %v", key, val)
|
2011-09-17 17:26:32 +00:00
|
|
|
|
2011-09-17 22:14:37 +00:00
|
|
|
c.dirty[key] = val
|
|
|
|
c.m[key] = val
|
2011-09-17 17:26:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *FlatCache) Save() {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2011-09-17 22:14:37 +00:00
|
|
|
if len(c.dirty) == 0 {
|
|
|
|
vprintf("FlatCache: Save, but nothing dirty")
|
|
|
|
return
|
|
|
|
}
|
2011-09-17 17:26:32 +00:00
|
|
|
|
|
|
|
f, err := os.OpenFile(c.filename, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("FlatCache OpenFile: %v", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
e := gob.NewEncoder(f)
|
|
|
|
write := func(v interface{}) {
|
|
|
|
if err := e.Encode(v); err != nil {
|
|
|
|
panic("Encode: " + err.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for k, v := range c.dirty {
|
|
|
|
write(k)
|
|
|
|
write(v)
|
|
|
|
}
|
|
|
|
c.dirty = make(map[string]fileInfoPutRes)
|
2011-09-17 22:14:37 +00:00
|
|
|
log.Printf("FlatCache: saved")
|
2011-09-17 17:26:32 +00:00
|
|
|
}
|