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 (
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"encoding/gob"
|
|
|
|
"errors"
|
2011-09-17 17:26:32 +00:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
|
|
|
"sync"
|
2012-04-20 21:27:52 +00:00
|
|
|
"time"
|
2011-09-17 17:26:32 +00:00
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"camlistore.org/pkg/blobref"
|
|
|
|
"camlistore.org/pkg/client"
|
|
|
|
"camlistore.org/pkg/osutil"
|
2011-09-17 17:26:32 +00:00
|
|
|
)
|
|
|
|
|
2012-04-20 21:27:52 +00:00
|
|
|
type statFingerprint struct {
|
|
|
|
Size int64
|
|
|
|
ModTime time.Time
|
|
|
|
// TODO: add ctime, etc
|
|
|
|
}
|
|
|
|
|
|
|
|
func fileInfoToFingerprint(fi os.FileInfo) (f statFingerprint) {
|
|
|
|
f.Size = fi.Size()
|
|
|
|
f.ModTime = fi.ModTime()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-09-17 17:26:32 +00:00
|
|
|
type fileInfoPutRes struct {
|
2012-04-20 21:27:52 +00:00
|
|
|
Fingerprint statFingerprint
|
|
|
|
Result client.PutResult
|
2011-09-17 17:26:32 +00:00
|
|
|
}
|
|
|
|
|
2011-09-17 23:59:04 +00:00
|
|
|
// FlatStatCache is an ugly hack, until leveldb-go is ready
|
2011-09-17 17:26:32 +00:00
|
|
|
// (http://code.google.com/p/leveldb-go/)
|
2011-09-17 23:59:04 +00:00
|
|
|
type FlatStatCache 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
|
|
|
|
}
|
|
|
|
|
2011-09-17 23:59:04 +00:00
|
|
|
func NewFlatStatCache() *FlatStatCache {
|
|
|
|
filename := filepath.Join(osutil.CacheDir(), "camput.statcache")
|
|
|
|
fc := &FlatStatCache{
|
2011-09-17 17:26:32 +00:00
|
|
|
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
|
|
|
|
}
|
2012-04-20 21:27:52 +00:00
|
|
|
val.Result.Skipped = true
|
2011-09-17 17:26:32 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2011-09-17 23:59:04 +00:00
|
|
|
var _ UploadCache = (*FlatStatCache)(nil)
|
2011-09-17 17:26:32 +00:00
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
var ErrCacheMiss = errors.New("not in cache")
|
2011-09-17 17:26:32 +00:00
|
|
|
|
|
|
|
// filename may be relative.
|
|
|
|
// returns ErrCacheMiss on miss
|
|
|
|
func cacheKey(pwd, filename string) string {
|
2012-04-20 21:27:52 +00:00
|
|
|
return filepath.Join(pwd, filename)
|
2011-09-17 17:26:32 +00:00
|
|
|
}
|
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func (c *FlatStatCache) CachedPutResult(pwd, filename string, fi os.FileInfo) (*client.PutResult, error) {
|
2011-09-17 17:26:32 +00:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
|
2012-04-20 21:27:52 +00:00
|
|
|
fp := fileInfoToFingerprint(fi)
|
|
|
|
|
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-25 00:20:47 +00:00
|
|
|
cachelog.Printf("cache MISS on %q: not in cache", key)
|
2011-09-17 17:26:32 +00:00
|
|
|
return nil, ErrCacheMiss
|
|
|
|
}
|
2012-04-20 21:27:52 +00:00
|
|
|
if !reflect.DeepEqual(val.Fingerprint, fp) {
|
|
|
|
cachelog.Printf("cache MISS on %q: stats not equal:\n%#v\n%#v", key, val.Fingerprint, fp)
|
2011-09-17 17:26:32 +00:00
|
|
|
return nil, ErrCacheMiss
|
|
|
|
}
|
2012-04-20 21:27:52 +00:00
|
|
|
pr := val.Result
|
2011-09-17 17:26:32 +00:00
|
|
|
return &pr, nil
|
|
|
|
}
|
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func (c *FlatStatCache) AddCachedPutResult(pwd, filename string, fi os.FileInfo, pr *client.PutResult) {
|
2011-09-17 17:26:32 +00:00
|
|
|
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)
|
2012-04-20 21:27:52 +00:00
|
|
|
val := fileInfoPutRes{fileInfoToFingerprint(fi), *pr}
|
2011-09-17 17:26:32 +00:00
|
|
|
|
2011-09-25 00:20:47 +00:00
|
|
|
cachelog.Printf("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
|
|
|
}
|
|
|
|
|
2011-09-17 23:59:04 +00:00
|
|
|
func (c *FlatStatCache) Save() {
|
2011-09-17 17:26:32 +00:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2011-09-17 22:14:37 +00:00
|
|
|
if len(c.dirty) == 0 {
|
2011-09-25 00:20:47 +00:00
|
|
|
cachelog.Printf("FlatStatCache: Save, but nothing dirty")
|
2011-09-17 22:14:37 +00:00
|
|
|
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 {
|
2011-09-17 23:59:04 +00:00
|
|
|
log.Fatalf("FlatStatCache OpenFile: %v", err)
|
2011-09-17 17:26:32 +00:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
e := gob.NewEncoder(f)
|
|
|
|
write := func(v interface{}) {
|
|
|
|
if err := e.Encode(v); err != nil {
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
panic("Encode: " + err.Error())
|
2011-09-17 17:26:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for k, v := range c.dirty {
|
|
|
|
write(k)
|
|
|
|
write(v)
|
|
|
|
}
|
|
|
|
c.dirty = make(map[string]fileInfoPutRes)
|
2011-09-25 00:20:47 +00:00
|
|
|
cachelog.Printf("FlatStatCache: saved")
|
2011-09-17 23:59:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type FlatHaveCache struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
filename string
|
|
|
|
m map[string]bool
|
|
|
|
dirty map[string]bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFlatHaveCache() *FlatHaveCache {
|
|
|
|
filename := filepath.Join(osutil.CacheDir(), "camput.havecache")
|
|
|
|
c := &FlatHaveCache{
|
|
|
|
filename: filename,
|
|
|
|
m: make(map[string]bool),
|
|
|
|
dirty: make(map[string]bool),
|
|
|
|
}
|
|
|
|
if f, err := os.Open(filename); err == nil {
|
|
|
|
defer f.Close()
|
|
|
|
d := gob.NewDecoder(f)
|
|
|
|
for {
|
|
|
|
var key string
|
|
|
|
if d.Decode(&key) != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
c.m[key] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *FlatHaveCache) BlobExists(br *blobref.BlobRef) bool {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
return c.m[br.String()]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *FlatHaveCache) NoteBlobExists(br *blobref.BlobRef) {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
k := br.String()
|
|
|
|
c.m[k] = true
|
|
|
|
c.dirty[k] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *FlatHaveCache) Save() {
|
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
|
|
|
if len(c.dirty) == 0 {
|
2011-09-25 00:20:47 +00:00
|
|
|
cachelog.Printf("FlatHaveCache: Save, but nothing dirty")
|
2011-09-17 23:59:04 +00:00
|
|
|
return
|
|
|
|
}
|
2012-04-20 00:59:41 +00:00
|
|
|
os.Mkdir(osutil.CacheDir(), 0700)
|
2011-09-17 23:59:04 +00:00
|
|
|
f, err := os.OpenFile(c.filename, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("FlatHaveCache OpenFile: %v", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
e := gob.NewEncoder(f)
|
|
|
|
write := func(v interface{}) {
|
|
|
|
if err := e.Encode(v); err != nil {
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
panic("Encode: " + err.Error())
|
2011-09-17 23:59:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for k, _ := range c.dirty {
|
|
|
|
write(k)
|
|
|
|
}
|
|
|
|
c.dirty = make(map[string]bool)
|
2011-09-25 00:20:47 +00:00
|
|
|
cachelog.Printf("FlatHaveCache: saved")
|
2011-09-17 17:26:32 +00:00
|
|
|
}
|