mirror of
https://bitbucket.org/s_l_teichmann/mtsatellite
synced 2024-11-08 11:10:27 +01:00
mtredisalize: Do the actual change tracking, store them in a map and serialize them to JSON when flushing is triggered.
TODO: Implement the POST request to the remote host with this JSON document.
This commit is contained in:
parent
400a65f563
commit
f4c188a87a
|
@ -1,23 +1,27 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"log"
|
"log"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"bitbucket.org/s_l_teichmann/mtredisalize/common"
|
"bitbucket.org/s_l_teichmann/mtredisalize/common"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type quantizedXZ struct {
|
||||||
|
X, Z int16
|
||||||
|
}
|
||||||
|
|
||||||
type ChangeTracker struct {
|
type ChangeTracker struct {
|
||||||
mutex sync.Mutex
|
changes map[quantizedXZ]bool
|
||||||
|
mutex sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewChangeTracker() *ChangeTracker {
|
func NewChangeTracker() *ChangeTracker {
|
||||||
return &ChangeTracker{}
|
return &ChangeTracker{changes: make(map[quantizedXZ]bool)}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ct *ChangeTracker) BlockChanged(key []byte) {
|
func (ct *ChangeTracker) BlockChanged(key []byte) {
|
||||||
ct.mutex.Lock()
|
|
||||||
defer ct.mutex.Unlock()
|
|
||||||
var err error
|
var err error
|
||||||
var coord common.Coord
|
var coord common.Coord
|
||||||
if coord, err = common.DecodeStringBytesToCoord(key); err != nil {
|
if coord, err = common.DecodeStringBytesToCoord(key); err != nil {
|
||||||
|
@ -25,9 +29,33 @@ func (ct *ChangeTracker) BlockChanged(key []byte) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Printf("changed block: %v", coord)
|
log.Printf("changed block: %v", coord)
|
||||||
|
ct.mutex.Lock()
|
||||||
|
ct.changes[quantizedXZ{X: coord.X / 16, Z: coord.Z / 16}] = true
|
||||||
|
ct.mutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ct *ChangeTracker) FlushChanges(url string) (err error) {
|
func (ct *ChangeTracker) FlushChanges(url string) (err error) {
|
||||||
log.Println("change flush triggered")
|
log.Println("change flush triggered")
|
||||||
|
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
|
||||||
|
}()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user