imposm3/cache/diff.go

182 lines
3.1 KiB
Go
Raw Normal View History

package cache
import (
2013-05-13 09:36:54 +04:00
"bytes"
"encoding/binary"
"io"
"log"
"os"
"path/filepath"
2013-05-06 15:42:58 +04:00
"sort"
"sync"
)
type DiffCache struct {
Dir string
Coords *RefIndex
Ways *RefIndex
opened bool
}
func (c *DiffCache) Close() {
if c.Coords != nil {
c.Coords.close()
c.Coords = nil
}
if c.Ways != nil {
c.Ways.close()
c.Ways = nil
}
}
func NewDiffCache(dir string) *DiffCache {
cache := &DiffCache{Dir: dir}
return cache
}
func (c *DiffCache) Open() error {
var err error
c.Coords, err = NewRefIndex(filepath.Join(c.Dir, "coords_index"))
if err != nil {
c.Close()
return err
}
c.Ways, err = NewRefIndex(filepath.Join(c.Dir, "ways_index"))
if err != nil {
c.Close()
return err
}
c.opened = true
return nil
}
func (c *DiffCache) Exists() bool {
if c.opened {
return true
}
if _, err := os.Stat(filepath.Join(c.Dir, "coords_index")); !os.IsNotExist(err) {
return true
}
if _, err := os.Stat(filepath.Join(c.Dir, "ways_index")); !os.IsNotExist(err) {
return true
}
return false
}
func (c *DiffCache) Remove() error {
if c.opened {
c.Close()
}
if err := os.RemoveAll(filepath.Join(c.Dir, "coords_index")); err != nil {
return err
}
if err := os.RemoveAll(filepath.Join(c.Dir, "ways_index")); err != nil {
return err
}
return nil
}
type RefIndex struct {
Cache
mu sync.Mutex
}
func NewRefIndex(path string) (*RefIndex, error) {
index := RefIndex{}
err := index.open(path)
if err != nil {
return nil, err
}
return &index, nil
}
func (index *RefIndex) Add(id, ref int64) error {
2013-05-10 13:11:29 +04:00
keyBuf := idToKeyBuf(id)
data, err := index.db.Get(index.ro, keyBuf)
if err != nil {
panic(err)
}
2013-05-13 09:36:54 +04:00
var refs []int64
if data != nil {
2013-05-13 09:36:54 +04:00
refs = UnmarshalRefs(data)
}
2013-05-13 09:36:54 +04:00
if refs == nil {
refs = make([]int64, 0, 1)
}
2013-05-13 09:36:54 +04:00
refs = insertRefs(refs, ref)
data = MarshalRefs(refs)
err = index.db.Put(index.wo, keyBuf, data)
return err
}
2013-05-13 09:36:54 +04:00
func insertRefs(refs []int64, ref int64) []int64 {
i := sort.Search(len(refs), func(i int) bool {
return refs[i] >= ref
2013-05-06 15:42:58 +04:00
})
2013-05-13 09:36:54 +04:00
if i < len(refs) && refs[i] >= ref {
refs = append(refs, 0)
copy(refs[i+1:], refs[i:])
refs[i] = ref
2013-05-06 15:42:58 +04:00
} else {
2013-05-13 09:36:54 +04:00
refs = append(refs, ref)
2013-05-06 15:42:58 +04:00
}
2013-05-13 09:36:54 +04:00
return refs
2013-05-06 15:42:58 +04:00
}
func (index *RefIndex) Get(id int64) []int64 {
2013-05-10 13:11:29 +04:00
keyBuf := idToKeyBuf(id)
data, err := index.db.Get(index.ro, keyBuf)
2013-05-13 09:36:54 +04:00
var refs []int64
if data != nil {
2013-05-13 09:36:54 +04:00
refs = UnmarshalRefs(data)
if err != nil {
panic(err)
}
}
2013-05-13 09:36:54 +04:00
return refs
}
func UnmarshalRefs(buf []byte) []int64 {
refs := make([]int64, 0, 8)
r := bytes.NewBuffer(buf)
lastRef := int64(0)
for {
ref, err := binary.ReadVarint(r)
if err == io.EOF {
break
}
if err != nil {
log.Println("error while unmarshaling refs:", err)
break
}
ref = lastRef + ref
refs = append(refs, ref)
lastRef = ref
}
return refs
}
func MarshalRefs(refs []int64) []byte {
buf := make([]byte, len(refs)*4+binary.MaxVarintLen64)
lastRef := int64(0)
nextPos := 0
for _, ref := range refs {
if len(buf)-nextPos < binary.MaxVarintLen64 {
tmp := make([]byte, len(buf)*2)
buf = append(tmp, buf...)
}
nextPos += binary.PutVarint(buf[nextPos:], ref-lastRef)
lastRef = ref
}
return buf[:nextPos]
}