1
0
forked from MTSR/mapserver
mapserver/tiledb/tiledb.go

77 lines
1.4 KiB
Go
Raw Normal View History

2019-02-09 18:05:40 +01:00
package tiledb
import (
2019-02-21 11:16:01 +01:00
"io/ioutil"
2019-02-09 18:05:40 +01:00
"mapserver/coords"
2019-02-21 11:16:01 +01:00
"os"
"strconv"
2019-02-09 18:05:40 +01:00
2019-12-24 23:35:48 +01:00
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
)
2019-02-22 08:54:11 +01:00
2019-02-09 18:05:40 +01:00
func New(path string) (*TileDB, error) {
return &TileDB{
2019-02-21 11:16:01 +01:00
path: path,
2019-02-09 18:05:40 +01:00
}, nil
}
type TileDB struct {
2019-02-21 11:16:01 +01:00
path string
2019-02-09 18:05:40 +01:00
}
2019-02-21 11:16:01 +01: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 18:05:40 +01:00
}
2019-02-10 18:38:30 +01:00
func (this *TileDB) GC() {
2019-02-21 11:16:01 +01:00
//No-Op
2019-02-10 18:38:30 +01:00
}
2019-02-09 18:05:40 +01:00
func (this *TileDB) GetTile(pos *coords.TileCoords) ([]byte, error) {
2019-02-22 14:18:00 +01:00
timer := prometheus.NewTimer(tiledbLoadDuration)
defer timer.ObserveDuration()
2019-02-22 14:10:41 +01:00
fields := logrus.Fields{
"pos": pos,
}
log.WithFields(fields).Debug("GetTile")
2019-02-21 11:16:01 +01:00
_, file := this.getDirAndFile(pos)
2019-02-21 13:15:49 +01:00
info, _ := os.Stat(file)
if info != nil {
2023-01-20 18:04:27 +01:00
content, err := os.ReadFile(file)
2019-02-21 11:16:01 +01:00
if err != nil {
2019-02-21 13:15:49 +01:00
panic(err)
2019-02-09 18:05:40 +01:00
}
2019-02-21 11:16:01 +01:00
return content, err
2019-02-09 18:05:40 +01:00
}
2019-02-21 11:16:01 +01:00
return nil, nil
2019-02-09 18:05:40 +01:00
}
func (this *TileDB) SetTile(pos *coords.TileCoords, tile []byte) error {
2019-02-22 14:18:00 +01:00
timer := prometheus.NewTimer(tiledbSaveDuration)
defer timer.ObserveDuration()
2019-02-22 14:10:41 +01:00
fields := logrus.Fields{
"pos": pos,
"size": len(tile),
}
log.WithFields(fields).Debug("SetTile")
2019-02-21 11:16:01 +01:00
dir, file := this.getDirAndFile(pos)
os.MkdirAll(dir, 0700)
2019-02-15 08:36:48 +01:00
2019-02-21 11:16:01 +01:00
err := ioutil.WriteFile(file, tile, 0644)
2019-02-09 18:05:40 +01:00
return err
}