2019-02-09 20:05:40 +03:00
|
|
|
package tiledb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"mapserver/coords"
|
|
|
|
|
|
|
|
"github.com/dgraph-io/badger"
|
2019-02-15 10:36:48 +03:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2019-02-09 20:05:40 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func New(path string) (*TileDB, error) {
|
|
|
|
opts := badger.DefaultOptions
|
|
|
|
opts.Dir = path
|
|
|
|
opts.ValueDir = path
|
|
|
|
db, err := badger.Open(opts)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &TileDB{
|
|
|
|
db: db,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type TileDB struct {
|
|
|
|
db *badger.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func getKey(pos *coords.TileCoords) []byte {
|
|
|
|
return []byte(fmt.Sprintf("%d/%d/%d/%d", pos.X, pos.Y, pos.Zoom, pos.LayerId))
|
|
|
|
}
|
|
|
|
|
2019-02-10 20:38:30 +03:00
|
|
|
func (this *TileDB) GC() {
|
2019-02-20 23:07:33 +03:00
|
|
|
this.db.RunValueLogGC(0.7)
|
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-15 10:36:48 +03:00
|
|
|
timer := prometheus.NewTimer(getDuration)
|
|
|
|
defer timer.ObserveDuration()
|
|
|
|
|
2019-02-09 20:05:40 +03:00
|
|
|
var tile []byte
|
|
|
|
err := this.db.View(func(txn *badger.Txn) error {
|
|
|
|
item, err := txn.Get(getKey(pos))
|
|
|
|
if item != nil {
|
|
|
|
tile, err = item.ValueCopy(nil)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return tile, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *TileDB) SetTile(pos *coords.TileCoords, tile []byte) error {
|
2019-02-15 10:36:48 +03:00
|
|
|
timer := prometheus.NewTimer(setDuration)
|
|
|
|
defer timer.ObserveDuration()
|
|
|
|
|
2019-02-09 20:05:40 +03:00
|
|
|
err := this.db.Update(func(txn *badger.Txn) error {
|
|
|
|
err := txn.Set(getKey(pos), tile)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|