Avoid redundant decoding when sendind coordinate to change tracker or coverage index.

Don't try to store in coverage index if the key already exists.
Handle more potential database errors.
This commit is contained in:
Sascha L. Teichmann 2015-07-25 22:18:16 +02:00
parent fed5b32469
commit 68bb1ee320
3 changed files with 25 additions and 20 deletions

View File

@ -27,13 +27,7 @@ func NewChangeTracker() *ChangeTracker {
return &ChangeTracker{changes: make(map[quantizedXZ]bool)}
}
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\n", err)
return
}
func (ct *ChangeTracker) BlockChanged(coord common.Coord) {
ct.mutex.Lock()
ct.changes[quantizedXZ{
X: coord.X / quantizationFactor,

View File

@ -189,17 +189,22 @@ func (ldbs *LevelDBSession) Store(hash, key, value []byte) (exists bool, err err
err = db.Put(wo, key, value)
wo.Close()
})
// This technically too early because this done in transactions
if err != nil {
return
}
// This technically too early because this is done in a transactions
// which are commited (and possible fail) later.
if ldbs.backend.coverage != nil {
if ldbs.backend.changeTracker != nil || ldbs.backend.coverage != nil {
c, err := common.DecodeStringBytesToCoord(origKey)
if err != nil {
return false, err
return exists, err
}
if ldbs.backend.coverage != nil && !exists {
ldbs.backend.coverage.Insert(c)
}
if ldbs.backend.changeTracker != nil {
ldbs.backend.changeTracker.BlockChanged(c)
}
ldbs.backend.coverage.Insert(c)
}
if ldbs.backend.changeTracker != nil {
ldbs.backend.changeTracker.BlockChanged(origKey)
}
return
}

View File

@ -239,15 +239,21 @@ func (ss *SqliteSession) Store(hash, key, value []byte) (exists bool, err error)
insertStmt := ss.txStmt(ss.backend.insertStmt)
_, err = insertStmt.Exec(pos, value)
}
// This technically too early because this done in transactions
if err != nil {
return
}
// This technically too early because this is done in a transactions
// which are commited (and possible fail) later.
if ss.backend.coverage != nil {
ss.backend.coverage.Insert(common.PlainToCoord(pos))
if ss.backend.changeTracker != nil || ss.backend.coverage != nil {
c := common.PlainToCoord(pos)
if ss.backend.coverage != nil && !exists {
ss.backend.coverage.Insert(c)
}
if ss.backend.changeTracker != nil {
ss.backend.changeTracker.BlockChanged(c)
}
}
if ss.backend.changeTracker != nil {
ss.backend.changeTracker.BlockChanged(key)
}
return
}