2014-09-17 09:51:34 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-09-17 10:16:51 +02:00
|
|
|
"encoding/json"
|
2014-09-17 09:51:34 +02:00
|
|
|
"log"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"bitbucket.org/s_l_teichmann/mtredisalize/common"
|
|
|
|
)
|
|
|
|
|
2014-09-17 10:16:51 +02:00
|
|
|
type quantizedXZ struct {
|
|
|
|
X, Z int16
|
|
|
|
}
|
|
|
|
|
2014-09-17 09:51:34 +02:00
|
|
|
type ChangeTracker struct {
|
2014-09-17 10:16:51 +02:00
|
|
|
changes map[quantizedXZ]bool
|
|
|
|
mutex sync.Mutex
|
2014-09-17 09:51:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewChangeTracker() *ChangeTracker {
|
2014-09-17 10:16:51 +02:00
|
|
|
return &ChangeTracker{changes: make(map[quantizedXZ]bool)}
|
2014-09-17 09:51:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ct *ChangeTracker) BlockChanged(key []byte) {
|
|
|
|
var err error
|
|
|
|
var coord common.Coord
|
|
|
|
if coord, err = common.DecodeStringBytesToCoord(key); err != nil {
|
|
|
|
log.Printf("decoding key failed: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Printf("changed block: %v", coord)
|
2014-09-17 10:16:51 +02:00
|
|
|
ct.mutex.Lock()
|
|
|
|
ct.changes[quantizedXZ{X: coord.X / 16, Z: coord.Z / 16}] = true
|
|
|
|
ct.mutex.Unlock()
|
2014-09-17 09:51:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ct *ChangeTracker) FlushChanges(url string) (err error) {
|
|
|
|
log.Println("change flush triggered")
|
2014-09-17 10:16:51 +02:00
|
|
|
var oldChanges map[quantizedXZ]bool
|
|
|
|
ct.mutex.Lock()
|
|
|
|
oldChanges = ct.changes
|
|
|
|
ct.changes = make(map[quantizedXZ]bool)
|
|
|
|
ct.mutex.Unlock()
|
|
|
|
go func() {
|
|
|
|
changes := make([]quantizedXZ, len(oldChanges))
|
|
|
|
i := 0
|
|
|
|
for change, _ := range oldChanges {
|
|
|
|
changes[i] = change
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
var encoded []byte
|
|
|
|
if encoded, err = json.Marshal(changes); err != nil {
|
|
|
|
log.Printf("WARN: encode changes to JSON failed: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// TODO: implement http POST
|
|
|
|
_ = encoded
|
|
|
|
}()
|
2014-09-17 09:51:34 +02:00
|
|
|
return
|
|
|
|
}
|