Skip to content

Commit

Permalink
initial support for MaxZoom on filecache. #96
Browse files Browse the repository at this point in the history
  • Loading branch information
ARolek committed Oct 24, 2017
1 parent 3cc2b70 commit 9bcafb8
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 22 deletions.
51 changes: 29 additions & 22 deletions cache/filecache/filecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,40 +31,44 @@ func init() {
// New instantiates a Filecache. The config expects the following params:
//
// basepath (string): a path to where the cache will be written
// max_zoom (int): max zoom to use the cache. beyond this zoom cache Set() calls will be ignored
//
func New(config map[string]interface{}) (cache.Interface, error) {
var err error

// new filecache
fc := Filecache{
Locker: map[string]sync.RWMutex{},
}

// parse the config
c := dict.M(config)

/*
maxZoom, err := c.Uint(ConfigKeyMaxZoom, nil)
if err != nil {
defaultMaxZoom := 0
maxZoom, err := c.Int(ConfigKeyMaxZoom, &defaultMaxZoom)
if err != nil {
return nil, err
}
if maxZoom != 0 {
fc.MaxZoom = uint(maxZoom)
}

return nil, ErrMissingBasepath
}
*/
basepath, err := c.String(ConfigKeyBasepath, nil)
fc.Basepath, err = c.String(ConfigKeyBasepath, nil)
if err != nil {
return nil, ErrMissingBasepath
}

if basepath == "" {
if fc.Basepath == "" {
return nil, ErrMissingBasepath
}

// make our basepath if it does not exist
if err = os.MkdirAll(basepath, os.ModePerm); err != nil {
if err = os.MkdirAll(fc.Basepath, os.ModePerm); err != nil {
return nil, err
}

fc := Filecache{
Basepath: basepath,
Locker: map[string]sync.RWMutex{},
}

// TODO: walk our basepath and full our Locker with already rendered keys
err = filepath.Walk(basepath, func(path string, info os.FileInfo, err error) error {
// walk our basepath and fill the filecache Locker with keys for already cached tiles
err = filepath.Walk(fc.Basepath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
Expand All @@ -75,7 +79,7 @@ func New(config map[string]interface{}) (cache.Interface, error) {
}

// remove the basepath for the file key
fileKey := path[len(basepath):]
fileKey := path[len(fc.Basepath):]

cacheKey, err := cache.ParseKey(fileKey)
if err != nil {
Expand Down Expand Up @@ -111,11 +115,9 @@ type Filecache struct {
// TODO: store a hash of the cache blob along with the Locker mutex
Locker map[string]sync.RWMutex

// MaxZoom determins which zoom max should leverage the cache.
// This is useful if the cache should not be leveraged for higher
// zooms (i.e. 10+).
//
// TODO: implement
// MaxZoom determins the max zoom the cache to persist. Beyond this
// zoom, cache Set() calls will be ignored. This is useful if the cache
// should not be leveraged for higher zooms when data changes often.
MaxZoom uint
}

Expand Down Expand Up @@ -164,6 +166,11 @@ func (fc *Filecache) Get(key *cache.Key) ([]byte, bool, error) {
func (fc *Filecache) Set(key *cache.Key, val []byte) error {
var err error

// check for maxzoom
if key.Z > int(fc.MaxZoom) {
return nil
}

path := filepath.Join(fc.Basepath, key.String())

// lookup our mutex
Expand Down
3 changes: 3 additions & 0 deletions util/dict/gen.pl
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ package dict
return v, fmt.Errorf("%v value is required.",key)
}
if v, ok = val.($T); !ok {
if def == nil {
return v, nil
}
return \*def, fmt.Errorf("%v value needs to be of type ${T}. Value is of type %T", key, val)
}
return v, nil
Expand Down
33 changes: 33 additions & 0 deletions util/dict/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ func (m M) String(key string, def *string) (v string, err error) {
return v, fmt.Errorf("%v value is required.", key)
}
if v, ok = val.(string); !ok {
if def == nil {
return v, nil
}
return *def, fmt.Errorf("%v value needs to be of type string. Value is of type %T", key, val)
}
return v, nil
Expand Down Expand Up @@ -73,6 +76,9 @@ func (m M) Int(key string, def *int) (v int, err error) {
return v, fmt.Errorf("%v value is required.", key)
}
if v, ok = val.(int); !ok {
if def == nil {
return v, nil
}
return *def, fmt.Errorf("%v value needs to be of type int. Value is of type %T", key, val)
}
return v, nil
Expand Down Expand Up @@ -114,6 +120,9 @@ func (m M) Uint(key string, def *uint) (v uint, err error) {
return v, fmt.Errorf("%v value is required.", key)
}
if v, ok = val.(uint); !ok {
if def == nil {
return v, nil
}
return *def, fmt.Errorf("%v value needs to be of type uint. Value is of type %T", key, val)
}
return v, nil
Expand Down Expand Up @@ -155,6 +164,9 @@ func (m M) Int8(key string, def *int8) (v int8, err error) {
return v, fmt.Errorf("%v value is required.", key)
}
if v, ok = val.(int8); !ok {
if def == nil {
return v, nil
}
return *def, fmt.Errorf("%v value needs to be of type int8. Value is of type %T", key, val)
}
return v, nil
Expand Down Expand Up @@ -196,6 +208,9 @@ func (m M) Uint8(key string, def *uint8) (v uint8, err error) {
return v, fmt.Errorf("%v value is required.", key)
}
if v, ok = val.(uint8); !ok {
if def == nil {
return v, nil
}
return *def, fmt.Errorf("%v value needs to be of type uint8. Value is of type %T", key, val)
}
return v, nil
Expand Down Expand Up @@ -237,6 +252,9 @@ func (m M) Int16(key string, def *int16) (v int16, err error) {
return v, fmt.Errorf("%v value is required.", key)
}
if v, ok = val.(int16); !ok {
if def == nil {
return v, nil
}
return *def, fmt.Errorf("%v value needs to be of type int16. Value is of type %T", key, val)
}
return v, nil
Expand Down Expand Up @@ -278,6 +296,9 @@ func (m M) Uint16(key string, def *uint16) (v uint16, err error) {
return v, fmt.Errorf("%v value is required.", key)
}
if v, ok = val.(uint16); !ok {
if def == nil {
return v, nil
}
return *def, fmt.Errorf("%v value needs to be of type uint16. Value is of type %T", key, val)
}
return v, nil
Expand Down Expand Up @@ -319,6 +340,9 @@ func (m M) Int32(key string, def *int32) (v int32, err error) {
return v, fmt.Errorf("%v value is required.", key)
}
if v, ok = val.(int32); !ok {
if def == nil {
return v, nil
}
return *def, fmt.Errorf("%v value needs to be of type int32. Value is of type %T", key, val)
}
return v, nil
Expand Down Expand Up @@ -360,6 +384,9 @@ func (m M) Uint32(key string, def *uint32) (v uint32, err error) {
return v, fmt.Errorf("%v value is required.", key)
}
if v, ok = val.(uint32); !ok {
if def == nil {
return v, nil
}
return *def, fmt.Errorf("%v value needs to be of type uint32. Value is of type %T", key, val)
}
return v, nil
Expand Down Expand Up @@ -401,6 +428,9 @@ func (m M) Int64(key string, def *int64) (v int64, err error) {
return v, fmt.Errorf("%v value is required.", key)
}
if v, ok = val.(int64); !ok {
if def == nil {
return v, nil
}
return *def, fmt.Errorf("%v value needs to be of type int64. Value is of type %T", key, val)
}
return v, nil
Expand Down Expand Up @@ -442,6 +472,9 @@ func (m M) Uint64(key string, def *uint64) (v uint64, err error) {
return v, fmt.Errorf("%v value is required.", key)
}
if v, ok = val.(uint64); !ok {
if def == nil {
return v, nil
}
return *def, fmt.Errorf("%v value needs to be of type uint64. Value is of type %T", key, val)
}
return v, nil
Expand Down

0 comments on commit 9bcafb8

Please sign in to comment.