2011-10-25 20:30:02 +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 server
|
|
|
|
|
|
|
|
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
|
|
|
"errors"
|
2013-12-14 17:37:56 +00:00
|
|
|
"fmt"
|
2011-10-25 20:30:02 +00:00
|
|
|
|
2013-08-04 02:54:30 +00:00
|
|
|
"camlistore.org/pkg/blob"
|
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/lru"
|
2013-12-14 17:37:56 +00:00
|
|
|
"camlistore.org/pkg/sorted"
|
2011-10-25 20:30:02 +00:00
|
|
|
)
|
|
|
|
|
2013-12-14 17:37:56 +00:00
|
|
|
const memLRUSize = 1024 // arbitrary
|
|
|
|
|
2014-06-14 20:14:34 +00:00
|
|
|
var errCacheMiss = errors.New("not in cache")
|
|
|
|
|
|
|
|
// ThumbMeta is a mapping from an image's scaling parameters (encoding
|
2013-12-14 17:37:56 +00:00
|
|
|
// as an opaque "key" string) and the blobref of the thumbnail
|
2014-06-14 20:14:34 +00:00
|
|
|
// (currently its file schema blob).
|
|
|
|
// ThumbMeta is safe for concurrent use by multiple goroutines.
|
2013-12-14 17:37:56 +00:00
|
|
|
//
|
|
|
|
// The key will be some string containing the original full-sized image's
|
|
|
|
// blobref, its target dimensions, and any possible transformations on
|
|
|
|
// it (e.g. cropping it to square).
|
2014-06-14 20:14:34 +00:00
|
|
|
type ThumbMeta struct {
|
|
|
|
mem *lru.Cache // key -> blob.Ref
|
2013-12-14 17:37:56 +00:00
|
|
|
kv sorted.KeyValue // optional
|
2011-10-25 20:30:02 +00:00
|
|
|
}
|
|
|
|
|
2014-06-14 20:14:34 +00:00
|
|
|
// NewThumbMeta returns a new in-memory ThumbMeta, backed with the
|
|
|
|
// optional kv.
|
|
|
|
// If kv is nil, key/value pairs are stored in memory only.
|
|
|
|
func NewThumbMeta(kv sorted.KeyValue) *ThumbMeta {
|
|
|
|
return &ThumbMeta{
|
2013-12-14 17:37:56 +00:00
|
|
|
mem: lru.New(memLRUSize),
|
|
|
|
kv: kv,
|
2011-10-25 20:30:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-14 20:14:34 +00:00
|
|
|
func (m *ThumbMeta) Get(key string) (blob.Ref, error) {
|
|
|
|
var br blob.Ref
|
2013-12-14 17:37:56 +00:00
|
|
|
if v, ok := m.mem.Get(key); ok {
|
|
|
|
return v.(blob.Ref), nil
|
2011-10-25 20:30:02 +00:00
|
|
|
}
|
2013-12-14 17:37:56 +00:00
|
|
|
if m.kv != nil {
|
|
|
|
v, err := m.kv.Get(key)
|
|
|
|
if err == sorted.ErrNotFound {
|
|
|
|
return br, errCacheMiss
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return br, err
|
|
|
|
}
|
|
|
|
br, ok := blob.Parse(v)
|
|
|
|
if !ok {
|
|
|
|
return br, fmt.Errorf("Invalid blobref %q found for key %q in thumbnail mea", v, key)
|
|
|
|
}
|
|
|
|
m.mem.Add(key, br)
|
|
|
|
return br, nil
|
|
|
|
}
|
|
|
|
return br, errCacheMiss
|
2011-10-25 20:30:02 +00:00
|
|
|
}
|
|
|
|
|
2014-06-14 20:14:34 +00:00
|
|
|
func (m *ThumbMeta) Put(key string, br blob.Ref) error {
|
2013-12-14 17:37:56 +00:00
|
|
|
m.mem.Add(key, br)
|
|
|
|
if m.kv != nil {
|
|
|
|
return m.kv.Set(key, br.String())
|
|
|
|
}
|
2011-10-25 20:30:02 +00:00
|
|
|
return nil
|
|
|
|
}
|