mapserver/server/tiledb/tiledb.go

66 lines
1.1 KiB
Go
Raw Normal View History

2019-02-09 20:05:40 +03:00
package tiledb
import (
2019-02-21 13:16:01 +03:00
"io/ioutil"
2019-02-09 20:05:40 +03:00
"mapserver/coords"
2019-02-21 13:16:01 +03:00
"os"
"strconv"
2019-02-22 10:54:11 +03:00
"sync"
2019-02-09 20:05:40 +03:00
)
2019-02-22 10:54:11 +03:00
var mutex = &sync.RWMutex{}
2019-02-09 20:05:40 +03:00
func New(path string) (*TileDB, error) {
return &TileDB{
2019-02-21 13:16:01 +03:00
path: path,
2019-02-09 20:05:40 +03:00
}, nil
}
type TileDB struct {
2019-02-21 13:16:01 +03:00
path string
2019-02-09 20:05:40 +03:00
}
2019-02-21 13:16:01 +03:00
func (this *TileDB) getDirAndFile(pos *coords.TileCoords) (string, string) {
dir := this.path + "/" +
strconv.Itoa(pos.LayerId) + "/" +
strconv.Itoa(pos.Zoom) + "/" +
strconv.Itoa(pos.X)
file := dir + "/" + strconv.Itoa(pos.Y) + ".png"
return dir, file
2019-02-09 20:05:40 +03:00
}
2019-02-10 20:38:30 +03:00
func (this *TileDB) GC() {
2019-02-21 13:16:01 +03:00
//No-Op
2019-02-10 20:38:30 +03:00
}
2019-02-09 20:05:40 +03:00
func (this *TileDB) GetTile(pos *coords.TileCoords) ([]byte, error) {
2019-02-22 10:54:11 +03:00
mutex.RLock()
defer mutex.RUnlock()
2019-02-21 13:16:01 +03:00
_, file := this.getDirAndFile(pos)
2019-02-21 15:15:49 +03:00
info, _ := os.Stat(file)
if info != nil {
2019-02-21 13:16:01 +03:00
content, err := ioutil.ReadFile(file)
if err != nil {
2019-02-21 15:15:49 +03:00
panic(err)
2019-02-09 20:05:40 +03:00
}
2019-02-21 13:16:01 +03:00
return content, err
2019-02-09 20:05:40 +03:00
}
2019-02-21 13:16:01 +03:00
return nil, nil
2019-02-09 20:05:40 +03:00
}
func (this *TileDB) SetTile(pos *coords.TileCoords, tile []byte) error {
2019-02-22 10:54:11 +03:00
mutex.Lock()
defer mutex.Unlock()
2019-02-21 13:16:01 +03:00
dir, file := this.getDirAndFile(pos)
os.MkdirAll(dir, 0700)
2019-02-15 10:36:48 +03:00
2019-02-21 13:16:01 +03:00
err := ioutil.WriteFile(file, tile, 0644)
2019-02-09 20:05:40 +03:00
return err
}