mirror of
https://bitbucket.org/s_l_teichmann/mtsatellite
synced 2025-01-11 01:10:17 +01:00
Merged feature branch redis-priority to remove unnecessary locking when using LevelBD.
This commit is contained in:
commit
9f8ff8b393
@ -6,7 +6,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"sync"
|
|
||||||
|
|
||||||
"bitbucket.org/s_l_teichmann/mtsatellite/common"
|
"bitbucket.org/s_l_teichmann/mtsatellite/common"
|
||||||
|
|
||||||
@ -22,7 +21,6 @@ type LevelDBBackend struct {
|
|||||||
decoder common.KeyTranscoder
|
decoder common.KeyTranscoder
|
||||||
|
|
||||||
changeTracker *changeTracker
|
changeTracker *changeTracker
|
||||||
mutex sync.RWMutex
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type LevelDBSession struct {
|
type LevelDBSession struct {
|
||||||
@ -72,7 +70,8 @@ func NewLeveDBBackend(
|
|||||||
interleaved: interleaved,
|
interleaved: interleaved,
|
||||||
encoder: encoder,
|
encoder: encoder,
|
||||||
decoder: decoder,
|
decoder: decoder,
|
||||||
changeTracker: changeTracker}
|
changeTracker: changeTracker,
|
||||||
|
}
|
||||||
|
|
||||||
if !interleaved {
|
if !interleaved {
|
||||||
if err = ldb.buildCoverage(); err != nil {
|
if err = ldb.buildCoverage(); err != nil {
|
||||||
@ -109,18 +108,6 @@ func (ldb *LevelDBBackend) buildCoverage() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ldb *LevelDBBackend) doRead(f func(db *leveldb.DB)) {
|
|
||||||
ldb.mutex.RLock()
|
|
||||||
f(ldb.db)
|
|
||||||
ldb.mutex.RUnlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ldb *LevelDBBackend) doWrite(f func(db *leveldb.DB)) {
|
|
||||||
ldb.mutex.Lock()
|
|
||||||
f(ldb.db)
|
|
||||||
ldb.mutex.Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ldb *LevelDBBackend) NewSession() (Session, error) {
|
func (ldb *LevelDBBackend) NewSession() (Session, error) {
|
||||||
return &LevelDBSession{ldb, nil}, nil
|
return &LevelDBSession{ldb, nil}, nil
|
||||||
}
|
}
|
||||||
@ -144,23 +131,21 @@ func (ldbs *LevelDBSession) Del(hash, key []byte) (success bool, err error) {
|
|||||||
if key, err = ldbs.backend.decoder(key); err != nil {
|
if key, err = ldbs.backend.decoder(key); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ldbs.backend.doWrite(func(db *leveldb.DB) {
|
ro := leveldb.NewReadOptions()
|
||||||
ro := leveldb.NewReadOptions()
|
defer ro.Close()
|
||||||
defer ro.Close()
|
var data []byte
|
||||||
var data []byte
|
data, err = ldbs.backend.db.Get(ro, key)
|
||||||
data, err = ldbs.backend.db.Get(ro, key)
|
if err != nil {
|
||||||
if err != nil {
|
return
|
||||||
return
|
}
|
||||||
}
|
if data == nil {
|
||||||
if data == nil {
|
success = false
|
||||||
success = false
|
return
|
||||||
return
|
}
|
||||||
}
|
success = true
|
||||||
success = true
|
wo := leveldb.NewWriteOptions()
|
||||||
wo := leveldb.NewWriteOptions()
|
defer wo.Close()
|
||||||
defer wo.Close()
|
err = ldbs.backend.db.Delete(wo, key)
|
||||||
err = ldbs.backend.db.Delete(wo, key)
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -168,16 +153,14 @@ func (ldbs *LevelDBSession) Fetch(hash, key []byte) (value []byte, err error) {
|
|||||||
if key, err = ldbs.backend.decoder(key); err != nil {
|
if key, err = ldbs.backend.decoder(key); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ldbs.backend.doRead(func(db *leveldb.DB) {
|
ro := leveldb.NewReadOptions()
|
||||||
ro := leveldb.NewReadOptions()
|
value, err = ldbs.backend.db.Get(ro, key)
|
||||||
value, err = ldbs.backend.db.Get(ro, key)
|
//if err != nil {
|
||||||
//if err != nil {
|
// log.Printf("Fetch key '%s' failed.\n", key)
|
||||||
// log.Printf("Fetch key '%s' failed.\n", key)
|
//} else {
|
||||||
//} else {
|
// log.Printf("Fetch key = '%s' len(value) = %d\n", key, len(value))
|
||||||
// log.Printf("Fetch key = '%s' len(value) = %d\n", key, len(value))
|
//}
|
||||||
//}
|
ro.Close()
|
||||||
ro.Close()
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -201,21 +184,20 @@ func (ldbs *LevelDBSession) Store(hash, key, value []byte) (exists bool, err err
|
|||||||
if key, err = ldbs.backend.decoder(key); err != nil {
|
if key, err = ldbs.backend.decoder(key); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ldbs.backend.doWrite(func(db *leveldb.DB) {
|
if exists, err = keyExists(ldbs.backend.db, key); err != nil {
|
||||||
if exists, err = keyExists(db, key); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if ldbs.tx != nil {
|
|
||||||
ldbs.tx.Put(key, value)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
wo := leveldb.NewWriteOptions()
|
|
||||||
err = db.Put(wo, key, value)
|
|
||||||
wo.Close()
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if ldbs.tx != nil {
|
||||||
|
ldbs.tx.Put(key, value)
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
wo := leveldb.NewWriteOptions()
|
||||||
|
err = ldbs.backend.db.Put(wo, key, value)
|
||||||
|
wo.Close()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
// This technically too early because this is done in a transactions
|
// This technically too early because this is done in a transactions
|
||||||
// which are commited (and possible fail) later.
|
// which are commited (and possible fail) later.
|
||||||
if ldbs.backend.changeTracker != nil || ldbs.backend.coverage != nil {
|
if ldbs.backend.changeTracker != nil || ldbs.backend.coverage != nil {
|
||||||
@ -245,13 +227,11 @@ func (ldbs *LevelDBSession) CommitTransaction() (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
ldbs.tx = nil
|
ldbs.tx = nil
|
||||||
ldbs.backend.doWrite(func(db *leveldb.DB) {
|
wo := leveldb.NewWriteOptions()
|
||||||
wo := leveldb.NewWriteOptions()
|
wo.SetSync(true)
|
||||||
wo.SetSync(true)
|
err = ldbs.backend.db.Write(wo, tx)
|
||||||
err = db.Write(wo, tx)
|
wo.Close()
|
||||||
wo.Close()
|
tx.Close()
|
||||||
tx.Close()
|
|
||||||
})
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -259,8 +239,6 @@ func (ldbs *LevelDBSession) AllKeys(
|
|||||||
hash []byte,
|
hash []byte,
|
||||||
done <-chan struct{}) (<-chan []byte, int, error) {
|
done <-chan struct{}) (<-chan []byte, int, error) {
|
||||||
|
|
||||||
ldbs.backend.mutex.RLock()
|
|
||||||
|
|
||||||
ro := leveldb.NewReadOptions()
|
ro := leveldb.NewReadOptions()
|
||||||
ro.SetFillCache(false)
|
ro.SetFillCache(false)
|
||||||
|
|
||||||
@ -274,14 +252,12 @@ func (ldbs *LevelDBSession) AllKeys(
|
|||||||
if err := it.GetError(); err != nil {
|
if err := it.GetError(); err != nil {
|
||||||
it.Close()
|
it.Close()
|
||||||
ro.Close()
|
ro.Close()
|
||||||
ldbs.backend.mutex.RUnlock()
|
|
||||||
return nil, n, err
|
return nil, n, err
|
||||||
}
|
}
|
||||||
|
|
||||||
keys := make(chan []byte)
|
keys := make(chan []byte)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
ldbs.backend.mutex.RUnlock()
|
|
||||||
defer ro.Close()
|
defer ro.Close()
|
||||||
defer close(keys)
|
defer close(keys)
|
||||||
defer it.Close()
|
defer it.Close()
|
||||||
@ -340,8 +316,6 @@ func (ldbs *LevelDBSession) plainSpatialQuery(
|
|||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
defer close(blocks)
|
defer close(blocks)
|
||||||
ldbs.backend.mutex.RLock()
|
|
||||||
defer ldbs.backend.mutex.RUnlock()
|
|
||||||
|
|
||||||
ro := leveldb.NewReadOptions()
|
ro := leveldb.NewReadOptions()
|
||||||
defer ro.Close()
|
defer ro.Close()
|
||||||
@ -402,8 +376,6 @@ func (ldbs *LevelDBSession) interleavedSpatialQuery(
|
|||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
defer close(blocks)
|
defer close(blocks)
|
||||||
ldbs.backend.mutex.RLock()
|
|
||||||
defer ldbs.backend.mutex.RUnlock()
|
|
||||||
|
|
||||||
ro := leveldb.NewReadOptions()
|
ro := leveldb.NewReadOptions()
|
||||||
defer ro.Close()
|
defer ro.Close()
|
||||||
|
Loading…
Reference in New Issue
Block a user