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

77 lines
1.4 KiB
Go
Raw Permalink 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-09 20:05:40 +03:00
2019-12-25 01:35:48 +03:00
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
)
2019-02-22 10:54:11 +03:00
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 16:18:00 +03:00
timer := prometheus.NewTimer(tiledbLoadDuration)
defer timer.ObserveDuration()
2019-02-22 16:10:41 +03:00
fields := logrus.Fields{
"pos": pos,
}
log.WithFields(fields).Debug("GetTile")
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 {
2023-01-20 20:04:27 +03:00
content, err := os.ReadFile(file)
2019-02-21 13:16:01 +03:00
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 16:18:00 +03:00
timer := prometheus.NewTimer(tiledbSaveDuration)
defer timer.ObserveDuration()
2019-02-22 16:10:41 +03:00
fields := logrus.Fields{
"pos": pos,
"size": len(tile),
}
log.WithFields(fields).Debug("SetTile")
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
}